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

Junit | slingRequestProcessor.process returning empty string

Avatar

Avatar
Level 1
nj2
Level 1

Likes

0 likes

Total Posts

4 posts

Correct Reply

0 solutions
View profile

Avatar
Level 1
nj2
Level 1

Likes

0 likes

Total Posts

4 posts

Correct Reply

0 solutions
View profile
nj2
Level 1

13-05-2021


Hi All,

Getting empty string for baos.toString("UTF-8") statement. Performing below steps to invoke a workflow process passing mock object.

1. Creating mock objects for request, response and resource resolver.
2. Passing mock values, as below
3. successfully getting all the mock objects in workflow process class.

Invoking getJSONFromPSPPPage method internally from execute method, but getting empty String for below statement

String baosString = baos.toString("UTF-8");

Any thoughts or Suggestion please.
===============================================

Junit class --

@Mock
MockSlingHttpServletRequest request;
@Mock
MockSlingHttpServletResponse response;
@Mock
private Replicator replicator;
@Mock
private RequestResponseFactory requestResponseFactory;
@Mock
private SlingRequestProcessor requestProcessor;

@BeforeEach
public void setup()
{ response = context.response();
request = new MockSlingHttpServletRequest(resourceResolverMock, context.bundleContext());
context.registerService(RequestResponseFactory.class, requestResponseFactory);
PrivateAccessor.setField(process, "requestResponseFactory", requestResponseFactory);
context.registerService(SlingRequestProcessor.class, requestProcessor);
PrivateAccessor.setField(process, "requestProcessor", requestProcessor);
}

@test
void test(){
String path = "/content/page/us";
context.load().json(getClass().getResourceAsStream("/com/test/workflow/processes/PSPPJSONArchiveProcessTest.json"),"/content");
request = new MockSlingHttpServletRequest(resourceResolverMock, context.bundleContext());
request.setResource(context.currentResource(path));
Mockito.when(requestResponseFactory.createRequest(Mockito.anyString(), Mockito.anyString()))
.thenReturn(request);
WCMMode.PREVIEW.toRequest(request);
Mockito.when(requestResponseFactory.createResponse(Mockito.any(OutputStream.class))).thenReturn(response);
Mockito.when(baosMock.toString()).thenReturn("test123asfasdfas");

process.execute(wfWorkItemMock, wfSessionMock, metaData);

}
===================================================================
Inside Workflow Process class

private String getJSONFromPSPPPage(String path, ResourceResolver resourceResolver) {
String psppJSON = null;
try(ByteArrayOutputStream baos = new ByteArrayOutputStream()) {

String psppPath = path + "/"+ JcrConstants.JCR_CONTENT + PSPP_JSON_EXTN;
logger.info("PSPP path for url : {}", psppPath);

HttpServletRequest req = requestResponseFactory.createRequest("GET", psppPath);
HttpServletResponse resp = requestResponseFactory.createResponse(baos);

requestProcessor.processRequest(req, resp, resourceResolver);
resp.getWriter().flush();

String baosString = baos.toString("UTF-8");
if (StringUtils.equals(resp.getContentType(), "application/json")) {
psppJSON = baosString;
}

logger.info("PSPP Json : {}", psppJSON);

} catch (Exception ex) {
logger.error("Error in creating json from PSPP", ex);
}

return psppJSON;

}
==================================

junit Mobile SlingRequestProcessorImpl workflow

Accepted Solutions (0)

Answers (1)

Answers (1)

Avatar

Avatar
Boost 5
Level 5
Ritesh_M
Level 5

Likes

101 likes

Total Posts

85 posts

Correct Reply

26 solutions
Top badges earned
Boost 5
Boost 3
Boost 1
Applaud 5
Affirm 1
View profile

Avatar
Boost 5
Level 5
Ritesh_M
Level 5

Likes

101 likes

Total Posts

85 posts

Correct Reply

26 solutions
Top badges earned
Boost 5
Boost 3
Boost 1
Applaud 5
Affirm 1
View profile
Ritesh_M
Level 5

13-05-2021

 
 

Hi @nj2 ,

 

I do not see any value assignment to baos variable inside getJSONFromPSPPPage method.

 

ByteArrayOutputStream baos = new ByteArrayOutputStrea

 

Above will give you empty baos object, which you are trying to convert to String with UTF-8 encoding so it will be empty too. So it it does not look like unit test issue.