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
BedrockMission!

Learn More

View all

Sign in to view all badges

Need Help with creating a stub for workflow model in the Junit test case

Avatar

Avatar
Validate 1
Level 1
subramanya_75
Level 1

Likes

0 likes

Total Posts

9 posts

Correct Reply

0 solutions
Top badges earned
Validate 1
View profile

Avatar
Validate 1
Level 1
subramanya_75
Level 1

Likes

0 likes

Total Posts

9 posts

Correct Reply

0 solutions
Top badges earned
Validate 1
View profile
subramanya_75
Level 1

12-04-2021

This is my servlet that is invoking a workflow and it is using a workflow model.

I am unable to create a stub for the workflow model in the testcase for the servlet.

I am using a mock workflow model but it is returning null or throwing exception.

The servlet code that starts workflow provided below.

 

WorkflowSession wfSession = workflowService.getWorkflowSession(session);        

WorkflowModel wfModel = wfSession.getModel("/var/workflow/models/dam/dam_download_asset");

WorkflowData wfData = wfSession.newWorkflowData("JCR_PATH", "/content/dam/we-retail/en/features/cart.png");

wfSession.startWorkflow(wfModel, wfData);

 
 
Testcase code below:
 

@rule

public AemContext aemContext = new AemContext(ResourceResolverType.JCR_OAK);

@Mock

WorkflowServiceworkflowService;

@Mock

WorkflowSessionwfSession;

@Mock

WorkflowModel wfmodel;

@Before

public void setup() {

 Mockito.lenient().when(workflowService.getWorkflowSession(resource.getResourceResolver().adaptTo(Session.class))).thenReturn(wfSession);

Mockito.lenient().when(resolverFactory.getServiceResourceResolver(this.serviceParams)).thenReturn(aemContext.resourceResolver());

wfmodel=workflowService.getWorkflowSession(resource.getResourceResolver().adaptTo(Session.class)).createNewModel("/var/workflow/models/dam/dam_download_asset");

 

The last line in the test case is throwing error. I don't know how to create the stub for the model.

Hence it would be highly helpful if I get the details to create the workflow-model stub in the junit test case.

 

Accepted Solutions (1)

Accepted Solutions (1)

Avatar

Avatar
Establish
MVP
Ravi_Pampana
MVP

Likes

183 likes

Total Posts

234 posts

Correct Reply

70 solutions
Top badges earned
Establish
Contributor
Shape 1
Ignite 5
Ignite 3
View profile

Avatar
Establish
MVP
Ravi_Pampana
MVP

Likes

183 likes

Total Posts

234 posts

Correct Reply

70 solutions
Top badges earned
Establish
Contributor
Shape 1
Ignite 5
Ignite 3
View profile
Ravi_Pampana
MVP

12-04-2021

Hi,

 

Try below code

 

@Mock

private WorkflowService mockWorkflowService;

 

@Mock

private WorkflowSession wfSession;

 

@Mock

private WorkflowModel workflowModel;

 

@Mock

private WorkflowData wfData;

 

setup method:

 

MockSlingHttpServletRequest mockRequest = aemContext.request();

mockRequest.addRequestParameter("path", "{add the page path}");

aemContext.registerService(WorkflowService.class, mockWorkflowService);

Mockito.when(mockWorkflowService.getWorkflowSession(Mockito.any(Session.class))).thenReturn(wfSession);

Mockito.when(wfSession.getModel(Mockito.anyString())).thenReturn(workflowModel);

Mockito.when(wfSession.newWorkflowData(Mockito.anyString(), Mockito.anyString())).thenReturn(wfData);

subramanya_75

One last issue I am facing at the end of the servlet doget method I am closing all of the resoure resolver sessions like beow

 

               try{

                       json.addProperty("success", true);

                     json.addProperty("destination_path", path);

                    response.setStatus(SlingHttpServletResponse.SC_OK);

                    response.setContentType("application/json");

                    response.getWriter().write(new Gson().toJson(json));

                    log.debug("TrashcanServlet Response 200 OK  with response text: {} ", new Gson().toJson(json) );

                } catch (TrashcanException e) {

                    log.error("Error occurred:", e);

                    try {

                        json.addProperty("success", false);

                        json.addProperty("errorCause", e.getReason());

                        response.setStatus(SlingHttpServletResponse.SC_OK);

                        response.setContentType("application/json");

                        response.getWriter().write(new Gson().toJson(json));

                    } catch (Exception ex) {

                        log.error("Error occurred:", ex);

                    }

                } catch (Exception e) {

                    log.error("Error Occurred: ", e);

                } finally {

                    if (workflowResourceResolver != null) {

                        try {

                            workflowResourceResolver.close();

                        } catch (Exception ex) {

                        log.error("Error occurred:", ex);

                        }

                    }

                    if (userResourceResolver != null) {

                        try {

                            userResourceResolver.close();

                        } catch (Exception ex) {

                        log.error("Error occurred:", ex);

                        }

                    }

                }

            } else {

                try {

                    response.sendError(HttpServletResponse.SC_NOT_FOUND, "Missing payload parameter");

                } catch (Exception ex) {

                    log.error("Error occurred:", ex);

                }

Due to all of resourceresolver sessions being closed by my code I agetting this exception in the test case, (even though my doget method is completed)

22:22:52.422 [main] WARN  o.a.s.j.r.i.h.j.JcrResourceProvider - Unable to revert pending changes.

javax.jcr.RepositoryException: This session has been closed.

Any idea to get resolve this exception.

 

            }

Answers (0)