AEM servlet JUNIT 5 help
Hi Team,
I am trying to write JUNIT5 test code for below servlet. I am stuck and not able to proceed further. When I debug Test Class and it takes me to doGet Method of servlet,It always gives me NULL pointer Exception for RestAPIConfigurations service(). Highlighted in Blue Color.
Below are servlet and JUNIT code.
Any help will be appreciated.
SERVLET CODE:
@8220494(service = Servlet.class, property = {
Constants.SERVICE_ID + "=Dynamic DataSource Servlet",
SLING_SERVLET_METHODS + "=" + HttpConstants.METHOD_GET,
SLING_SERVLET_RESOURCE_TYPES + "=mysite/product/details"
})
public class HubSourceServlet extends SlingSafeMethodsServlet {
private static final String TAG = HubSourceServlet.class.getSimpleName();
private static final Logger LOGGER = LoggerFactory.getLogger(HubSourceServlet.class);
@3214626
RestAPIConfigurations restAPIConfigurations;
@9944223
protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) {
try {
ResourceResolver resourceResolver = request.getResourceResolver();
String domainName = restAPIConfigurations.getDomain();
LOGGER.debug(TAG, domainName);
Resource currentResource = request.getResource();
String selector = Objects.requireNonNull(currentResource.getChild("datasource")).getValueMap().get("selector", String.class);
String jsonData = StringUtils.EMPTY;
boolean fifthLevelFlag = true;
if(null != selector && StringUtils.equals("news", selector)){
fifthLevelFlag = false;
}
if(null != selector && StringUtils.equals("articles",selector)){
LOGGER.info("selector in HubSourceServlet is :",selector);
jsonData = getData(domainName+"/api/v1/REST/articles-groups");
}
else {
jsonData = getData(domainName+"/api/v1/REST/news-groups");
}
JSONArray data = new JSONArray(jsonData);
DataSource ds = new SimpleDataSource(getValueMapResource(data, resourceResolver));
request.setAttribute(DataSource.class.getName(), ds);
} catch (IOException | JSONException e) {
}
}
public String getData(String path) throws IOException {
TrustManager[] trustAll= new TrustManager[] {
new X509TrustManager() {
public java.security.cert.X509Certificate[] getAcceptedIssuers() {
return null;
}
public void checkClientTrusted(
java.security.cert.X509Certificate[] certs, String authType) {
}
public void checkServerTrusted(
java.security.cert.X509Certificate[] certs, String authType) {
}
}
};
SSLContext sc = null;
try {
sc = SSLContext.getInstance("SSL");
sc.init(null, trustAll, new java.security.SecureRandom());
HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
} catch (Exception e) {
e.printStackTrace();
}
InputStream is = null;
String content = null;
try {
LOGGER.info("In HubSourceServlet getData url: {}", path);
URL urlObj = new URL(path);
String auth = restAPIConfigurations.getUserName() + ":" + restAPIConfigurations.getPassName();
byte[] encodedAuth = Base64.getEncoder().encode(auth.getBytes(StandardCharsets.UTF_8));
String authHeaderValue = "Basic " + new String(encodedAuth);
HttpURLConnection httpURLConnection = (HttpURLConnection) urlObj.openConnection();
httpURLConnection.setRequestProperty("Authorization", authHeaderValue);
httpURLConnection.setRequestMethod("GET");
is = httpURLConnection.getInputStream();
content = IOUtils.toString(is, StandardCharsets.UTF_8);
LOGGER.info("In HubSourceServlet getData content: {}", content);
} finally {
IOUtils.closeQuietly(is);
}
return content;
}
public void setrestAPIConfigurations(restAPIConfigurations restAPIConfigurations) {
this.restAPIConfigurations = restAPIConfigurations;
}
}
JUNIT CLASS:
@ExtendWith(AemContextExtension.class)
@ExtendWith(MockitoExtension.class)
class HubSourceServletTest {
private final AemContext context = AppAemContext.newAemContext();
private static HubSourceServlet HubSourceServlet;
private static JSONArray data;
private static String domainName;
@Mock
private static RestAPIConfigurations restAPIConfigurations;
@Mock
private static Resource currentResource;
@Mock
private ResourceResolver resourceResolver;
@BeforeAll
public static void setUp() {
HubSourceServlet = new HubSourceServlet();
}
@BeforeEach
public void setUpBefore() {
when(restAPIConfigurations.getDomain()).thenReturn("mydomain");
}
@2785667
void doGet() {
SlingHttpServletRequest request = context.request();
SlingHttpServletResponse response = context.response();
HubSourceServlet.doGet(request,response);
assert (true);
}
}