diff --git a/changes.xml b/changes.xml index 511d3980..376d43c0 100644 --- a/changes.xml +++ b/changes.xml @@ -27,6 +27,9 @@ MockComponent: Implement isContainer method. + + MockPageManager: Allow to create page without template. + Remove outdated dependency to org.apache.sling.commons.json. diff --git a/core/src/main/java/io/wcm/testing/mock/aem/MockPageManager.java b/core/src/main/java/io/wcm/testing/mock/aem/MockPageManager.java index 108c144b..887fa909 100644 --- a/core/src/main/java/io/wcm/testing/mock/aem/MockPageManager.java +++ b/core/src/main/java/io/wcm/testing/mock/aem/MockPageManager.java @@ -119,20 +119,14 @@ else if (!JcrUtil.isValidName(childResourceName)) { props = new HashMap<>(); props.put(JCR_PRIMARYTYPE, "cq:PageContent"); props.put(JCR_TITLE, title); - props.put(PN_TEMPLATE, template); + if (template != null) { + props.put(PN_TEMPLATE, template); + } Resource contentResource = this.resourceResolver.create(pageResource, JCR_CONTENT, props); // create initial content from template - Resource templateResource = resourceResolver.getResource(template); - if (templateResource != null) { - Template templateInstance = templateResource.adaptTo(Template.class); - if (templateInstance != null) { - String initialContentPath = templateInstance.getInitialContentPath(); - Resource initialContentResource = resourceResolver.getResource(initialContentPath); - if (initialContentResource != null) { - copyContent(initialContentResource, contentResource, true); - } - } + if (template != null) { + createInitialContentFromTemplate(contentResource, template); } if (autoSave) { @@ -146,6 +140,20 @@ else if (!JcrUtil.isValidName(childResourceName)) { return pageResource.adaptTo(Page.class); } + private void createInitialContentFromTemplate(@NotNull Resource contentResource, @NotNull String template) throws PersistenceException { + Resource templateResource = resourceResolver.getResource(template); + if (templateResource != null) { + Template templateInstance = templateResource.adaptTo(Template.class); + if (templateInstance != null) { + String initialContentPath = templateInstance.getInitialContentPath(); + Resource initialContentResource = resourceResolver.getResource(initialContentPath); + if (initialContentResource != null) { + copyContent(initialContentResource, contentResource, true); + } + } + } + } + @SuppressFBWarnings("STYLE") private void copyContent(Resource source, Resource target, boolean skipPrimaryType) throws PersistenceException { ValueMap sourceProps = source.adaptTo(ValueMap.class); diff --git a/core/src/test/java/io/wcm/testing/mock/aem/MockPageManagerTest.java b/core/src/test/java/io/wcm/testing/mock/aem/MockPageManagerTest.java index f0606b50..7f48d145 100644 --- a/core/src/test/java/io/wcm/testing/mock/aem/MockPageManagerTest.java +++ b/core/src/test/java/io/wcm/testing/mock/aem/MockPageManagerTest.java @@ -89,6 +89,12 @@ public void testCreatePage() throws WCMException { assertTrue(this.resourceResolver.hasChanges()); } + @Test + public void testCreatePageWithNullTemplate() throws WCMException { + testCreatePageInternal(false, null); + assertTrue(this.resourceResolver.hasChanges()); + } + @Test public void testCreatePageWithAutoSave() throws WCMException { testCreatePageInternal(true, "/apps/sample/templates/homepage");