Skip to content

Commit

Permalink
PageUrl generation #589
Browse files Browse the repository at this point in the history
  • Loading branch information
anatol-sialitski committed Oct 12, 2023
1 parent ee4a6a9 commit b567b1a
Show file tree
Hide file tree
Showing 21 changed files with 406 additions and 122 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 );
}
Expand Down Expand Up @@ -218,8 +217,7 @@ private List<GraphQLFieldDefinition> 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" ) ) );
Expand All @@ -245,9 +243,7 @@ private List<GraphQLFieldDefinition> 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" ) ),
Expand All @@ -258,8 +254,6 @@ private List<GraphQLFieldDefinition> 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() ) );

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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" ) );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 );
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 );
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 );
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 );
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.enonic.app.guillotine.graphql.fetchers;

import com.enonic.xp.content.ContentId;
import com.enonic.xp.content.ContentNotFoundException;
import com.enonic.xp.content.ContentPath;
import com.enonic.xp.content.ContentService;

class SitePathResolver
{
private final ContentService contentService;

SitePathResolver( final ContentService contentService )
{
this.contentService = contentService;
}

Check warning on line 15 in src/main/java/com/enonic/app/guillotine/graphql/fetchers/SitePathResolver.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/com/enonic/app/guillotine/graphql/fetchers/SitePathResolver.java#L13-L15

Added lines #L13 - L15 were not covered by tests

ContentPath resolve( final String siteKey )
{
if ( siteKey == null || siteKey.isEmpty() )
{
return null;

Check warning on line 21 in src/main/java/com/enonic/app/guillotine/graphql/fetchers/SitePathResolver.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/com/enonic/app/guillotine/graphql/fetchers/SitePathResolver.java#L21

Added line #L21 was not covered by tests
}
try
{
if ( siteKey.startsWith( "/" ) )
{
return contentService.getByPath( ContentPath.from( siteKey ) ).getPath();

Check warning on line 27 in src/main/java/com/enonic/app/guillotine/graphql/fetchers/SitePathResolver.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/com/enonic/app/guillotine/graphql/fetchers/SitePathResolver.java#L27

Added line #L27 was not covered by tests
}
else
{
return contentService.getById( ContentId.from( siteKey ) ).getPath();

Check warning on line 31 in src/main/java/com/enonic/app/guillotine/graphql/fetchers/SitePathResolver.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/com/enonic/app/guillotine/graphql/fetchers/SitePathResolver.java#L31

Added line #L31 was not covered by tests
}
}
catch ( ContentNotFoundException e )

Check warning on line 34 in src/main/java/com/enonic/app/guillotine/graphql/fetchers/SitePathResolver.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/com/enonic/app/guillotine/graphql/fetchers/SitePathResolver.java#L34

Added line #L34 was not covered by tests
{
return null;

Check warning on line 36 in src/main/java/com/enonic/app/guillotine/graphql/fetchers/SitePathResolver.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/com/enonic/app/guillotine/graphql/fetchers/SitePathResolver.java#L36

Added line #L36 was not covered by tests
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package com.enonic.app.guillotine.graphql.helper;

import java.util.Collection;
import java.util.Iterator;
import java.util.Map;
import java.util.Objects;

import com.google.common.collect.Multimap;
import com.google.common.net.UrlEscapers;

public class ParamsUrHelper

Check warning on line 11 in src/main/java/com/enonic/app/guillotine/graphql/helper/ParamsUrHelper.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/com/enonic/app/guillotine/graphql/helper/ParamsUrHelper.java#L11

Added line #L11 was not covered by tests
{
public static void resolveParams( final Multimap<String, String> holder, final Object originalParams )
{
if ( originalParams instanceof Map )
{
for ( Map.Entry<String, Object> entry : ( (Map<String, Object>) originalParams ).entrySet() )
{
applyParam( holder, entry.getKey(), entry.getValue() );
}
}
}

public static void appendParams( final StringBuilder str, final Collection<Map.Entry<String, String>> params )
{
if ( params == null || params.isEmpty() )
{
return;

Check warning on line 28 in src/main/java/com/enonic/app/guillotine/graphql/helper/ParamsUrHelper.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/com/enonic/app/guillotine/graphql/helper/ParamsUrHelper.java#L28

Added line #L28 was not covered by tests
}
str.append( "?" );
final Iterator<Map.Entry<String, String>> it = params.iterator();
appendParam( str, it.next() );

Check warning on line 32 in src/main/java/com/enonic/app/guillotine/graphql/helper/ParamsUrHelper.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/com/enonic/app/guillotine/graphql/helper/ParamsUrHelper.java#L30-L32

Added lines #L30 - L32 were not covered by tests
while ( it.hasNext() )
{
str.append( "&" );
appendParam( str, it.next() );

Check warning on line 36 in src/main/java/com/enonic/app/guillotine/graphql/helper/ParamsUrHelper.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/com/enonic/app/guillotine/graphql/helper/ParamsUrHelper.java#L35-L36

Added lines #L35 - L36 were not covered by tests
}
}

Check warning on line 38 in src/main/java/com/enonic/app/guillotine/graphql/helper/ParamsUrHelper.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/com/enonic/app/guillotine/graphql/helper/ParamsUrHelper.java#L38

Added line #L38 was not covered by tests

private static void applyParam( final Multimap<String, String> 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<String, String> holder, final String key, final Iterable values )
{
for ( final Object value : values )
{
holder.put( key, Objects.toString( value, null ) );
}
}


private static void appendParam( final StringBuilder str, final Map.Entry<String, String> param )
{
final String value = param.getValue();
str.append( urlEncode( param.getKey() ) );

Check warning on line 64 in src/main/java/com/enonic/app/guillotine/graphql/helper/ParamsUrHelper.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/com/enonic/app/guillotine/graphql/helper/ParamsUrHelper.java#L63-L64

Added lines #L63 - L64 were not covered by tests
if ( value != null )
{
str.append( "=" ).append( urlEncode( value ) );

Check warning on line 67 in src/main/java/com/enonic/app/guillotine/graphql/helper/ParamsUrHelper.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/com/enonic/app/guillotine/graphql/helper/ParamsUrHelper.java#L67

Added line #L67 was not covered by tests
}
}

Check warning on line 69 in src/main/java/com/enonic/app/guillotine/graphql/helper/ParamsUrHelper.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/com/enonic/app/guillotine/graphql/helper/ParamsUrHelper.java#L69

Added line #L69 was not covered by tests

private static String urlEncode( final String value )
{
return UrlEscapers.urlFormParameterEscaper().escape( value );

Check warning on line 73 in src/main/java/com/enonic/app/guillotine/graphql/helper/ParamsUrHelper.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/com/enonic/app/guillotine/graphql/helper/ParamsUrHelper.java#L73

Added line #L73 was not covered by tests
}
}
Original file line number Diff line number Diff line change
@@ -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<String, Object> result = executeQuery( graphQLSchema, readGraphQLQuery( "graphql/getMediaContent.graphql" ) );

assertFalse( result.containsKey( "errors" ) );
assertTrue( result.containsKey( "data" ) );

Map<String, Object> 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<String, Object> result = executeQuery( graphQLSchema, readGraphQLQuery( "graphql/getMediaContent.graphql" ) );

assertFalse( result.containsKey( "errors" ) );
assertTrue( result.containsKey( "data" ) );

Map<String, Object> downloadAttachmentUrlField = CastHelper.cast( getFieldFromGuillotine( result, "downloadAttachmentUrl" ) );
assertEquals( "url?download", downloadAttachmentUrlField.get( "mediaUrl" ) );
}
}
Loading

0 comments on commit b567b1a

Please sign in to comment.