Search in sources :

Example 16 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 testRequestWithWidthDesignNotAllowed.

@Test
public void testRequestWithWidthDesignNotAllowed() throws Exception {
    Pair<MockSlingHttpServletRequest, MockSlingHttpServletResponse> requestResponsePair = prepareRequestResponsePair(IMAGE0_PATH, "img.1000", "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("Expected a 404 response when the design does not allow the requested width to be rendered.", HttpServletResponse.SC_NOT_FOUND, response.getStatus());
    assertArrayEquals("Expected an empty response output.", new byte[0], response.getOutput());
}
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)

Example 17 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 testRequestNoWidthNoDesign.

@Test
public void testRequestNoWidthNoDesign() throws Exception {
    Pair<MockSlingHttpServletRequest, MockSlingHttpServletResponse> requestResponsePair = prepareRequestResponsePair(IMAGE0_PATH, "img", "png");
    MockSlingHttpServletRequest request = requestResponsePair.getLeft();
    MockSlingHttpServletResponse response = spy(requestResponsePair.getRight());
    servlet.doGet(request, response);
    verify(response).setContentType("image/png");
    ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(response.getOutput());
    BufferedImage image = ImageIO.read(byteArrayInputStream);
    Dimension expectedDimension = new Dimension(ADAPTIVE_IMAGE_SERVLET_DEFAULT_RESIZE_WIDTH, ADAPTIVE_IMAGE_SERVLET_DEFAULT_RESIZE_WIDTH);
    Dimension actualDimension = new Dimension(image.getWidth(), image.getHeight());
    assertEquals("Expected image rendered with the default resize configuration width.", expectedDimension, actualDimension);
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) MockSlingHttpServletRequest(org.apache.sling.testing.mock.sling.servlet.MockSlingHttpServletRequest) MockSlingHttpServletResponse(org.apache.sling.testing.mock.sling.servlet.MockSlingHttpServletResponse) BufferedImage(java.awt.image.BufferedImage) Test(org.junit.Test)

Example 18 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 testGIFUploadedToDAM.

@Test
public void testGIFUploadedToDAM() throws Exception {
    Pair<MockSlingHttpServletRequest, MockSlingHttpServletResponse> requestResponsePair = prepareRequestResponsePair(IMAGE6_PATH, "img", "gif");
    MockSlingHttpServletRequest request = requestResponsePair.getLeft();
    MockSlingHttpServletResponse response = requestResponsePair.getRight();
    servlet.doGet(request, response);
    ByteArrayInputStream stream = new ByteArrayInputStream(response.getOutput());
    InputStream directStream = this.getClass().getClassLoader().getResourceAsStream("image/Adobe_Systems_logo_and_wordmark.svg.gif");
    assertTrue(IOUtils.contentEquals(stream, directStream));
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) MockSlingHttpServletRequest(org.apache.sling.testing.mock.sling.servlet.MockSlingHttpServletRequest) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) MockSlingHttpServletResponse(org.apache.sling.testing.mock.sling.servlet.MockSlingHttpServletResponse) Test(org.junit.Test)

Example 19 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 testDAMAssetCropScalingWithRotation.

@Test
public void testDAMAssetCropScalingWithRotation() throws Exception {
    Pair<MockSlingHttpServletRequest, MockSlingHttpServletResponse> requestResponsePair = prepareRequestResponsePair(IMAGE13_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 515px width, since the servlet should not perform cropping upscaling.", 515, image.getWidth());
    assertEquals("Expected the cropped rectangle to have a 1390px height, since the servlet should not perform cropping upscaling.", 1390, 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 20 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 testDAMAssetCrop.

@Test
public void testDAMAssetCrop() throws Exception {
    Pair<MockSlingHttpServletRequest, MockSlingHttpServletResponse> requestResponsePair = prepareRequestResponsePair(IMAGE11_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 1440px width, since the servlet should not perform cropping upscaling.", 1440, image.getWidth());
    assertEquals("Expected the cropped rectangle to have a 1440px height, since the servlet should not perform cropping upscaling.", 1440, 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)

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