Search in sources :

Example 1 with MockSlingHttpServletResponse

use of org.apache.sling.commons.testing.sling.MockSlingHttpServletResponse in project sling by apache.

the class JsonResponseTest method testSend.

public void testSend() throws Exception {
    res.onChange("modified", "argument1");
    MockSlingHttpServletResponse response = new MockSlingHttpServletResponse();
    res.send(response, true);
    JsonObject result = Json.createReader(new StringReader(response.getOutput().toString())).readObject();
    assertProperty(result, HtmlResponse.PN_STATUS_CODE, Integer.toString(HttpServletResponse.SC_OK));
    assertEquals(JSONResponse.RESPONSE_CONTENT_TYPE, response.getContentType());
    assertEquals(JSONResponse.RESPONSE_CHARSET, response.getCharacterEncoding());
}
Also used : StringReader(java.io.StringReader) MockSlingHttpServletResponse(org.apache.sling.commons.testing.sling.MockSlingHttpServletResponse) JsonObject(javax.json.JsonObject)

Example 2 with MockSlingHttpServletResponse

use of org.apache.sling.commons.testing.sling.MockSlingHttpServletResponse in project sling by apache.

the class JsonResponseTest method testSetError.

@SuppressWarnings({ "ThrowableInstanceNeverThrown" })
public void testSetError() throws IOException {
    String errMsg = "Dummy error";
    res.setError(new Error(errMsg));
    MockSlingHttpServletResponse resp = new MockSlingHttpServletResponse();
    res.send(resp, true);
    JsonObject json = res.getJson();
    JsonValue error = assertProperty(json, "error");
    assertProperty((JsonObject) error, "class", Error.class.getName());
    assertProperty((JsonObject) error, "message", errMsg);
}
Also used : JsonValue(javax.json.JsonValue) MockSlingHttpServletResponse(org.apache.sling.commons.testing.sling.MockSlingHttpServletResponse) JsonObject(javax.json.JsonObject) JsonString(javax.json.JsonString)

Example 3 with MockSlingHttpServletResponse

use of org.apache.sling.commons.testing.sling.MockSlingHttpServletResponse in project acs-aem-commons by Adobe-Consulting-Services.

the class CustomComponentActivatorListServletTest method testCustom.

@Test
public void testCustom() throws Exception {
    CustomComponentActivatorListServlet servlet = new CustomComponentActivatorListServlet();
    servlet.activate(Collections.singletonMap("components", new String[] { "test=my/test/component" }));
    MockSlingHttpServletRequest request = new MockSlingHttpServletRequest(null, null, null, null, null);
    MockSlingHttpServletResponse response = new MockSlingHttpServletResponse();
    servlet.doGet(request, response);
    String result = response.getOutput().toString();
    JSONAssert.assertEquals("{\"components\":[{\"propertyName\":\"test\",\"componentPath\":\"my/test/component\"}]}", result, JSONCompareMode.NON_EXTENSIBLE);
}
Also used : MockSlingHttpServletRequest(org.apache.sling.commons.testing.sling.MockSlingHttpServletRequest) MockSlingHttpServletResponse(org.apache.sling.commons.testing.sling.MockSlingHttpServletResponse) Test(org.junit.Test)

Example 4 with MockSlingHttpServletResponse

use of org.apache.sling.commons.testing.sling.MockSlingHttpServletResponse in project acs-aem-commons by Adobe-Consulting-Services.

the class CustomComponentActivatorListServletTest method testDefault.

@Test
public void testDefault() throws Exception {
    CustomComponentActivatorListServlet servlet = new CustomComponentActivatorListServlet();
    servlet.activate(Collections.emptyMap());
    MockSlingHttpServletRequest request = new MockSlingHttpServletRequest(null, null, null, null, null);
    MockSlingHttpServletResponse response = new MockSlingHttpServletResponse();
    servlet.doGet(request, response);
    String result = response.getOutput().toString();
    JSONAssert.assertEquals(DEFAULT_RESULT, result, JSONCompareMode.NON_EXTENSIBLE);
}
Also used : MockSlingHttpServletRequest(org.apache.sling.commons.testing.sling.MockSlingHttpServletRequest) MockSlingHttpServletResponse(org.apache.sling.commons.testing.sling.MockSlingHttpServletResponse) Test(org.junit.Test)

Example 5 with MockSlingHttpServletResponse

use of org.apache.sling.commons.testing.sling.MockSlingHttpServletResponse in project acs-aem-commons by Adobe-Consulting-Services.

the class OptionsServletTest method testWithBadType.

@Test
public void testWithBadType() throws Exception {
    MockSlingHttpServletRequest request = new MockSlingHttpServletRequest("/apps/acs-commons/components/utilities/designer/clientlibsmanager/options", "html", "json", null, null);
    MockSlingHttpServletResponse response = new MockSlingHttpServletResponse();
    servlet.doGet(request, response);
    assertEquals("application/json", response.getContentType());
    assertEquals("[]", response.getOutput().toString());
}
Also used : MockSlingHttpServletRequest(org.apache.sling.commons.testing.sling.MockSlingHttpServletRequest) MockSlingHttpServletResponse(org.apache.sling.commons.testing.sling.MockSlingHttpServletResponse) Test(org.junit.Test)

Aggregations

MockSlingHttpServletResponse (org.apache.sling.commons.testing.sling.MockSlingHttpServletResponse)8 MockSlingHttpServletRequest (org.apache.sling.commons.testing.sling.MockSlingHttpServletRequest)6 Test (org.junit.Test)5 JsonObject (javax.json.JsonObject)2 ClientLibrary (com.adobe.granite.ui.clientlibs.ClientLibrary)1 StringReader (java.io.StringReader)1 HashMap (java.util.HashMap)1 JsonString (javax.json.JsonString)1 JsonValue (javax.json.JsonValue)1 JSONArray (org.apache.sling.commons.json.JSONArray)1 JsonRendererServlet (org.apache.sling.servlets.get.impl.helpers.JsonRendererServlet)1