diff --git a/package-lock.json b/package-lock.json index 211f10ae..0cbbbc15 100644 --- a/package-lock.json +++ b/package-lock.json @@ -9659,9 +9659,7 @@ "resolved": "https://registry.npmjs.org/ajv-formats/-/ajv-formats-2.1.1.tgz", "integrity": "sha512-Wx0Kx52hxE7C18hkMEggYlEifqWZtYaRgouJor+WMdPnQyEK13vgEWyVNup7SoeeoLMsr4kf5h6dOW11I15MUA==", "dev": true, - "requires": { - "ajv": "^8.0.0" - } + "requires": {} }, "ajv-keywords": { "version": "5.1.0", diff --git a/src/main/java/com/enonic/app/guillotine/graphql/factory/ContentTypesFactory.java b/src/main/java/com/enonic/app/guillotine/graphql/factory/ContentTypesFactory.java index c09c27e2..dd23d0b8 100644 --- a/src/main/java/com/enonic/app/guillotine/graphql/factory/ContentTypesFactory.java +++ b/src/main/java/com/enonic/app/guillotine/graphql/factory/ContentTypesFactory.java @@ -29,7 +29,6 @@ import com.enonic.app.guillotine.graphql.fetchers.GetContentDataDataFetcher; import com.enonic.app.guillotine.graphql.fetchers.GetContentFieldDataFetcher; import com.enonic.app.guillotine.graphql.fetchers.GetContentParentDataFetcher; -import com.enonic.app.guillotine.graphql.fetchers.GetContentPathDataFetcher; import com.enonic.app.guillotine.graphql.fetchers.GetContentReferencesDataFetcher; import com.enonic.app.guillotine.graphql.fetchers.GetContentSiteDataFetcher; import com.enonic.app.guillotine.graphql.fetchers.GetImageUrlDataFetcher; @@ -162,7 +161,7 @@ private GraphQLFieldDefinition createMediaUrlField() arguments.add( newArgument( "download", Scalars.GraphQLBoolean ) ); arguments.add( newArgument( "type", GraphQLTypeReference.typeRef( "UrlType" ) ) ); - arguments.add( newArgument( "params", Scalars.GraphQLString ) ); + arguments.add( newArgument( "params", ExtendedScalars.Json ) ); return outputField( "mediaUrl", Scalars.GraphQLString, arguments ); } @@ -218,8 +217,7 @@ private List getGenericContentFields( String contentType result.add( outputField( "_id", new GraphQLNonNull( Scalars.GraphQLID ) ) ); result.add( outputField( "_name", new GraphQLNonNull( Scalars.GraphQLString ) ) ); - result.add( outputField( "_path", new GraphQLNonNull( Scalars.GraphQLString ), - List.of( newArgument( "type", GraphQLTypeReference.typeRef( "ContentPathType" ) ) ) ) ); + result.add( outputField( "_path", new GraphQLNonNull( Scalars.GraphQLString ) ) ); result.add( outputField( "_references", new GraphQLList( GraphQLTypeReference.typeRef( "Content" ) ) ) ); result.add( outputField( "_score", Scalars.GraphQLFloat ) ); result.add( outputField( "creator", GraphQLTypeReference.typeRef( "PrincipalKey" ) ) ); @@ -245,9 +243,7 @@ private List getGenericContentFields( String contentType newArgument( "resolveFragment", Scalars.GraphQLBoolean ) ) ) ); result.add( outputField( "attachments", new GraphQLList( GraphQLTypeReference.typeRef( "Attachment" ) ) ) ); result.add( outputField( "publish", GraphQLTypeReference.typeRef( "PublishInfo" ) ) ); - result.add( outputField( "pageUrl", Scalars.GraphQLString, - List.of( newArgument( "type", GraphQLTypeReference.typeRef( "UrlType" ) ), - newArgument( "params", Scalars.GraphQLString ) ) ) ); + result.add( outputField( "pageUrl", Scalars.GraphQLString, List.of( newArgument( "params", ExtendedScalars.Json ) ) ) ); result.add( outputField( "site", GraphQLTypeReference.typeRef( "portal_Site" ) ) ); result.add( outputField( "parent", GraphQLTypeReference.typeRef( "Content" ) ) ); result.add( outputField( "children", new GraphQLList( GraphQLTypeReference.typeRef( "Content" ) ), @@ -258,8 +254,6 @@ private List getGenericContentFields( String contentType newArgument( "sort", Scalars.GraphQLString ) ) ) ); result.add( outputField( "permissions", GraphQLTypeReference.typeRef( "Permissions" ) ) ); - context.registerDataFetcher( contentType, "_path", new GetContentPathDataFetcher( serviceFacade.getContentService() ) ); - context.registerDataFetcher( contentType, "contentType", new ContentTypeDataFetcher( serviceFacade.getMixinService(), serviceFacade.getContentTypeService() ) ); diff --git a/src/main/java/com/enonic/app/guillotine/graphql/factory/EnumTypesFactory.java b/src/main/java/com/enonic/app/guillotine/graphql/factory/EnumTypesFactory.java index 72262260..ee327988 100644 --- a/src/main/java/com/enonic/app/guillotine/graphql/factory/EnumTypesFactory.java +++ b/src/main/java/com/enonic/app/guillotine/graphql/factory/EnumTypesFactory.java @@ -21,8 +21,6 @@ public void create() { registerEnum( "UrlType", "URL type.", List.of( "server", "absolute" ) ); - registerEnum( "ContentPathType", "Content path type.", List.of( "siteRelative" ) ); - registerEnum( "MediaIntentType", "Media intent type.", List.of( "download", "inline" ) ); registerEnum( "DslOperatorType", "DSL Operator type.", List.of( "OR", "AND" ) ); diff --git a/src/main/java/com/enonic/app/guillotine/graphql/factory/GenericTypesFactory.java b/src/main/java/com/enonic/app/guillotine/graphql/factory/GenericTypesFactory.java index c1c0be39..ced8bf41 100644 --- a/src/main/java/com/enonic/app/guillotine/graphql/factory/GenericTypesFactory.java +++ b/src/main/java/com/enonic/app/guillotine/graphql/factory/GenericTypesFactory.java @@ -133,7 +133,7 @@ private void createAttachmentType() fields.add( outputField( "attachmentUrl", Scalars.GraphQLString, List.of( newArgument( "download", Scalars.GraphQLBoolean ), newArgument( "type", GraphQLTypeReference.typeRef( "UrlType" ) ), - newArgument( "params", Scalars.GraphQLString ) ) ) ); + newArgument( "params", ExtendedScalars.Json ) ) ) ); GraphQLObjectType outputObject = newObject( context.uniqueName( "Attachment" ), "Attachment.", fields ); context.registerType( outputObject.getName(), outputObject ); diff --git a/src/main/java/com/enonic/app/guillotine/graphql/fetchers/GetAttachmentUrlByIdDataFetcher.java b/src/main/java/com/enonic/app/guillotine/graphql/fetchers/GetAttachmentUrlByIdDataFetcher.java index 4531f921..056aa741 100644 --- a/src/main/java/com/enonic/app/guillotine/graphql/fetchers/GetAttachmentUrlByIdDataFetcher.java +++ b/src/main/java/com/enonic/app/guillotine/graphql/fetchers/GetAttachmentUrlByIdDataFetcher.java @@ -7,6 +7,7 @@ import graphql.schema.DataFetchingEnvironment; import com.enonic.app.guillotine.graphql.helper.GuillotineLocalContextHelper; +import com.enonic.app.guillotine.graphql.helper.ParamsUrHelper; import com.enonic.xp.portal.PortalRequest; import com.enonic.xp.portal.PortalRequestAccessor; import com.enonic.xp.portal.url.AttachmentUrlParams; @@ -41,6 +42,8 @@ private String doGet( final DataFetchingEnvironment environment ) Objects.toString( environment.getArgument( "download" ), "false" ) ).type( environment.getArgument( "type" ) ).portalRequest( portalRequest ); + ParamsUrHelper.resolveParams( params.getParams(), environment.getArgument( "params" ) ); + return portalUrlService.attachmentUrl( params ); } } diff --git a/src/main/java/com/enonic/app/guillotine/graphql/fetchers/GetAttachmentUrlByNameDataFetcher.java b/src/main/java/com/enonic/app/guillotine/graphql/fetchers/GetAttachmentUrlByNameDataFetcher.java index 92bdccf8..66350a6e 100644 --- a/src/main/java/com/enonic/app/guillotine/graphql/fetchers/GetAttachmentUrlByNameDataFetcher.java +++ b/src/main/java/com/enonic/app/guillotine/graphql/fetchers/GetAttachmentUrlByNameDataFetcher.java @@ -8,6 +8,7 @@ import com.enonic.app.guillotine.graphql.Constants; import com.enonic.app.guillotine.graphql.helper.GuillotineLocalContextHelper; +import com.enonic.app.guillotine.graphql.helper.ParamsUrHelper; import com.enonic.xp.portal.PortalRequest; import com.enonic.xp.portal.PortalRequestAccessor; import com.enonic.xp.portal.url.AttachmentUrlParams; @@ -41,6 +42,9 @@ private String doGet( final DataFetchingEnvironment environment ) AttachmentUrlParams params = new AttachmentUrlParams().id( attachmentAsMap.get( Constants.CONTENT_ID_FIELD ).toString() ).name( attachmentAsMap.get( "name" ).toString() ).download( Objects.toString( environment.getArgument( "download" ), "false" ) ).type( environment.getArgument( "type" ) ).portalRequest( portalRequest ); + + ParamsUrHelper.resolveParams( params.getParams(), environment.getArgument( "params" ) ); + return portalUrlService.attachmentUrl( params ); } } diff --git a/src/main/java/com/enonic/app/guillotine/graphql/fetchers/GetContentPathDataFetcher.java b/src/main/java/com/enonic/app/guillotine/graphql/fetchers/GetContentPathDataFetcher.java deleted file mode 100644 index 672576c8..00000000 --- a/src/main/java/com/enonic/app/guillotine/graphql/fetchers/GetContentPathDataFetcher.java +++ /dev/null @@ -1,65 +0,0 @@ -package com.enonic.app.guillotine.graphql.fetchers; - -import java.util.Map; -import java.util.Objects; - -import graphql.schema.DataFetcher; -import graphql.schema.DataFetchingEnvironment; - -import com.enonic.app.guillotine.graphql.commands.GetContentCommand; -import com.enonic.app.guillotine.graphql.helper.GuillotineLocalContextHelper; -import com.enonic.xp.content.ContentService; -import com.enonic.xp.context.Context; -import com.enonic.xp.context.ContextAccessor; -import com.enonic.xp.context.ContextBuilder; -import com.enonic.xp.security.RoleKeys; -import com.enonic.xp.security.auth.AuthenticationInfo; - -public class GetContentPathDataFetcher - implements DataFetcher -{ - private final ContentService contentService; - - public GetContentPathDataFetcher( final ContentService contentService ) - { - this.contentService = contentService; - } - - @Override - public String get( final DataFetchingEnvironment environment ) - throws Exception - { - Map contentAsMap = environment.getSource(); - String originalPath = contentAsMap.get( "_path" ).toString(); - - if ( Objects.equals( "siteRelative", environment.getArgument( "type" ) ) ) - { - String sitePath = adminContext().callWith( () -> GuillotineLocalContextHelper.executeInContext( environment, () -> { - String siteKey = GuillotineLocalContextHelper.getSiteKey( environment ); - return Objects.requireNonNullElse( getSitePathBySiteKey( siteKey, environment ), originalPath ); - } ) ); - String normalizedPath = originalPath.replace( sitePath, "" ); - return normalizedPath.startsWith( "/" ) ? normalizedPath.substring( 1 ) : normalizedPath; - } - else - { - return originalPath; - } - } - - private String getSitePathBySiteKey( final String siteKey, DataFetchingEnvironment environment ) - { - Map contentAsMap = new GetContentCommand( contentService ).execute( siteKey, environment ); - if ( contentAsMap == null ) - { - return null; - } - return contentAsMap.get( "_path" ).toString(); - } - - public Context adminContext() - { - return ContextBuilder.from( ContextAccessor.current() ).authInfo( - AuthenticationInfo.copyOf( ContextAccessor.current().getAuthInfo() ).principals( RoleKeys.ADMIN ).build() ).build(); - } -} diff --git a/src/main/java/com/enonic/app/guillotine/graphql/fetchers/GetImageUrlDataFetcher.java b/src/main/java/com/enonic/app/guillotine/graphql/fetchers/GetImageUrlDataFetcher.java index d35a0c35..800b97d6 100644 --- a/src/main/java/com/enonic/app/guillotine/graphql/fetchers/GetImageUrlDataFetcher.java +++ b/src/main/java/com/enonic/app/guillotine/graphql/fetchers/GetImageUrlDataFetcher.java @@ -6,6 +6,7 @@ import graphql.schema.DataFetchingEnvironment; import com.enonic.app.guillotine.graphql.helper.GuillotineLocalContextHelper; +import com.enonic.app.guillotine.graphql.helper.ParamsUrHelper; import com.enonic.xp.portal.PortalRequest; import com.enonic.xp.portal.PortalRequestAccessor; import com.enonic.xp.portal.url.ImageUrlParams; @@ -63,6 +64,8 @@ private String doGet( final DataFetchingEnvironment environment ) params.type( environment.getArgument( "type" ) ); } + ParamsUrHelper.resolveParams( params.getParams(), environment.getArgument( "params" ) ); + return portalUrlService.imageUrl( params ); } } diff --git a/src/main/java/com/enonic/app/guillotine/graphql/fetchers/GetPageUrlDataFetcher.java b/src/main/java/com/enonic/app/guillotine/graphql/fetchers/GetPageUrlDataFetcher.java index de3ad83e..fbbe3b7c 100644 --- a/src/main/java/com/enonic/app/guillotine/graphql/fetchers/GetPageUrlDataFetcher.java +++ b/src/main/java/com/enonic/app/guillotine/graphql/fetchers/GetPageUrlDataFetcher.java @@ -6,6 +6,7 @@ import graphql.schema.DataFetchingEnvironment; import com.enonic.app.guillotine.graphql.helper.GuillotineLocalContextHelper; +import com.enonic.app.guillotine.graphql.helper.ParamsUrHelper; import com.enonic.xp.portal.PortalRequest; import com.enonic.xp.portal.PortalRequestAccessor; import com.enonic.xp.portal.url.PageUrlParams; @@ -38,7 +39,9 @@ private String doGet( final DataFetchingEnvironment environment ) PageUrlParams params = new PageUrlParams().id( sourceAsMap.get( "_id" ).toString() ).type( environment.getArgument( "type" ) ).portalRequest( - portalRequest ); // TODO include params + portalRequest ); + + ParamsUrHelper.resolveParams( params.getParams(), environment.getArgument( "params" ) ); return portalUrlService.pageUrl( params ); } diff --git a/src/main/java/com/enonic/app/guillotine/graphql/helper/ParamsUrHelper.java b/src/main/java/com/enonic/app/guillotine/graphql/helper/ParamsUrHelper.java new file mode 100644 index 00000000..d10df065 --- /dev/null +++ b/src/main/java/com/enonic/app/guillotine/graphql/helper/ParamsUrHelper.java @@ -0,0 +1,40 @@ +package com.enonic.app.guillotine.graphql.helper; + +import java.util.Map; +import java.util.Objects; + +import com.google.common.collect.Multimap; + +public class ParamsUrHelper +{ + public static void resolveParams( final Multimap holder, final Object originalParams ) + { + if ( originalParams instanceof Map ) + { + for ( Map.Entry entry : ( (Map) originalParams ).entrySet() ) + { + applyParam( holder, entry.getKey(), entry.getValue() ); + } + } + } + + private static void applyParam( final Multimap holder, final String key, final Object value ) + { + if ( value instanceof Iterable ) + { + applyParam( holder, key, (Iterable) value ); + } + else + { + holder.put( key, Objects.toString( value, null ) ); + } + } + + private static void applyParam( final Multimap holder, final String key, final Iterable values ) + { + for ( final Object value : values ) + { + holder.put( key, Objects.toString( value, null ) ); + } + } +} diff --git a/src/test/java/com/enonic/app/guillotine/graphql/GetMediaContentGraphQLIntegrationTest.java b/src/test/java/com/enonic/app/guillotine/graphql/GetMediaContentGraphQLIntegrationTest.java new file mode 100644 index 00000000..323e426f --- /dev/null +++ b/src/test/java/com/enonic/app/guillotine/graphql/GetMediaContentGraphQLIntegrationTest.java @@ -0,0 +1,63 @@ +package com.enonic.app.guillotine.graphql; + +import java.util.Map; + +import org.junit.jupiter.api.Test; + +import graphql.schema.GraphQLSchema; + +import com.enonic.app.guillotine.graphql.helper.CastHelper; +import com.enonic.xp.content.ContentId; +import com.enonic.xp.portal.url.AttachmentUrlParams; + +import static com.enonic.app.guillotine.graphql.ResourceHelper.readGraphQLQuery; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.when; + +public class GetMediaContentGraphQLIntegrationTest + extends BaseGraphQLIntegrationTest +{ + @Override + protected void initialize() + throws Exception + { + super.initialize(); + } + + @Test + public void testMediaAndAttachmentUrls() + { + when( serviceFacade.getPortalUrlService().attachmentUrl( any( AttachmentUrlParams.class ) ) ).thenReturn( "url?a=1&b=2&b=3&c" ); + when( contentService.getById( ContentId.from( "contentId" ) ) ).thenReturn( ContentFixtures.createMediaContent() ); + + GraphQLSchema graphQLSchema = getBean().createSchema(); + + Map result = executeQuery( graphQLSchema, readGraphQLQuery( "graphql/getMediaContent.graphql" ) ); + + assertFalse( result.containsKey( "errors" ) ); + assertTrue( result.containsKey( "data" ) ); + + Map attachmentUrlField = CastHelper.cast( getFieldFromGuillotine( result, "attachmentUrl" ) ); + assertEquals( "url?a=1&b=2&b=3&c", attachmentUrlField.get( "mediaUrl" ) ); + } + + @Test + public void testDownloadAttachmentUrl() + { + when( serviceFacade.getPortalUrlService().attachmentUrl( any( AttachmentUrlParams.class ) ) ).thenReturn( "url?download" ); + when( contentService.getById( ContentId.from( "contentId" ) ) ).thenReturn( ContentFixtures.createMediaContent() ); + + GraphQLSchema graphQLSchema = getBean().createSchema(); + + Map result = executeQuery( graphQLSchema, readGraphQLQuery( "graphql/getMediaContent.graphql" ) ); + + assertFalse( result.containsKey( "errors" ) ); + assertTrue( result.containsKey( "data" ) ); + + Map downloadAttachmentUrlField = CastHelper.cast( getFieldFromGuillotine( result, "downloadAttachmentUrl" ) ); + assertEquals( "url?download", downloadAttachmentUrlField.get( "mediaUrl" ) ); + } +} diff --git a/src/test/java/com/enonic/app/guillotine/graphql/GetPageUrlGraphQLIntegrationTest.java b/src/test/java/com/enonic/app/guillotine/graphql/GetPageUrlGraphQLIntegrationTest.java new file mode 100644 index 00000000..0c685d46 --- /dev/null +++ b/src/test/java/com/enonic/app/guillotine/graphql/GetPageUrlGraphQLIntegrationTest.java @@ -0,0 +1,49 @@ +package com.enonic.app.guillotine.graphql; + +import java.util.Locale; +import java.util.Map; + +import org.junit.jupiter.api.Test; +import org.mockito.Mockito; + +import graphql.schema.GraphQLSchema; + +import com.enonic.app.guillotine.graphql.helper.CastHelper; +import com.enonic.xp.content.ContentId; +import com.enonic.xp.content.ContentPath; +import com.enonic.xp.portal.url.PageUrlParams; +import com.enonic.xp.site.Site; + +import static com.enonic.app.guillotine.graphql.ResourceHelper.readGraphQLQuery; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.when; + +public class GetPageUrlGraphQLIntegrationTest + extends BaseGraphQLIntegrationTest +{ + @Test + public void testMediaAndAttachmentUrls() + { + when( serviceFacade.getPortalUrlService().pageUrl( any( PageUrlParams.class ) ) ).thenReturn( "pageUrl" ); + when( contentService.getById( ContentId.from( "contentId" ) ) ).thenReturn( ContentFixtures.createMediaContent() ); + when( contentService.getByPath( Mockito.any() ) ).thenReturn( Site.create(). + description( "Site" ). + name( "test-site" ). + parentPath( ContentPath.ROOT ). + language( Locale.ENGLISH ). + build() ); + + GraphQLSchema graphQLSchema = getBean().createSchema(); + + Map result = executeQuery( graphQLSchema, readGraphQLQuery( "graphql/getPageUrl.graphql" ) ); + + assertFalse( result.containsKey( "errors" ) ); + assertTrue( result.containsKey( "data" ) ); + + Map getField = CastHelper.cast( getFieldFromGuillotine( result, "get" ) ); + assertEquals( "pageUrl", getField.get( "pageUrl" ) ); + } +} diff --git a/src/test/java/com/enonic/app/guillotine/graphql/GuillotineApiGraphQLIntegrationTest.java b/src/test/java/com/enonic/app/guillotine/graphql/GuillotineApiGraphQLIntegrationTest.java index 7e60c6b0..2da25685 100644 --- a/src/test/java/com/enonic/app/guillotine/graphql/GuillotineApiGraphQLIntegrationTest.java +++ b/src/test/java/com/enonic/app/guillotine/graphql/GuillotineApiGraphQLIntegrationTest.java @@ -284,14 +284,6 @@ public void testGetContentPath() { when( contentService.getByPath( ContentPath.from( "/a/b" ) ) ).thenReturn( ContentFixtures.createContent( "id_1", "b", "/a" ) ); - final Site site = Site.create().name( "site" ).type( ContentTypeName.site() ).parentPath( ContentPath.ROOT ).data( - new PropertyTree() ).displayName( "Site" ).id( ContentId.from( "id_0" ) ).build(); - - when( contentService.findNearestSiteByPath( ContentPath.from( "/site" ) ) ).thenReturn( site ); - when( contentService.getByPath( ContentPath.from( "/site/c/d" ) ) ).thenReturn( - ContentFixtures.createContent( "id_2", "d", "/site/c" ) ); - when( contentService.getByPath( ContentPath.from( "/site" ) ) ).thenReturn( site ); - GraphQLSchema graphQLSchema = getBean().createSchema(); Map result = executeQuery( graphQLSchema, ResourceHelper.readGraphQLQuery( "graphql/getContentPath.graphql" ) ); @@ -299,16 +291,7 @@ public void testGetContentPath() assertFalse( result.containsKey( "errors" ) ); assertTrue( result.containsKey( "data" ) ); - Map data = CastHelper.cast( result.get( "data" ) ); - - assertTrue( data.containsKey( "q1" ) ); - Map q1 = CastHelper.cast( data.get( "q1" ) ); - Map getForQ1 = CastHelper.cast( q1.get( "get" ) ); - assertEquals( "", getForQ1.get( "_path" ) ); - - assertTrue( data.containsKey( "q2" ) ); - Map q2 = CastHelper.cast( data.get( "q2" ) ); - Map getForQ2 = CastHelper.cast( q2.get( "get" ) ); - assertEquals( "c/d", getForQ2.get( "_path" ) ); + Map getField = CastHelper.cast( getFieldFromGuillotine( result, "get" ) ); + assertEquals( "/a/b", getField.get( "_path" ) ); } } diff --git a/src/test/java/com/enonic/app/guillotine/graphql/factory/ContentTypesVerifier.java b/src/test/java/com/enonic/app/guillotine/graphql/factory/ContentTypesVerifier.java index bfb05247..b7cb17b1 100644 --- a/src/test/java/com/enonic/app/guillotine/graphql/factory/ContentTypesVerifier.java +++ b/src/test/java/com/enonic/app/guillotine/graphql/factory/ContentTypesVerifier.java @@ -161,7 +161,6 @@ private void verifyUntypedContent() GraphQLFieldDefinition pathField = type.getFieldDefinition( "_path" ); assertEquals( Scalars.GraphQLString, getOriginalTypeFromGraphQLNonNull( type, "_path" ) ); - assertEquals( "ContentPathType", getNameForGraphQLTypeReference( pathField.getArgument( "type" ).getType() ) ); assertEquals( "Content", getNameForGraphQLTypeReference( getOriginalTypeFromGraphQLList( type, "_references" ) ) ); assertEquals( Scalars.GraphQLFloat, type.getField( "_score" ).getType() ); @@ -194,8 +193,7 @@ private void verifyUntypedContent() assertEquals( "Attachment", getNameForGraphQLTypeReference( getOriginalTypeFromGraphQLList( type, "attachments" ) ) ); assertEquals( "PublishInfo", getNameForGraphQLTypeReference( type.getField( "publish" ).getType() ) ); assertEquals( Scalars.GraphQLString, type.getField( "pageUrl" ).getType() ); - assertEquals( "UrlType", getNameForGraphQLTypeReference( type.getField( "pageUrl" ).getArgument( "type" ).getType() ) ); - assertEquals( Scalars.GraphQLString, type.getField( "pageUrl" ).getArgument( "params" ).getType() ); + assertEquals( ExtendedScalars.Json, type.getField( "pageUrl" ).getArgument( "params" ).getType() ); assertEquals( "portal_Site", getNameForGraphQLTypeReference( type.getField( "site" ).getType() ) ); assertEquals( "Content", getNameForGraphQLTypeReference( type.getField( "parent" ).getType() ) ); @@ -221,7 +219,6 @@ private static void verifyCommonContentFields( final GraphQLInterfaceType type ) GraphQLFieldDefinition pathField = type.getFieldDefinition( "_path" ); assertEquals( Scalars.GraphQLString, getOriginalTypeFromGraphQLNonNull( type, "_path" ) ); - assertEquals( "ContentPathType", getNameForGraphQLTypeReference( pathField.getArgument( "type" ).getType() ) ); assertEquals( "Content", getNameForGraphQLTypeReference( getOriginalTypeFromGraphQLList( type, "_references" ) ) ); assertEquals( Scalars.GraphQLFloat, type.getField( "_score" ).getType() ); @@ -254,8 +251,7 @@ private static void verifyCommonContentFields( final GraphQLInterfaceType type ) assertEquals( "Attachment", getNameForGraphQLTypeReference( getOriginalTypeFromGraphQLList( type, "attachments" ) ) ); assertEquals( "PublishInfo", getNameForGraphQLTypeReference( type.getField( "publish" ).getType() ) ); assertEquals( Scalars.GraphQLString, type.getField( "pageUrl" ).getType() ); - assertEquals( "UrlType", getNameForGraphQLTypeReference( type.getField( "pageUrl" ).getArgument( "type" ).getType() ) ); - assertEquals( Scalars.GraphQLString, type.getField( "pageUrl" ).getArgument( "params" ).getType() ); + assertEquals( ExtendedScalars.Json, type.getField( "pageUrl" ).getArgument( "params" ).getType() ); assertEquals( "portal_Site", getNameForGraphQLTypeReference( type.getField( "site" ).getType() ) ); assertEquals( "Content", getNameForGraphQLTypeReference( type.getField( "parent" ).getType() ) ); diff --git a/src/test/java/com/enonic/app/guillotine/graphql/factory/EnumTypesVerifier.java b/src/test/java/com/enonic/app/guillotine/graphql/factory/EnumTypesVerifier.java index 61d974cc..5f9cc9cf 100644 --- a/src/test/java/com/enonic/app/guillotine/graphql/factory/EnumTypesVerifier.java +++ b/src/test/java/com/enonic/app/guillotine/graphql/factory/EnumTypesVerifier.java @@ -18,7 +18,6 @@ public EnumTypesVerifier( final GuillotineContext context ) public void verify() { verifyUrlType(); - verifyContentPathType(); verifyMediaIntentType(); verifyDslOperatorType(); verifyDslSortDirectionType(); @@ -44,16 +43,6 @@ private void verifyUrlType() verifyTypeValue( type, "absolute" ); } - private void verifyContentPathType() - { - GraphQLEnumType type = context.getEnumType( "ContentPathType" ); - - assertEquals( "ContentPathType", type.getName() ); - assertEquals( "Content path type.", type.getDescription() ); - assertEquals( 1, type.getValues().size() ); - verifyTypeValue( type, "siteRelative" ); - } - private void verifyMediaIntentType() { GraphQLEnumType type = context.getEnumType( "MediaIntentType" ); diff --git a/src/test/java/com/enonic/app/guillotine/graphql/factory/GenericTypesVerifier.java b/src/test/java/com/enonic/app/guillotine/graphql/factory/GenericTypesVerifier.java index b78a0cab..3f6d5bc9 100644 --- a/src/test/java/com/enonic/app/guillotine/graphql/factory/GenericTypesVerifier.java +++ b/src/test/java/com/enonic/app/guillotine/graphql/factory/GenericTypesVerifier.java @@ -166,7 +166,7 @@ private void verifyAttachment() assertEquals( 3, attachmentUrlField.getArguments().size() ); assertEquals( Scalars.GraphQLBoolean, attachmentUrlField.getArgument( "download" ).getType() ); - assertEquals( Scalars.GraphQLString, attachmentUrlField.getArgument( "params" ).getType() ); + assertEquals( ExtendedScalars.Json, attachmentUrlField.getArgument( "params" ).getType() ); assertEquals( "UrlType", getNameForGraphQLTypeReference( attachmentUrlField.getArgument( "type" ).getType() ) ); } diff --git a/src/test/java/com/enonic/app/guillotine/graphql/fetchers/GetAttachmentUrlByNameDataFetcherTest.java b/src/test/java/com/enonic/app/guillotine/graphql/fetchers/GetAttachmentUrlByNameDataFetcherTest.java new file mode 100644 index 00000000..f4a661be --- /dev/null +++ b/src/test/java/com/enonic/app/guillotine/graphql/fetchers/GetAttachmentUrlByNameDataFetcherTest.java @@ -0,0 +1,61 @@ +package com.enonic.app.guillotine.graphql.fetchers; + +import java.util.HashMap; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; + +import org.junit.jupiter.api.Test; +import org.mockito.Mockito; + +import graphql.schema.DataFetchingEnvironment; + +import com.enonic.app.guillotine.graphql.Constants; +import com.enonic.xp.branch.Branch; +import com.enonic.xp.portal.PortalRequest; +import com.enonic.xp.portal.PortalRequestAccessor; +import com.enonic.xp.portal.url.AttachmentUrlParams; +import com.enonic.xp.portal.url.PortalUrlService; +import com.enonic.xp.repository.RepositoryId; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +public class GetAttachmentUrlByNameDataFetcherTest +{ + @Test + public void testGet() + throws Exception + { + PortalRequest portalRequest = mock( PortalRequest.class ); + when( portalRequest.getRepositoryId() ).thenReturn( RepositoryId.from( "com.enonic.cms.default" ) ); + when( portalRequest.getBranch() ).thenReturn( Branch.from( "master" ) ); + + PortalRequestAccessor.set( portalRequest ); + + DataFetchingEnvironment environment = mock( DataFetchingEnvironment.class ); + + when( environment.getSource() ).thenReturn( Map.of( "name", "Name", Constants.CONTENT_ID_FIELD, "contentId" ) ); + when( environment.getLocalContext() ).thenReturn( new HashMap<>() ); + when( environment.getArgument( "download" ) ).thenReturn( "true" ); + when( environment.getArgument( "type" ) ).thenReturn( null ); + + Map params = new LinkedHashMap<>(); + params.put( "a", "1" ); + params.put( "b", List.of( 2, 3 ) ); + params.put( "c", null ); + + when( environment.getArgument( "params" ) ).thenReturn( params ); + + PortalUrlService portalUrlService = mock( PortalUrlService.class ); + + when( portalUrlService.attachmentUrl( Mockito.any( AttachmentUrlParams.class ) ) ).thenReturn( "url?a=1&b=2&b=3&c" ); + + GetAttachmentUrlByNameDataFetcher instance = new GetAttachmentUrlByNameDataFetcher( portalUrlService ); + + String attachmentUrl = instance.get( environment ); + + assertEquals( "url?a=1&b=2&b=3&c", attachmentUrl ); + } +} diff --git a/src/test/java/com/enonic/app/guillotine/graphql/fetchers/GetPageUrlDataFetcherTest.java b/src/test/java/com/enonic/app/guillotine/graphql/fetchers/GetPageUrlDataFetcherTest.java new file mode 100644 index 00000000..427a3190 --- /dev/null +++ b/src/test/java/com/enonic/app/guillotine/graphql/fetchers/GetPageUrlDataFetcherTest.java @@ -0,0 +1,65 @@ +package com.enonic.app.guillotine.graphql.fetchers; + +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.mockito.Mockito; + +import graphql.GraphQLContext; +import graphql.schema.DataFetchingEnvironment; + +import com.enonic.xp.branch.Branch; +import com.enonic.xp.portal.PortalRequest; +import com.enonic.xp.portal.PortalRequestAccessor; +import com.enonic.xp.portal.url.PageUrlParams; +import com.enonic.xp.portal.url.PortalUrlService; +import com.enonic.xp.repository.RepositoryId; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +public class GetPageUrlDataFetcherTest +{ + private DataFetchingEnvironment environment; + + @BeforeEach + public void setUp() + { + PortalRequest portalRequest = mock( PortalRequest.class ); + when( portalRequest.getRepositoryId() ).thenReturn( RepositoryId.from( "com.enonic.cms.default" ) ); + when( portalRequest.getBranch() ).thenReturn( Branch.from( "master" ) ); + PortalRequestAccessor.set( portalRequest ); + + environment = mock( DataFetchingEnvironment.class ); + + when( environment.getSource() ).thenReturn( Map.of( "_id", "contentId" ) ); + when( environment.getLocalContext() ).thenReturn( Map.of() ); + when( environment.getGraphQlContext() ).thenReturn( GraphQLContext.getDefault() ); + when( environment.getArgument( "type" ) ).thenReturn( "server" ); + } + + @Test + public void testGet() + throws Exception + { + Map params = new LinkedHashMap<>(); + params.put( "a", "1" ); + params.put( "b", List.of( 2, 3 ) ); + params.put( "c", null ); + + when( environment.getArgument( "params" ) ).thenReturn( params ); + + PortalUrlService portalUrlService = mock( PortalUrlService.class ); + when( portalUrlService.pageUrl( Mockito.any( PageUrlParams.class ) ) ).thenReturn( "url?a=1&b=2&b=3&c" ); + + GetPageUrlDataFetcher instance = new GetPageUrlDataFetcher( portalUrlService ); + + String pageUrl = instance.get( environment ); + + assertEquals( "url?a=1&b=2&b=3&c", pageUrl ); + } +} diff --git a/src/test/resources/graphql/getContentPath.graphql b/src/test/resources/graphql/getContentPath.graphql index f61e3354..2129f095 100644 --- a/src/test/resources/graphql/getContentPath.graphql +++ b/src/test/resources/graphql/getContentPath.graphql @@ -1,12 +1,7 @@ query { - q1: guillotine(repo: "repo", branch: "draft") { + guillotine(repo: "repo", branch: "draft") { get(key: "/a/b"){ - _path(type: siteRelative) - } - }, - q2: guillotine(repo: "repo", branch: "draft", siteKey: "/site") { - get(key: "${site}/c/d"){ - _path(type: siteRelative) + _path } } } diff --git a/src/test/resources/graphql/getMediaContent.graphql b/src/test/resources/graphql/getMediaContent.graphql new file mode 100644 index 00000000..a52bee85 --- /dev/null +++ b/src/test/resources/graphql/getMediaContent.graphql @@ -0,0 +1,18 @@ +query { + guillotine { + attachmentUrl: get(key: "contentId") { + ... on media_Image { + mediaUrl(params: { + a: 1, + b: [2, 3], + c: null + }) + } + } + downloadAttachmentUrl: get(key: "contentId") { + ... on media_Image { + mediaUrl(download: true) + } + } + } +} diff --git a/src/test/resources/graphql/getPageUrl.graphql b/src/test/resources/graphql/getPageUrl.graphql new file mode 100644 index 00000000..719368b0 --- /dev/null +++ b/src/test/resources/graphql/getPageUrl.graphql @@ -0,0 +1,11 @@ +query { + guillotine { + get(key: "contentId") { + pageUrl(params: { + a: 1, + b: [2, 3], + c: null + }) + } + } +}