Unable to get 'jcr:created' property from imported JSON file

434 views
Skip to first unread message

Ruen Zapanta

unread,
Mar 31, 2016, 9:45:10 AM3/31/16
to wcm-io Developers
Hello,

I would like to confirm if this is happening not just on my machine. If it does, is there any workaround for this?
I am trying to load a page JSON into the test and my code is using the 'jcr:created' property. Unfortunately, this is not being returned when the test page is imported via JSON.

It gets included when I create the page via the `create().page(...)` method though.

I was hoping to do it via JSON import since it will be faster for our team.

I'm using AEM 6.1 and io.wcm.testing.aem-mock 1.7.0.

Here is a sample JSON that I'm using

{
"jcr:primaryType": "cq:Page",
"jcr:content": {
"jcr:primaryType": "cq:PageContent",
"jcr:title": "English",
"cq:template": "/apps/sample/templates/homepage",
"sling:resourceType": "sample/components/homepage",
"jcr:createdBy": "admin",
"jcr:created": "Thu Aug 07 2014 16:32:59 GMT+0200"
}
}

Here is a JUnit test case

// imports //
...
import io.wcm.testing.mock.aem.junit.AemContext;

public class TestPageContentParsingFromJson {

@Rule
public final AemContext context = new AemContext();

private ResourceResolver resolver;

@Before
public void setUp() throws Exception {
// load page from json
context.load().json("/page.json", "/content/mock/from/json");

// create page via builder
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(1407421979000L); // "Thu Aug 07 2014 16:32:59 GMT+0200"
calendar.setTimeZone(TimeZone.getTimeZone("GMT+0200"));

context.create().page("/content/mock/from/builder",
"/apps/sample/templates/homepage",
ImmutableMap.<String, Object>builder()
.put("jcr:title", "English")
.put("jcr:created", calendar.getTime())
.put("sling:resourceType", "sample/components/homepage")
.put("jcr:createdBy", "admin")
.build()
);

resolver = context.resourceResolver();
}

private Page loadPageFromContext(String path) {
Resource resource = resolver.getResource(path);
Page page = resource.adaptTo(Page.class);
assertNotNull(page);

return page;
}

@Test
public void testEquality() {
Page pageJson = loadPageFromContext("/content/mock/from/json");
System.out.println("\n");
Page pageFromBuilder = loadPageFromContext("/content/mock/from/builder");

ValueMap propsFromJson = pageJson.getProperties();
ValueMap propsFromBuilder = pageFromBuilder.getProperties();

assertEquals("Should be equals to 'sample/components/homepage'", propsFromJson.get("sling:resourceType", Calendar.class), propsFromBuilder.get("sling:resourceType", Calendar.class));
assertEquals("Should be equals to '/apps/sample/templates/homepage'", propsFromJson.get("cq:template", Calendar.class), propsFromBuilder.get("cq:template", Calendar.class));
assertEquals("Should be equals to 'admin'", propsFromJson.get("jcr:createdBy", String.class), propsFromBuilder.get("jcr:createdBy", String.class));
assertEquals("Should be equals to 'English'", propsFromJson.get("jcr:title", Calendar.class), propsFromBuilder.get("jcr:title", Calendar.class));
assertEquals("Should be equals to 'cq:PageContent'", propsFromJson.get("jcr:primaryType", Calendar.class), propsFromBuilder.get("jcr:primaryType", Calendar.class));

// this fails
assertEquals("Should be equals to 'Thu Aug 07 2014 16:32:59 GMT+0200'", propsFromJson.get("jcr:created", Calendar.class), propsFromBuilder.get("jcr:created", Calendar.class));
}

}


Regards,
Ruen

Stefan Seifert

unread,
Apr 11, 2016, 4:46:03 AM4/11/16
to wcm-i...@googlegroups.com
this problem is more related to sling-mock of the Apache Sling project than to wcm.io itself, because it affects the ContentLoader from sling-mock.

the ContentLoader [1] has a default list of properties that is ignored when loading content, and "jcr:created" is one of them. the reason is that if using a real repository this property is write-protected when using the JCR API because it is set by the repository automatically.

from the test case i see that you are using the default resource resolver type RESOURCERESOLVER_MOCK, which does not have an underlying "real" repository. one could argue if it makes sense to ignore the jcr:created property for this as well, but currently the same ContentLoader implementation is used for all resource resolver types.

if you wish to change this behavior best place for discussion is the sling-dev mailing list.

stefan

[1] https://svn.apache.org/repos/asf/sling/trunk/testing/mocks/sling-mock/src/main/java/org/apache/sling/testing/mock/sling/loader/ContentLoader.java
>--
>You received this message because you are subscribed to the Google Groups
>"wcm-io Developers" group.
>To unsubscribe from this group and stop receiving emails from it, send an
>email to wcm-io-dev+...@googlegroups.com.
>To view this discussion on the web visit
>https://groups.google.com/d/msgid/wcm-io-dev/d7032b60-5856-4676-9ff4-
>c18b7cdcde50%40googlegroups.com.
>For more options, visit https://groups.google.com/d/optout.

Ruen Zapanta

unread,
Apr 14, 2016, 7:59:10 AM4/14/16
to wcm-io Developers
Hi Stefan,

Thanks for pointing that out. That makes sense actually. :)

Regards,
Ruen

Reply all
Reply to author
Forward
0 new messages