Highlighted

JUnit for Workflow

rajareddych

07-11-2019

Writing code is easy, but I'm finding it difficult to write JUnit tests for that code. Could anyone help in writing JUnit for the below workflow.

public void execute(WorkItem workItem, WorkflowSession workflowSession, MetaDataMap args) {

  log.info("\n------------- Workflow {Start} --------------------");

  String contentPath = "";

  WorkflowData workflowData = workItem.getWorkflowData();

  final String type = workflowData.getPayloadType();

  ResourceResolver resourceResolver = null;

  log.info("Workflow type = " + type);

  if (type == "JCR_PATH") {

  try {

  resourceResolver = getResourceResolver(workflowSession.getSession());

  contentPath = workflowData.getPayload().toString();

  Resource componentResource = resourceResolver.getResource(contentPath);

  Resource parentResource = componentResource.getParent();

  if (parentResource.getName().equalsIgnoreCase("assets")) {

  ModifiableValueMap map = componentResource.adaptTo(ModifiableValueMap.class);

  if(map != null) {

  map.put("someID", UUID.randomUUID().toString());

  componentResource.getResourceResolver().commit();

  String zeroID = ((String) map.get("someID"));

  if(zeroID.equalsIgnoreCase("0")) {

  log.error("Some ID is 0");

  } else {

  log.info("Some ID is : " +componentResource.getValueMap().containsKey("someID"));

  }

  }

  } else if (componentResource.getResourceType().equalsIgnoreCase(ServiceConstants.EXAMPLE_RESOURCE)

  || componentResource.getResourceType().equalsIgnoreCase(ServiceConstants.SOME_RESOURCE)) {

  ModifiableValueMap map = componentResource.adaptTo(ModifiableValueMap.class);

                  if(null != map) {

                 

                  map.put("ExampleID", UUID.randomUUID().toString());

                  componentResource.getResourceResolver().commit();

                  }

  }

  } catch (Exception e) {

  log.error(e.getLocalizedMessage());

  log.error("\n------------- Workflow {Error} --------------------");

  }

  }

  log.info("\n------------- Workflow {End} --------------------");

  }

  /**

  * Method to create the resourceResolver object using workflowSession

  *

  * @param session

  * @throws org.apache.sling.api.resource.LoginException

  */

  private ResourceResolver getResourceResolver(Session session) throws LoginException {

  return resourceResolverFactory.getResourceResolver(

  Collections.<String, Object>singletonMap(JcrResourceConstants.AUTHENTICATION_INFO_SESSION, session));

  }

}