Expand my Community achievements bar.

AEM Forms Assembler Service object throws Null Pointer

Avatar

Level 1

I am trying to write test cases using JUNIT4 for a class (AssemblerHelperService.java) with method (GetTotalPages) that accepts Assembler Service (com.adobe.fd.assembler.service.AssemblerService) instance and a PDF File (com.adobe.aemfd.docmanager.Document) as parameters.

I created a separate Test class (AssemblerHelperServiceTest) with a method getTotalPages, created an instance of Assembler Service and then read a PDF file that i placed in resources folder of my project and then passed them both to the actual method that does the processing.

when i run my test case, i always get Null Pointer exception for the Assembler Service object that i created within my test class, so the same gets passed to the actual method and eventually throws same Null Pointer Exception. Any Help would be much appreciated

I was thinking I might need to have a running local AEM server to retrieve the Assembler Service but did not find a way to do it

java.lang.NullPointerException at pdfgenerator.core.services.AssemblerHelperService.GetTotalPages(AssemblerHelperService.java:66) at pdfgenerator.core.services. AssemblerHelperServiceTest.getTotalPages(AssemblerHelperServiceTest.java:55)

Here is my sample Test code.

public class AssemblerHelperServiceTest 
{
private static final String processName = "Get Pages Test";

@Reference
public ServletConfigurer configurer1;

@Reference
private OutputService outputService;

private inputStream inputStream;

public AssemblerService asm;

@Test
public void getTotalPages() throws IOException, ParserConfigurationException, SAXException, TransformerException {
@NotNull AssemblerService asm = null;
InputStream is = Files.newInputStream(Paths.get("src/main/resources/test.pdf"));
Document pdfFile = new Document (is);
Integer expcount = 18;
Integer pgcount = null;
//THis is line 55 form the error i mentioned above, "asm" is null here
pgcount = Integer.valueOf(AssemblerHelperService.GetTotalPages(asm, pdfFile));
assertEquals(expcount, pgcount);
}

Here is the actual Method I am trying to call from my test method

 

public static String GetTotalPages(AssemblerService asm, com.adobe.aemfd.docmanager.Document pdfFile) {
AdobeStandardLogger.logMessageInfo(processName, "Inside GetTotalPages");
Document ddFile = null;
String pageCount = "";
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db;
try {
// Create a Document object based on the DDX file
db = dbf.newDocumentBuilder();
ddFile = db.newDocument();
Element root = (Element) ddFile.createElement("DDX");
root.setAttribute("xmlns", "http://ns.adobe.com/DDX/1.0/");
ddFile.appendChild(root);
//AdobeStandardLogger.logMessageInfo(processName, "root appended");
Element documentInformation = (Element) ddFile.createElement("DocumentInformation");
documentInformation.setAttribute("result", "info.xml");
documentInformation.setAttribute("source", "flatOutput");
root.appendChild(documentInformation);
//AdobeStandardLogger.logMessageInfo(processName, "documentInformation appended");
com.adobe.aemfd.docmanager.Document myDDX = ConversionUtils.convertDomToAEMFD(ddFile);

// Create a Map object to store PDF source documents
Map<String,Object> inputs = new HashMap<String,Object>();
inputs.put("flatOutput", pdfFile);

// Create an AssemblerOptionsSpec object
AssemblerOptionSpec assemblerSpec = new AssemblerOptionSpec();
assemblerSpec.setFailOnError(false);

// Submit the job to Assembler service, this is the line 66 from the error, "asm" is null here
AssemblerResult jobResult = asm.invoke(myDDX, inputs, assemblerSpec);
Map allDocs = jobResult.getDocuments();
com.adobe.aemfd.docmanager.Document outDoc = null;
// Iterate through the map object to retrieve the result PDF document
for (Iterator i = allDocs.entrySet().iterator(); i.hasNext();) {
//AdobeStandardLogger.logMessageInfo(processName, "Inside Iterator" + i.toString());
// Retrieve the Map object�s value
Map.Entry e = (Map.Entry) i.next();
// Get the key name as specified in the
// DDX document
String keyName = (String) e.getKey();
//AdobeStandardLogger.logMessageInfo(processName, "keyName=" + keyName);
if (keyName.equalsIgnoreCase("info.xml")) {
Object o = e.getValue();
outDoc = (com.adobe.aemfd.docmanager.Document) o;
//AdobeStandardLogger.logMessageInfo(processName, "outDoc value" + outDoc);
}
}
org.w3c.dom.Document doc = ConversionUtils.convertAEMFDToDom(outDoc);
doc.getDocumentElement().normalize();
pageCount = doc.getElementsByTagName("NumPages").item(0).getTextContent();
}
catch (ParserConfigurationException | TransformerException | TransformerFactoryConfigurationError| SAXException | IOException e) {
e.printStackTrace();
} catch (OperationException e1) {
e1.printStackTrace();
}
return pageCount;
}

 

4 Replies

Avatar

Moderator

In the first function getTotalPages, asm is initialised to null. So, it should produce NPE (null pointer exception). To create an object of a java class, in this context, it is either supposed to be referred using @reference or get initialised using a constructor. I am not saying there cannot be other ways possible but the object needs to be initialised with a non null value.

Avatar

Level 1

Thanks, Khushwant, I tried @reference but did not have any luck getting through NPE. I will explore the below framework and let you know if i have any success.

 

Regards,

Abhishek

Avatar

Employee

You are facing a NPE because the Junit test you are writing is a basic unit test with no server-side integration and hence, the instance of AssemblerService will always be null. What you need to write is an integration test using Apache Sling Teleporter framework which will help you write the test like a simple unit test but it will actually be an IT. 

 

Apache Sling Teleporter 

Avatar

Level 1

Thank you, i will explore this option and let you know hot it goes.