Created
December 9, 2016 21:35
-
-
Save yupadhyay/72aacb699429ce728ec5801b19f319f8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.wemblog.test; | |
import com.adobe.granite.xss.XSSAPI; | |
import com.google.gson.JsonArray; | |
import java.io.ByteArrayOutputStream; | |
import java.io.IOException; | |
import java.io.PrintWriter; | |
import static org.junit.Assert.*; | |
import static org.mockito.Mockito.any; | |
import static org.mockito.Mockito.mock; | |
import static org.mockito.Mockito.when; | |
import static org.mockito.Mockito.doThrow; | |
import static org.mockito.Mockito.doAnswer; | |
import io.wcm.testing.mock.aem.junit.AemContext; | |
import org.apache.sling.testing.mock.sling.ResourceResolverType; | |
import com.google.gson.JsonObject; | |
import com.google.gson.JsonParser; | |
import org.apache.sling.api.SlingHttpServletRequest; | |
import org.apache.sling.api.SlingHttpServletResponse; | |
import org.junit.Before; | |
import org.junit.Test; | |
import org.junit.Rule; | |
import org.junit.runner.RunWith; | |
import org.mockito.InjectMocks; | |
import org.mockito.Mock; | |
import org.mockito.invocation.InvocationOnMock; | |
import org.mockito.stubbing.Answer; | |
import org.mockito.runners.MockitoJUnitRunner; | |
@RunWith(MockitoJUnitRunner.class) | |
public class AEMServletTest { | |
private String answerFromDoGet = ""; | |
private int statusForDoGet = 0; | |
private JsonParser parser = new JsonParser(); | |
@Rule | |
public final AemContext _context = new AemContext(ResourceResolverType.RESOURCERESOLVER_MOCK); | |
@Mock | |
private AEMTreatmentService _aemTreatServe = mock(AEMTreatmentService.class); | |
SlingHttpServletRequest _httpServletRequest = mock(SlingHttpServletRequest.class); | |
SlingHttpServletResponse _httpServletResponse = mock(SlingHttpServletResponse.class); | |
XSSAPI _xssapi = mock(XSSAPI.class); | |
PrintWriter _respOut = mock(PrintWriter.class); | |
@InjectMocks | |
AEMTreatmentServlet _aemServlet; | |
@Before | |
public void setUp() { | |
_context.registerService(AEMTreatmentService.class, _aemTreatServe); | |
try { | |
when(_httpServletResponse.getWriter()).thenReturn(_respOut); | |
doAnswer(new Answer<Object>() { | |
public Object answer(InvocationOnMock invocation) throws IOException { | |
Object[] args = invocation.getArguments(); | |
answerFromDoGet = (String) args[0]; | |
return null; | |
} | |
}).when(_respOut).write(any(String.class)); | |
} catch (Exception e) { | |
} | |
} | |
@Test | |
public void testDoGet() { | |
try { | |
when(_httpServletRequest.getParameter(AEMTreatmentService.TREATMENT_PARAM)).thenReturn("foo"); | |
_aemServlet.doGet(_httpServletRequest, _httpServletResponse); | |
JsonObject result = parser.parse(answerFromDoGet).getAsJsonObject(); | |
assertEquals("correctly set status", SlingHttpServletResponse.SC_OK, | |
result.get(AEMTreatmentService.STATUS_KEY).getAsInt()); | |
} catch (Exception e) { | |
assertTrue("error running testDoGet: " + e.getMessage(), false); | |
} | |
} | |
/** | |
* Test that the servlet returns the correct error state when the user fails | |
* to provide a testKey parameter. | |
*/ | |
@Test | |
public void testDoGetNoTestKey() { | |
try { | |
when(_httpServletRequest.getParameter(AEMTreatmentService.TEST_PARAM)).thenReturn(""); | |
_aemServlet.doGet(_httpServletRequest, _httpServletResponse); | |
JsonObject result = parser.parse(answerFromDoGet).getAsJsonObject(); | |
assertEquals("correctly set status", SlingHttpServletResponse.SC_BAD_REQUEST, | |
result.get(AEMTreatmentService.STATUS_KEY).getAsInt()); | |
} catch (Exception e) { | |
assertTrue("error running testDoGet: " + e.getMessage(), false); | |
} | |
} | |
@Test | |
public void testDoGetQAMode() { | |
try { | |
when(_httpServletRequest.getParameter(AEMTreatmentService.QA_MODE_PARAM)).thenReturn("true"); | |
_aemServlet.doGet(_httpServletRequest, _httpServletResponse); | |
JsonObject result = parser.parse(answerFromDoGet).getAsJsonObject(); | |
assertEquals("correctly set QA override", true, result.get(AEMTreatmentService.QA_MODE_KEY).getAsBoolean()); | |
} catch (Exception e) { | |
assertTrue("error running testDoGet: " + e.getMessage(), false); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment