diff --git a/modules/core/core-content/src/main/java/com/enonic/xp/core/impl/content/ContentNodeTranslator.java b/modules/core/core-content/src/main/java/com/enonic/xp/core/impl/content/ContentNodeTranslator.java index 00a50ab8283..eec31d307ab 100644 --- a/modules/core/core-content/src/main/java/com/enonic/xp/core/impl/content/ContentNodeTranslator.java +++ b/modules/core/core-content/src/main/java/com/enonic/xp/core/impl/content/ContentNodeTranslator.java @@ -25,10 +25,10 @@ public class ContentNodeTranslator private final ContentDataSerializer contentDataSerializer; - public ContentNodeTranslator( final NodeService nodeService ) + public ContentNodeTranslator( final NodeService nodeService, final ContentDataSerializer contentDataSerializer ) { this.nodeService = nodeService; - this.contentDataSerializer = new ContentDataSerializer(); + this.contentDataSerializer = contentDataSerializer; } public Contents fromNodes( final Nodes nodes, final boolean resolveHasChildren ) diff --git a/modules/core/core-content/src/main/java/com/enonic/xp/core/impl/content/ContentOutboundDependenciesIdsResolver.java b/modules/core/core-content/src/main/java/com/enonic/xp/core/impl/content/ContentOutboundDependenciesIdsResolver.java index 94a8363e63f..55323ac4a1e 100644 --- a/modules/core/core-content/src/main/java/com/enonic/xp/core/impl/content/ContentOutboundDependenciesIdsResolver.java +++ b/modules/core/core-content/src/main/java/com/enonic/xp/core/impl/content/ContentOutboundDependenciesIdsResolver.java @@ -20,10 +20,10 @@ class ContentOutboundDependenciesIdsResolver private final ContentDataSerializer contentDataSerializer; - ContentOutboundDependenciesIdsResolver( final ContentService contentService ) + ContentOutboundDependenciesIdsResolver( final ContentService contentService, final ContentDataSerializer contentDataSerializer ) { this.contentService = contentService; - this.contentDataSerializer = new ContentDataSerializer(); + this.contentDataSerializer = contentDataSerializer; } public ContentIds resolve( final ContentId contentId ) diff --git a/modules/core/core-content/src/main/java/com/enonic/xp/core/impl/content/ContentServiceImpl.java b/modules/core/core-content/src/main/java/com/enonic/xp/core/impl/content/ContentServiceImpl.java index b7303b7703a..3a14f7b1732 100644 --- a/modules/core/core-content/src/main/java/com/enonic/xp/core/impl/content/ContentServiceImpl.java +++ b/modules/core/core-content/src/main/java/com/enonic/xp/core/impl/content/ContentServiceImpl.java @@ -100,6 +100,7 @@ import com.enonic.xp.content.processor.ContentProcessor; import com.enonic.xp.context.ContextAccessor; import com.enonic.xp.context.ContextBuilder; +import com.enonic.xp.core.impl.content.serializer.ContentDataSerializer; import com.enonic.xp.data.PropertyTree; import com.enonic.xp.event.EventPublisher; import com.enonic.xp.form.FormDefaultValuesProcessor; @@ -171,6 +172,8 @@ public class ContentServiceImpl private final LayoutDescriptorService layoutDescriptorService; + private final ContentDataSerializer contentDataSerializer; + private ContentAuditLogSupport contentAuditLogSupport; private volatile ContentConfig config; @@ -184,7 +187,14 @@ public ContentServiceImpl( @Reference final NodeService nodeService, @Reference this.pageDescriptorService = pageDescriptorService; this.partDescriptorService = partDescriptorService; this.layoutDescriptorService = layoutDescriptorService; - this.translator = new ContentNodeTranslator( nodeService ); + + this.contentDataSerializer = ContentDataSerializer.create(). + layoutDescriptorService( layoutDescriptorService ). + pageDescriptorService( pageDescriptorService ). + partDescriptorService( partDescriptorService ). + build(); + + this.translator = new ContentNodeTranslator( nodeService, contentDataSerializer ); } @Activate @@ -226,6 +236,7 @@ public Site create( final CreateSiteParams params ) pageDescriptorService( this.pageDescriptorService ). partDescriptorService( this.partDescriptorService ). layoutDescriptorService( this.layoutDescriptorService ). + contentDataSerializer( this.contentDataSerializer ). allowUnsafeAttachmentNames( config.attachments_allowUnsafeNames() ). params( createContentParams ). build(). @@ -265,6 +276,7 @@ public Content create( final CreateContentParams params ) pageDescriptorService( this.pageDescriptorService ). partDescriptorService( this.partDescriptorService ). layoutDescriptorService( this.layoutDescriptorService ). + contentDataSerializer( this.contentDataSerializer ). allowUnsafeAttachmentNames( config.attachments_allowUnsafeNames() ). params( params ). build(). @@ -311,6 +323,7 @@ public Content create( final CreateMediaParams params ) pageDescriptorService( this.pageDescriptorService ). partDescriptorService( this.partDescriptorService ). layoutDescriptorService( this.layoutDescriptorService ). + contentDataSerializer( this.contentDataSerializer ). allowUnsafeAttachmentNames( config.attachments_allowUnsafeNames() ). build(). execute(); @@ -337,6 +350,7 @@ public Content update( final UpdateContentParams params ) pageDescriptorService( this.pageDescriptorService ). partDescriptorService( this.partDescriptorService ). layoutDescriptorService( this.layoutDescriptorService ). + contentDataSerializer( this.contentDataSerializer ). allowUnsafeAttachmentNames( config.attachments_allowUnsafeNames() ). build(). execute(); @@ -364,6 +378,7 @@ public Content update( final UpdateMediaParams params ) xDataService( this.xDataService ). contentProcessors( this.contentProcessors ). contentValidators( this.contentValidators ). + contentDataSerializer( this.contentDataSerializer ). allowUnsafeAttachmentNames( config.attachments_allowUnsafeNames() ). build(). execute(); @@ -776,6 +791,7 @@ public Content rename( final RenameContentParams params ) pageDescriptorService( this.pageDescriptorService ). partDescriptorService( this.partDescriptorService ). layoutDescriptorService( this.layoutDescriptorService ). + contentDataSerializer( this.contentDataSerializer ). build(). execute(); @@ -1069,7 +1085,7 @@ public ContentDependencies getDependencies( final ContentId id ) @Override public ContentIds getOutboundDependencies( final ContentId id ) { - return new ContentOutboundDependenciesIdsResolver( this ).resolve( id ); + return new ContentOutboundDependenciesIdsResolver( this, contentDataSerializer ).resolve( id ); } @Override @@ -1169,6 +1185,7 @@ public Content reprocess( final ContentId contentId ) pageDescriptorService( this.pageDescriptorService ). partDescriptorService( this.partDescriptorService ). layoutDescriptorService( this.layoutDescriptorService ). + contentDataSerializer( this.contentDataSerializer ). siteService( this.siteService ). xDataService( this.xDataService ). contentProcessors( this.contentProcessors ). @@ -1227,6 +1244,7 @@ public ImportContentResult importContent( final ImportContentParams params ) params( params ). nodeService( nodeService ). contentTypeService( contentTypeService ). + contentDataSerializer( contentDataSerializer ). eventPublisher( eventPublisher ). translator( translator ). build(). diff --git a/modules/core/core-content/src/main/java/com/enonic/xp/core/impl/content/CreateContentCommand.java b/modules/core/core-content/src/main/java/com/enonic/xp/core/impl/content/CreateContentCommand.java index 663a7872759..9ab0c40a7e9 100644 --- a/modules/core/core-content/src/main/java/com/enonic/xp/core/impl/content/CreateContentCommand.java +++ b/modules/core/core-content/src/main/java/com/enonic/xp/core/impl/content/CreateContentCommand.java @@ -24,6 +24,7 @@ import com.enonic.xp.content.processor.ProcessCreateParams; import com.enonic.xp.content.processor.ProcessCreateResult; import com.enonic.xp.context.ContextAccessor; +import com.enonic.xp.core.impl.content.serializer.ContentDataSerializer; import com.enonic.xp.core.impl.content.validate.InputValidator; import com.enonic.xp.data.Property; import com.enonic.xp.form.FormDefaultValuesProcessor; @@ -62,6 +63,8 @@ final class CreateContentCommand private final LayoutDescriptorService layoutDescriptorService; + private final ContentDataSerializer contentDataSerializer; + private CreateContentCommand( final Builder builder ) { super( builder ); @@ -71,6 +74,7 @@ private CreateContentCommand( final Builder builder ) this.pageDescriptorService = builder.pageDescriptorService; this.partDescriptorService = builder.partDescriptorService; this.layoutDescriptorService = builder.layoutDescriptorService; + this.contentDataSerializer = builder.contentDataSerializer; } static Builder create() @@ -108,6 +112,7 @@ private Content doExecute() .xDataService( this.xDataService ) .partDescriptorService( this.partDescriptorService ) .layoutDescriptorService( this.layoutDescriptorService ) + .contentDataSerializer( this.contentDataSerializer ) .siteService( this.siteService ) .build() .produce().refresh( params.isRefresh() ? RefreshMode.ALL : RefreshMode.STORAGE ).build(); @@ -328,6 +333,8 @@ static class Builder private LayoutDescriptorService layoutDescriptorService; + private ContentDataSerializer contentDataSerializer; + private Builder() { } @@ -373,6 +380,12 @@ Builder layoutDescriptorService( final LayoutDescriptorService value ) return this; } + Builder contentDataSerializer( final ContentDataSerializer value ) + { + this.contentDataSerializer = value; + return this; + } + @Override void validate() { diff --git a/modules/core/core-content/src/main/java/com/enonic/xp/core/impl/content/CreateMediaCommand.java b/modules/core/core-content/src/main/java/com/enonic/xp/core/impl/content/CreateMediaCommand.java index e3a21bc021e..ffd58470c09 100644 --- a/modules/core/core-content/src/main/java/com/enonic/xp/core/impl/content/CreateMediaCommand.java +++ b/modules/core/core-content/src/main/java/com/enonic/xp/core/impl/content/CreateMediaCommand.java @@ -9,6 +9,7 @@ import com.enonic.xp.content.Content; import com.enonic.xp.content.CreateContentParams; import com.enonic.xp.content.CreateMediaParams; +import com.enonic.xp.core.impl.content.serializer.ContentDataSerializer; import com.enonic.xp.data.PropertyTree; import com.enonic.xp.form.FormDefaultValuesProcessor; import com.enonic.xp.media.MediaInfo; @@ -34,6 +35,8 @@ final class CreateMediaCommand private final LayoutDescriptorService layoutDescriptorService; + private final ContentDataSerializer contentDataSerializer; + private CreateMediaCommand( final Builder builder ) { super( builder ); @@ -43,6 +46,7 @@ private CreateMediaCommand( final Builder builder ) this.pageDescriptorService = builder.pageDescriptorService; this.partDescriptorService = builder.partDescriptorService; this.layoutDescriptorService = builder.layoutDescriptorService; + this.contentDataSerializer = builder.contentDataSerializer; } Content execute() @@ -110,6 +114,7 @@ private Content doExecute() pageDescriptorService( this.pageDescriptorService ). partDescriptorService( this.partDescriptorService ). layoutDescriptorService( this.layoutDescriptorService ). + contentDataSerializer( this.contentDataSerializer ). allowUnsafeAttachmentNames( this.allowUnsafeAttachmentNames ). build(); @@ -146,6 +151,8 @@ public static class Builder private LayoutDescriptorService layoutDescriptorService; + private ContentDataSerializer contentDataSerializer; + public Builder params( final CreateMediaParams params ) { this.params = params; @@ -182,6 +189,13 @@ Builder layoutDescriptorService( final LayoutDescriptorService value ) return this; } + Builder contentDataSerializer( final ContentDataSerializer value ) + { + this.contentDataSerializer = value; + return this; + } + + @Override void validate() { diff --git a/modules/core/core-content/src/main/java/com/enonic/xp/core/impl/content/CreateNodeParamsFactory.java b/modules/core/core-content/src/main/java/com/enonic/xp/core/impl/content/CreateNodeParamsFactory.java index 83ff3555919..9be7d16f35f 100644 --- a/modules/core/core-content/src/main/java/com/enonic/xp/core/impl/content/CreateNodeParamsFactory.java +++ b/modules/core/core-content/src/main/java/com/enonic/xp/core/impl/content/CreateNodeParamsFactory.java @@ -45,6 +45,8 @@ public class CreateNodeParamsFactory private final SiteService siteService; + private final ContentDataSerializer contentDataSerializer; + public CreateNodeParamsFactory( final Builder builder ) { this.params = builder.params; @@ -54,11 +56,11 @@ public CreateNodeParamsFactory( final Builder builder ) this.pageDescriptorService = builder.pageDescriptorService; this.partDescriptorService = builder.partDescriptorService; this.layoutDescriptorService = builder.layoutDescriptorService; + this.contentDataSerializer = builder.contentDataSerializer; } public CreateNodeParams.Builder produce() { - final ContentDataSerializer contentDataSerializer = new ContentDataSerializer(); final PropertyTree contentAsData = contentDataSerializer.toCreateNodeData( params ); final PropertySet extraDataSet = contentAsData.getPropertySet( PropertyPath.from( ContentPropertyNames.EXTRA_DATA ) ); @@ -147,6 +149,8 @@ public static class Builder private LayoutDescriptorService layoutDescriptorService; + private ContentDataSerializer contentDataSerializer; + private SiteService siteService; Builder( final CreateContentTranslatorParams params ) @@ -190,6 +194,12 @@ Builder layoutDescriptorService( final LayoutDescriptorService value ) return this; } + Builder contentDataSerializer( final ContentDataSerializer value ) + { + this.contentDataSerializer = value; + return this; + } + void validate() { Preconditions.checkNotNull( params ); @@ -197,6 +207,7 @@ void validate() Preconditions.checkNotNull( pageDescriptorService ); Preconditions.checkNotNull( siteService ); Preconditions.checkNotNull( xDataService ); + Preconditions.checkNotNull( contentDataSerializer ); } public CreateNodeParamsFactory build() diff --git a/modules/core/core-content/src/main/java/com/enonic/xp/core/impl/content/ImportContentCommand.java b/modules/core/core-content/src/main/java/com/enonic/xp/core/impl/content/ImportContentCommand.java index 193ba58e536..ba194e2f6fa 100644 --- a/modules/core/core-content/src/main/java/com/enonic/xp/core/impl/content/ImportContentCommand.java +++ b/modules/core/core-content/src/main/java/com/enonic/xp/core/impl/content/ImportContentCommand.java @@ -4,6 +4,7 @@ import com.enonic.xp.content.ImportContentParams; import com.enonic.xp.content.ImportContentResult; +import com.enonic.xp.core.impl.content.serializer.ContentDataSerializer; import com.enonic.xp.node.ImportNodeParams; import com.enonic.xp.node.ImportNodeResult; import com.enonic.xp.node.Node; @@ -14,10 +15,13 @@ final class ImportContentCommand { private final ImportContentParams params; + private final ContentDataSerializer contentDataSerializer; + private ImportContentCommand( final Builder builder ) { super( builder ); this.params = builder.params; + this.contentDataSerializer = builder.contentDataSerializer; } static Builder create() @@ -34,6 +38,7 @@ private ImportContentResult doExecute() { final Node importNode = ImportContentFactory.create(). params( params ). + contentDataSerializer( contentDataSerializer ). build().execute(); final ImportNodeParams importNodeParams = ImportNodeParams.create().importNode( importNode ) @@ -55,6 +60,8 @@ static class Builder { private ImportContentParams params; + private ContentDataSerializer contentDataSerializer; + private Builder() { } @@ -65,11 +72,18 @@ Builder params( final ImportContentParams params ) return this; } + Builder contentDataSerializer( final ContentDataSerializer contentDataSerializer ) + { + this.contentDataSerializer = contentDataSerializer; + return this; + } + @Override void validate() { super.validate(); Preconditions.checkNotNull( params, "params must be given" ); + Preconditions.checkNotNull( contentDataSerializer, "contentDataSerializer must be given" ); } public ImportContentCommand build() diff --git a/modules/core/core-content/src/main/java/com/enonic/xp/core/impl/content/ImportContentFactory.java b/modules/core/core-content/src/main/java/com/enonic/xp/core/impl/content/ImportContentFactory.java index 9fe4c6395d2..ec8bcc97e63 100644 --- a/modules/core/core-content/src/main/java/com/enonic/xp/core/impl/content/ImportContentFactory.java +++ b/modules/core/core-content/src/main/java/com/enonic/xp/core/impl/content/ImportContentFactory.java @@ -20,9 +20,12 @@ public class ImportContentFactory { private final ImportContentParams params; + private final ContentDataSerializer contentDataSerializer; + private ImportContentFactory( Builder builder ) { this.params = builder.params; + this.contentDataSerializer = builder.contentDataSerializer; } public static Builder create() @@ -32,7 +35,6 @@ public static Builder create() public Node execute() { - final ContentDataSerializer contentDataSerializer = new ContentDataSerializer(); final PropertyTree nodeData = contentDataSerializer.toNodeData( params.getContent() ); if ( params.getInherit() != null ) @@ -72,6 +74,8 @@ public static final class Builder { private ImportContentParams params; + private ContentDataSerializer contentDataSerializer; + private Builder() { } @@ -82,6 +86,12 @@ public Builder params( final ImportContentParams params ) return this; } + public Builder contentDataSerializer( final ContentDataSerializer contentDataSerializer ) + { + this.contentDataSerializer = contentDataSerializer; + return this; + } + private void validate() { Preconditions.checkNotNull( this.params, "params cannot be null" ); diff --git a/modules/core/core-content/src/main/java/com/enonic/xp/core/impl/content/RenameContentCommand.java b/modules/core/core-content/src/main/java/com/enonic/xp/core/impl/content/RenameContentCommand.java index c7aacbf4db6..78ee70e3751 100644 --- a/modules/core/core-content/src/main/java/com/enonic/xp/core/impl/content/RenameContentCommand.java +++ b/modules/core/core-content/src/main/java/com/enonic/xp/core/impl/content/RenameContentCommand.java @@ -7,6 +7,7 @@ import com.enonic.xp.content.RenameContentParams; import com.enonic.xp.content.UpdateContentParams; import com.enonic.xp.content.ValidationErrors; +import com.enonic.xp.core.impl.content.serializer.ContentDataSerializer; import com.enonic.xp.node.Node; import com.enonic.xp.node.NodeAlreadyExistAtPathException; import com.enonic.xp.node.NodeId; @@ -31,6 +32,8 @@ final class RenameContentCommand private final LayoutDescriptorService layoutDescriptorService; + private final ContentDataSerializer contentDataSerializer; + private RenameContentCommand( final Builder builder ) { super( builder ); @@ -38,6 +41,7 @@ private RenameContentCommand( final Builder builder ) this.pageDescriptorService = builder.pageDescriptorService; this.partDescriptorService = builder.partDescriptorService; this.layoutDescriptorService = builder.layoutDescriptorService; + this.contentDataSerializer = builder.contentDataSerializer; } public static Builder create( final RenameContentParams params ) @@ -117,6 +121,7 @@ private Content updateValidState( final Content content ) .pageDescriptorService( this.pageDescriptorService ) .partDescriptorService( this.partDescriptorService ) .layoutDescriptorService( this.layoutDescriptorService ) + .contentDataSerializer( this.contentDataSerializer ) .build() .execute(); } @@ -132,6 +137,8 @@ public static class Builder private LayoutDescriptorService layoutDescriptorService; + private ContentDataSerializer contentDataSerializer; + Builder( final RenameContentParams params ) { this.params = params; @@ -155,6 +162,12 @@ Builder layoutDescriptorService( final LayoutDescriptorService value ) return this; } + Builder contentDataSerializer( final ContentDataSerializer value ) + { + this.contentDataSerializer = value; + return this; + } + @Override void validate() { diff --git a/modules/core/core-content/src/main/java/com/enonic/xp/core/impl/content/ReprocessContentCommand.java b/modules/core/core-content/src/main/java/com/enonic/xp/core/impl/content/ReprocessContentCommand.java index 2e34d96c2c8..c3022fc3feb 100644 --- a/modules/core/core-content/src/main/java/com/enonic/xp/core/impl/content/ReprocessContentCommand.java +++ b/modules/core/core-content/src/main/java/com/enonic/xp/core/impl/content/ReprocessContentCommand.java @@ -10,6 +10,7 @@ import com.enonic.xp.content.Media; import com.enonic.xp.content.ReprocessContentParams; import com.enonic.xp.content.UpdateMediaParams; +import com.enonic.xp.core.impl.content.serializer.ContentDataSerializer; import com.enonic.xp.media.MediaInfoService; import com.enonic.xp.node.Node; import com.enonic.xp.node.NodeId; @@ -35,6 +36,8 @@ final class ReprocessContentCommand private final LayoutDescriptorService layoutDescriptorService; + private final ContentDataSerializer contentDataSerializer; + private ReprocessContentCommand( final Builder builder ) { super( builder ); @@ -43,6 +46,7 @@ private ReprocessContentCommand( final Builder builder ) this.pageDescriptorService = builder.pageDescriptorService; this.partDescriptorService = builder.partDescriptorService; this.layoutDescriptorService = builder.layoutDescriptorService; + this.contentDataSerializer = builder.contentDataSerializer; } Content execute() @@ -80,6 +84,7 @@ private Content reprocessMedia( final Media media ) pageDescriptorService( this.pageDescriptorService ). partDescriptorService( this.partDescriptorService ). layoutDescriptorService( this.layoutDescriptorService ). + contentDataSerializer( this.contentDataSerializer ). build().execute(); } @@ -113,6 +118,8 @@ public static class Builder private LayoutDescriptorService layoutDescriptorService; + private ContentDataSerializer contentDataSerializer; + private Builder( final ReprocessContentParams params ) { this.params = params; @@ -142,6 +149,12 @@ public Builder layoutDescriptorService( final LayoutDescriptorService value ) return this; } + public Builder contentDataSerializer( final ContentDataSerializer value ) + { + this.contentDataSerializer = value; + return this; + } + @Override void validate() { diff --git a/modules/core/core-content/src/main/java/com/enonic/xp/core/impl/content/SyncContentServiceImpl.java b/modules/core/core-content/src/main/java/com/enonic/xp/core/impl/content/SyncContentServiceImpl.java index 7403bcccadf..fe3e47845f9 100644 --- a/modules/core/core-content/src/main/java/com/enonic/xp/core/impl/content/SyncContentServiceImpl.java +++ b/modules/core/core-content/src/main/java/com/enonic/xp/core/impl/content/SyncContentServiceImpl.java @@ -8,9 +8,13 @@ import com.enonic.xp.content.ProjectSyncParams; import com.enonic.xp.content.ResetContentInheritParams; import com.enonic.xp.content.SyncContentService; +import com.enonic.xp.core.impl.content.serializer.ContentDataSerializer; import com.enonic.xp.event.EventPublisher; import com.enonic.xp.node.NodeService; +import com.enonic.xp.page.PageDescriptorService; import com.enonic.xp.project.ProjectService; +import com.enonic.xp.region.LayoutDescriptorService; +import com.enonic.xp.region.PartDescriptorService; import com.enonic.xp.schema.content.ContentTypeService; @Component @@ -34,6 +38,9 @@ public class SyncContentServiceImpl @Activate public SyncContentServiceImpl( @Reference final ContentTypeService contentTypeService, @Reference final NodeService nodeService, @Reference final EventPublisher eventPublisher, + @Reference final PageDescriptorService pageDescriptorService, + @Reference final PartDescriptorService partDescriptorService, + @Reference final LayoutDescriptorService layoutDescriptorService, @Reference final ProjectService projectService, @Reference final ContentService contentService, @Reference final ContentSynchronizer contentSynchronizer ) { @@ -43,7 +50,14 @@ public SyncContentServiceImpl( @Reference final ContentTypeService contentTypeSe this.projectService = projectService; this.contentService = contentService; this.contentSynchronizer = contentSynchronizer; - this.translator = new ContentNodeTranslator( nodeService ); + + final ContentDataSerializer contentDataSerializer = ContentDataSerializer.create(). + layoutDescriptorService( layoutDescriptorService ). + pageDescriptorService( pageDescriptorService ). + partDescriptorService( partDescriptorService ). + build(); + + this.translator = new ContentNodeTranslator( nodeService, contentDataSerializer ); } @Override diff --git a/modules/core/core-content/src/main/java/com/enonic/xp/core/impl/content/UpdateContentCommand.java b/modules/core/core-content/src/main/java/com/enonic/xp/core/impl/content/UpdateContentCommand.java index e24f6134eca..b5f073f2281 100644 --- a/modules/core/core-content/src/main/java/com/enonic/xp/core/impl/content/UpdateContentCommand.java +++ b/modules/core/core-content/src/main/java/com/enonic/xp/core/impl/content/UpdateContentCommand.java @@ -34,6 +34,7 @@ import com.enonic.xp.content.processor.ContentProcessor; import com.enonic.xp.content.processor.ProcessUpdateParams; import com.enonic.xp.content.processor.ProcessUpdateResult; +import com.enonic.xp.core.impl.content.serializer.ContentDataSerializer; import com.enonic.xp.core.impl.content.validate.InputValidator; import com.enonic.xp.core.internal.HexCoder; import com.enonic.xp.core.internal.security.MessageDigests; @@ -69,6 +70,8 @@ final class UpdateContentCommand private final LayoutDescriptorService layoutDescriptorService; + private final ContentDataSerializer contentDataSerializer; + private UpdateContentCommand( final Builder builder ) { super( builder ); @@ -77,6 +80,7 @@ private UpdateContentCommand( final Builder builder ) this.pageDescriptorService = builder.pageDescriptorService; this.partDescriptorService = builder.partDescriptorService; this.layoutDescriptorService = builder.layoutDescriptorService; + this.contentDataSerializer = builder.contentDataSerializer; } public static Builder create( final UpdateContentParams params ) @@ -197,6 +201,7 @@ private Content doExecute() .pageDescriptorService( this.pageDescriptorService ) .partDescriptorService( this.partDescriptorService ) .layoutDescriptorService( this.layoutDescriptorService ) + .contentDataSerializer( this.contentDataSerializer ) .siteService( this.siteService ) .build() .produce(); @@ -361,6 +366,8 @@ public static class Builder private LayoutDescriptorService layoutDescriptorService; + private ContentDataSerializer contentDataSerializer; + Builder( final UpdateContentParams params ) { this.params = params; @@ -401,6 +408,12 @@ Builder layoutDescriptorService( final LayoutDescriptorService value ) return this; } + Builder contentDataSerializer( final ContentDataSerializer value ) + { + this.contentDataSerializer = value; + return this; + } + @Override void validate() { diff --git a/modules/core/core-content/src/main/java/com/enonic/xp/core/impl/content/UpdateMediaCommand.java b/modules/core/core-content/src/main/java/com/enonic/xp/core/impl/content/UpdateMediaCommand.java index f521e19b050..d2ec027e057 100644 --- a/modules/core/core-content/src/main/java/com/enonic/xp/core/impl/content/UpdateMediaCommand.java +++ b/modules/core/core-content/src/main/java/com/enonic/xp/core/impl/content/UpdateMediaCommand.java @@ -9,6 +9,7 @@ import com.enonic.xp.content.Content; import com.enonic.xp.content.UpdateContentParams; import com.enonic.xp.content.UpdateMediaParams; +import com.enonic.xp.core.impl.content.serializer.ContentDataSerializer; import com.enonic.xp.media.MediaInfo; import com.enonic.xp.media.MediaInfoService; import com.enonic.xp.page.PageDescriptorService; @@ -30,6 +31,8 @@ final class UpdateMediaCommand private final LayoutDescriptorService layoutDescriptorService; + private final ContentDataSerializer contentDataSerializer; + private UpdateMediaCommand( final Builder builder ) { super( builder ); @@ -38,6 +41,7 @@ private UpdateMediaCommand( final Builder builder ) this.pageDescriptorService = builder.pageDescriptorService; this.partDescriptorService = builder.partDescriptorService; this.layoutDescriptorService = builder.layoutDescriptorService; + this.contentDataSerializer = builder.contentDataSerializer; } public static Builder create( final UpdateMediaParams params ) @@ -112,6 +116,7 @@ private Content doExecute() .pageDescriptorService( this.pageDescriptorService ) .partDescriptorService( this.partDescriptorService ) .layoutDescriptorService( this.layoutDescriptorService ) + .contentDataSerializer( this.contentDataSerializer ) .build() .execute(); } @@ -129,6 +134,7 @@ public static class Builder private LayoutDescriptorService layoutDescriptorService; + private ContentDataSerializer contentDataSerializer; Builder( final UpdateMediaParams params ) { @@ -165,6 +171,12 @@ public Builder layoutDescriptorService( final LayoutDescriptorService value ) return this; } + public Builder contentDataSerializer( final ContentDataSerializer value ) + { + this.contentDataSerializer = value; + return this; + } + @Override void validate() { diff --git a/modules/core/core-content/src/main/java/com/enonic/xp/core/impl/content/UpdateNodeParamsFactory.java b/modules/core/core-content/src/main/java/com/enonic/xp/core/impl/content/UpdateNodeParamsFactory.java index 373ba3bf726..7a4e2f75048 100644 --- a/modules/core/core-content/src/main/java/com/enonic/xp/core/impl/content/UpdateNodeParamsFactory.java +++ b/modules/core/core-content/src/main/java/com/enonic/xp/core/impl/content/UpdateNodeParamsFactory.java @@ -44,6 +44,8 @@ public class UpdateNodeParamsFactory private final SiteService siteService; + private final ContentDataSerializer contentDataSerializer; + public UpdateNodeParamsFactory( final Builder builder ) { this.editedContent = builder.editedContent; @@ -55,6 +57,7 @@ public UpdateNodeParamsFactory( final Builder builder ) this.pageDescriptorService = builder.pageDescriptorService; this.partDescriptorService = builder.partDescriptorService; this.layoutDescriptorService = builder.layoutDescriptorService; + this.contentDataSerializer = builder.contentDataSerializer; this.siteService = builder.siteService; } @@ -81,7 +84,6 @@ public static Builder create() private NodeEditor toNodeEditor() { - final ContentDataSerializer contentDataSerializer = new ContentDataSerializer(); final PropertyTree nodeData = contentDataSerializer.toUpdateNodeData( editedContent, modifier, attachments ); final ContentIndexConfigFactory indexConfigFactory = ContentIndexConfigFactory.create(). @@ -127,6 +129,8 @@ public static class Builder private LayoutDescriptorService layoutDescriptorService; + private ContentDataSerializer contentDataSerializer; + private SiteService siteService; Builder editedContent( final Content editedContent ) @@ -189,6 +193,12 @@ Builder siteService( final SiteService value ) return this; } + Builder contentDataSerializer( final ContentDataSerializer contentDataSerializer ) + { + this.contentDataSerializer = contentDataSerializer; + return this; + } + void validate() { Preconditions.checkNotNull( modifier, "modifier cannot be null" ); @@ -201,6 +211,7 @@ void validate() Preconditions.checkNotNull( pageDescriptorService ); Preconditions.checkNotNull( partDescriptorService ); Preconditions.checkNotNull( layoutDescriptorService ); + Preconditions.checkNotNull( contentDataSerializer ); } public UpdateNodeParamsFactory build() diff --git a/modules/core/core-content/src/main/java/com/enonic/xp/core/impl/content/page/region/GetComponentByKeyCommand.java b/modules/core/core-content/src/main/java/com/enonic/xp/core/impl/content/page/region/GetComponentByKeyCommand.java index 859a64b884c..d943fd8d937 100644 --- a/modules/core/core-content/src/main/java/com/enonic/xp/core/impl/content/page/region/GetComponentByKeyCommand.java +++ b/modules/core/core-content/src/main/java/com/enonic/xp/core/impl/content/page/region/GetComponentByKeyCommand.java @@ -5,12 +5,12 @@ import com.enonic.xp.region.Component; import com.enonic.xp.region.LayoutComponent; import com.enonic.xp.region.LayoutDescriptor; +import com.enonic.xp.region.LayoutDescriptorNotFoundException; import com.enonic.xp.region.LayoutDescriptorService; -import com.enonic.xp.region.LayoutRegions; import com.enonic.xp.region.PartComponent; import com.enonic.xp.region.PartDescriptor; +import com.enonic.xp.region.PartDescriptorNotFoundException; import com.enonic.xp.region.PartDescriptorService; -import com.enonic.xp.region.Region; import com.enonic.xp.resource.ResourceKey; import com.enonic.xp.resource.ResourceService; @@ -38,21 +38,11 @@ public Component execute() { return LayoutComponent.create(). descriptor( layoutDescriptor.getKey() ). - regions( buildLayoutRegions( layoutDescriptor ) ). build(); } return null; } - private LayoutRegions buildLayoutRegions( final LayoutDescriptor layoutDescriptor ) - { - final LayoutRegions.Builder regionsBuilder = LayoutRegions.create(); - layoutDescriptor.getRegions() - .forEach( regionDescriptor -> regionsBuilder.add( Region.create().name( regionDescriptor.getName() ).build() ) ); - - return regionsBuilder.build(); - } - private boolean componentExists( final ResourceKey componentPath ) { return resourceService.getResource( componentPath.resolve( componentPath.getName() + ".xml" ) ).exists(); @@ -60,12 +50,26 @@ private boolean componentExists( final ResourceKey componentPath ) private PartDescriptor getPartDescriptor() { - return partDescriptorService.getByKey( descriptorKey ); + try + { + return partDescriptorService.getByKey( descriptorKey ); + } + catch ( PartDescriptorNotFoundException e ) + { + return null; + } } private LayoutDescriptor getLayoutDescriptor() { - return layoutDescriptorService.getByKey( descriptorKey ); + try + { + return layoutDescriptorService.getByKey( descriptorKey ); + } + catch ( LayoutDescriptorNotFoundException e ) + { + return null; + } } public GetComponentByKeyCommand descriptorKey( final DescriptorKey descriptorKey ) diff --git a/modules/core/core-content/src/main/java/com/enonic/xp/core/impl/content/serializer/ComponentDataSerializerProvider.java b/modules/core/core-content/src/main/java/com/enonic/xp/core/impl/content/serializer/ComponentDataSerializerProvider.java index 00b391c8227..2765eaf4d7f 100644 --- a/modules/core/core-content/src/main/java/com/enonic/xp/core/impl/content/serializer/ComponentDataSerializerProvider.java +++ b/modules/core/core-content/src/main/java/com/enonic/xp/core/impl/content/serializer/ComponentDataSerializerProvider.java @@ -1,10 +1,14 @@ package com.enonic.xp.core.impl.content.serializer; +import com.google.common.base.Preconditions; + import com.enonic.xp.region.ComponentType; import com.enonic.xp.region.FragmentComponentType; import com.enonic.xp.region.ImageComponentType; import com.enonic.xp.region.LayoutComponentType; +import com.enonic.xp.region.LayoutDescriptorService; import com.enonic.xp.region.PartComponentType; +import com.enonic.xp.region.PartDescriptorService; import com.enonic.xp.region.TextComponentType; public final class ComponentDataSerializerProvider @@ -21,12 +25,12 @@ public final class ComponentDataSerializerProvider private final RegionDataSerializer regionDataSerializer; - public ComponentDataSerializerProvider( ) + private ComponentDataSerializerProvider( final Builder builder ) { this.regionDataSerializer = new RegionDataSerializer( this ); this.partDataSerializer = new PartComponentDataSerializer(); this.textDataSerializer = new TextComponentDataSerializer(); - this.layoutDataSerializer = new LayoutComponentDataSerializer( this.regionDataSerializer ); + this.layoutDataSerializer = new LayoutComponentDataSerializer( builder.layoutDescriptorService, this.regionDataSerializer ); this.imageDataSerializer = new ImageComponentDataSerializer(); this.fragmentDataSerializer = new FragmentComponentDataSerializer(); } @@ -64,4 +68,40 @@ public RegionDataSerializer getRegionDataSerializer() return regionDataSerializer; } + public static Builder create() + { + return new Builder(); + } + + public static class Builder + { + private PartDescriptorService partDescriptorService; + + private LayoutDescriptorService layoutDescriptorService; + + public Builder partDescriptorService( final PartDescriptorService value ) + { + this.partDescriptorService = value; + return this; + } + + public Builder layoutDescriptorService( final LayoutDescriptorService value ) + { + this.layoutDescriptorService = value; + return this; + } + + void validate() + { + Preconditions.checkNotNull( partDescriptorService ); + Preconditions.checkNotNull( layoutDescriptorService ); + } + + public ComponentDataSerializerProvider build() + { + validate(); + return new ComponentDataSerializerProvider( this ); + } + } + } diff --git a/modules/core/core-content/src/main/java/com/enonic/xp/core/impl/content/serializer/ContentDataSerializer.java b/modules/core/core-content/src/main/java/com/enonic/xp/core/impl/content/serializer/ContentDataSerializer.java index 500552cd9b2..dd337d6dcac 100644 --- a/modules/core/core-content/src/main/java/com/enonic/xp/core/impl/content/serializer/ContentDataSerializer.java +++ b/modules/core/core-content/src/main/java/com/enonic/xp/core/impl/content/serializer/ContentDataSerializer.java @@ -9,6 +9,7 @@ import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; +import com.google.common.base.Preconditions; import com.enonic.xp.attachment.Attachment; import com.enonic.xp.attachment.AttachmentNames; @@ -37,7 +38,10 @@ import com.enonic.xp.json.ObjectMapperHelper; import com.enonic.xp.node.NodeId; import com.enonic.xp.page.Page; +import com.enonic.xp.page.PageDescriptorService; import com.enonic.xp.project.ProjectName; +import com.enonic.xp.region.LayoutDescriptorService; +import com.enonic.xp.region.PartDescriptorService; import com.enonic.xp.schema.content.ContentTypeName; import com.enonic.xp.security.PrincipalKey; import com.enonic.xp.util.BinaryReference; @@ -87,14 +91,24 @@ public class ContentDataSerializer private final PublishInfoSerializer publishInfoSerializer; - public ContentDataSerializer( ) + private ContentDataSerializer( final Builder builder ) { - this.pageDataSerializer = new PageDataSerializer(); + this.pageDataSerializer = PageDataSerializer.create() + .pageDescriptorService( builder.pageDescriptorService ) + .partDescriptorService( builder.partDescriptorService ) + .layoutDescriptorService( builder.layoutDescriptorService ) + .build(); + this.extraDataSerializer = new ExtraDataSerializer(); this.workflowInfoSerializer = new WorkflowInfoSerializer(); this.publishInfoSerializer = new PublishInfoSerializer(); } + public static Builder create() + { + return new Builder(); + } + public PropertyTree toCreateNodeData( final CreateContentTranslatorParams params ) { final PropertyTree propertyTree = new PropertyTree(); @@ -564,4 +578,45 @@ else if ( validationError instanceof AttachmentValidationError ) } ).toArray( PropertySet[]::new ) ); } } + + public static class Builder + { + private PageDescriptorService pageDescriptorService; + + private PartDescriptorService partDescriptorService; + + private LayoutDescriptorService layoutDescriptorService; + + public Builder pageDescriptorService( final PageDescriptorService value ) + { + this.pageDescriptorService = value; + return this; + } + + public Builder partDescriptorService( final PartDescriptorService value ) + { + this.partDescriptorService = value; + return this; + } + + public Builder layoutDescriptorService( final LayoutDescriptorService value ) + { + this.layoutDescriptorService = value; + return this; + } + + void validate() + { + Preconditions.checkNotNull( pageDescriptorService ); + Preconditions.checkNotNull( partDescriptorService ); + Preconditions.checkNotNull( layoutDescriptorService ); + } + + public ContentDataSerializer build() + { + validate(); + return new ContentDataSerializer( this ); + } + } + } diff --git a/modules/core/core-content/src/main/java/com/enonic/xp/core/impl/content/serializer/LayoutComponentDataSerializer.java b/modules/core/core-content/src/main/java/com/enonic/xp/core/impl/content/serializer/LayoutComponentDataSerializer.java index c6787af16b0..dd3351c83c8 100644 --- a/modules/core/core-content/src/main/java/com/enonic/xp/core/impl/content/serializer/LayoutComponentDataSerializer.java +++ b/modules/core/core-content/src/main/java/com/enonic/xp/core/impl/content/serializer/LayoutComponentDataSerializer.java @@ -4,24 +4,25 @@ import java.util.ArrayList; import java.util.List; -import com.google.common.collect.Iterators; - import com.enonic.xp.data.PropertySet; import com.enonic.xp.page.DescriptorKey; -import com.enonic.xp.region.ComponentPath; import com.enonic.xp.region.LayoutComponent; import com.enonic.xp.region.LayoutComponentType; +import com.enonic.xp.region.LayoutDescriptor; +import com.enonic.xp.region.LayoutDescriptorService; import com.enonic.xp.region.LayoutRegions; import com.enonic.xp.region.Region; -import com.enonic.xp.region.RegionDescriptors; final class LayoutComponentDataSerializer extends DescriptorBasedComponentDataSerializer { + private final LayoutDescriptorService layoutDescriptorService; + private final RegionDataSerializer regionDataSerializer; - LayoutComponentDataSerializer( final RegionDataSerializer regionDataSerializer ) + LayoutComponentDataSerializer( final LayoutDescriptorService layoutDescriptorService, final RegionDataSerializer regionDataSerializer ) { + this.layoutDescriptorService = layoutDescriptorService; this.regionDataSerializer = regionDataSerializer; } @@ -47,36 +48,33 @@ public LayoutComponent fromData( final PropertySet data ) public LayoutComponent fromData( final PropertySet layoutData, final List componentsAsData ) { - final PropertySet layoutDataSet = layoutData.getSet( LayoutComponentType.INSTANCE.toString() ); - final LayoutComponent.Builder layoutBuilder = LayoutComponent.create(); + final LayoutComponent.Builder layoutComponent = LayoutComponent.create(); + + final LayoutRegions.Builder layoutRegionsBuilder = LayoutRegions.create(); - if ( layoutDataSet != null && layoutDataSet.isNotNull( DESCRIPTOR ) ) + final PropertySet specialBlockSet = layoutData.getSet( LayoutComponentType.INSTANCE.toString() ); + + if ( specialBlockSet != null && specialBlockSet.isNotNull( DESCRIPTOR ) ) { - final DescriptorKey descriptorKey = DescriptorKey.from( layoutDataSet.getString( DESCRIPTOR ) ); + final DescriptorKey descriptorKey = DescriptorKey.from( specialBlockSet.getString( DESCRIPTOR ) ); - layoutBuilder - .descriptor( descriptorKey ) - .config( getConfigFromData( layoutDataSet, descriptorKey ) ) - .regions( buildLayoutRegions( layoutData.getString( PATH ), componentsAsData ) ); - } + layoutComponent.descriptor( descriptorKey ); + layoutComponent.config( getConfigFromData( specialBlockSet, descriptorKey ) ); - return layoutBuilder.build(); - } + final LayoutDescriptor layoutDescriptor = layoutDescriptorService.getByKey( descriptorKey ); - private LayoutRegions buildLayoutRegions( final String layoutPath, final List componentsAsData ) - { - final LayoutRegions.Builder layoutRegionsBuilder = LayoutRegions.create(); + final String layoutPath = layoutData.getString( PATH ); - getRegionDescriptors( layoutPath, componentsAsData ).forEach( regionDescriptor -> { - layoutRegionsBuilder.add( regionDataSerializer.fromData( regionDescriptor, layoutPath, componentsAsData ) ); - } ); + if ( layoutDescriptor.getRegions() != null && layoutDescriptor.getRegions().numberOfRegions() > 0 ) + { + layoutDescriptor.getRegions().forEach( regionDescriptor -> { + layoutRegionsBuilder.add( regionDataSerializer.fromData( regionDescriptor, layoutPath, componentsAsData ) ); + } ); + } + } - return layoutRegionsBuilder.build(); - } + layoutComponent.regions( layoutRegionsBuilder.build() ); - private RegionDescriptors getRegionDescriptors( final String layoutPath, final List componentsAsData ) - { - final int childrenLevel = Iterators.size( ComponentPath.from( layoutPath ).iterator() ) + 1; - return regionDataSerializer.getRegionDescriptorsAtLevel( childrenLevel, componentsAsData ); + return layoutComponent.build(); } } diff --git a/modules/core/core-content/src/main/java/com/enonic/xp/core/impl/content/serializer/PageDataSerializer.java b/modules/core/core-content/src/main/java/com/enonic/xp/core/impl/content/serializer/PageDataSerializer.java index d7a079d4df7..c8e8a5069e7 100644 --- a/modules/core/core-content/src/main/java/com/enonic/xp/core/impl/content/serializer/PageDataSerializer.java +++ b/modules/core/core-content/src/main/java/com/enonic/xp/core/impl/content/serializer/PageDataSerializer.java @@ -1,17 +1,24 @@ package com.enonic.xp.core.impl.content.serializer; +import java.util.Comparator; import java.util.List; import java.util.stream.Collectors; +import com.google.common.base.Preconditions; + import com.enonic.xp.data.Property; import com.enonic.xp.data.PropertySet; import com.enonic.xp.node.NodeId; import com.enonic.xp.page.DescriptorKey; import com.enonic.xp.page.Page; +import com.enonic.xp.page.PageDescriptor; +import com.enonic.xp.page.PageDescriptorService; import com.enonic.xp.page.PageRegions; import com.enonic.xp.page.PageTemplateKey; import com.enonic.xp.region.Component; import com.enonic.xp.region.ComponentPath; +import com.enonic.xp.region.LayoutDescriptorService; +import com.enonic.xp.region.PartDescriptorService; import com.enonic.xp.region.Region; import com.enonic.xp.region.RegionDescriptors; import com.enonic.xp.util.Reference; @@ -34,9 +41,16 @@ final class PageDataSerializer private final ComponentDataSerializerProvider componentDataSerializerProvider; - PageDataSerializer( ) + private final PageDescriptorService pageDescriptorService; + + private PageDataSerializer( final Builder builder ) { - this.componentDataSerializerProvider = new ComponentDataSerializerProvider(); + this.pageDescriptorService = builder.pageDescriptorService; + + this.componentDataSerializerProvider = ComponentDataSerializerProvider.create(). + layoutDescriptorService( builder.layoutDescriptorService ). + partDescriptorService( builder.partDescriptorService ). + build(); } @Override @@ -107,20 +121,32 @@ private void addRegions( final Page page, final PropertySet asSet ) @Override public Page fromData( final PropertySet asSet ) { - final List componentsAsData = asSet.getProperties( COMPONENTS ) - .stream() - .filter( Property::hasNotNullValue ) - .map( Property::getSet ) - .collect( Collectors.toList() ); + final List componentsAsData = asSet.getProperties( COMPONENTS ).stream().filter( Property::hasNotNullValue ). + map( Property::getSet ).collect( Collectors.toList() ); if ( componentsAsData.isEmpty() ) { return null; } + if ( !isRootComponent( componentsAsData.get( 0 ) ) ) + { + componentsAsData.sort( Comparator.comparing( this::getComponentPath ) ); + } + return fromData( componentsAsData ); } + private boolean isRootComponent( final PropertySet componentData ) + { + return getComponentPath( componentData ).equals( ComponentPath.DIVIDER ); + } + + private String getComponentPath( final PropertySet componentData ) + { + return componentData.getString( PATH ); + } + private Page fromData( final List componentsAsData ) { final PropertySet pageData = componentsAsData.get( 0 ); @@ -163,7 +189,7 @@ private Page fromPageData( final PropertySet pageData, final List c final DescriptorKey descriptorKey = DescriptorKey.from( specialBlockSet.getString( DESCRIPTOR ) ); page.descriptor( descriptorKey ); page.config( getConfigFromData( specialBlockSet, descriptorKey ) ); - page.regions( getPageRegions( componentsAsData ) ); + page.regions( getPageRegions( descriptorKey, componentsAsData ) ); } if ( specialBlockSet.isNotNull( TEMPLATE ) ) @@ -180,18 +206,70 @@ private Page fromPageData( final PropertySet pageData, final List c return page.build(); } - private PageRegions getPageRegions( final List componentsAsData ) + private PageRegions getPageRegions( final DescriptorKey descriptorKey, final List componentsAsData ) { - final RegionDescriptors regionDescriptors = - componentDataSerializerProvider.getRegionDataSerializer().getRegionDescriptorsAtLevel( 1, componentsAsData ); + final PageDescriptor pageDescriptor = pageDescriptorService.getByKey( descriptorKey ); + + final RegionDescriptors regionDescriptors = pageDescriptor.getRegions(); + + if ( regionDescriptors.numberOfRegions() == 0 ) + { + return null; + } final PageRegions.Builder pageRegionsBuilder = PageRegions.create(); regionDescriptors.forEach( regionDescriptor -> { - pageRegionsBuilder.add( componentDataSerializerProvider.getRegionDataSerializer() - .fromData( regionDescriptor, ComponentPath.DIVIDER, componentsAsData ) ); + pageRegionsBuilder.add( + componentDataSerializerProvider.getRegionDataSerializer().fromData( regionDescriptor, ComponentPath.DIVIDER, + componentsAsData ) ); } ); return pageRegionsBuilder.build(); } + + public static Builder create() + { + return new Builder(); + } + + public static class Builder + { + private PageDescriptorService pageDescriptorService; + + private PartDescriptorService partDescriptorService; + + private LayoutDescriptorService layoutDescriptorService; + + public Builder pageDescriptorService( final PageDescriptorService value ) + { + this.pageDescriptorService = value; + return this; + } + + public Builder partDescriptorService( final PartDescriptorService value ) + { + this.partDescriptorService = value; + return this; + } + + public Builder layoutDescriptorService( final LayoutDescriptorService value ) + { + this.layoutDescriptorService = value; + return this; + } + + void validate() + { + Preconditions.checkNotNull( pageDescriptorService ); + Preconditions.checkNotNull( partDescriptorService ); + Preconditions.checkNotNull( layoutDescriptorService ); + } + + public PageDataSerializer build() + { + validate(); + return new PageDataSerializer( this ); + } + } } diff --git a/modules/core/core-content/src/main/java/com/enonic/xp/core/impl/content/serializer/RegionDataSerializer.java b/modules/core/core-content/src/main/java/com/enonic/xp/core/impl/content/serializer/RegionDataSerializer.java index a2fe646e852..9bc9afd8470 100644 --- a/modules/core/core-content/src/main/java/com/enonic/xp/core/impl/content/serializer/RegionDataSerializer.java +++ b/modules/core/core-content/src/main/java/com/enonic/xp/core/impl/content/serializer/RegionDataSerializer.java @@ -5,8 +5,6 @@ import java.util.List; import java.util.stream.Collectors; -import com.google.common.collect.Iterators; - import com.enonic.xp.core.impl.content.page.region.ComponentTypes; import com.enonic.xp.data.PropertySet; import com.enonic.xp.region.Component; @@ -14,7 +12,6 @@ import com.enonic.xp.region.ComponentType; import com.enonic.xp.region.Region; import com.enonic.xp.region.RegionDescriptor; -import com.enonic.xp.region.RegionDescriptors; import static com.enonic.xp.core.impl.content.serializer.ComponentDataSerializer.PATH; import static com.enonic.xp.core.impl.content.serializer.ComponentDataSerializer.TYPE; @@ -86,20 +83,6 @@ private int getLevel( final String path ) return path.split( ComponentPath.DIVIDER ).length; } - public RegionDescriptors getRegionDescriptorsAtLevel( final int level, final List componentsAsData ) { - final RegionDescriptors.Builder builder = RegionDescriptors.create(); - - componentsAsData.stream() - .map( componentSet -> ComponentPath.from( componentSet.getString( PATH ) ) ) - .filter( path -> Iterators.size( path.iterator() ) == level ) - .map( path -> Iterators.getLast( path.iterator() ).getRegionName() ) - .distinct() - .map( regionName -> RegionDescriptor.create().name( regionName ).build() ) - .forEach( builder::add ); - - return builder.build(); - } - Component getComponent( final PropertySet componentData, final List componentsAsData ) { final ComponentType type = ComponentTypes.byShortName( componentData.getString( TYPE ) ); diff --git a/modules/core/core-content/src/test/java/com/enonic/xp/core/impl/content/ContentNodeTranslatorTest.java b/modules/core/core-content/src/test/java/com/enonic/xp/core/impl/content/ContentNodeTranslatorTest.java index 4228521a3d7..98d9dbc648c 100644 --- a/modules/core/core-content/src/test/java/com/enonic/xp/core/impl/content/ContentNodeTranslatorTest.java +++ b/modules/core/core-content/src/test/java/com/enonic/xp/core/impl/content/ContentNodeTranslatorTest.java @@ -10,6 +10,7 @@ import com.enonic.xp.content.ContentPath; import com.enonic.xp.content.ContentPropertyNames; import com.enonic.xp.content.Contents; +import com.enonic.xp.core.impl.content.serializer.ContentDataSerializer; import com.enonic.xp.data.PropertySet; import com.enonic.xp.data.PropertyTree; import com.enonic.xp.node.Node; @@ -17,6 +18,9 @@ import com.enonic.xp.node.NodePath; import com.enonic.xp.node.NodeService; import com.enonic.xp.node.Nodes; +import com.enonic.xp.page.PageDescriptorService; +import com.enonic.xp.region.LayoutDescriptorService; +import com.enonic.xp.region.PartDescriptorService; import com.enonic.xp.schema.content.ContentTypeName; import static org.assertj.core.api.Assertions.assertThat; @@ -43,7 +47,17 @@ public void setUp() throws Exception { this.nodeService = Mockito.mock( NodeService.class ); - this.contentNodeTranslator = new ContentNodeTranslator( nodeService ); + final PartDescriptorService partDescriptorService = Mockito.mock( PartDescriptorService.class ); + final LayoutDescriptorService layoutDescriptorService = Mockito.mock( LayoutDescriptorService.class ); + final PageDescriptorService pageDescriptorService = Mockito.mock( PageDescriptorService.class ); + + final ContentDataSerializer contentDataSerializer = ContentDataSerializer.create() + .layoutDescriptorService( layoutDescriptorService ) + .pageDescriptorService( pageDescriptorService ) + .partDescriptorService( partDescriptorService ) + .build(); + + this.contentNodeTranslator = new ContentNodeTranslator( nodeService, contentDataSerializer ); } @Test diff --git a/modules/core/core-content/src/test/java/com/enonic/xp/core/impl/content/ContentOutboundDependenciesIdsResolverTest.java b/modules/core/core-content/src/test/java/com/enonic/xp/core/impl/content/ContentOutboundDependenciesIdsResolverTest.java index 90326088348..36d6d3a4120 100644 --- a/modules/core/core-content/src/test/java/com/enonic/xp/core/impl/content/ContentOutboundDependenciesIdsResolverTest.java +++ b/modules/core/core-content/src/test/java/com/enonic/xp/core/impl/content/ContentOutboundDependenciesIdsResolverTest.java @@ -14,6 +14,7 @@ import com.enonic.xp.content.Contents; import com.enonic.xp.content.ExtraData; import com.enonic.xp.content.ExtraDatas; +import com.enonic.xp.core.impl.content.serializer.ContentDataSerializer; import com.enonic.xp.data.PropertyTree; import com.enonic.xp.schema.content.ContentTypeName; import com.enonic.xp.schema.xdata.XDataName; @@ -35,7 +36,7 @@ public void setUp() throws Exception { this.contentService = Mockito.mock( ContentService.class ); - this.resolver = new ContentOutboundDependenciesIdsResolver( contentService ); + this.resolver = new ContentOutboundDependenciesIdsResolver( contentService, Mockito.mock( ContentDataSerializer.class ) ); } private Content createContent( final String id, final PropertyTree data, final ContentTypeName contentTypeName ) diff --git a/modules/core/core-content/src/test/java/com/enonic/xp/core/impl/content/CreateContentCommandTest.java b/modules/core/core-content/src/test/java/com/enonic/xp/core/impl/content/CreateContentCommandTest.java index eb8fb50cf4d..58a554b67a4 100644 --- a/modules/core/core-content/src/test/java/com/enonic/xp/core/impl/content/CreateContentCommandTest.java +++ b/modules/core/core-content/src/test/java/com/enonic/xp/core/impl/content/CreateContentCommandTest.java @@ -17,6 +17,7 @@ import com.enonic.xp.content.ContentPath; import com.enonic.xp.content.ContentPropertyNames; import com.enonic.xp.content.CreateContentParams; +import com.enonic.xp.core.impl.content.serializer.ContentDataSerializer; import com.enonic.xp.core.impl.schema.content.BuiltinContentTypesAccessor; import com.enonic.xp.data.PropertySet; import com.enonic.xp.data.PropertyTree; @@ -69,6 +70,8 @@ public class CreateContentCommandTest private EventPublisher eventPublisher; + private ContentDataSerializer contentDataSerializer; + @BeforeEach public void setUp() throws Exception @@ -82,7 +85,13 @@ public void setUp() this.partDescriptorService = Mockito.mock( PartDescriptorService.class ); this.layoutDescriptorService = Mockito.mock( LayoutDescriptorService.class ); - this.translator = new ContentNodeTranslator( nodeService ); + this.contentDataSerializer = ContentDataSerializer.create() + .layoutDescriptorService( layoutDescriptorService ) + .pageDescriptorService( pageDescriptorService ) + .partDescriptorService( partDescriptorService ) + .build(); + + this.translator = new ContentNodeTranslator( nodeService, contentDataSerializer ); Mockito.when( this.nodeService.hasChildren( Mockito.any( Node.class ) ) ).thenReturn( false ); Mockito.when( this.nodeService.create( Mockito.any( CreateNodeParams.class ) ) ).thenAnswer( this::mockNodeServiceCreate ); @@ -591,6 +600,7 @@ private CreateContentCommand createContentCommand( CreateContentParams params ) .xDataService( this.xDataService ) .siteService( this.siteService ) .pageDescriptorService( this.pageDescriptorService ) + .contentDataSerializer( contentDataSerializer ) .formDefaultValuesProcessor( ( form, data ) -> { } ) .build(); diff --git a/modules/core/core-content/src/test/java/com/enonic/xp/core/impl/content/ImportContentFactoryTest.java b/modules/core/core-content/src/test/java/com/enonic/xp/core/impl/content/ImportContentFactoryTest.java index 7a88e8009ca..a59f3cfc5d8 100644 --- a/modules/core/core-content/src/test/java/com/enonic/xp/core/impl/content/ImportContentFactoryTest.java +++ b/modules/core/core-content/src/test/java/com/enonic/xp/core/impl/content/ImportContentFactoryTest.java @@ -17,9 +17,13 @@ import com.enonic.xp.content.ContentPath; import com.enonic.xp.content.ContentPublishInfo; import com.enonic.xp.content.ImportContentParams; +import com.enonic.xp.core.impl.content.serializer.ContentDataSerializer; import com.enonic.xp.data.PropertyTree; import com.enonic.xp.node.Node; +import com.enonic.xp.page.PageDescriptorService; import com.enonic.xp.project.ProjectName; +import com.enonic.xp.region.LayoutDescriptorService; +import com.enonic.xp.region.PartDescriptorService; import com.enonic.xp.schema.content.ContentTypeName; import com.enonic.xp.security.PrincipalKey; import com.enonic.xp.security.acl.AccessControlList; @@ -105,8 +109,21 @@ public void removePublishInfo() } + private ContentDataSerializer createContentDataSerializer() + { + final PageDescriptorService pageDescriptorService = Mockito.mock( PageDescriptorService.class ); + final PartDescriptorService partDescriptorService = Mockito.mock( PartDescriptorService.class ); + final LayoutDescriptorService layoutDescriptorService = Mockito.mock( LayoutDescriptorService.class ); + + return ContentDataSerializer.create() + .partDescriptorService( partDescriptorService ) + .pageDescriptorService( pageDescriptorService ) + .layoutDescriptorService( layoutDescriptorService ) + .build(); + } + private ImportContentFactory createFactory() { - return ImportContentFactory.create().params( this.params ).build(); + return ImportContentFactory.create().params( this.params ).contentDataSerializer( createContentDataSerializer() ).build(); } } diff --git a/modules/core/core-content/src/test/java/com/enonic/xp/core/impl/content/RenameContentCommandTest.java b/modules/core/core-content/src/test/java/com/enonic/xp/core/impl/content/RenameContentCommandTest.java index a9760ecb84f..da69ab46961 100644 --- a/modules/core/core-content/src/test/java/com/enonic/xp/core/impl/content/RenameContentCommandTest.java +++ b/modules/core/core-content/src/test/java/com/enonic/xp/core/impl/content/RenameContentCommandTest.java @@ -86,7 +86,12 @@ void setUp() this.pageDescriptorService = mock( PageDescriptorService.class ); this.partDescriptorService = mock( PartDescriptorService.class ); this.layoutDescriptorService = mock( LayoutDescriptorService.class ); - this.contentDataSerializer = new ContentDataSerializer(); + + this.contentDataSerializer = ContentDataSerializer.create() + .layoutDescriptorService( layoutDescriptorService ) + .pageDescriptorService( pageDescriptorService ) + .partDescriptorService( partDescriptorService ) + .build(); final ContentType contentType = ContentType.create().superType( ContentTypeName.documentMedia() ).name( ContentTypeName.dataMedia() ).build(); @@ -199,6 +204,7 @@ private RenameContentCommand createCommand( final RenameContentParams params ) .partDescriptorService( this.partDescriptorService ) .contentValidators( List.of( new ContentNameValidator() ) ) .layoutDescriptorService( this.layoutDescriptorService ) + .contentDataSerializer( this.contentDataSerializer ) .build(); } } diff --git a/modules/core/core-content/src/test/java/com/enonic/xp/core/impl/content/page/AbstractDataSerializerTest.java b/modules/core/core-content/src/test/java/com/enonic/xp/core/impl/content/page/AbstractDataSerializerTest.java index 9f0d08ea6ac..a0413143a07 100644 --- a/modules/core/core-content/src/test/java/com/enonic/xp/core/impl/content/page/AbstractDataSerializerTest.java +++ b/modules/core/core-content/src/test/java/com/enonic/xp/core/impl/content/page/AbstractDataSerializerTest.java @@ -21,12 +21,15 @@ public class AbstractDataSerializerTest { protected PartDescriptorService partDescriptorService; + protected LayoutDescriptorService layoutDescriptorService; + protected ContentService contentService; @BeforeEach void setUpAbstractDataSerializerTest() { this.partDescriptorService = Mockito.mock( PartDescriptorService.class ); + this.layoutDescriptorService = Mockito.mock( LayoutDescriptorService.class ); this.contentService = Mockito.mock( ContentService.class ); } diff --git a/modules/core/core-content/src/test/java/com/enonic/xp/core/impl/content/serializer/ContentDataSerializerTest.java b/modules/core/core-content/src/test/java/com/enonic/xp/core/impl/content/serializer/ContentDataSerializerTest.java index d55e4d1aad0..bbc862a2c33 100644 --- a/modules/core/core-content/src/test/java/com/enonic/xp/core/impl/content/serializer/ContentDataSerializerTest.java +++ b/modules/core/core-content/src/test/java/com/enonic/xp/core/impl/content/serializer/ContentDataSerializerTest.java @@ -3,6 +3,7 @@ import java.util.Map; import org.junit.jupiter.api.Test; +import org.mockito.Mockito; import com.google.common.io.ByteSource; @@ -27,6 +28,9 @@ import com.enonic.xp.data.PropertySet; import com.enonic.xp.data.PropertyTree; import com.enonic.xp.index.ChildOrder; +import com.enonic.xp.page.PageDescriptorService; +import com.enonic.xp.region.LayoutDescriptorService; +import com.enonic.xp.region.PartDescriptorService; import com.enonic.xp.schema.content.ContentTypeName; import com.enonic.xp.schema.xdata.XDataName; import com.enonic.xp.security.PrincipalKey; @@ -41,7 +45,7 @@ public class ContentDataSerializerTest @Test public void create_propertyTree_populated_with_attachment_properties() { - final ContentDataSerializer contentDataSerializer = new ContentDataSerializer(); + final ContentDataSerializer contentDataSerializer = createContentDataSerializer(); final String binaryName = "myName"; final String binaryLabel = "myLabel"; @@ -78,7 +82,7 @@ public void create_propertyTree_populated_with_attachment_properties() @Test public void update_propertyTree_populated_with_new_attachment_properties() { - final ContentDataSerializer contentDataSerializer = new ContentDataSerializer(); + final ContentDataSerializer contentDataSerializer = createContentDataSerializer(); final String binaryName = "myName"; final String binaryLabel = "myLabel"; @@ -111,7 +115,7 @@ public void update_propertyTree_populated_with_new_attachment_properties() @Test public void update_validationErrors() { - final ContentDataSerializer contentDataSerializer = new ContentDataSerializer(); + final ContentDataSerializer contentDataSerializer = createContentDataSerializer(); final ValidationErrors validationErrors = ValidationErrors.create() .add( ValidationError.attachmentError( ValidationErrorCode.from( ApplicationKey.SYSTEM, "SOME_CODE" ), @@ -143,7 +147,7 @@ public void update_validationErrors() @Test public void create_propertyTree_populated_with_extraData() { - final ContentDataSerializer contentDataSerializer = new ContentDataSerializer(); + final ContentDataSerializer contentDataSerializer = createContentDataSerializer(); PropertyTree mixinData = new PropertyTree(); mixinData.setString( "myKey1", "myValue1" ); @@ -179,7 +183,7 @@ public void create_propertyTree_populated_with_extraData() @Test public void update_propertyTree_populated_with_extraData() { - final ContentDataSerializer contentDataSerializer = new ContentDataSerializer(); + final ContentDataSerializer contentDataSerializer = createContentDataSerializer(); PropertyTree mixinData = new PropertyTree(); mixinData.setString( "myKey1", "myValue1" ); @@ -213,7 +217,7 @@ public void update_propertyTree_populated_with_extraData() @Test public void create_add_content_data() { - final ContentDataSerializer contentDataSerializer = new ContentDataSerializer(); + final ContentDataSerializer contentDataSerializer = createContentDataSerializer(); final PropertyTree propertyTree = new PropertyTree(); propertyTree.setString( "myData", "myValue" ); @@ -238,7 +242,7 @@ public void create_add_content_data() @Test public void update_add_content_data() { - final ContentDataSerializer contentDataSerializer = new ContentDataSerializer(); + final ContentDataSerializer contentDataSerializer = createContentDataSerializer(); final PropertyTree propertyTree = new PropertyTree(); propertyTree.setString( "myData", "myValue" ); @@ -256,6 +260,19 @@ public void update_add_content_data() assertEquals( "myValue", contentData.getString( "myData" ) ); } + private ContentDataSerializer createContentDataSerializer() + { + final PageDescriptorService pageDescriptorService = Mockito.mock( PageDescriptorService.class ); + final PartDescriptorService partDescriptorService = Mockito.mock( PartDescriptorService.class ); + final LayoutDescriptorService layoutDescriptorService = Mockito.mock( LayoutDescriptorService.class ); + + return ContentDataSerializer.create() + .partDescriptorService( partDescriptorService ) + .pageDescriptorService( pageDescriptorService ) + .layoutDescriptorService( layoutDescriptorService ) + .build(); + } + @Test public void create_propertyTree_populated_with_workflowInfo() { @@ -270,7 +287,7 @@ public void create_propertyTree_populated_with_workflowInfo() .checks( Map.of( check1Name, check1State, check2Name, check2State ) ) .build(); - final ContentDataSerializer contentDataSerializer = new ContentDataSerializer(); + final ContentDataSerializer contentDataSerializer = createContentDataSerializer(); final CreateContentTranslatorParams params = CreateContentTranslatorParams.create() .parent( ContentPath.ROOT ) @@ -296,7 +313,7 @@ public void create_propertyTree_populated_with_workflowInfo() @Test public void update_add_workflow_info() { - final ContentDataSerializer contentDataSerializer = new ContentDataSerializer(); + final ContentDataSerializer contentDataSerializer = createContentDataSerializer(); final PropertyTree propertyTree = new PropertyTree(); propertyTree.setString( "myData", "myValue" ); diff --git a/modules/core/core-content/src/test/java/com/enonic/xp/core/impl/content/serializer/LayoutDataSerializerTest.java b/modules/core/core-content/src/test/java/com/enonic/xp/core/impl/content/serializer/LayoutDataSerializerTest.java index 606b5b188b0..31924b36a00 100644 --- a/modules/core/core-content/src/test/java/com/enonic/xp/core/impl/content/serializer/LayoutDataSerializerTest.java +++ b/modules/core/core-content/src/test/java/com/enonic/xp/core/impl/content/serializer/LayoutDataSerializerTest.java @@ -1,92 +1,99 @@ package com.enonic.xp.core.impl.content.serializer; -import java.util.Arrays; import java.util.List; import java.util.stream.Collectors; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; +import org.mockito.Mockito; import com.enonic.xp.core.impl.content.page.AbstractDataSerializerTest; import com.enonic.xp.data.Property; import com.enonic.xp.data.PropertySet; import com.enonic.xp.data.PropertyTree; +import com.enonic.xp.form.Form; import com.enonic.xp.page.DescriptorKey; import com.enonic.xp.region.LayoutComponent; +import com.enonic.xp.region.LayoutDescriptor; import com.enonic.xp.region.LayoutRegions; import com.enonic.xp.region.Region; +import com.enonic.xp.region.RegionDescriptor; +import com.enonic.xp.region.RegionDescriptors; import com.enonic.xp.region.TextComponent; import static com.enonic.xp.core.impl.content.serializer.ComponentDataSerializer.COMPONENTS; import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertTrue; -public class LayoutDataSerializerTest - extends AbstractDataSerializerTest +public class LayoutDataSerializerTest extends AbstractDataSerializerTest { private LayoutComponentDataSerializer layoutDataSerializer; @BeforeEach public void setUp() { - final RegionDataSerializer regionSerializer = new RegionDataSerializer( new ComponentDataSerializerProvider() ); + final RegionDataSerializer regionSerializer = new RegionDataSerializer( ComponentDataSerializerProvider.create(). + layoutDescriptorService( layoutDescriptorService ). + partDescriptorService( partDescriptorService ). + build() ); - this.layoutDataSerializer = new LayoutComponentDataSerializer( regionSerializer ); + this.layoutDataSerializer = new LayoutComponentDataSerializer( this.layoutDescriptorService, regionSerializer ); } @Test public void testLayoutWithTwoRegions() { - final DescriptorKey layoutDescriptorKey = DescriptorKey.from( "layoutDescriptor:name" ); - genAndCheckLayout( layoutDescriptorKey, "left", "right" ); + genAndCheckLayout( "left", "right"); } @Test public void testLayoutWithRegionsWithSimilarNames() { - final DescriptorKey layoutDescriptorKey = DescriptorKey.from( "layoutDescriptor:name" ); - genAndCheckLayout( layoutDescriptorKey, "left", "left2" ); - } - - @Test - public void testLayoutWithDescriptorServiceNotAvailable() - { - final DescriptorKey layoutDescriptorKey = DescriptorKey.from( "layoutDescriptor:name" ); - genAndCheckLayout( layoutDescriptorKey, "up", "down" ); + genAndCheckLayout( "left", "left2"); } - @Test - public void testNoComponents() + private void genAndCheckLayout( final String regionName1, final String regionName2 ) { - final LayoutComponent parsedLayout = layoutDataSerializer.fromData( new PropertySet() ); - assertTrue(parsedLayout.getRegions().isEmpty()); - } + final LayoutComponent layoutComponent = createLayoutComponent( regionName1, regionName2 ); - private void genAndCheckLayout( final DescriptorKey layoutDescriptorKey, final String... regionNames ) - { - final LayoutComponent layoutComponent = createLayoutComponent( layoutDescriptorKey, regionNames ); final PropertyTree layoutAsData = new PropertyTree(); - layoutDataSerializer.toData( layoutComponent, layoutAsData.getRoot() ); - - final List componentsAsData = layoutAsData.getProperties( COMPONENTS ) - .stream() - .filter( Property::hasNotNullValue ) - .map( Property::getSet ) - .collect( Collectors.toList() ); - + final List componentsAsData = layoutAsData.getProperties( COMPONENTS ).stream().filter( Property::hasNotNullValue ). + map( Property::getSet ).collect( Collectors.toList() ); final LayoutComponent parsedLayout = layoutDataSerializer.fromData( layoutAsData.getSet( COMPONENTS ), componentsAsData ); assertEquals( layoutComponent, parsedLayout ); } - private LayoutComponent createLayoutComponent( final DescriptorKey layoutDescriptorKey, final String... regionNames ) + private LayoutComponent createLayoutComponent( final String regionName1, final String regionName2) { - final LayoutRegions.Builder layoutRegionsBuilder = LayoutRegions.create(); - Arrays.stream( regionNames ) - .map( regionName -> Region.create().name( regionName ).add( TextComponent.create().build() ).build() ) - .forEach( layoutRegionsBuilder::add ); + final String layoutName = "MyLayout"; + final DescriptorKey layoutDescriptorKey = DescriptorKey.from( "layoutDescriptor:name" ); - return LayoutComponent.create().descriptor( layoutDescriptorKey ).regions( layoutRegionsBuilder.build() ).build(); + final Region region1 = Region.create(). + name( regionName1 ). + build(); + + final Region region2 = Region.create(). + name( regionName2 ). + add( TextComponent.create().build() ). + build(); + + final LayoutRegions layoutRegions = LayoutRegions.create().add( region1 ).add( region2 ).build(); + + Mockito.when( layoutDescriptorService.getByKey( layoutDescriptorKey ) ).thenReturn( LayoutDescriptor.create(). + key( layoutDescriptorKey ). + displayName( layoutName ). + config( Form.create().build() ). + regions( RegionDescriptors.create(). + add( RegionDescriptor.create(). + name( regionName1 ). + build() ). + add( RegionDescriptor.create(). + name( regionName2 ). + build() ). + build() ). + build() ); + + return LayoutComponent.create().descriptor( layoutDescriptorKey ).regions( layoutRegions ).build(); } } diff --git a/modules/core/core-content/src/test/java/com/enonic/xp/core/impl/content/serializer/PageDataSerializerTest.java b/modules/core/core-content/src/test/java/com/enonic/xp/core/impl/content/serializer/PageDataSerializerTest.java index 8dd0951d619..002171be2da 100644 --- a/modules/core/core-content/src/test/java/com/enonic/xp/core/impl/content/serializer/PageDataSerializerTest.java +++ b/modules/core/core-content/src/test/java/com/enonic/xp/core/impl/content/serializer/PageDataSerializerTest.java @@ -1,25 +1,36 @@ package com.enonic.xp.core.impl.content.serializer; +import java.util.ArrayList; +import java.util.List; +import java.util.stream.Collectors; + import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; +import org.mockito.Mockito; import com.enonic.xp.core.impl.content.page.AbstractDataSerializerTest; +import com.enonic.xp.data.Property; import com.enonic.xp.data.PropertySet; import com.enonic.xp.data.PropertyTree; +import com.enonic.xp.form.Form; import com.enonic.xp.page.DescriptorKey; import com.enonic.xp.page.Page; +import com.enonic.xp.page.PageDescriptor; +import com.enonic.xp.page.PageDescriptorService; import com.enonic.xp.page.PageRegions; import com.enonic.xp.page.PageTemplateKey; import com.enonic.xp.region.ImageComponent; import com.enonic.xp.region.LayoutComponent; +import com.enonic.xp.region.LayoutDescriptor; import com.enonic.xp.region.LayoutRegions; import com.enonic.xp.region.PartComponent; import com.enonic.xp.region.Region; +import com.enonic.xp.region.RegionDescriptor; +import com.enonic.xp.region.RegionDescriptors; import com.enonic.xp.region.TextComponent; import static com.enonic.xp.core.impl.content.serializer.ComponentDataSerializer.COMPONENTS; import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertNull; import static org.junit.jupiter.api.Assertions.assertTrue; public class PageDataSerializerTest @@ -27,10 +38,18 @@ public class PageDataSerializerTest { private PageDataSerializer pageDataSerializer; + private PageDescriptorService pageDescriptorService; + @BeforeEach public void setUp() { - this.pageDataSerializer = new PageDataSerializer(); + this.pageDescriptorService = Mockito.mock( PageDescriptorService.class ); + + this.pageDataSerializer = PageDataSerializer.create(). + pageDescriptorService( pageDescriptorService ). + partDescriptorService( partDescriptorService ). + layoutDescriptorService( layoutDescriptorService ). + build(); } @Test @@ -59,12 +78,6 @@ public void page_config() assertEquals( "42.0", pageOnlyData.getString( "page.config.app-key.d-name.aim" ) ); } - @Test - public void noComponentsNoPage() - { - assertNull( pageDataSerializer.fromData( new PropertySet() ) ); - } - @Test public void component_config() { @@ -78,6 +91,39 @@ public void component_config() assertEquals( "somevalue", componentOnlyData.getString( "part.config.app-descriptor-x.name-x.some" ) ); } + @Test + public void page_deserialize_custom_order_of_components() + { + final Page page = createPage(); + + final PropertyTree pageAsData = new PropertyTree(); + pageDataSerializer.toData( page, pageAsData.getRoot() ); + + final List componentsAsData = + pageAsData.getRoot().getProperties( COMPONENTS ).stream().filter( Property::hasNotNullValue ).map( + item -> item.getSet() ).collect( Collectors.toList() ); + + final List customOrdercomponentsAsData = new ArrayList<>(); + customOrdercomponentsAsData.add( componentsAsData.get( 7 ) ); + customOrdercomponentsAsData.add( componentsAsData.get( 11 ) ); + customOrdercomponentsAsData.add( componentsAsData.get( 4 ) ); + customOrdercomponentsAsData.add( componentsAsData.get( 3 ) ); + customOrdercomponentsAsData.add( componentsAsData.get( 1 ) ); + customOrdercomponentsAsData.add( componentsAsData.get( 10 ) ); + customOrdercomponentsAsData.add( componentsAsData.get( 5 ) ); + customOrdercomponentsAsData.add( componentsAsData.get( 6 ) ); + customOrdercomponentsAsData.add( componentsAsData.get( 8 ) ); + customOrdercomponentsAsData.add( componentsAsData.get( 2 ) ); + customOrdercomponentsAsData.add( componentsAsData.get( 0 ) ); + customOrdercomponentsAsData.add( componentsAsData.get( 9 ) ); + + final PropertySet root = new PropertySet(); + customOrdercomponentsAsData.forEach( componentData -> root.addSet( COMPONENTS, componentData ) ); + final Page parsedPage = pageDataSerializer.fromData( root ); + + assertEquals( page, parsedPage ); + } + @Test public void page_template() { @@ -103,9 +149,15 @@ public void page_with_regions_as_null() final Page page = Page.create(). config( pageConfig ). descriptor( pageDescriptorKey ). - regions( PageRegions.create().build() ). + regions( null ). build(); + Mockito.when( pageDescriptorService.getByKey( pageDescriptorKey ) ).thenReturn( PageDescriptor.create(). + config( Form.create().build() ). + key( pageDescriptorKey ). + regions( RegionDescriptors.create().build() ). + build() ); + final PropertyTree pageAsData = new PropertyTree(); pageDataSerializer.toData( page, pageAsData.getRoot() ); final Page parsedPage = pageDataSerializer.fromData( pageAsData.getRoot() ); @@ -114,6 +166,7 @@ public void page_with_regions_as_null() assertEquals( page, parsedPage ); } + @Test public void fragmentPage() { @@ -168,11 +221,21 @@ private Page createPage() regions( regions ). build(); + Mockito.when( pageDescriptorService.getByKey( pageDescriptorKey ) ).thenReturn( PageDescriptor.create(). + config( Form.create().build() ). + key( pageDescriptorKey ). + regions( RegionDescriptors.create(). + add( RegionDescriptor.create().name( regionName1 ).build() ). + add( RegionDescriptor.create().name( regionName2 ).build() ). + build() ). + build() ); + return page; } private LayoutComponent createLayoutComponent() { + final String layoutName = "MyLayout"; final DescriptorKey layoutDescriptorKey = DescriptorKey.from( "layoutDescriptor:name" ); final String regionName1 = "left"; final String regionName2 = "right"; @@ -196,6 +259,20 @@ private LayoutComponent createLayoutComponent() final LayoutRegions layoutRegions = LayoutRegions.create().add( region1 ).add( region2 ).build(); + Mockito.when( layoutDescriptorService.getByKey( layoutDescriptorKey ) ).thenReturn( LayoutDescriptor.create(). + key( layoutDescriptorKey ). + displayName( layoutName ). + config( Form.create().build() ). + regions( RegionDescriptors.create(). + add( RegionDescriptor.create(). + name( regionName1 ). + build() ). + add( RegionDescriptor.create(). + name( regionName2 ). + build() ). + build() ). + build() ); + return LayoutComponent.create().descriptor( layoutDescriptorKey ).regions( layoutRegions ).build(); } } diff --git a/modules/core/core-content/src/test/java/com/enonic/xp/core/impl/content/serializer/RegionDataSerializerTest.java b/modules/core/core-content/src/test/java/com/enonic/xp/core/impl/content/serializer/RegionDataSerializerTest.java index c282aa9695a..38a17176ccf 100644 --- a/modules/core/core-content/src/test/java/com/enonic/xp/core/impl/content/serializer/RegionDataSerializerTest.java +++ b/modules/core/core-content/src/test/java/com/enonic/xp/core/impl/content/serializer/RegionDataSerializerTest.java @@ -5,16 +5,20 @@ import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; +import org.mockito.Mockito; import com.enonic.xp.core.impl.content.page.AbstractDataSerializerTest; import com.enonic.xp.data.PropertySet; import com.enonic.xp.data.PropertyTree; +import com.enonic.xp.form.Form; import com.enonic.xp.page.DescriptorKey; import com.enonic.xp.region.ComponentPath; import com.enonic.xp.region.ImageComponent; import com.enonic.xp.region.LayoutComponent; +import com.enonic.xp.region.LayoutDescriptor; import com.enonic.xp.region.Region; import com.enonic.xp.region.RegionDescriptor; +import com.enonic.xp.region.RegionDescriptors; import static org.junit.jupiter.api.Assertions.assertEquals; @@ -26,7 +30,10 @@ public class RegionDataSerializerTest @BeforeEach public void setUp() { - this.regionSerializer = new RegionDataSerializer( new ComponentDataSerializerProvider() ); + this.regionSerializer = new RegionDataSerializer( ComponentDataSerializerProvider.create(). + layoutDescriptorService( layoutDescriptorService ). + partDescriptorService( partDescriptorService ). + build() ); } @Test @@ -34,6 +41,7 @@ public void region() { final PropertyTree myPartConfig = new PropertyTree(); myPartConfig.addString( "some", "config" ); + final String layoutName = "MyOtherPart"; final DescriptorKey layoutDescriptorKey = DescriptorKey.from( "app-descr:layout-name" ); final Region region = Region.create(). @@ -43,6 +51,13 @@ public void region() add( LayoutComponent.create().descriptor( layoutDescriptorKey ).build() ). build(); + Mockito.when( layoutDescriptorService.getByKey( layoutDescriptorKey ) ).thenReturn( LayoutDescriptor.create(). + key( layoutDescriptorKey ). + displayName( layoutName ). + config( Form.create().build() ). + regions( RegionDescriptors.create().build() ). + build() ); + final PropertyTree regionAsData = new PropertyTree(); // exercise diff --git a/modules/itest/itest-core/src/test/java/com/enonic/xp/core/content/ParentContentSynchronizerTest.java b/modules/itest/itest-core/src/test/java/com/enonic/xp/core/content/ParentContentSynchronizerTest.java index 89a2b8cf196..1fef94c69e3 100644 --- a/modules/itest/itest-core/src/test/java/com/enonic/xp/core/content/ParentContentSynchronizerTest.java +++ b/modules/itest/itest-core/src/test/java/com/enonic/xp/core/content/ParentContentSynchronizerTest.java @@ -62,7 +62,8 @@ void setUp() synchronizer = new ParentContentSynchronizer( this.contentService ); syncContentService = - new SyncContentServiceImpl( contentTypeService, nodeService, eventPublisher, projectService, contentService, synchronizer ); + new SyncContentServiceImpl( contentTypeService, nodeService, eventPublisher, pageDescriptorService, partDescriptorService, + layoutDescriptorService, projectService, contentService, synchronizer ); } private Content syncCreated( final ContentId contentId ) diff --git a/modules/itest/itest-core/src/test/java/com/enonic/xp/core/content/SyncContentServiceImplTest.java b/modules/itest/itest-core/src/test/java/com/enonic/xp/core/content/SyncContentServiceImplTest.java index 2a6954f3c9f..c767322562d 100644 --- a/modules/itest/itest-core/src/test/java/com/enonic/xp/core/content/SyncContentServiceImplTest.java +++ b/modules/itest/itest-core/src/test/java/com/enonic/xp/core/content/SyncContentServiceImplTest.java @@ -41,7 +41,8 @@ void setUp() synchronizer = new ParentContentSynchronizer( contentService ); syncContentService = - new SyncContentServiceImpl( contentTypeService, nodeService, eventPublisher, projectService, contentService, synchronizer ); + new SyncContentServiceImpl( contentTypeService, nodeService, eventPublisher, pageDescriptorService, partDescriptorService, + layoutDescriptorService, projectService, contentService, synchronizer ); } @Test diff --git a/modules/portal/portal-impl/src/main/java/com/enonic/xp/portal/impl/handler/render/ComponentHandler.java b/modules/portal/portal-impl/src/main/java/com/enonic/xp/portal/impl/handler/render/ComponentHandler.java index 4cc6aa80c48..88224fd6279 100644 --- a/modules/portal/portal-impl/src/main/java/com/enonic/xp/portal/impl/handler/render/ComponentHandler.java +++ b/modules/portal/portal-impl/src/main/java/com/enonic/xp/portal/impl/handler/render/ComponentHandler.java @@ -5,7 +5,6 @@ import org.osgi.service.component.annotations.Reference; import com.enonic.xp.content.ContentService; -import com.enonic.xp.page.PageDescriptorService; import com.enonic.xp.page.PageTemplateService; import com.enonic.xp.portal.PortalRequest; import com.enonic.xp.portal.PortalResponse; @@ -15,7 +14,6 @@ import com.enonic.xp.portal.impl.rendering.RendererDelegate; import com.enonic.xp.portal.postprocess.PostProcessor; import com.enonic.xp.region.ComponentPath; -import com.enonic.xp.region.LayoutDescriptorService; import com.enonic.xp.trace.Trace; import com.enonic.xp.trace.Tracer; import com.enonic.xp.web.WebRequest; @@ -35,23 +33,15 @@ public final class ComponentHandler private final ContentService contentService; - private final PageDescriptorService pageDescriptorService; - - private final LayoutDescriptorService layoutDescriptorService; - @Activate public ComponentHandler( @Reference final ContentService contentService, @Reference final RendererDelegate rendererDelegate, - @Reference final PageTemplateService pageTemplateService, @Reference final PostProcessor postProcessor, - @Reference final PageDescriptorService pageDescriptorService, - @Reference final LayoutDescriptorService layoutDescriptorService ) + @Reference final PageTemplateService pageTemplateService, @Reference final PostProcessor postProcessor ) { super( "component" ); this.contentService = contentService; this.rendererDelegate = rendererDelegate; this.pageTemplateService = pageTemplateService; this.postProcessor = postProcessor; - this.pageDescriptorService = pageDescriptorService; - this.layoutDescriptorService = layoutDescriptorService; } @Override @@ -73,7 +63,7 @@ protected PortalResponse doHandle( final WebRequest webRequest, final WebRespons worker.contentService = contentService; worker.contentResolver = new ContentResolver( contentService ); worker.rendererDelegate = rendererDelegate; - worker.pageResolver = new PageResolver( pageTemplateService, pageDescriptorService, layoutDescriptorService ); + worker.pageResolver = new PageResolver( pageTemplateService ); worker.postProcessor = postProcessor; final Trace trace = Tracer.newTrace( "renderComponent" ); if ( trace == null ) diff --git a/modules/portal/portal-impl/src/main/java/com/enonic/xp/portal/impl/handler/render/ComponentHandlerWorker.java b/modules/portal/portal-impl/src/main/java/com/enonic/xp/portal/impl/handler/render/ComponentHandlerWorker.java index e0b8e596cfa..a45051b8291 100644 --- a/modules/portal/portal-impl/src/main/java/com/enonic/xp/portal/impl/handler/render/ComponentHandlerWorker.java +++ b/modules/portal/portal-impl/src/main/java/com/enonic/xp/portal/impl/handler/render/ComponentHandlerWorker.java @@ -53,16 +53,19 @@ public PortalResponse execute() throws Exception { final ContentResolverResult resolvedContent = contentResolver.resolve( this.request ); + final Content content = resolvedContent.getContentOrElseThrow(); + final Site site = resolvedContent.getNearestSiteOrElseThrow(); + final PageResolverResult resolvedPage = pageResolver.resolve( request.getMode(), content, site ); + Component component = null; if ( content.getType().isFragment() ) { // fragment content, try resolving component path in Layout fragment - final Component fragmentComponent = resolvedPage.getEffectivePage().getFragment(); - + final Component fragmentComponent = content.getPage().getFragment(); if ( this.componentPath.isEmpty() ) { component = fragmentComponent; diff --git a/modules/portal/portal-impl/src/main/java/com/enonic/xp/portal/impl/handler/render/PageHandler.java b/modules/portal/portal-impl/src/main/java/com/enonic/xp/portal/impl/handler/render/PageHandler.java index ea8d16858c9..382a63b94a3 100644 --- a/modules/portal/portal-impl/src/main/java/com/enonic/xp/portal/impl/handler/render/PageHandler.java +++ b/modules/portal/portal-impl/src/main/java/com/enonic/xp/portal/impl/handler/render/PageHandler.java @@ -12,7 +12,6 @@ import com.enonic.xp.portal.impl.ContentResolver; import com.enonic.xp.portal.impl.rendering.RendererDelegate; import com.enonic.xp.portal.url.PortalUrlService; -import com.enonic.xp.region.LayoutDescriptorService; import com.enonic.xp.trace.Trace; import com.enonic.xp.trace.Tracer; import com.enonic.xp.web.WebRequest; @@ -31,8 +30,6 @@ public final class PageHandler private PageDescriptorService pageDescriptorService; - private LayoutDescriptorService layoutDescriptorService; - private PageTemplateService pageTemplateService; private PortalUrlService portalUrlService; @@ -57,8 +54,8 @@ protected PortalResponse doHandle( final WebRequest webRequest, final WebRespons final PageHandlerWorker worker = new PageHandlerWorker( (PortalRequest) webRequest ); worker.contentResolver = new ContentResolver( contentService ); worker.rendererDelegate = rendererDelegate; + worker.pageResolver = new PageResolver( pageTemplateService ); worker.pageDescriptorService = pageDescriptorService; - worker.pageResolver = new PageResolver( pageTemplateService, pageDescriptorService, layoutDescriptorService ); worker.portalUrlService = portalUrlService; final Trace trace = Tracer.newTrace( "renderComponent" ); if ( trace == null ) @@ -86,12 +83,6 @@ public void setPageDescriptorService( final PageDescriptorService pageDescriptor this.pageDescriptorService = pageDescriptorService; } - @Reference - public void setLayoutDescriptorService( final LayoutDescriptorService layoutDescriptorService ) - { - this.layoutDescriptorService = layoutDescriptorService; - } - @Reference public void setPageTemplateService( final PageTemplateService pageTemplateService ) { diff --git a/modules/portal/portal-impl/src/main/java/com/enonic/xp/portal/impl/handler/render/PageHandlerWorker.java b/modules/portal/portal-impl/src/main/java/com/enonic/xp/portal/impl/handler/render/PageHandlerWorker.java index a9e70b5f356..6178c519ac2 100644 --- a/modules/portal/portal-impl/src/main/java/com/enonic/xp/portal/impl/handler/render/PageHandlerWorker.java +++ b/modules/portal/portal-impl/src/main/java/com/enonic/xp/portal/impl/handler/render/PageHandlerWorker.java @@ -7,6 +7,7 @@ import com.enonic.xp.content.Content; import com.enonic.xp.data.Property; import com.enonic.xp.data.PropertySet; +import com.enonic.xp.page.DescriptorKey; import com.enonic.xp.page.PageDescriptorService; import com.enonic.xp.portal.PortalRequest; import com.enonic.xp.portal.PortalResponse; @@ -62,10 +63,12 @@ public PortalResponse execute() final Content effectiveContent = Content.create( content ).page( resolvedPage.getEffectivePage() ).build(); + final DescriptorKey pageDescriptorKey = resolvedPage.getController(); this.request.setSite( site ); this.request.setContent( effectiveContent ); - this.request.setApplicationKey( resolvedPage.getApplicationKey() ); - this.request.setPageDescriptor( resolvedPage.getPageDescriptor() ); + + this.request.setApplicationKey( pageDescriptorKey != null ? pageDescriptorKey.getApplicationKey() : null ); + this.request.setPageDescriptor( pageDescriptorKey != null ? this.pageDescriptorService.getByKey( pageDescriptorKey ) : null ); final Trace trace = Tracer.current(); if ( trace != null ) diff --git a/modules/portal/portal-impl/src/main/java/com/enonic/xp/portal/impl/handler/render/PageResolver.java b/modules/portal/portal-impl/src/main/java/com/enonic/xp/portal/impl/handler/render/PageResolver.java index bc80a9e8c9a..e3996c1133f 100644 --- a/modules/portal/portal-impl/src/main/java/com/enonic/xp/portal/impl/handler/render/PageResolver.java +++ b/modules/portal/portal-impl/src/main/java/com/enonic/xp/portal/impl/handler/render/PageResolver.java @@ -1,27 +1,15 @@ package com.enonic.xp.portal.impl.handler.render; -import com.google.common.collect.ImmutableList; - import com.enonic.xp.content.Content; import com.enonic.xp.content.ContentNotFoundException; import com.enonic.xp.content.ContentPath; import com.enonic.xp.page.DescriptorKey; import com.enonic.xp.page.GetDefaultPageTemplateParams; import com.enonic.xp.page.Page; -import com.enonic.xp.page.PageDescriptor; -import com.enonic.xp.page.PageDescriptorService; -import com.enonic.xp.page.PageRegions; import com.enonic.xp.page.PageTemplate; import com.enonic.xp.page.PageTemplateKey; import com.enonic.xp.page.PageTemplateService; import com.enonic.xp.portal.RenderMode; -import com.enonic.xp.region.Component; -import com.enonic.xp.region.LayoutComponent; -import com.enonic.xp.region.LayoutDescriptor; -import com.enonic.xp.region.LayoutDescriptorService; -import com.enonic.xp.region.LayoutRegions; -import com.enonic.xp.region.Region; -import com.enonic.xp.region.RegionDescriptor; import com.enonic.xp.schema.content.ContentTypeName; import com.enonic.xp.site.Site; import com.enonic.xp.web.HttpStatus; @@ -31,16 +19,9 @@ public class PageResolver { private final PageTemplateService pageTemplateService; - private final PageDescriptorService pageDescriptorService; - - private final LayoutDescriptorService layoutDescriptorService; - - public PageResolver( final PageTemplateService pageTemplateService, final PageDescriptorService pageDescriptorService, - final LayoutDescriptorService layoutDescriptorService ) + public PageResolver( final PageTemplateService pageTemplateService ) { this.pageTemplateService = pageTemplateService; - this.pageDescriptorService = pageDescriptorService; - this.layoutDescriptorService = layoutDescriptorService; } public PageResolverResult resolve( final RenderMode mode, final Content content, final Site site ) @@ -61,7 +42,7 @@ else if ( page != null ) if ( page.getFragment() != null ) { controller = null; - effectivePage = buildPageFromFragment( page ); + effectivePage = page; } else if ( page.getDescriptor() != null ) { @@ -108,12 +89,7 @@ else if ( page.getTemplate() != null ) } } - if ( controller == null ) - { - return new PageResolverResult( effectivePage, null, null ); - } - - return buildPageWithRegionsFromController( effectivePage, controller ); + return new PageResolverResult( effectivePage, controller ); } private PageTemplate getPageTemplateOrFindDefault( final PageTemplateKey pageTemplate, final ContentTypeName contentType, @@ -184,111 +160,4 @@ private static Page mergePageFromPageTemplate( final PageTemplate pageTemplate, } } - private Page buildPageFromFragment( final Page effectivePage ) - { - final Component fragmentComponent = effectivePage.getFragment(); - - if ( fragmentComponent instanceof LayoutComponent ) - { - final Page.Builder pageBuilder = Page.create( effectivePage ); - pageBuilder.fragment( processLayoutComponent( (LayoutComponent) fragmentComponent ) ); - return pageBuilder.build(); - } - - return effectivePage; - } - - private PageResolverResult buildPageWithRegionsFromController( final Page effectivePage, final DescriptorKey controller ) - { - final PageDescriptor pageDescriptor = pageDescriptorService.getByKey( controller ); - - if ( pageDescriptor == null || pageDescriptor.getModifiedTime() == null ) - { - return new PageResolverResult( effectivePage, controller, null ); - } - - final Page resultingPage = buildPageWithRegions( effectivePage, pageDescriptor ); - - return new PageResolverResult( resultingPage, controller, pageDescriptor ); - } - - private Page buildPageWithRegions( final Page sourcePage, final PageDescriptor pageDescriptor ) - { - final Page.Builder pageBuilder = Page.create( sourcePage ); - final PageRegions.Builder pageRegionsBuilder = PageRegions.create(); - - if ( pageDescriptor.getRegions() != null ) - { - pageDescriptor.getRegions().forEach( regionDescriptor -> { - pageRegionsBuilder.add( getOrCreatePageRegion( regionDescriptor, sourcePage ) ); - } ); - } - - return pageBuilder.regions( pageRegionsBuilder.build() ).build(); - } - - private Region getOrCreatePageRegion( final RegionDescriptor regionDescriptor, final Page sourcePage ) - { - final Region existingRegion = sourcePage.getRegion( regionDescriptor.getName() ); - - return existingRegion == null - ? Region.create().name( regionDescriptor.getName() ).build() - : processExistingPageRegion( existingRegion ); - } - - private Region processExistingPageRegion( final Region existingRegion ) - { - final Region.Builder builder = Region.create( existingRegion ); - final ImmutableList components = existingRegion.getComponents(); - - for ( int i = 0; i < components.size(); i++) - { - builder.set( i, processComponent( components.get( i ) ) ); - } - - return builder.build(); - } - - private Component processComponent( final Component component ) - { - if ( component instanceof LayoutComponent ) - { - return processLayoutComponent( (LayoutComponent) component ); - } - else - { - return component; - } - } - - private LayoutComponent processLayoutComponent( final LayoutComponent component ) - { - final LayoutDescriptor layoutDescriptor = - component.hasDescriptor() ? layoutDescriptorService.getByKey( component.getDescriptor() ) : null; - - if ( layoutDescriptor == null || layoutDescriptor.getModifiedTime() == null ) - { - return component; - } - - return buildLayoutWithRegions( component, layoutDescriptor ); - } - - private LayoutComponent buildLayoutWithRegions( final LayoutComponent existingLayout, final LayoutDescriptor layoutDescriptor ) - { - final LayoutComponent.Builder layoutBuilder = LayoutComponent.create( existingLayout ); - final LayoutRegions.Builder regionsBuilder = LayoutRegions.create(); - - if ( layoutDescriptor.getRegions() != null ) - { - layoutDescriptor.getRegions().forEach( region -> { - final Region existingRegion = existingLayout.getRegion( region.getName() ); - final Region regionToAdd = existingRegion == null ? Region.create().name( region.getName() ).build() : existingRegion; - regionsBuilder.add( regionToAdd ); - } ); - } - - return layoutBuilder.regions( regionsBuilder.build() ).build(); - } - } diff --git a/modules/portal/portal-impl/src/main/java/com/enonic/xp/portal/impl/handler/render/PageResolverResult.java b/modules/portal/portal-impl/src/main/java/com/enonic/xp/portal/impl/handler/render/PageResolverResult.java index 475af95d24a..1b14294d4b7 100644 --- a/modules/portal/portal-impl/src/main/java/com/enonic/xp/portal/impl/handler/render/PageResolverResult.java +++ b/modules/portal/portal-impl/src/main/java/com/enonic/xp/portal/impl/handler/render/PageResolverResult.java @@ -1,9 +1,7 @@ package com.enonic.xp.portal.impl.handler.render; -import com.enonic.xp.app.ApplicationKey; import com.enonic.xp.page.DescriptorKey; import com.enonic.xp.page.Page; -import com.enonic.xp.page.PageDescriptor; public final class PageResolverResult { @@ -11,13 +9,10 @@ public final class PageResolverResult private final DescriptorKey controller; - private final PageDescriptor pageDescriptor; - - public PageResolverResult( final Page effectivePage, final DescriptorKey controller, final PageDescriptor pageDescriptor ) + public PageResolverResult( final Page effectivePage, final DescriptorKey controller ) { this.effectivePage = effectivePage; this.controller = controller; - this.pageDescriptor = pageDescriptor; } public Page getEffectivePage() @@ -29,14 +24,4 @@ public DescriptorKey getController() { return controller; } - - public ApplicationKey getApplicationKey() - { - return this.pageDescriptor == null ? null : pageDescriptor.getApplicationKey(); - } - - public PageDescriptor getPageDescriptor() - { - return pageDescriptor; - } } diff --git a/modules/portal/portal-impl/src/main/java/com/enonic/xp/portal/impl/postprocess/instruction/ComponentInstruction.java b/modules/portal/portal-impl/src/main/java/com/enonic/xp/portal/impl/postprocess/instruction/ComponentInstruction.java index 8ecf6a93da0..8088b4d5d83 100644 --- a/modules/portal/portal-impl/src/main/java/com/enonic/xp/portal/impl/postprocess/instruction/ComponentInstruction.java +++ b/modules/portal/portal-impl/src/main/java/com/enonic/xp/portal/impl/postprocess/instruction/ComponentInstruction.java @@ -21,7 +21,6 @@ import com.enonic.xp.region.ComponentService; import com.enonic.xp.region.LayoutComponent; import com.enonic.xp.region.LayoutRegions; -import com.enonic.xp.region.Region; import com.enonic.xp.trace.Trace; import com.enonic.xp.trace.Tracer; @@ -53,14 +52,6 @@ public void setComponentService( final ComponentService componentService ) @Override public PortalResponse evaluate( final PortalRequest portalRequest, final String instruction ) - { - final String componentPath = getComponentPathFromInstruction( instruction ); - final PortalResponse portalResponse = componentPath == null ? null : renderComponent( portalRequest, componentPath ); - - return portalResponse; - } - - private String getComponentPathFromInstruction( final String instruction ) { if ( !Instruction.isInstruction( instruction, COMPONENT_INSTRUCTION_PREFIX ) ) { @@ -68,40 +59,34 @@ private String getComponentPathFromInstruction( final String instruction ) } final List list = Splitter.on( ' ' ).omitEmptyStrings().splitToList( instruction ); - if ( list.size() != 2 ) { return null; } - return list.get( 1 ); + final String path = list.get( 1 ); + return renderComponent( portalRequest, path ); } private PortalResponse renderComponent( final PortalRequest portalRequest, final String componentSelector ) { - final Component component = resolveComponent( portalRequest, componentSelector ); - return renderComponent( portalRequest, component ); - } - - private Component resolveComponent( final PortalRequest portalRequest, final String componentSelector ) - { + final Component component; if ( FRAGMENT_COMPONENT.equalsIgnoreCase( componentSelector ) ) { - return resolveFragmentComponent( portalRequest ); + component = getPageFragment( portalRequest ); } - - if ( componentSelector.startsWith( APPLICATION_COMPONENT_PREFIX ) ) + else if ( !componentSelector.startsWith( APPLICATION_COMPONENT_PREFIX ) ) { - return resolveComponentFromModule( componentSelector, portalRequest.getApplicationKey() ); + final ComponentPath componentPath = ComponentPath.from( componentSelector ); + component = resolveComponent( portalRequest, componentPath ); } - - return resolveComponent( portalRequest, ComponentPath.from( componentSelector ) ); - } - - private Component resolveComponentFromModule( final String componentSelector, final ApplicationKey currentApplication ) - { - final String name = componentSelector.substring( APPLICATION_COMPONENT_PREFIX.length() ); - return currentApplication == null ? null : componentService.getByKey( DescriptorKey.from( currentApplication, name ) ); + else + { + final String name = componentSelector.substring( APPLICATION_COMPONENT_PREFIX.length() ); + final ApplicationKey currentApplication = portalRequest.getApplicationKey(); + component = currentApplication == null ? null : componentService.getByKey( DescriptorKey.from( currentApplication, name ) ); + } + return renderComponent( portalRequest, component ); } private PortalResponse renderComponent( final PortalRequest portalRequest, final Component component ) @@ -120,7 +105,6 @@ private PortalResponse renderComponent( final PortalRequest portalRequest, final private Component resolveComponent( final PortalRequest portalRequest, final ComponentPath path ) { final Content content = portalRequest.getContent(); - if ( content == null ) { return null; @@ -132,116 +116,45 @@ private Component resolveComponent( final PortalRequest portalRequest, final Com } Component component = content.getPage().getRegions().getComponent( path ); - if ( component == null ) { throw new RenderException( MessageFormat.format( "Component not found: [{0}]", path ) ); } - if ( component instanceof LayoutComponent ) - { - return resolveLayoutWithRegions( (LayoutComponent) component, content.getPage() ); - } - return component; } - private LayoutComponent resolveLayoutWithRegions( final LayoutComponent existingLayout, final Page page ) - { - if ( !existingLayout.hasDescriptor() ) - { - return existingLayout; - } - - final LayoutComponent layoutFromDescriptor = (LayoutComponent) componentService.getByKey( existingLayout.getDescriptor() ); - - if ( layoutFromDescriptor == null ) - { - return existingLayout; - } - - final LayoutComponent layoutComponent = buildLayoutWithRegions( existingLayout, layoutFromDescriptor ); - setParentRegionOnLayout( page, existingLayout, layoutComponent ); - return layoutComponent; - } - - private LayoutComponent buildLayoutWithRegions( final LayoutComponent existingLayout, final LayoutComponent layoutFromDescriptor ) - { - final LayoutComponent.Builder layoutBuilder = LayoutComponent.create( existingLayout ); - final LayoutRegions.Builder regionsBuilder = LayoutRegions.create(); - - layoutFromDescriptor.getRegions().forEach( region -> { - final Region existingRegion = existingLayout.getRegion( region.getName() ); - final Region regionToAdd = existingRegion == null ? Region.create().name( region.getName() ).build() : existingRegion; - regionsBuilder.add( regionToAdd ); - } ); - - return layoutBuilder.regions( regionsBuilder.build() ).build(); - } - - private void setParentRegionOnLayout( final Page page, final LayoutComponent existingLayout, final LayoutComponent resultingLayout ) - { - if ( page == null || page.getRegions() == null ) - { - return; - } - - page.getRegions().forEach( region -> { - final int index = region.getIndex( existingLayout ); - - if ( index > -1 ) - { - Region.create( region ).set( index, resultingLayout ).build(); // The only way to set layout's parent region - } - } ); - } - private Component resolveComponentInFragment( final Content content, final ComponentPath path ) { final Component fragmentComponent = content.getPage().getFragment(); - if ( !( fragmentComponent instanceof LayoutComponent ) ) { throw new RenderException( MessageFormat.format( "Component not found: [{0}]", path ) ); } - final LayoutComponent layout = (LayoutComponent) fragmentComponent; + final LayoutRegions pageRegions = layout.getRegions(); final Component component = pageRegions.getComponent( path ); - if ( component == null ) { throw new RenderException( MessageFormat.format( "Component not found: [{0}]", path ) ); } - return component; } - private Component resolveFragmentComponent( final PortalRequest portalRequest ) - { - final Component fragmentComponent = getPageFragment( portalRequest ); - return processFragment( fragmentComponent ); - } - - private Component processFragment( final Component fragmentComponent ) - { - if ( fragmentComponent instanceof LayoutComponent ) - { - return resolveLayoutWithRegions( (LayoutComponent) fragmentComponent, null ); - } - - return fragmentComponent; - } - private Component getPageFragment( final PortalRequest portalRequest ) { final Content content = portalRequest.getContent(); - - if ( content == null || content.getPage() == null ) + if ( content == null ) { return null; } - return content.getPage().getFragment(); + final Page page = content.getPage(); + if ( page == null ) + { + return null; + } + return page.getFragment(); } } diff --git a/modules/portal/portal-impl/src/test/java/com/enonic/xp/portal/impl/handler/render/ComponentHandlerTest.java b/modules/portal/portal-impl/src/test/java/com/enonic/xp/portal/impl/handler/render/ComponentHandlerTest.java index e043d11fb87..e056155d036 100644 --- a/modules/portal/portal-impl/src/test/java/com/enonic/xp/portal/impl/handler/render/ComponentHandlerTest.java +++ b/modules/portal/portal-impl/src/test/java/com/enonic/xp/portal/impl/handler/render/ComponentHandlerTest.java @@ -15,12 +15,9 @@ import com.enonic.xp.page.PageRegions; import com.enonic.xp.page.PageTemplateKey; import com.enonic.xp.portal.PortalResponse; -import com.enonic.xp.region.Component; import com.enonic.xp.region.FragmentComponent; -import com.enonic.xp.region.LayoutComponent; -import com.enonic.xp.region.LayoutRegions; +import com.enonic.xp.region.PartComponent; import com.enonic.xp.region.Region; -import com.enonic.xp.region.TextComponent; import com.enonic.xp.schema.content.ContentTypeName; import com.enonic.xp.security.PrincipalKey; import com.enonic.xp.security.RoleKeys; @@ -48,8 +45,7 @@ public class ComponentHandlerTest public final void setup() throws Exception { - this.handler = new ComponentHandler( this.contentService, this.rendererDelegate, this.pageTemplateService, this.postProcessor, - this.pageDescriptorService, this.layoutDescriptorService ); + this.handler = new ComponentHandler( this.contentService, this.rendererDelegate, this.pageTemplateService, this.postProcessor ); this.request.setMethod( HttpMethod.GET ); this.request.setContentPath( ContentPath.from( "/site/somepath/content" ) ); @@ -139,7 +135,7 @@ public void getComponentPageNotFound() final WebException e = assertThrows( WebException.class, () -> this.handler.handle( this.request, PortalResponse.create().build(), null ) ); assertAll( () -> assertEquals( HttpStatus.NOT_FOUND, e.getStatus() ), - () -> assertEquals( "No default template found for content", e.getMessage() ) ); + () -> assertEquals( "No default template found for content", e.getMessage() )); } @Test @@ -190,85 +186,15 @@ public void getSiteNotFound() assertEquals( "Site for [/site/somepath/content] not found", e.getMessage() ); } - @Test - public void testEmptyFragmentIsResolved() - throws Exception - { - setupSite(); - setupContent( createPageWithFragment( null ) ); - setupTemplates(); - - final PortalResponse portalResponse = - PortalResponse.create().body( "component rendered" ).header( "some-header", "some-value" ).status( HttpStatus.OK ).build(); - - Mockito.when( this.postProcessor.processResponseInstructions( Mockito.any(), Mockito.any() ) ).thenReturn( portalResponse ); - - setRendererResult( portalResponse ); - - this.request.setEndpointPath( "/_/component/main-region/0" ); - - final WebResponse res = this.handler.handle( this.request, PortalResponse.create().build(), null ); - assertResponseOK( res ); - } - - @Test - public void testFragmentContentNotFound() - throws Exception - { - setupSite(); - setupContent( createPageWithFragment() ); - setupTemplates(); - - Mockito.when( this.contentService.getById( ContentId.from( "fragmentId" ) ) ).thenThrow( ContentNotFoundException.class ); - - final PortalResponse portalResponse = - PortalResponse.create().body( "component rendered" ).header( "some-header", "some-value" ).status( HttpStatus.OK ).build(); - - Mockito.when( this.postProcessor.processResponseInstructions( Mockito.any(), Mockito.any() ) ).thenReturn( portalResponse ); - - setRendererResult( portalResponse ); - - this.request.setEndpointPath( "/_/component/main-region/0" ); - - final WebResponse res = this.handler.handle( this.request, PortalResponse.create().build(), null ); - assertResponseOK( res ); - } - - @Test - public void testFragmentWithoutComponentsIsResolved() - throws Exception - { - setupSite(); - setupContent( createPageWithFragment() ); - setupTemplates(); - - Mockito.when( this.contentService.getById( ContentId.from( "fragmentId" ) ) ) - .thenReturn( createContentWithPage( null, ContentTypeName.from( "contentTypeName" ) ) ); - - final PortalResponse portalResponse = - PortalResponse.create().body( "component rendered" ).header( "some-header", "some-value" ).status( HttpStatus.OK ).build(); - - Mockito.when( this.postProcessor.processResponseInstructions( Mockito.any(), Mockito.any() ) ).thenReturn( portalResponse ); - - setRendererResult( portalResponse ); - - this.request.setEndpointPath( "/_/component/main-region/0" ); - - final WebResponse res = this.handler.handle( this.request, PortalResponse.create().build(), null ); - assertResponseOK( res ); - } @Test - public void testFragmentWithComponentsIsResolved() + public void testComponentFragment() throws Exception { setupSite(); - setupContent( createPageWithFragment() ); + setupContentFragment(); setupTemplates(); - Mockito.when( this.contentService.getById( ContentId.from( "fragmentId" ) ) ) - .thenReturn( createPageThatIsFragment( TextComponent.create().build() ) ); - final PortalResponse portalResponse = PortalResponse.create().body( "component rendered" ).header( "some-header", "some-value" ).status( HttpStatus.OK ).build(); @@ -279,53 +205,6 @@ public void testFragmentWithComponentsIsResolved() this.request.setEndpointPath( "/_/component/main-region/0" ); final WebResponse res = this.handler.handle( this.request, PortalResponse.create().build(), null ); - assertResponseOK( res ); - } - - @Test - public void testFragmentComponentAsTextComponent() - throws Exception - { - final TextComponent textComponent = TextComponent.create().build(); - final Content content = createPageThatIsFragment( textComponent ); - setupContent( content ); - - testFragmentContent( content, "/_/component/" ); - } - - @Test - public void testFragmentComponentAsLayoutComponent() - throws Exception - { - final LayoutRegions layoutRegions = - LayoutRegions.create().add( Region.create().name( "main-region" ).add( TextComponent.create().build() ).build() ).build(); - final LayoutComponent layoutComponent = LayoutComponent.create().regions( layoutRegions ).build(); - final Content content = createPageThatIsFragment( layoutComponent ); - - testFragmentContent( content, "/_/component/main-region/0" ); - } - - private void testFragmentContent( final Content content, final String endpointPath ) - throws Exception - { - setupSite(); - setupContent( content ); - - final PortalResponse portalResponse = - PortalResponse.create().body( "component rendered" ).header( "some-header", "some-value" ).status( HttpStatus.OK ).build(); - - Mockito.when( this.postProcessor.processResponseInstructions( Mockito.any(), Mockito.any() ) ).thenReturn( portalResponse ); - - setRendererResult( portalResponse ); - - this.request.setEndpointPath( endpointPath ); - - final WebResponse res = this.handler.handle( this.request, PortalResponse.create().build(), null ); - assertResponseOK( res ); - } - - private void assertResponseOK( final WebResponse res ) - { assertNotNull( res ); assertEquals( HttpStatus.OK, res.getStatus() ); assertEquals( MediaType.PLAIN_TEXT_UTF_8, res.getContentType() ); @@ -333,52 +212,70 @@ private void assertResponseOK( final WebResponse res ) assertEquals( "component rendered", res.getBody() ); } - private void setupContent( final Content content ) + private void setupContentFragment() { - Mockito.when( this.contentService.getByPath( ContentPath.from( "site/somepath/content" ).asAbsolute() ) ).thenReturn( content ); - Mockito.when( this.contentService.getById( content.getId() ) ).thenReturn( content ); - } + final Content content = createPageWithFragment( "id", "site/somepath/content", "myapplication:ctype", true ); + final Content fragment = createFragmentContent(); - private Content createPageThatIsFragment( final Component fragmentComponent ) - { - Page page = Page.create().fragment( fragmentComponent ).build(); - return createContentWithPage( page, ContentTypeName.fragment() ); - } + Mockito.when( this.contentService.getByPath( ContentPath.from( "site/somepath/content" ).asAbsolute() ) ).thenReturn( content ); - private Content createPageWithFragment() - { - return createPageWithFragment( ContentId.from( "fragmentId" ) ); - } + Mockito.when( this.contentService.getById( content.getId() ) ).thenReturn( content ); - private Content createPageWithFragment( final ContentId fragmentId ) - { - PageRegions pageRegions = PageRegions.create() - .add( Region.create().name( "main-region" ).add( FragmentComponent.create().fragment( fragmentId ).build() ).build() ) - .build(); + Mockito.when( this.contentService.getByPath( fragment.getPath() ) ).thenReturn( fragment ); - Page page = Page.create().template( PageTemplateKey.from( "my-page" ) ).regions( pageRegions ).config( new PropertyTree() ).build(); + Mockito.when( this.contentService.getById( fragment.getId() ) ).thenReturn( fragment ); - return createContentWithPage( page, ContentTypeName.from( "contentTypeName" ) ); } - final Content createContentWithPage( final Page page, final ContentTypeName contentTypeName ) + private Content createPageWithFragment( final String id, final String path, final String contentTypeName, final boolean withPage ) { PropertyTree rootDataSet = new PropertyTree(); rootDataSet.addString( "property1", "value1" ); final Content.Builder content = Content.create() - .id( ContentId.from( "id" ) ) - .path( ContentPath.from( "site/somepath/content" ) ) + .id( ContentId.from( id ) ) + .path( ContentPath.from( path ) ) .owner( PrincipalKey.from( "user:myStore:me" ) ) .displayName( "My Content" ) .modifier( PrincipalKey.from( "user:system:admin" ) ) - .type( contentTypeName ) + .type( ContentTypeName.from( contentTypeName ) ) .permissions( AccessControlList.create() .add( AccessControlEntry.create().allow( Permission.READ ).principal( RoleKeys.EVERYONE ).build() ) .build() ); - content.page( page ); + if ( withPage ) + { + PageRegions pageRegions = PageRegions.create() + .add( Region.create() + .name( "main-region" ) + .add( FragmentComponent.create().fragment( ContentId.from( "fragmentId" ) ).build() ) + .build() ) + .build(); + + Page page = Page.create().template( PageTemplateKey.from( "my-page" ) ).regions( pageRegions ).config( rootDataSet ).build(); + content.page( page ); + } + return content.build(); + } + private Content createFragmentContent() + { + PropertyTree rootDataSet = new PropertyTree(); + + final Content.Builder content = Content.create() + .id( ContentId.from( "fragmentId" ) ) + .path( ContentPath.from( "site/somepath/fragment" ) ) + .owner( PrincipalKey.from( "user:myStore:me" ) ) + .displayName( "My Content" ) + .modifier( PrincipalKey.from( "user:system:admin" ) ) + .type( ContentTypeName.fragment() ); + + Page page = Page.create() + .template( PageTemplateKey.from( "my-page" ) ) + .fragment( PartComponent.create().descriptor( "myapp:mypart" ).build() ) + .config( rootDataSet ) + .build(); + content.page( page ); return content.build(); } } diff --git a/modules/portal/portal-impl/src/test/java/com/enonic/xp/portal/impl/handler/render/PageHandlerTest.java b/modules/portal/portal-impl/src/test/java/com/enonic/xp/portal/impl/handler/render/PageHandlerTest.java index 3022335a596..df45529142d 100644 --- a/modules/portal/portal-impl/src/test/java/com/enonic/xp/portal/impl/handler/render/PageHandlerTest.java +++ b/modules/portal/portal-impl/src/test/java/com/enonic/xp/portal/impl/handler/render/PageHandlerTest.java @@ -52,7 +52,6 @@ public final void setup() this.handler = new PageHandler(); this.handler.setContentService( this.contentService ); this.handler.setPageDescriptorService( this.pageDescriptorService ); - this.handler.setLayoutDescriptorService( this.layoutDescriptorService ); this.handler.setPageTemplateService( this.pageTemplateService ); this.handler.setRendererDelegate( this.rendererDelegate ); this.handler.setPortalUrlService( this.portalUrlService ); diff --git a/modules/portal/portal-impl/src/test/java/com/enonic/xp/portal/impl/handler/render/PageResolverTest.java b/modules/portal/portal-impl/src/test/java/com/enonic/xp/portal/impl/handler/render/PageResolverTest.java index 3cddf9abf61..2c8cb149c71 100644 --- a/modules/portal/portal-impl/src/test/java/com/enonic/xp/portal/impl/handler/render/PageResolverTest.java +++ b/modules/portal/portal-impl/src/test/java/com/enonic/xp/portal/impl/handler/render/PageResolverTest.java @@ -1,41 +1,26 @@ package com.enonic.xp.portal.impl.handler.render; -import java.time.Instant; - import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.Mock; -import org.mockito.Mockito; import org.mockito.junit.jupiter.MockitoExtension; -import com.google.common.collect.Iterators; - import com.enonic.xp.content.Content; import com.enonic.xp.content.ContentId; import com.enonic.xp.content.ContentNotFoundException; import com.enonic.xp.content.ContentPath; import com.enonic.xp.data.PropertyTree; -import com.enonic.xp.form.Form; import com.enonic.xp.page.DescriptorKey; import com.enonic.xp.page.Page; -import com.enonic.xp.page.PageDescriptor; -import com.enonic.xp.page.PageDescriptorService; import com.enonic.xp.page.PageRegions; import com.enonic.xp.page.PageTemplate; import com.enonic.xp.page.PageTemplateKey; import com.enonic.xp.page.PageTemplateService; import com.enonic.xp.portal.RenderMode; -import com.enonic.xp.region.Component; import com.enonic.xp.region.FragmentComponent; -import com.enonic.xp.region.LayoutComponent; -import com.enonic.xp.region.LayoutDescriptor; -import com.enonic.xp.region.LayoutDescriptorService; import com.enonic.xp.region.PartComponent; import com.enonic.xp.region.Region; -import com.enonic.xp.region.RegionDescriptor; -import com.enonic.xp.region.RegionDescriptors; -import com.enonic.xp.region.TextComponent; import com.enonic.xp.schema.content.ContentTypeName; import com.enonic.xp.schema.content.ContentTypeNames; import com.enonic.xp.site.Site; @@ -43,15 +28,10 @@ import com.enonic.xp.web.WebException; import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertNotSame; import static org.junit.jupiter.api.Assertions.assertNull; import static org.junit.jupiter.api.Assertions.assertSame; import static org.junit.jupiter.api.Assertions.assertThrows; -import static org.junit.jupiter.api.Assertions.assertTrue; -import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.notNull; -import static org.mockito.Mockito.times; -import static org.mockito.Mockito.verify; import static org.mockito.Mockito.verifyNoInteractions; import static org.mockito.Mockito.when; @@ -69,12 +49,6 @@ public class PageResolverTest @Mock PageTemplateService pageTemplateService; - @Mock - PageDescriptorService pageDescriptorService; - - @Mock - LayoutDescriptorService layoutDescriptorService; - PageResolver pageResolver; @BeforeEach @@ -94,14 +68,19 @@ public void before() .add( Region.create().name( "regionB" ).add( PartComponent.create().descriptor( "myapp:my-part" ).build() ).build() ) .build(); - pageResolver = new PageResolver( pageTemplateService, pageDescriptorService, layoutDescriptorService ); + pageResolver = new PageResolver( pageTemplateService ); } @Test public void given_Content_without_Page_then_effective_Page_is_same_as_in_Template() { // setup - final Site site = createSite(); + Site site = Site.create() + .id( ContentId.from( "site-id" ) ) + .path( ContentPath.from( "/site" ) ) + .displayName( "My Site" ) + .type( ContentTypeName.from( "portal:site" ) ) + .build(); PageTemplate template = PageTemplate.newPageTemplate() .key( PageTemplateKey.from( "t-1" ) ) @@ -129,10 +108,18 @@ public void given_Content_without_Page_then_effective_Page_is_same_as_in_Templat @Test public void content_without_Page_and_Template_withoutPage() { - final Site site = createSite(); + Site site = Site.create() + .id( ContentId.from( "site-id" ) ) + .path( ContentPath.from( "/site" ) ) + .displayName( "My Site" ) + .type( ContentTypeName.from( "portal:site" ) ) + .build(); - PageTemplate template = - PageTemplate.newPageTemplate().key( PageTemplateKey.from( "t-1" ) ).parentPath( site.getPath() ).name( "my-template" ).build(); + PageTemplate template = PageTemplate.newPageTemplate() + .key( PageTemplateKey.from( "t-1" ) ) + .parentPath( site.getPath() ) + .name( "my-template" ) + .build(); Content content = Content.create().parentPath( site.getPath() ).name( "my-content" ).build(); @@ -149,7 +136,12 @@ public void content_without_Page_and_Template_withoutPage() @Test public void content_without_Page_and_no_default_template() { - final Site site = createSite(); + Site site = Site.create() + .id( ContentId.from( "site-id" ) ) + .path( ContentPath.from( "/site" ) ) + .displayName( "My Site" ) + .type( ContentTypeName.from( "portal:site" ) ) + .build(); Content content = Content.create().parentPath( site.getPath() ).name( "my-content" ).build(); @@ -179,9 +171,16 @@ public void content_without_Page_and_no_default_template() @Test public void content_with_Page_without_template_or_descriptor() { - final Site site = createSite(); + Site site = Site.create() + .id( ContentId.from( "site-id" ) ) + .path( ContentPath.from( "/site" ) ) + .displayName( "My Site" ) + .type( ContentTypeName.from( "portal:site" ) ) + .build(); + final Page page = Page.create().build(); - final Content content = Content.create().page( page ).parentPath( site.getPath() ).name( "my-content" ).build(); + + Content content = Content.create().page( page ).parentPath( site.getPath() ).name( "my-content" ).build(); final WebException webExceptionInLive = assertThrows( WebException.class, () -> pageResolver.resolve( RenderMode.LIVE, content, site ) ); @@ -210,7 +209,12 @@ public void content_with_Page_without_template_or_descriptor() public void given_Content_with_Page_without_regions_then_effective_Page_gets_regions_from_Template() { // setup - final Site site = createSite(); + Site site = Site.create() + .id( ContentId.from( "site-id" ) ) + .path( ContentPath.from( "/site" ) ) + .displayName( "My Site" ) + .type( ContentTypeName.from( "portal:site" ) ) + .build(); PageTemplate template = PageTemplate.newPageTemplate() .key( PageTemplateKey.from( "t-1" ) ) @@ -245,7 +249,12 @@ public void given_Content_with_Page_without_regions_then_effective_Page_gets_reg public void given_Content_with_Page_without_config_then_effective_Page_gets_config_from_Template() { // setup - final Site site = createSite(); + Site site = Site.create() + .id( ContentId.from( "site-id" ) ) + .path( ContentPath.from( "/site" ) ) + .displayName( "My Site" ) + .type( ContentTypeName.from( "portal:site" ) ) + .build(); PageTemplate template = PageTemplate.newPageTemplate() .key( PageTemplateKey.from( "t-1" ) ) @@ -278,7 +287,13 @@ public void given_Content_with_Page_without_config_then_effective_Page_gets_conf @Test public void page_with_own_descriptor() { - final Site site = createSite(); + Site site = Site.create() + .id( ContentId.from( "site-id" ) ) + .path( ContentPath.from( "/site" ) ) + .displayName( "My Site" ) + .type( ContentTypeName.from( "portal:site" ) ) + .build(); + final Page page = Page.create().descriptor( DescriptorKey.from( "myapp:my-descriptor" ) ).config( configB ).build(); Content content = Content.create() @@ -298,54 +313,15 @@ public void page_with_own_descriptor() verifyNoInteractions( pageTemplateService ); } - @Test - public void page_with_region_added_from_descriptor() - { - final Site site = createSite(); - - final DescriptorKey descriptorKey = DescriptorKey.from( "myapp:my-descriptor" ); - final Region region2 = Region.create().name( "regionWithTextComponent" ).add( TextComponent.create().build() ).build(); - final Region region3 = Region.create().name( "regionWithLayoutComponent" ).add( LayoutComponent.create().build() ).build(); - final PageRegions pageRegions = PageRegions.create().add( region2 ).add( region3 ).build(); - final Page page = Page.create().descriptor( descriptorKey ).regions( pageRegions ).config( configB ).build(); - - final Content content = Content.create() - .parentPath( site.getPath() ) - .name( "my-content" ) - .page( page ) - .type( ContentTypeName.templateFolder() ) - .build(); - - final RegionDescriptors regionDescriptors = RegionDescriptors.create() - .add( RegionDescriptor.create().name( "emptyRegion" ).build() ) - .add( RegionDescriptor.create().name( "regionWithTextComponent" ).build() ) - .add( RegionDescriptor.create().name( "regionWithLayoutComponent" ).build() ) - .build(); - - final PageDescriptor pageDescriptor = PageDescriptor.create() - .key( descriptorKey ) - .regions( regionDescriptors ) - .config( Form.create().build() ) - .modifiedTime( Instant.now() ) - .build(); - - when( pageDescriptorService.getByKey( Mockito.any( DescriptorKey.class ) ) ).thenReturn( pageDescriptor ); - - PageResolverResult result = pageResolver.resolve( RenderMode.LIVE, content, site ); - - final Page effectivePage = result.getEffectivePage(); - - assertNotSame( page, effectivePage ); - assertEquals( DescriptorKey.from( "myapp:my-descriptor" ), result.getController() ); - assertEquals( Iterators.size( effectivePage.getRegions().iterator() ), 3 ); - - verifyNoInteractions( pageTemplateService ); - } - @Test public void content_with_Page_but_template_was_deleted_fallback_to_default() { - final Site site = createSite(); + Site site = Site.create() + .id( ContentId.from( "site-id" ) ) + .path( ContentPath.from( "/site" ) ) + .displayName( "My Site" ) + .type( ContentTypeName.from( "portal:site" ) ) + .build(); Content content = Content.create() .parentPath( site.getPath() ) @@ -365,7 +341,8 @@ public void content_with_Page_but_template_was_deleted_fallback_to_default() .canRender( ContentTypeNames.from( ContentTypeName.templateFolder() ) ) .build(); - when( pageTemplateService.getByKey( PageTemplateKey.from( "t-not-exists" ) ) ).thenThrow( ContentNotFoundException.class ); + when( pageTemplateService.getByKey( PageTemplateKey.from( "t-not-exists" ) ) ).thenThrow( + ContentNotFoundException.class ); when( pageTemplateService.getDefault( notNull() ) ).thenReturn( template ); PageResolverResult result = pageResolver.resolve( RenderMode.LIVE, content, site ); @@ -379,7 +356,12 @@ public void content_with_Page_but_template_was_deleted_fallback_to_default() @Test public void content_with_Page_but_template_was_deleted_fallback_to_default_not_found() { - final Site site = createSite(); + Site site = Site.create() + .id( ContentId.from( "site-id" ) ) + .path( ContentPath.from( "/site" ) ) + .displayName( "My Site" ) + .type( ContentTypeName.from( "portal:site" ) ) + .build(); Content content = Content.create() .parentPath( site.getPath() ) @@ -413,7 +395,12 @@ public void content_with_Page_but_template_was_deleted_fallback_to_default_not_f @Test public void content_is_PageTemplate() { - final Site site = createSite(); + Site site = Site.create() + .id( ContentId.from( "site-id" ) ) + .path( ContentPath.from( "/site" ) ) + .displayName( "My Site" ) + .type( ContentTypeName.from( "portal:site" ) ) + .build(); final Page page = Page.create().descriptor( DescriptorKey.from( "myapp:my-descriptor" ) ).config( configA ).regions( regionsA ).build(); @@ -437,33 +424,16 @@ public void content_is_PageTemplate() } @Test - public void contentPageWithFragment() + public void content_is_Fragment() { - final Site site = createSite(); - final Page page = Page.create().fragment( FragmentComponent.create().build() ).build(); - - Content content = Content.create() - .parentPath( site.getPath() ) - .name( "my-content" ) - .page( page ) - .type( ContentTypeName.templateFolder() ) + Site site = Site.create() + .id( ContentId.from( "site-id" ) ) + .path( ContentPath.from( "/site" ) ) + .displayName( "My Site" ) + .type( ContentTypeName.from( "portal:site" ) ) .build(); - PageResolverResult result = pageResolver.resolve( RenderMode.LIVE, content, site ); - final Page effectivePage = result.getEffectivePage(); - - assertSame( page, effectivePage ); - assertNull( result.getController() ); - - verifyNoInteractions( pageTemplateService ); - } - - @Test - public void contentPageIsFragmentWithEmptyLayout() - { - final Site site = createSite(); - final LayoutComponent layoutComponent = LayoutComponent.create().build(); - final Page page = Page.create().fragment( layoutComponent ).build(); + final Page page = Page.create().fragment( FragmentComponent.create().build() ).build(); Content content = Content.create() .parentPath( site.getPath() ) @@ -475,58 +445,9 @@ public void contentPageIsFragmentWithEmptyLayout() PageResolverResult result = pageResolver.resolve( RenderMode.LIVE, content, site ); final Page effectivePage = result.getEffectivePage(); - assertNotSame( page, effectivePage ); + assertSame( page, effectivePage ); assertNull( result.getController() ); verifyNoInteractions( pageTemplateService ); } - - @Test - public void contentPageIsFragmentWithLayoutAndEmptyRegion() - { - final Site site = createSite(); - final DescriptorKey key = DescriptorKey.from( "someapp:sonelayout" ); - final LayoutComponent layoutComponent = LayoutComponent.create().descriptor( key ).build(); - final Page page = Page.create().fragment( layoutComponent ).build(); - - final RegionDescriptors regionDescriptors = - RegionDescriptors.create().add( RegionDescriptor.create().name( "main" ).build() ).build(); - final LayoutDescriptor layoutDescriptor = LayoutDescriptor.create() - .regions( regionDescriptors ) - .modifiedTime( Instant.now() ) - .key( key ) - .config( Form.create().build() ) - .build(); - - Content content = Content.create() - .parentPath( site.getPath() ) - .name( "my-content" ) - .page( page ) - .type( ContentTypeName.templateFolder() ) - .build(); - - when( layoutDescriptorService.getByKey( key ) ).thenReturn( layoutDescriptor ); - PageResolverResult result = pageResolver.resolve( RenderMode.LIVE, content, site ); - final Page effectivePage = result.getEffectivePage(); - - verify( layoutDescriptorService, times( 1 ) ).getByKey( any( DescriptorKey.class ) ); - assertNotSame( page, effectivePage ); - assertNull( result.getController() ); - - final Component builtComponent = effectivePage.getFragment(); - assertTrue( builtComponent instanceof LayoutComponent ); - final LayoutComponent buitLayoutComponent = (LayoutComponent) builtComponent; - assertTrue( buitLayoutComponent.hasRegions() ); - assertTrue( buitLayoutComponent.getRegion( "main" ) != null ); - } - - private Site createSite() - { - return Site.create() - .id( ContentId.from( "site-id" ) ) - .path( ContentPath.from( "/site" ) ) - .displayName( "My Site" ) - .type( ContentTypeName.from( "portal:site" ) ) - .build(); - } } diff --git a/modules/portal/portal-impl/src/test/java/com/enonic/xp/portal/impl/handler/render/RenderBaseHandlerTest.java b/modules/portal/portal-impl/src/test/java/com/enonic/xp/portal/impl/handler/render/RenderBaseHandlerTest.java index 1a2a382707b..7587b38e46b 100644 --- a/modules/portal/portal-impl/src/test/java/com/enonic/xp/portal/impl/handler/render/RenderBaseHandlerTest.java +++ b/modules/portal/portal-impl/src/test/java/com/enonic/xp/portal/impl/handler/render/RenderBaseHandlerTest.java @@ -24,7 +24,6 @@ import com.enonic.xp.portal.impl.rendering.RendererDelegate; import com.enonic.xp.portal.postprocess.PostProcessor; import com.enonic.xp.portal.url.PortalUrlService; -import com.enonic.xp.region.LayoutDescriptorService; import com.enonic.xp.region.PartComponent; import com.enonic.xp.region.Region; import com.enonic.xp.schema.content.ContentTypeName; @@ -57,8 +56,6 @@ public abstract class RenderBaseHandlerTest protected PageDescriptorService pageDescriptorService; - protected LayoutDescriptorService layoutDescriptorService; - protected ApplicationService applicationService; protected PortalUrlService portalUrlService; @@ -76,7 +73,6 @@ void setupRenderBaseHandlerTest() this.contentService = mock( ContentService.class ); this.pageTemplateService = mock( PageTemplateService.class ); this.pageDescriptorService = mock( PageDescriptorService.class ); - this.layoutDescriptorService = mock( LayoutDescriptorService.class ); this.applicationService = mock( ApplicationService.class ); this.portalUrlService = mock( PortalUrlService.class ); diff --git a/modules/portal/portal-impl/src/test/java/com/enonic/xp/portal/impl/postprocess/instruction/ComponentInstructionTest.java b/modules/portal/portal-impl/src/test/java/com/enonic/xp/portal/impl/postprocess/instruction/ComponentInstructionTest.java index 20606e632a3..52e6bd3c31a 100644 --- a/modules/portal/portal-impl/src/test/java/com/enonic/xp/portal/impl/postprocess/instruction/ComponentInstructionTest.java +++ b/modules/portal/portal-impl/src/test/java/com/enonic/xp/portal/impl/postprocess/instruction/ComponentInstructionTest.java @@ -1,9 +1,6 @@ package com.enonic.xp.portal.impl.postprocess.instruction; -import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; -import org.mockito.ArgumentCaptor; -import org.mockito.Mockito; import com.enonic.xp.content.Content; import com.enonic.xp.content.ContentId; @@ -15,12 +12,9 @@ import com.enonic.xp.page.PageTemplateKey; import com.enonic.xp.portal.PortalRequest; import com.enonic.xp.portal.PortalResponse; -import com.enonic.xp.portal.impl.rendering.RenderException; import com.enonic.xp.portal.impl.rendering.RendererDelegate; import com.enonic.xp.region.Component; import com.enonic.xp.region.ComponentService; -import com.enonic.xp.region.LayoutComponent; -import com.enonic.xp.region.LayoutRegions; import com.enonic.xp.region.PartComponent; import com.enonic.xp.region.Region; import com.enonic.xp.schema.content.ContentTypeName; @@ -28,8 +22,6 @@ import com.enonic.xp.site.Site; import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertNull; -import static org.junit.jupiter.api.Assertions.assertThrows; import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.isA; import static org.mockito.Mockito.doReturn; @@ -38,246 +30,73 @@ public class ComponentInstructionTest { - - private RendererDelegate rendererDelegate; - - private ComponentService componentService; - - private ComponentInstruction instruction; - - @BeforeEach - public final void setup() + @Test + public void testInstruction() throws Exception { - this.rendererDelegate = mock( RendererDelegate.class ); - this.componentService = mock( ComponentService.class ); - this.instruction = new ComponentInstruction(); - + RendererDelegate rendererDelegate = newRendererFactory( "part content" ); + ComponentService componentService = mock( ComponentService.class ); + ComponentInstruction instruction = new ComponentInstruction(); instruction.setRendererDelegate( rendererDelegate ); instruction.setComponentService( componentService ); - } - @Test - public void testInstructionWithPart() - throws Exception - { - returnOnRender( "part content" ); - - final PortalRequest portalRequest = new PortalRequest(); - final Content content = createPage( "content-id", "content-name", "myapplication:content-type" ); + PortalRequest portalRequest = new PortalRequest(); + Content content = createPage( "content-id", "content-name", "myapplication:content-type" ); portalRequest.setContent( content ); - final Site site = createSite( "site-id", "site-name", "myapplication:content-type" ); + Site site = createSite( "site-id", "site-name", "myapplication:content-type" ); portalRequest.setSite( site ); - final String outputHtml = instruction.evaluate( portalRequest, "COMPONENT myRegion/0" ).getAsString(); + String outputHtml = instruction.evaluate( portalRequest, "COMPONENT myRegion/0" ).getAsString(); assertEquals( "part content", outputHtml ); } - @Test - public void testLayoutNoServiceReturnsSameObject() - { - final LayoutComponent layoutComponent = - LayoutComponent.create().descriptor( DescriptorKey.from( "myapplication:layout" ) ).build(); - - testLayoutIsReturned(layoutComponent); - } - - @Test - public void testEmptyLayout() - { - testLayoutIsReturned(LayoutComponent.create().build()); - } - - private void testLayoutIsReturned( final LayoutComponent layoutComponent ) - { - final ArgumentCaptor captor = ArgumentCaptor.forClass( Component.class ); - returnOnRender( "render result", captor.capture() ); - - final PortalRequest portalRequest = new PortalRequest(); - - final Content content = createPage( "content-id", "content-name", "myapplication:content-type", layoutComponent ); - portalRequest.setContent( content ); - - instruction.evaluate( portalRequest, "COMPONENT myRegion/0" ); - - assertEquals( captor.getValue(), layoutComponent ); - } - - @Test - public void testLayoutIsReturnedWithRegions() - throws Exception - { - final ArgumentCaptor captor = ArgumentCaptor.forClass( Component.class ); - returnOnRender( "render result", captor.capture() ); - - final DescriptorKey layoutDescriptorKey = DescriptorKey.from( "myapplication:layout" ); - final LayoutRegions regions = - LayoutRegions.create().add( Region.create().name( "r1" ).build() ).add( Region.create().name( "r2" ).build() ).build(); - final LayoutComponent layoutFromService = LayoutComponent.create().descriptor( layoutDescriptorKey ).regions( regions ).build(); - - when( componentService.getByKey( Mockito.any( DescriptorKey.class) ) ).thenReturn( layoutFromService ); - - final PortalRequest portalRequest = new PortalRequest(); - final LayoutComponent emptyLayoutComponent = - LayoutComponent.create().descriptor( DescriptorKey.from( "myapplication:layout" ) ).build(); - - final Content content = createPage( "content-id", "content-name", "myapplication:content-type", emptyLayoutComponent ); - portalRequest.setContent( content ); - - instruction.evaluate( portalRequest, "COMPONENT myRegion/0" ); - - assertEquals( captor.getValue(), layoutFromService ); - } - - @Test - public void testFragmentContentNotLayoutThrowsException() - { - final PortalRequest portalRequest = new PortalRequest(); - - final Content content = createFragmentPage( "content-id", "content-name" ); - portalRequest.setContent( content ); - - assertThrows( RenderException.class, () -> instruction.evaluate( portalRequest, "COMPONENT myRegion/0" )); - } - - @Test - public void testFragmentContentThrowsWhenComponentNotFound() - { - final DescriptorKey layoutDescriptorKey = DescriptorKey.from( "myapplication:layout" ); - final LayoutRegions regions = - LayoutRegions.create().add( Region.create().name( "r1" ).build() ).add( Region.create().name( "r2" ).build() ).build(); - final LayoutComponent layoutComponent = LayoutComponent.create().descriptor( layoutDescriptorKey ).regions( regions ).build(); - - final PortalRequest portalRequest = new PortalRequest(); - final Content content = createFragmentPage( "content-id", "content-name", layoutComponent ); - portalRequest.setContent( content ); - - assertThrows( RenderException.class, () -> instruction.evaluate( portalRequest, "COMPONENT r1/0" )); - } - - @Test - public void testFragmentContent() - { - final ArgumentCaptor captor = ArgumentCaptor.forClass( Component.class ); - returnOnRender( "render result", captor.capture() ); - - final DescriptorKey layoutDescriptorKey = DescriptorKey.from( "myapplication:layout" ); - final PartComponent partComponent = createPartComponent(); - final LayoutRegions regions = LayoutRegions.create() - .add( Region.create().name( "r1" ).add( partComponent ).build() ) - .add( Region.create().name( "r2" ).build() ) - .build(); - final LayoutComponent layoutComponent = LayoutComponent.create().descriptor( layoutDescriptorKey ).regions( regions ).build(); - - final PortalRequest portalRequest = new PortalRequest(); - final Content content = createFragmentPage( "content-id", "content-name", layoutComponent ); - portalRequest.setContent( content ); - - final String outputHtml = instruction.evaluate( portalRequest, "COMPONENT r1/0" ).getAsString(); - assertEquals( "render result", outputHtml ); - } - @Test public void testInstructionRenderByName() throws Exception { - returnOnRender( "part content" ); + RendererDelegate rendererFactory = newRendererFactory( "part content" ); + ComponentService componentService = mock( ComponentService.class ); - final Component component = createPartComponent(); + Component component = createPartComponent(); doReturn( component ).when( componentService ).getByKey( isA( DescriptorKey.class ) ); + ComponentInstruction instruction = new ComponentInstruction(); + instruction.setRendererDelegate( rendererFactory ); + instruction.setComponentService( componentService ); - final PortalRequest portalRequest = new PortalRequest(); - final Content content = createPage( "content-id", "content-name", "myapplication:content-type" ); + PortalRequest portalRequest = new PortalRequest(); + Content content = createPage( "content-id", "content-name", "myapplication:content-type" ); portalRequest.setContent( content ); - final Site site = createSite( "site-id", "site-name", "myapplication:content-type" ); + Site site = createSite( "site-id", "site-name", "myapplication:content-type" ); portalRequest.setSite( site ); - final String outputHtml = instruction.evaluate( portalRequest, "COMPONENT module:myPartComponent" ).getAsString(); + String outputHtml = instruction.evaluate( portalRequest, "COMPONENT module:myPartComponent" ).getAsString(); assertEquals( "part content", outputHtml ); } @Test public void testInstructionRenderFragment() + throws Exception { - returnOnRender( "part content" ); + RendererDelegate rendererDelegate = newRendererFactory( "part content" ); + ComponentService componentService = mock( ComponentService.class ); - final Component component = createPartComponent(); + Component component = createPartComponent(); doReturn( component ).when( componentService ).getByKey( isA( DescriptorKey.class ) ); + ComponentInstruction instruction = new ComponentInstruction(); + instruction.setRendererDelegate( rendererDelegate ); + instruction.setComponentService( componentService ); - final PortalRequest portalRequest = new PortalRequest(); - final Content content = createFragmentPage( "content-id", "content-name" ); - portalRequest.setContent( content ); - final Site site = createSite( "site-id", "site-name", "myapplication:content-type" ); - portalRequest.setSite( site ); - - final String outputHtml = instruction.evaluate( portalRequest, "COMPONENT fragment" ).getAsString(); - assertEquals( "part content", outputHtml ); - } - - @Test - public void testInstructionRenderFragmentWithLayout() - { - returnOnRender( "part content" ); - - final DescriptorKey layoutDescriptorKey = DescriptorKey.from( "myapplication:layout" ); - final LayoutRegions regions = - LayoutRegions.create().add( Region.create().name( "r1" ).build() ).add( Region.create().name( "r2" ).build() ).build(); - final LayoutComponent layoutComponent = LayoutComponent.create().descriptor( layoutDescriptorKey ).regions( regions ).build(); - - doReturn( layoutComponent ).when( componentService ).getByKey( isA( DescriptorKey.class ) ); - - final PortalRequest portalRequest = new PortalRequest(); - final LayoutComponent emptyLayoutComponent = - LayoutComponent.create().descriptor( DescriptorKey.from( "myapplication:layout" ) ).build(); - final Content content = createFragmentPage( "content-id", "content-name", emptyLayoutComponent ); - + PortalRequest portalRequest = new PortalRequest(); + Content content = createFragmentPage( "content-id", "content-name" ); portalRequest.setContent( content ); - final Site site = createSite( "site-id", "site-name", "myapplication:content-type" ); + Site site = createSite( "site-id", "site-name", "myapplication:content-type" ); portalRequest.setSite( site ); - final String outputHtml = instruction.evaluate( portalRequest, "COMPONENT fragment" ).getAsString(); + String outputHtml = instruction.evaluate( portalRequest, "COMPONENT fragment" ).getAsString(); assertEquals( "part content", outputHtml ); } - @Test - public void testFragmentInstructionWithoutRequestContentReturnsNull() - { - final PortalResponse response = instruction.evaluate( new PortalRequest(), "COMPONENT fragment" ); - assertNull( response ); - } - - @Test - public void testWrongInstruction() - { - final PortalResponse response = instruction.evaluate( new PortalRequest(), "WRONG module:myPartComponent" ); - assertNull( response ); - } - - @Test - public void testWrongInstructionLength() - { - final PortalResponse response = instruction.evaluate( new PortalRequest(), "COMPONENT module:myPartComponent tooLong" ); - assertNull( response ); - } - - @Test - public void testNoContentInRequestReturnsNull() - { - final PortalResponse response = instruction.evaluate( new PortalRequest(), "COMPONENT r1/0" ); - assertNull( response ); - } - - @Test - public void testComponentNotFoundThrows() - { - final PortalRequest portalRequest = new PortalRequest(); - final Content content = createPage( "content-id", "content-name", "myapplication:content-type" ); - portalRequest.setContent( content ); - - assertThrows( RenderException.class, () -> instruction.evaluate( portalRequest, "COMPONENT myRegion/1" )); - } - - private PartComponent createPartComponent() + private Component createPartComponent() { return PartComponent.create(). descriptor( DescriptorKey.from( "myapplication:myparttemplate" ) ). @@ -285,18 +104,15 @@ private PartComponent createPartComponent() } private Content createPage( final String id, final String name, final String contentTypeName ) - { - return createPage( id, name, contentTypeName, createPartComponent() ); - } - - private Content createPage( final String id, final String name, final String contentTypeName, final Component regionComponent ) { PropertyTree rootDataSet = new PropertyTree(); rootDataSet.addString( "property1", "value1" ); Region region = Region.create(). name( "myRegion" ). - add( regionComponent ). + add( PartComponent.create(). + descriptor( DescriptorKey.from( "myapplication:myparttemplate" ) ). + build() ). build(); PageRegions pageRegions = PageRegions.create().add( region ).build(); @@ -318,11 +134,13 @@ private Content createPage( final String id, final String name, final String con private Content createFragmentPage( final String id, final String name ) { - return createFragmentPage( id, name, createPartComponent() ); - } + PropertyTree rootDataSet = new PropertyTree(); + rootDataSet.addString( "property1", "value1" ); + + PartComponent fragmentComponent = PartComponent.create(). + descriptor( DescriptorKey.from( "myapplication:myparttemplate" ) ). + build(); - private Content createFragmentPage( final String id, final String name, final Component fragmentComponent ) - { Page page = Page.create(). template( PageTemplateKey.from( "my-page" ) ). fragment( fragmentComponent ). @@ -360,14 +178,11 @@ private Site createSite( final String id, final String name, final String conten build(); } - private RendererDelegate returnOnRender( final String renderResult, final Object renderObject ) + private RendererDelegate newRendererFactory( final String renderResult ) { - when( rendererDelegate.render( renderObject, any() ) ).thenReturn( PortalResponse.create().body( renderResult ).build() ); - return rendererDelegate; - } + RendererDelegate rendererDelegate = mock( RendererDelegate.class ); - private RendererDelegate returnOnRender( final String renderResult ) - { - return returnOnRender( renderResult, any() ); + when( rendererDelegate.render( any(), any() ) ).thenReturn( PortalResponse.create().body( renderResult ).build() ); + return rendererDelegate; } }