Your achievements

Level 1

0% to

Level 2

Tip /
Sign in

Sign in to Community

to gain points, level up, and earn exciting badges like the new
Bedrock Mission!

Learn more

View all

Sign in to view all badges

SOLVED

Write JUnit for overlayed Analytics getData()

Avatar

Level 8

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
Level 8

@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

Level 3

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
Level 8

@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".