Expand my Community achievements bar.

SOLVED

How to write Junit test cases for path based sling servlet for pdf generation using output service

Avatar

Level 2

Can anyone please suggest to write Junit test cases for the following path based sling servlet?

-  Servlet for PDF generation, using output service with post method

- Takes two input parameter

  1. xdp from path /content/dam/formsanddocuments .
  2. xml data from postman.

- Using generatePDFOutput method of output service to generate pdf

Code snippet:

@component(service = { Servlet.class }, property = { SLING_SERVLET_METHODS + "=POST",

SLING_SERVLET_EXTENSIONS + "=json", SLING_SERVLET_PATHS + "=/bin/generatePDFService" })

 

public class PDFCreationServlet extends SlingAllMethodsServlet {

 

private static final String XDP_PATH = "/content/dam/formsanddocuments/";

 

private static String XML_PATH = "";

 

private static final long serialVersionUID = 1L;

 

private static final String SERVICE_USER_ACCOUNT = "serviceuser";

 

 

 

@reference

private transient ResourceResolverFactory resourceResolverFactory;

 

@reference

OutputService outputService;

 

public void doPost(final SlingHttpServletRequest request, final SlingHttpServletResponse response)

throws ServletException, IOException {

 

final Map<String, Object> serviceUserSession = Collections.singletonMap(ResourceResolverFactory.SUBSERVICE,

SERVICE_USER_ACCOUNT);

 

try {

ResourceResolver resolver = resourceResolverFactory.getServiceResourceResolver(serviceUserSession);

Resource res = resolver.getResource(XDP_PATH + "Autumn.xdp");

 

// getting xdp from repository and converting into document

Asset asset = res.adaptTo(Asset.class);

Resource xdp = asset.getOriginal();

InputStream xdpIS = xdp.adaptTo(InputStream.class);

Document xdpDocument = new Document(xdpIS);

 

 

// getting XML data via postman and converting into document

XML_PATH = request.getParameter("formData");

String xmlData=null;

String xmlFile = new String(Files.readAllBytes(Paths.get(XML_PATH)));

// xml parsing

xmlData = com.UticaPoC.parsing.XmlParserData.parseXmlData(xmlFile);

InputStream xmlIS = IOUtils.toInputStream(xmlData, StandardCharsets.UTF_8);

Document xmlDocument = new Document(xmlIS);

 

//output options

PDFOutputOptions outputOptions = new PDFOutputOptions();

outputOptions.setAcrobatVersion(AcrobatVersion.Acrobat_11);

Document doc;

doc = outputService.generatePDFOutput(xdpDocument, xmlDocument, outputOptions);

 

// Save the result in a PDF file

File pdfFile = new File("C:\\PDF's\\GeneratedPDF.pdf");

doc.copyToFile(pdfFile);

 

response.setContentType("application/pdf");

response.addHeader("Content-Disposition", "attachment; filename=output.pdf");

OutputStream responseOutputStream = response.getOutputStream();

IOUtils.copy(doc.getInputStream(), responseOutputStream);

 

responseOutputStream.flush();

responseOutputStream.close();

} catch (OutputServiceException e) {

e.printStackTrace();

response.getWriter().write("OutputServiceException occurred: PDF generation failed. " + e.getMessage());

} catch (LoginException e) {

// TODO Auto-generated catch block

e.printStackTrace();

response.getWriter().write("LoginException occurred: " + e.getMessage());

} catch (Exception e) {

// TODO Auto-generated catch block

e.printStackTrace();

response.getWriter().write("Exception occurred: " + e.getMessage());

}

}

 

Any reference or suggestion will be helpful to write junit test cases for the above servlet.

Thanks.

 

 

 

 

 

 

1 Accepted Solution

Avatar

Correct answer by
Employee Advisor
1 Reply

Avatar

Correct answer by
Employee Advisor

@chayanika_27 are you not able to mock it like any other servlet? 

 

https://taylor.callsen.me/unit-testing-aem-sling-models-and-servlets/