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;
}

 

0 Replies