Write JUnit for overlayed Analytics getData() | Community
Skip to main content
Rohan_Garg
Community Advisor
Community Advisor
October 31, 2022
Solved

Write JUnit for overlayed Analytics getData()

  • October 31, 2022
  • 1 reply
  • 687 views

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-data-layer/data-layer-customize.html?lang=en

@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

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 Rohan_Garg

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

 

This is based on ComponentUtils static method -

 

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

 

1 reply

Sady_Rifat
Community Advisor
Community Advisor
November 1, 2022

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.

Rohan_Garg
Community Advisor
Rohan_GargCommunity AdvisorAuthorAccepted solution
Community Advisor
November 1, 2022

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

 

This is based on ComponentUtils static method -

 

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