Search in sources :

Example 36 with MockSlingHttpServletRequest

use of org.apache.sling.testing.mock.sling.servlet.MockSlingHttpServletRequest in project sling by apache.

the class MockSlingScriptHelperTest method setUp.

@Before
public void setUp() throws Exception {
    this.bundleContext = MockOsgi.newBundleContext();
    this.resourceResolver = MockSling.newResourceResolver(bundleContext);
    this.request = new MockSlingHttpServletRequest(this.resourceResolver, bundleContext);
    this.response = new MockSlingHttpServletResponse();
    this.scriptHelper = MockSling.newSlingScriptHelper(this.request, this.response, this.bundleContext);
}
Also used : MockSlingHttpServletRequest(org.apache.sling.testing.mock.sling.servlet.MockSlingHttpServletRequest) MockSlingHttpServletResponse(org.apache.sling.testing.mock.sling.servlet.MockSlingHttpServletResponse) Before(org.junit.Before)

Example 37 with MockSlingHttpServletRequest

use of org.apache.sling.testing.mock.sling.servlet.MockSlingHttpServletRequest in project sling by apache.

the class RatingPostServletTest method successfulSave.

//    @Test
public void successfulSave() throws Exception {
    Map<String, Object> params = new HashMap<String, Object>();
    params.put(RatingsUtil.PROPERTY_RATING, "5");
    context.registerService(RatingsService.class, Mockito.mock(RatingsService.class));
    RatingPostServlet servlet = context.registerInjectActivateService(new RatingPostServlet());
    MockSlingHttpServletRequest request = context.request();
    request.setRemoteUser("admin");
    request.setParameterMap(params);
    request.setResource(context.create().resource(SlingshotConstants.APP_ROOT_PATH + "/content/admin/travel"));
    MockSlingHttpServletResponse response = new MockSlingHttpServletResponse();
    servlet.doPost(request, response);
    assertThat(response.getStatus(), Matchers.equalTo(SC_OK));
    String output = response.getOutputAsString();
    assertThat(output, equalTo("{  \"rating\" : 0}"));
}
Also used : HashMap(java.util.HashMap) MockSlingHttpServletRequest(org.apache.sling.testing.mock.sling.servlet.MockSlingHttpServletRequest) RatingsService(org.apache.sling.sample.slingshot.ratings.RatingsService) MockSlingHttpServletResponse(org.apache.sling.testing.mock.sling.servlet.MockSlingHttpServletResponse)

Example 38 with MockSlingHttpServletRequest

use of org.apache.sling.testing.mock.sling.servlet.MockSlingHttpServletRequest in project sling by apache.

the class LogTracerTest method recordingWithTracing.

@Test
public void recordingWithTracing() throws Exception {
    activateTracerAndServlet();
    MockSlingHttpServletRequest request = new MockSlingHttpServletRequest(context.bundleContext()) {

        @Override
        public RequestProgressTracker getRequestProgressTracker() {
            return createTracker("x", "y");
        }

        @Override
        public String getRequestURI() {
            return "foo";
        }
    };
    request.setHeader(TracerLogServlet.HEADER_TRACER_RECORDING, "true");
    request.setHeader(LogTracer.HEADER_TRACER_CONFIG, "a.b.c;level=trace,a.b;level=debug");
    HttpServletResponse response = mock(HttpServletResponse.class);
    FilterChain chain = new FilterChain() {

        @Override
        public void doFilter(ServletRequest request, ServletResponse response) throws IOException, ServletException {
            assertNotNull(context.getService(TurboFilter.class));
            getLogContext().addTurboFilter(context.getService(TurboFilter.class));
            getLogger("a.b").info("a.b-info");
        }
    };
    prepareChain(chain).doFilter(request, response);
    String requestId = getRequestId(response);
    assertNotNull(requestId);
    Recording r = ((TracerLogServlet) context.getService(Servlet.class)).getRecording(requestId);
    assertTrue(r instanceof JSONRecording);
    JSONRecording jr = (JSONRecording) r;
    StringWriter sw = new StringWriter();
    jr.render(sw);
    JsonObject json = Json.createReader(new StringReader(sw.toString())).readObject();
    assertEquals(2, json.getJsonArray("requestProgressLogs").size());
    assertEquals(1, json.getJsonArray("logs").size());
}
Also used : SlingHttpServletRequest(org.apache.sling.api.SlingHttpServletRequest) HttpServletRequest(javax.servlet.http.HttpServletRequest) MockSlingHttpServletRequest(org.apache.sling.testing.mock.sling.servlet.MockSlingHttpServletRequest) ServletRequest(javax.servlet.ServletRequest) HttpServletResponse(javax.servlet.http.HttpServletResponse) ServletResponse(javax.servlet.ServletResponse) FilterChain(javax.servlet.FilterChain) HttpServletResponse(javax.servlet.http.HttpServletResponse) JsonObject(javax.json.JsonObject) StringWriter(java.io.StringWriter) MockSlingHttpServletRequest(org.apache.sling.testing.mock.sling.servlet.MockSlingHttpServletRequest) StringReader(java.io.StringReader) Servlet(javax.servlet.Servlet) TurboFilter(ch.qos.logback.classic.turbo.TurboFilter) Test(org.junit.Test)

Example 39 with MockSlingHttpServletRequest

use of org.apache.sling.testing.mock.sling.servlet.MockSlingHttpServletRequest in project sling by apache.

the class TracerLogServletTest method recordingWhenRequested.

@Test
public void recordingWhenRequested() throws Exception {
    TracerLogServlet logServlet = new TracerLogServlet(context.bundleContext());
    request = new MockSlingHttpServletRequest(context.bundleContext());
    Recording recording = logServlet.startRecording(request, response);
    assertNotNull(recording);
    //Once recording is created then it should be returned
    Recording recording2 = logServlet.getRecordingForRequest(request);
    assertSame(recording, recording2);
    //Repeated call should return same recording instance
    Recording recording3 = logServlet.startRecording(request, response);
    assertSame(recording, recording3);
    logServlet.resetCache();
    //If recording gets lost then NOOP must be returned
    Recording recording4 = logServlet.getRecordingForRequest(request);
    assertSame(Recording.NOOP, recording4);
}
Also used : MockSlingHttpServletRequest(org.apache.sling.testing.mock.sling.servlet.MockSlingHttpServletRequest) Test(org.junit.Test)

Aggregations

MockSlingHttpServletRequest (org.apache.sling.testing.mock.sling.servlet.MockSlingHttpServletRequest)39 MockSlingHttpServletResponse (org.apache.sling.testing.mock.sling.servlet.MockSlingHttpServletResponse)29 Test (org.junit.Test)29 ContentPolicy (com.day.cq.wcm.api.policies.ContentPolicy)17 ContentPolicyMapping (com.day.cq.wcm.api.policies.ContentPolicyMapping)17 ByteArrayInputStream (java.io.ByteArrayInputStream)15 BufferedImage (java.awt.image.BufferedImage)11 SlingBindings (org.apache.sling.api.scripting.SlingBindings)8 Resource (org.apache.sling.api.resource.Resource)7 InputStream (java.io.InputStream)4 Page (com.day.cq.wcm.api.Page)3 StringWriter (java.io.StringWriter)3 SlingHttpServletRequest (org.apache.sling.api.SlingHttpServletRequest)3 TurboFilter (ch.qos.logback.classic.turbo.TurboFilter)2 Style (com.day.cq.wcm.api.designer.Style)2 StringReader (java.io.StringReader)2 HashMap (java.util.HashMap)2 JsonObject (javax.json.JsonObject)2 FilterChain (javax.servlet.FilterChain)2 Servlet (javax.servlet.Servlet)2