Expand my Community achievements bar.

Dive into Adobe Summit 2024! Explore curated list of AEM sessions & labs, register, connect with experts, ask questions, engage, and share insights. Don't miss the excitement.
SOLVED

Write JUnit for overlayed Analytics getData()

Avatar

Community Advisor

Hi Team,

 

I have written a custom getData() method overlayed as per the below documentation to extend and customize the Adobe Client Data Layer.

https://experienceleague.adobe.com/docs/experience-manager-learn/sites/integrations/adobe-client-dat...

@Override
public String getCustomData() {
Resource accordionResource = this.request.getResource();

if (ComponentUtils.isDataLayerEnabled(accordionResource)) {
Map<String, Object> accordionProperties = new HashMap<>();
accordionProperties.put("@type", accordionResource.getResourceType());
accordionProperties.put("repo:modifyDate", PlatformUtils.isoDateFormat(accordion.getData().getLastModifiedDate(), dateutil));
accordionProperties.put("parentId", accordion.getData().getParentId());

//Adding New Custom Properties
accordionProperties.put("Component Name", "Accordion");

String bylineComponentID = ComponentUtils.getId(accordionResource, this.currentPage, this.componentContext);
try {
return String.format("{\"%s\":%s}",
bylineComponentID,
// Use the ObjectMapper to serialize the bylineProperties to a JSON string
new ObjectMapper().writeValueAsString(accordionProperties));
} catch (JsonProcessingException e) {
log.error("Unable to generate dataLayer JSON string", e);
}
}
return null;
}

I want to now write a JUnit for the same. The challenge here is that the JSON object does not have customData as other properties.

Can you please help with this ?

 

@arunpatidar, @Vijayalakshmi_S, @Gaurav-Behl, @B_Sravan, @Mohit_KBansal, @kautuk_sahni 

 

The problem is primarily with the method - if (ComponentUtils.isDataLayerEnabled(accordionResource)) {

For a mocked resource this always evaluates to false.

Tried using - lenient().when(ComponentUtils.isDataLayerEnabled(resource)).thenReturn(true) but this is not working as well.

Thanks,

Rohan Garg

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

@Sady_Rifat - Thanks for your response. I have created a mock utility function to enable the data layer as shown below -

Rohan_Garg_0-1667286748444.png

 

This is based on ComponentUtils static method -

Rohan_Garg_1-1667286803963.png

 

This should have worked ideally.

Update - The utility function is working correctly. The Configuration Builder package was being incorrectly imported. The correct package is "org.apache.sling.caconfig.ConfigurationBuilder".

 

View solution in original post

2 Replies

Avatar

Community Advisor

You can't mock the static method

So lenient().when(ComponentUtils.isDataLayerEnabled(resource)).thenReturn(true) line will not work. ComponentUtils.isDataLayerEnabled(resource) will always call the real method.

Avatar

Correct answer by
Community Advisor

@Sady_Rifat - Thanks for your response. I have created a mock utility function to enable the data layer as shown below -

Rohan_Garg_0-1667286748444.png

 

This is based on ComponentUtils static method -

Rohan_Garg_1-1667286803963.png

 

This should have worked ideally.

Update - The utility function is working correctly. The Configuration Builder package was being incorrectly imported. The correct package is "org.apache.sling.caconfig.ConfigurationBuilder".