Search in sources :

Example 16 with MockSlingHttpServletRequest

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

the class AdaptiveImageServletTest method testRequestNoWidthWithDesign.

@Test
public void testRequestNoWidthWithDesign() throws Exception {
    Pair<MockSlingHttpServletRequest, MockSlingHttpServletResponse> requestResponsePair = prepareRequestResponsePair(IMAGE0_PATH, "img", "png");
    MockSlingHttpServletRequest request = requestResponsePair.getLeft();
    MockSlingHttpServletResponse response = spy(requestResponsePair.getRight());
    ContentPolicyMapping mapping = request.getResource().adaptTo(ContentPolicyMapping.class);
    ContentPolicy contentPolicy = mapping.getPolicy();
    when(contentPolicyManager.getPolicy(request.getResource())).thenReturn(contentPolicy);
    servlet.doGet(request, response);
    verify(response).setContentType("image/png");
    ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(response.getOutput());
    BufferedImage image = ImageIO.read(byteArrayInputStream);
    Dimension expectedDimension = new Dimension(600, 600);
    Dimension actualDimension = new Dimension(image.getWidth(), image.getHeight());
    assertEquals("Expected image rendered using the first width defined in the content policy.", expectedDimension, actualDimension);
}
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 17 with MockSlingHttpServletRequest

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

the class AdaptiveImageServletTest method testDAMFileUpscaledPNG.

@Test
public void testDAMFileUpscaledPNG() throws Exception {
    Pair<MockSlingHttpServletRequest, MockSlingHttpServletResponse> requestResponsePair = prepareRequestResponsePair(IMAGE0_PATH, "img.2500", "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);
    InputStream directStream = this.getClass().getClassLoader().getResourceAsStream("image/Adobe_Systems_logo_and_wordmark.svg.png");
    ByteArrayInputStream stream = new ByteArrayInputStream(response.getOutput());
    assertTrue("Expected to get the original asset back, since the requested width would result in upscaling the image.", IOUtils.contentEquals(directStream, stream));
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) MockSlingHttpServletRequest(org.apache.sling.testing.mock.sling.servlet.MockSlingHttpServletRequest) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) 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 18 with MockSlingHttpServletRequest

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

the class AdaptiveImageServletTest method testRequestWithWidthNoDesign.

@Test
public void testRequestWithWidthNoDesign() throws Exception {
    Pair<MockSlingHttpServletRequest, MockSlingHttpServletResponse> requestResponsePair = prepareRequestResponsePair(IMAGE0_PATH, "img.800", "png");
    MockSlingHttpServletRequest request = requestResponsePair.getLeft();
    MockSlingHttpServletResponse response = requestResponsePair.getRight();
    servlet.doGet(request, response);
    assertEquals("Expected a 404 response when the request contains width information but no content policy has been defined.", 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) MockSlingHttpServletResponse(org.apache.sling.testing.mock.sling.servlet.MockSlingHttpServletResponse) Test(org.junit.Test)

Example 19 with MockSlingHttpServletRequest

use of org.apache.sling.testing.mock.sling.servlet.MockSlingHttpServletRequest 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 20 with MockSlingHttpServletRequest

use of org.apache.sling.testing.mock.sling.servlet.MockSlingHttpServletRequest 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)

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