AEM Servlet Unit Test

Avatar

Avatar
Level 1
ihsan_a
Level 1

Likes

0 likes

Total Posts

2 posts

Correct reply

0 solutions
View profile

Avatar
Level 1
ihsan_a
Level 1

Likes

0 likes

Total Posts

2 posts

Correct reply

0 solutions
View profile
ihsan_a
Level 1

04-06-2019

I have the servlet below to use com.adobe.fd.forms.api.FormsService. I'm trying to create a unit test (see below) but I'm getting the following error. It's failing at line 33 of the servlet:

Document xmlDocument = new Document(xmlData.getBytes());

It seems that the servelt is not initialized correctly or messing the references. Can you please help?

Error:

Servlet:

import java.io.IOException;

import javax.servlet.Servlet;

import org.apache.commons.httpclient.HttpStatus;

import org.apache.commons.io.IOUtils;

import org.apache.sling.api.SlingHttpServletRequest;

import org.apache.sling.api.SlingHttpServletResponse;

import org.apache.sling.api.servlets.HttpConstants;

import org.civ.forms.core.services.document.FormsDocumentServices;

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.adobe.aemfd.docmanager.Document;

@Component(service = Servlet.class,

property =

{ Constants.SERVICE_DESCRIPTION + "= ",

"sling.servlet.methods=" + HttpConstants.METHOD_POST,

"sling.servlet.paths=/bin/render",

"sling.auth.requirements=/bin/render"

})

public class FormRenderServlet extends org.apache.sling.api.servlets.SlingAllMethodsServlet {

@Reference

private transient FormsDocumentServices documentServices;

private static final String APPLICATION_PDF = "application/pdf";

private final transient Logger logger = LoggerFactory.getLogger(getClass());

@Override

protected void doPost(SlingHttpServletRequest request, SlingHttpServletResponse response) throws IOException {

try {

String xdpName = request.getParameter("xdpname");

String xmlData = request.getParameter("xmldata");

Document xmlDocument = new Document(xmlData.getBytes());

Document renderedPDF = documentServices.render(xdpName, xmlDocument);

String pdfFileName = xdpName.replace(".xdp", ".pdf");

response.setContentType(APPLICATION_PDF);

response.addHeader("Content-Disposition", "attachment; filename=" + pdfFileName);

response.setContentLength((int) renderedPDF.length());

IOUtils.copy(renderedPDF.getInputStream(), response.getOutputStream());

} catch (Exception e) {

response.setStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR);

logger.error("reading or writing opertion failed : {}", e);

}

}

}

Test:

import static org.junit.Assert.assertEquals;

import static org.mockito.Mockito.mock;

import static org.mockito.Mockito.when;

import java.io.IOException;

import javax.servlet.ServletException;

import org.apache.sling.api.SlingHttpServletRequest;

import org.apache.sling.api.SlingHttpServletResponse;

import org.apache.sling.testing.mock.sling.junit.SlingContext;

import org.eclipse.jetty.http.HttpStatus;

import org.junit.Rule;

import org.junit.Test;

public class FormRenderServletTest {

    @Rule

    public final SlingContext context = new SlingContext();

    private FormRenderServlet fixture = new FormRenderServlet();

    @Test

    public void doGet() throws ServletException, IOException {

    SlingHttpServletRequest request = mock(SlingHttpServletRequest.class);      

        SlingHttpServletResponse response = mock(SlingHttpServletResponse.class);   

       

        when(request.getParameter("xdpname")).thenReturn("xdpTemplate.xdp");

        when(request.getParameter("xmldata")).thenReturn("");

        fixture.doPost(request, response);

        assertEquals(HttpStatus.OK_200, response.getStatus());

    }

}

Replies

Avatar

Avatar
Boost 1
Level 1
abhinashs680544
Level 1

Like

1 like

Total Posts

2 posts

Correct reply

0 solutions
Top badges earned
Boost 1
View profile

Avatar
Boost 1
Level 1
abhinashs680544
Level 1

Like

1 like

Total Posts

2 posts

Correct reply

0 solutions
Top badges earned
Boost 1
View profile
abhinashs680544
Level 1

06-08-2019

Hi ihsan, I am facing same issue. Have you find the solution. If yes, please share

Avatar

Avatar
Ignite 1
Level 2
narindera257447
Level 2

Likes

5 likes

Total Posts

16 posts

Correct reply

0 solutions
Top badges earned
Ignite 1
Give Back 3
Give Back
Boost 5
Boost 3
View profile

Avatar
Ignite 1
Level 2
narindera257447
Level 2

Likes

5 likes

Total Posts

16 posts

Correct reply

0 solutions
Top badges earned
Ignite 1
Give Back 3
Give Back
Boost 5
Boost 3
View profile
narindera257447
Level 2

18-08-2019

Use @RunWith(MockitoJUnitRunner.class) on top of your classname since you are using Mockito.

Avatar

Avatar
Ignite 1
Level 2
narindera257447
Level 2

Likes

5 likes

Total Posts

16 posts

Correct reply

0 solutions
Top badges earned
Ignite 1
Give Back 3
Give Back
Boost 5
Boost 3
View profile

Avatar
Ignite 1
Level 2
narindera257447
Level 2

Likes

5 likes

Total Posts

16 posts

Correct reply

0 solutions
Top badges earned
Ignite 1
Give Back 3
Give Back
Boost 5
Boost 3
View profile
narindera257447
Level 2

18-08-2019

Also for third party API , you might need to use Mockito spy just trying having a check on it as well.

Avatar

Avatar
Ignite 1
Level 1
pradeep_kumar_3
Level 1

Like

1 like

Total Posts

6 posts

Correct reply

0 solutions
Top badges earned
Ignite 1
Give Back
Boost 1
View profile

Avatar
Ignite 1
Level 1
pradeep_kumar_3
Level 1

Like

1 like

Total Posts

6 posts

Correct reply

0 solutions
Top badges earned
Ignite 1
Give Back
Boost 1
View profile
pradeep_kumar_3
Level 1

21-08-2019

Hi Narindra, It will be great if you can share the sample code.

Avatar

Avatar
Boost 5
Level 2
abhinash007
Level 2

Likes

11 likes

Total Posts

5 posts

Correct reply

0 solutions
Top badges earned
Boost 5
Boost 3
Boost 10
Boost 1
View profile

Avatar
Boost 5
Level 2
abhinash007
Level 2

Likes

11 likes

Total Posts

5 posts

Correct reply

0 solutions
Top badges earned
Boost 5
Boost 3
Boost 10
Boost 1
View profile
abhinash007
Level 2

22-08-2019

Hi Everyone,

     You need to add Adobe AEMDS Core Document Manager Support (adobe-aemds-core-docmanager) bundle in classpath or you can add it dependency. This bundle is present in AEM. It will resolve DocumentSourceHandler class

1818982_pastedImage_0.png