Search in sources :

Example 1 with MockSlingHttpServletResponse

use of org.apache.sling.testing.mock.sling.servlet.MockSlingHttpServletResponse in project aem-core-wcm-components by Adobe-Marketing-Cloud.

the class AdaptiveImageServletTest method testDAMAssetCropScaling.

@Test
public void testDAMAssetCropScaling() throws Exception {
    Pair<MockSlingHttpServletRequest, MockSlingHttpServletResponse> requestResponsePair = prepareRequestResponsePair(IMAGE9_PATH, "img.1440", "png");
    MockSlingHttpServletRequest request = requestResponsePair.getLeft();
    MockSlingHttpServletResponse response = requestResponsePair.getRight();
    ContentPolicyMapping mapping = request.getResource().adaptTo(ContentPolicyMapping.class);
    ContentPolicy contentPolicy = mapping.getPolicy();
    when(contentPolicyManager.getPolicy(request.getResource())).thenReturn(contentPolicy);
    servlet.doGet(request, response);
    BufferedImage image = ImageIO.read(new ByteArrayInputStream(response.getOutput()));
    assertEquals("Expected the cropped rectangle to have a 1390px width, since the servlet should not perform cropping upscaling.", 1390, image.getWidth());
    assertEquals("Expected the cropped rectangle to have a 515px height, since the servlet should not perform cropping upscaling.", 515, image.getHeight());
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) MockSlingHttpServletRequest(org.apache.sling.testing.mock.sling.servlet.MockSlingHttpServletRequest) ContentPolicyMapping(com.day.cq.wcm.api.policies.ContentPolicyMapping) ContentPolicy(com.day.cq.wcm.api.policies.ContentPolicy) MockSlingHttpServletResponse(org.apache.sling.testing.mock.sling.servlet.MockSlingHttpServletResponse) BufferedImage(java.awt.image.BufferedImage) Test(org.junit.Test)

Example 2 with MockSlingHttpServletResponse

use of org.apache.sling.testing.mock.sling.servlet.MockSlingHttpServletResponse in project aem-core-wcm-components by Adobe-Marketing-Cloud.

the class AdaptiveImageServletTest method prepareRequestResponsePair.

private Pair<MockSlingHttpServletRequest, MockSlingHttpServletResponse> prepareRequestResponsePair(String resourcePath, String selectorString, String extension) {
    final MockSlingHttpServletRequest request = new MockSlingHttpServletRequest(aemContext.resourceResolver(), aemContext.bundleContext());
    final MockSlingHttpServletResponse response = new MockSlingHttpServletResponse();
    Resource resource = resourceResolver.getResource(resourcePath);
    request.setResource(resource);
    MockRequestPathInfo requestPathInfo = (MockRequestPathInfo) request.getRequestPathInfo();
    requestPathInfo.setSelectorString(selectorString);
    requestPathInfo.setExtension(extension);
    SlingBindings bindings = new SlingBindings();
    bindings.put(SlingBindings.REQUEST, request);
    bindings.put(SlingBindings.RESPONSE, response);
    bindings.put(SlingBindings.SLING, aemContext.slingScriptHelper());
    bindings.put(SlingBindings.RESOLVER, resourceResolver);
    request.setAttribute(SlingBindings.class.getName(), bindings);
    return new Pair<MockSlingHttpServletRequest, MockSlingHttpServletResponse>() {

        @Override
        public MockSlingHttpServletRequest getLeft() {
            return request;
        }

        @Override
        public MockSlingHttpServletResponse getRight() {
            return response;
        }

        @Override
        public MockSlingHttpServletResponse setValue(MockSlingHttpServletResponse value) {
            throw new UnsupportedOperationException();
        }
    };
}
Also used : MockRequestPathInfo(org.apache.sling.testing.mock.sling.servlet.MockRequestPathInfo) SlingBindings(org.apache.sling.api.scripting.SlingBindings) MockSlingHttpServletRequest(org.apache.sling.testing.mock.sling.servlet.MockSlingHttpServletRequest) Resource(org.apache.sling.api.resource.Resource) MockSlingHttpServletResponse(org.apache.sling.testing.mock.sling.servlet.MockSlingHttpServletResponse) Pair(org.apache.commons.lang3.tuple.Pair)

Example 3 with MockSlingHttpServletResponse

use of org.apache.sling.testing.mock.sling.servlet.MockSlingHttpServletResponse in project aem-core-wcm-components by Adobe-Marketing-Cloud.

the class AdaptiveImageServletTest method testGIFFileBrowserCached.

@Test
public void testGIFFileBrowserCached() throws Exception {
    Pair<MockSlingHttpServletRequest, MockSlingHttpServletResponse> requestResponsePair = prepareRequestResponsePair(IMAGE5_PATH, "img", "gif");
    MockSlingHttpServletRequest request = requestResponsePair.getLeft();
    MockSlingHttpServletResponse response = requestResponsePair.getRight();
    // 1 millisecond less than the jcr:lastModified value from test-conf.json
    request.addDateHeader("If-Modified-Since", 1489998822137L);
    servlet.doGet(request, response);
    assertEquals(HttpServletResponse.SC_NOT_MODIFIED, response.getStatus());
}
Also used : MockSlingHttpServletRequest(org.apache.sling.testing.mock.sling.servlet.MockSlingHttpServletRequest) MockSlingHttpServletResponse(org.apache.sling.testing.mock.sling.servlet.MockSlingHttpServletResponse) Test(org.junit.Test)

Example 4 with MockSlingHttpServletResponse

use of org.apache.sling.testing.mock.sling.servlet.MockSlingHttpServletResponse in project aem-core-wcm-components by Adobe-Marketing-Cloud.

the class AdaptiveImageServletTest method testRequestWithWidthDesignAllowed.

@Test
public void testRequestWithWidthDesignAllowed() throws Exception {
    Pair<MockSlingHttpServletRequest, MockSlingHttpServletResponse> requestResponsePair = prepareRequestResponsePair(IMAGE0_PATH, "img.800", "png");
    MockSlingHttpServletRequest request = requestResponsePair.getLeft();
    MockSlingHttpServletResponse response = requestResponsePair.getRight();
    ContentPolicyMapping mapping = request.getResource().adaptTo(ContentPolicyMapping.class);
    ContentPolicy contentPolicy = mapping.getPolicy();
    when(contentPolicyManager.getPolicy(request.getResource())).thenReturn(contentPolicy);
    servlet.doGet(request, response);
    ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(response.getOutput());
    BufferedImage image = ImageIO.read(byteArrayInputStream);
    Dimension expectedDimension = new Dimension(800, 800);
    Dimension actualDimension = new Dimension(image.getWidth(), image.getHeight());
    assertEquals("Expected image rendered at requested size.", expectedDimension, actualDimension);
    assertEquals("Expected a PNG image.", "image/png", response.getContentType());
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) MockSlingHttpServletRequest(org.apache.sling.testing.mock.sling.servlet.MockSlingHttpServletRequest) ContentPolicyMapping(com.day.cq.wcm.api.policies.ContentPolicyMapping) ContentPolicy(com.day.cq.wcm.api.policies.ContentPolicy) MockSlingHttpServletResponse(org.apache.sling.testing.mock.sling.servlet.MockSlingHttpServletResponse) BufferedImage(java.awt.image.BufferedImage) Test(org.junit.Test)

Example 5 with MockSlingHttpServletResponse

use of org.apache.sling.testing.mock.sling.servlet.MockSlingHttpServletResponse in project aem-core-wcm-components by Adobe-Marketing-Cloud.

the class AdaptiveImageServletTest method testDAMAssetWithNegativeRequestedWidth.

@Test
public void testDAMAssetWithNegativeRequestedWidth() throws Exception {
    Pair<MockSlingHttpServletRequest, MockSlingHttpServletResponse> requestResponsePair = prepareRequestResponsePair(IMAGE8_PATH, "img.-1", "png");
    MockSlingHttpServletRequest request = requestResponsePair.getLeft();
    MockSlingHttpServletResponse response = requestResponsePair.getRight();
    ContentPolicyMapping mapping = request.getResource().adaptTo(ContentPolicyMapping.class);
    ContentPolicy contentPolicy = mapping.getPolicy();
    when(contentPolicyManager.getPolicy(request.getResource())).thenReturn(contentPolicy);
    servlet.doGet(request, response);
    assertEquals(HttpServletResponse.SC_BAD_REQUEST, response.getStatus());
}
Also used : MockSlingHttpServletRequest(org.apache.sling.testing.mock.sling.servlet.MockSlingHttpServletRequest) ContentPolicyMapping(com.day.cq.wcm.api.policies.ContentPolicyMapping) ContentPolicy(com.day.cq.wcm.api.policies.ContentPolicy) MockSlingHttpServletResponse(org.apache.sling.testing.mock.sling.servlet.MockSlingHttpServletResponse) Test(org.junit.Test)

Aggregations

MockSlingHttpServletRequest (org.apache.sling.testing.mock.sling.servlet.MockSlingHttpServletRequest)29 MockSlingHttpServletResponse (org.apache.sling.testing.mock.sling.servlet.MockSlingHttpServletResponse)29 Test (org.junit.Test)24 ContentPolicy (com.day.cq.wcm.api.policies.ContentPolicy)15 ContentPolicyMapping (com.day.cq.wcm.api.policies.ContentPolicyMapping)15 ByteArrayInputStream (java.io.ByteArrayInputStream)15 BufferedImage (java.awt.image.BufferedImage)11 InputStream (java.io.InputStream)4 Resource (org.apache.sling.api.resource.Resource)2 SlingBindings (org.apache.sling.api.scripting.SlingBindings)2 MockRequestPathInfo (org.apache.sling.testing.mock.sling.servlet.MockRequestPathInfo)2 Page (com.day.cq.wcm.api.Page)1 HashMap (java.util.HashMap)1 Pair (org.apache.commons.lang3.tuple.Pair)1 SlingHttpServletRequest (org.apache.sling.api.SlingHttpServletRequest)1 SlingHttpServletResponse (org.apache.sling.api.SlingHttpServletResponse)1 RatingsService (org.apache.sling.sample.slingshot.ratings.RatingsService)1 Before (org.junit.Before)1 Logger (org.slf4j.Logger)1