Hi Team,
I have written a custom getData() method overlayed as per the below documentation to extend and customize the Adobe Client Data Layer.
@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
Solved! Go to Solution.
@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".
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.
@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".
Views
Likes
Replies
Views
Likes
Replies
Views
Likes
Replies
Views
Likes
Replies