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?
Solved! Go to Solution.
Topics help categorize Community content and increase your ability to discover relevant content.
Views
Replies
Total Likes
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/t...
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
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());
}
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...
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;
}
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/t...
@Kishore_Kumar_ this actually worked, thank you!
Views
Likes
Replies
Views
Likes
Replies