Search in sources :

Example 6 with ContentPolicy

use of com.day.cq.wcm.api.policies.ContentPolicy in project aem-core-wcm-components by Adobe-Marketing-Cloud.

the class AdaptiveImageServletTest method testWithInvalidDesignWidth.

@Test
public void testWithInvalidDesignWidth() throws Exception {
    Logger logger = spy(LoggerFactory.getLogger("FakeLogger"));
    setFinalStatic(AdaptiveImageServlet.class.getDeclaredField("LOGGER"), logger);
    Pair<MockSlingHttpServletRequest, MockSlingHttpServletResponse> requestResponsePair = prepareRequestResponsePair(IMAGE1_PATH, "img.700", "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(700, 700);
    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());
    verify(logger).warn("One of the configured widths ({}) from the {} content policy is not a valid Integer.", "invalid", "/conf/coretest/settings/wcm/policies/coretest/components/content/image/policy_1478854677327");
}
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) Logger(org.slf4j.Logger) BufferedImage(java.awt.image.BufferedImage) Test(org.junit.Test)

Example 7 with ContentPolicy

use of com.day.cq.wcm.api.policies.ContentPolicy in project aem-core-wcm-components by Adobe-Marketing-Cloud.

the class AdaptiveImageServletTest method testCorrectScalingPNGAssetWidth.

@Test
public void testCorrectScalingPNGAssetWidth() throws Exception {
    Pair<MockSlingHttpServletRequest, MockSlingHttpServletResponse> requestResponsePair = prepareRequestResponsePair(IMAGE0_PATH, "img.2000", "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 is equal to the image's width.", 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 8 with ContentPolicy

use of com.day.cq.wcm.api.policies.ContentPolicy 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 9 with ContentPolicy

use of com.day.cq.wcm.api.policies.ContentPolicy 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 10 with ContentPolicy

use of com.day.cq.wcm.api.policies.ContentPolicy 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)

Aggregations

ContentPolicy (com.day.cq.wcm.api.policies.ContentPolicy)20 ContentPolicyMapping (com.day.cq.wcm.api.policies.ContentPolicyMapping)17 MockSlingHttpServletRequest (org.apache.sling.testing.mock.sling.servlet.MockSlingHttpServletRequest)17 MockSlingHttpServletResponse (org.apache.sling.testing.mock.sling.servlet.MockSlingHttpServletResponse)15 Test (org.junit.Test)15 ByteArrayInputStream (java.io.ByteArrayInputStream)12 BufferedImage (java.awt.image.BufferedImage)10 Resource (org.apache.sling.api.resource.Resource)4 ContentPolicyManager (com.day.cq.wcm.api.policies.ContentPolicyManager)3 ResourceResolver (org.apache.sling.api.resource.ResourceResolver)3 Page (com.day.cq.wcm.api.Page)2 Style (com.day.cq.wcm.api.designer.Style)2 InputStream (java.io.InputStream)2 ArrayList (java.util.ArrayList)2 ValueMap (org.apache.sling.api.resource.ValueMap)2 SlingBindings (org.apache.sling.api.scripting.SlingBindings)2 SightlyWCMMode (com.adobe.cq.sightly.SightlyWCMMode)1 MockStyle (com.adobe.cq.wcm.core.components.context.MockStyle)1 DownloadResource (com.day.cq.commons.DownloadResource)1 ImageResource (com.day.cq.commons.ImageResource)1