Help is appreciated in writing JUNIT for below code.
@PostConstruct
protected void invokepost() {
super.Initialize();
qs = new ArrayList<SomeComponent>();
Iterator<Resource> iteratorExp = resource.getChild("root/childParsys/").listChildren();
while (iteratorExp.hasNext()) {
Resource childResource = iteratorExp.next();
SomeComponent map = childResource.adaptTo(SomeComponent.class);
if(map != null) {
qs.add(map);
}
if (childResource.getResourceType().equalsIgnoreCase(ServiceConstants.SOME_RESOURCE)) {
name = "SomeResource";
} else if (childResource.getResourceType().equalsIgnoreCase(ServiceConstants.EXAMPLE_RESOURCE)) {
name = "ExampleResource";
}
}
}
Solved! Go to Solution.
Views
Replies
Total Likes
I have resolved this myself. Thanks
Views
Replies
Total Likes
See [1] as an example how to use AEM Mocks to work with resources in unittests.
[1] Writing unittests for AEM (part 3): Mocking resources – Things on a content management system
Views
Replies
Total Likes
did not help, i've gone through a lot of articles.
Now I'm struck with
if (childResource.getResourceType().equalsIgnoreCase(ServiceConstants.SOME_RESOURCE)) {
name = "SomeResource";
} else if (childResource.getResourceType().equalsIgnoreCase(ServiceConstants.EXAMPLE_RESOURCE)) {
name = "ExampleResource";
}
Views
Replies
Total Likes
it would be great, if you could share the code you want to unittest, embedded into a working maven setup, on github. That would be the best way to review your approach, and contribute a working example.
Sharing more than a few lines of code here in the forum is not working well.
Views
Replies
Total Likes
package service.core.workflows;
import java.util.Collections;
import java.util.UUID;
import javax.jcr.Session;
import org.apache.sling.api.resource.LoginException;
import org.apache.sling.api.resource.ModifiableValueMap;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ResourceResolverFactory;
import org.apache.sling.jcr.resource.api.JcrResourceConstants;
import org.osgi.framework.Constants;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.day.cq.workflow.WorkflowSession;
import com.day.cq.workflow.exec.WorkItem;
import com.day.cq.workflow.exec.WorkflowData;
import com.day.cq.workflow.exec.WorkflowProcess;
import com.day.cq.workflow.metadata.MetaDataMap;
import service.core.constants.ServiceConstants;
@Component(
service = WorkflowProcess.class,
immediate = true,
property = {
Constants.SERVICE_DESCRIPTION + "= Update 'new comp' in a new or copied node inside the folder /content/project/projectcontent",
"process.label = UUID"
}
)
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));
}
}
Views
Replies
Total Likes
I have resolved this myself. Thanks
Views
Replies
Total Likes
Sorry, forgot to followup :-(
Can you quickly share your approach?
Views
Replies
Total Likes