Integration Test with Content Fragments - How can I create a Content Fragment? | Community
Skip to main content
August 26, 2021
Solved

Integration Test with Content Fragments - How can I create a Content Fragment?

  • August 26, 2021
  • 2 replies
  • 2270 views

In my integration test I want to create a Content Fragment in a testfolder and then implement this Content Fragment in a testpage. (Sidenote: Manually everything I want to do in this test works.)

So far I managed to create a testpage and a testfolder, but I couldn't find a solution to create a Content Fragment. 

 

This code shows how I created a user (admin).

 

@ClassRule
public static final CQAuthorClassRule cqBaseClassRule = new CQAuthorClassRule();

@Rule
public CQRule cqBaseRule = new CQRule(cqBaseClassRule.authorRule);

static CQClient adminAuthor;

@BeforeClass
public static void beforeClass() {
adminAuthor = cqBaseClassRule.authorRule.getAdminClient(CQClient.class);
}

and here I created a testpage and testfolder in my Test

try {
SlingHttpResponse page = adminAuthor.createPageWithRetry(pageName, pageTitle, pageParentPath, templatePath, MINUTES.toMillis(1), 500, HttpStatus.SC_OK);

String pagePath = page.getSlingLocation();

LOG.info("Created page at {}", pagePath);

// This shows that the page exists
CQAssert.assertCQPageExistsWithTimeout(adminAuthor, pagePath, TIMEOUT, 500);


SlingHttpResponse folder = adminAuthor.createFolder(folderName, folderTitle, folderParentPath, HttpStatus.SC_OK);

String folderPath = folder.getSlingLocation();

LOG.info("Created folder at {}", folderPath);

// This shows that the folder exists
CQAssert.assertFolderExists(adminAuthor, folderPath, folderTitle);
}

 

There is nothing like "createContentFragment" only "createNode"... Bu then I would have to create several Nodes and update these nodes to have the structure I want in my CRX. 
Any idea how I can create this content Fragment? Is there an API to use in integration tests? 

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by Kishore_Kumar_

Hi @marie_p_h ,

 

Could you please check if this helps. 

 

I found some of the test samples from here https://github.com/adobe/aem-test-samples 

 

It has some integration testing samples for content fragments creation. Please refer below link

https://github.com/adobe/aem-test-samples/blob/aem-cloud/cf-smoke/src/main/java/com/adobe/cq/cloud/testing/it/cf/smoke/CFSmokeIT.java 

2 replies

Ritesh_Mittal
Community Advisor and Adobe Champion
Community Advisor and Adobe Champion
August 26, 2021

Hi @marie_p_h ,

 

While creating unit tests for any Content Fragment business logic, we do not create the actual CF but we mock the resource to CF using Mockito.when. Check below example

 

https://github.com/adobe/aem-core-wcm-components/blob/master/bundles/core/src/test/java/com/adobe/cq/wcm/core/components/internal/ContentFragmentUtilsTest.java

 

 

Though, for actual CF creation for any functionality you can use Content Fragment Server side API, like below 

Imports -

import com.adobe.cq.dam.cfm.ContentFragment;
import com.adobe.cq.dam.cfm.ContentFragmentException;
import com.adobe.cq.dam.cfm.FragmentTemplate;

 

Code snippet-

try (ResourceResolver resourceResolver = getResourceResolverForPage("datawrite")) {


Resource templateRes= resourceResolver.getResource("/conf/myproject/settings/dam/cfm/models/my-cf-model");
Resource cfFolderresource = resourceResolver.getResource("/content/dam/myfolder/myCf");
FragmentTemplate tpl = templateRes.adaptTo(FragmentTemplate.class);
ContentFragment newFragment = tpl.createFragment(cfFolderresource, "myCFName",
"Description");
resourceResolver.commit();

catch (PersistenceException | ContentFragmentException e) {

log.error("Exception occurred while creating CF {}", e.getMessage());

}

 

marie_p_hAuthor
August 26, 2021

Thank you for your reply! Where does the method "getResourceResolverForPage()" come from?
I was tasked with writing an actual integration test, not a unit test so I can not rely on the AemContext or Mockito...

Ritesh_Mittal
Community Advisor and Adobe Champion
Community Advisor and Adobe Champion
August 26, 2021

Hi @marie_p_h ,

 

I missed to add the method, please see it below-

public ResourceResolver getResourceResolverForPage(String name) {
ResourceResolver resourceResolver = null;
try {
Map<String, Object> param = new HashMap<>();
param.put(ResourceResolverFactory.SUBSERVICE, name);
resourceResolver = resolverFactory.getServiceResourceResolver(param);
} catch (Exception e) {
}
return resourceResolver;
}

Kishore_Kumar_
Kishore_Kumar_Accepted solution
Level 9
August 27, 2021

Hi @marie_p_h ,

 

Could you please check if this helps. 

 

I found some of the test samples from here https://github.com/adobe/aem-test-samples 

 

It has some integration testing samples for content fragments creation. Please refer below link

https://github.com/adobe/aem-test-samples/blob/aem-cloud/cf-smoke/src/main/java/com/adobe/cq/cloud/testing/it/cf/smoke/CFSmokeIT.java 

marie_p_hAuthor
August 27, 2021

@kishore_kumar_ this actually worked, thank you!