Junits services and servlets

Avatar

Avatar
Give Back 3
Level 2
sampath_kumar_g
Level 2

Likes

3 likes

Total Posts

17 posts

Correct reply

0 solutions
Top badges earned
Give Back 3
Give Back
Ignite 1
Validate 1
Boost 3
View profile

Avatar
Give Back 3
Level 2
sampath_kumar_g
Level 2

Likes

3 likes

Total Posts

17 posts

Correct reply

0 solutions
Top badges earned
Give Back 3
Give Back
Ignite 1
Validate 1
Boost 3
View profile
sampath_kumar_g
Level 2

01-07-2021

package com.combotool.community.core.services;

import static org.junit.Assert.assertEquals;

import java.io.IOException;
import java.io.PrintWriter;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Iterator;
import java.util.Locale;
import java.util.Map;

import javax.jcr.Node;
import javax.jcr.Property;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.SlingHttpServletResponse;
import org.apache.sling.api.resource.LoginException;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ResourceResolverFactory;
import org.apache.sling.api.resource.ValueMap;
import org.apache.sling.commons.json.JSONArray;
import org.apache.sling.commons.json.JSONException;
import org.apache.sling.commons.json.JSONObject;
import org.junit.Before;
import org.junit.Test;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;

import com.combotool.community.constants.ComboConstants;
import com.day.cq.commons.Filter;
import com.day.cq.tagging.Tag;
import com.day.cq.wcm.api.Page;
import com.day.cq.wcm.api.PageManager;
import com.day.cq.wcm.api.Template;
import com.day.cq.wcm.api.WCMException;

public class GuidelinesServiceTest {
@InjectMocks

GuidelinesService guidelinesService;
// HeLinksService heLinksService;

@Mock
ResourceResolverFactory resourceResolverFactory;
@Mock
ResourceResolver resourceResolver;
@Mock
Session session;
@Mock
PageManager pageManager;
@Mock
SlingHttpServletRequest request;
@Mock
SlingHttpServletResponse response;
@Mock
Page page1;
@Mock
Iterator<Page> iterator;
@Mock
Resource resource;
@Mock
Node node;
@Mock
Node subNode;
@Mock
PrintWriter print;
@Mock
Page childPage;
@Mock
ValueMap childProps;
@Mock
Property docTitle;

@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
}

@SuppressWarnings("unchecked")
@test
public void test() throws LoginException, RepositoryException, JSONException, IOException, ParseException {

Mockito.when(resourceResolverFactory.getServiceResourceResolver(Mockito.anyMap())).thenReturn(resourceResolver);
Mockito.when(resourceResolver.adaptTo(Session.class)).thenReturn(session);
Mockito.when(resourceResolver.adaptTo(PageManager.class)).thenReturn(pageManager);
Mockito.when(pageManager.getPage(ComboConstants.TRIAGE_GUIDELINES_SERVICE_URL)).thenReturn(page1);

Mockito.when(page1.listChildren()).thenReturn(iterator);
Mockito.when(iterator.hasNext()).thenReturn(true);
Mockito.when(iterator.next()).thenReturn(childPage);

Mockito.when(childPage.getContentResource()).thenReturn(resource);
Mockito.when(resource.adaptTo(Node.class)).thenReturn(node);
Mockito.when(node.hasNode("guideline")).thenReturn(true);
Mockito.when(node.hasProperty(Mockito.anyString())).thenReturn(true);
Mockito.when(childPage.getProperties()).thenReturn(childProps);
Mockito.when(node.getNode("guideline")).thenReturn(subNode);
Mockito.when(node.hasProperty("title")).thenReturn(true);
Mockito.when(node.getProperty("title")).thenReturn(docTitle);
Mockito.when(subNode.hasProperty("title")).thenReturn(true);
Mockito.when(subNode.getProperty("title")).thenReturn(docTitle);
//Mockito.when(childProps.get(Mockito.anyString(), Mockito.anyString())).thenReturn("2018-01-09T11:11:02.0+03:00");
Mockito.when(subNode.hasProperty("gender")).thenReturn(true);
Mockito.when(subNode.getProperty("gender")).thenReturn(docTitle);

JSONObject guidelineJsonObj = new JSONObject();
JSONObject jsonObj = new JSONObject();
JSONArray jsonArray = new JSONArray();
guidelineJsonObj.put("doctitle", docTitle);
jsonArray.put(guidelineJsonObj);
jsonObj.put("PageDetails", jsonArray);

Mockito.when(response.getWriter()).thenReturn(print);
guidelinesService.processRequest(request, response);
}
}

Servlet :

package com.combotool.community.core.servlets;


import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;

import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.SlingHttpServletResponse;
import org.apache.sling.commons.json.JSONObject;
import org.junit.Before;
import org.junit.Test;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;

import com.combotool.community.core.services.DbService;

public class DbServletTest {

@InjectMocks
DbServlet dbObj;

@Mock
DbService dbService;
@Mock
PrintWriter print;
@Mock
SlingHttpServletRequest request;
@Mock
SlingHttpServletResponse reponse;

@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
}

@test
public void testdoGet() throws IOException, ServletException {
JSONObject obj = new JSONObject();
Mockito.when(reponse.getWriter()).thenReturn(print);
Mockito.when(dbService.getDbObj()).thenReturn(obj);
dbObj.doPost(request, reponse);
}


}

Replies

Avatar

Avatar
Boost 100
Level 6
Dipti_Chauhan
Level 6

Likes

111 likes

Total Posts

133 posts

Correct reply

39 solutions
Top badges earned
Boost 100
Give Back 10
Affirm 25
Boost 50
Boost 25
View profile

Avatar
Boost 100
Level 6
Dipti_Chauhan
Level 6

Likes

111 likes

Total Posts

133 posts

Correct reply

39 solutions
Top badges earned
Boost 100
Give Back 10
Affirm 25
Boost 50
Boost 25
View profile
Dipti_Chauhan
Level 6

01-07-2021

@sampath_kumar_g 

 What you wanted to ask??

Avatar

Avatar
Boost 100
Level 6
snbaem
Level 6

Likes

112 likes

Total Posts

245 posts

Correct reply

35 solutions
Top badges earned
Boost 100
Springboard
Establish
Validate 25
Validate 10
View profile

Avatar
Boost 100
Level 6
snbaem
Level 6

Likes

112 likes

Total Posts

245 posts

Correct reply

35 solutions
Top badges earned
Boost 100
Springboard
Establish
Validate 25
Validate 10
View profile
snbaem
Level 6

18-07-2021

Hi @sampath_kumar_g 

If there no question here and rather an intent to demonstrate usage of Junit services and servlets, please consider adding this as a discussion.