Here is my Java class I wanted write Junit test cases for this class basically this servlet will return last modified date Error i am getting Null pointer on Asset asset = assetManager.getAsset(pdfUrl);
package com.aem.geeks.core.servlets;
import javax.servlet.Servlet;
import javax.servlet.ServletException;
import com.adobe.granite.asset.api.Asset;
import com.adobe.granite.asset.api.AssetManager;
import org.apache.felix.scr.annotations.sling.SlingServlet;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.SlingHttpServletResponse;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ValueMap;
import org.apache.sling.api.servlets.HttpConstants;
import org.apache.sling.api.servlets.SlingAllMethodsServlet;
import org.apache.sling.api.servlets.SlingSafeMethodsServlet;
import org.json.JSONException;
import org.json.JSONObject;
import org.osgi.service.component.annotations.Component;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
@Component(
service = Servlet.class,
property = {
"sling.servlet.methods=" + HttpConstants.METHOD_GET,
"sling.servlet.paths=" + "/bin/data/pdf",
})
public class GetpdfmdfDate extends SlingAllMethodsServlet {
private static final long serialVersionUID = 1L;
private static final Logger LOG = LoggerFactory.getLogger(GetpdfmdfDate.class);
@Override
protected void doGet(final SlingHttpServletRequest req,
final SlingHttpServletResponse resp) throws ServletException, IOException {
try{
String pdfString = req.getParameter("pdfs");
LOG.info("pdfs"+pdfString);
String []pdfs = pdfString.split(",");
ResourceResolver resourceResolver =req.getResourceResolver();
List<String> lastReplicatedDates= new ArrayList<>();
for(String pdfUrl: pdfs) {
LOG.info("pdf--->"+pdfUrl);
AssetManager assetManager = resourceResolver.adaptTo(AssetManager.class);
Asset asset = assetManager.getAsset(pdfUrl);
LOG.info("asset--->"+asset.getPath());
ValueMap map = asset.adaptTo(ValueMap.class);
if(map.containsKey("jcr:lastModified")) {
Date date = map.get("jcr:lastModified",Date.class);
SimpleDateFormat sdf = new SimpleDateFormat("MMMM d, yyyy");
LOG.info("date..."+sdf.format(date));
lastReplicatedDates.add(sdf.format(date)); // set last Modified date
}else {
lastReplicatedDates.add("");
}
LOG.info(" lastReplicatedDates--->"+lastReplicatedDates.size());
}
//resp.setContentType("application/json");
JSONObject json = new JSONObject();
json.put("lastModifiedDates", lastReplicatedDates);
resp.getWriter().write(json.toString());
} catch (JSONException e) {
// TODO Auto-generated catch block
LOG.info("Error json--->"+e.getMessage());
}
}
}