Search in sources :

Example 21 with MockSlingHttpServletRequest

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

the class HiddenImplTest method prepareHiddenFieldForTest.

private Field prepareHiddenFieldForTest(String resourcePath) {
    MockSlingHttpServletRequest request = new MockSlingHttpServletRequest(CONTEXT.resourceResolver(), CONTEXT.bundleContext());
    Resource resource = CONTEXT.resourceResolver().getResource(resourcePath);
    request.setResource(resource);
    Page currentPage = CONTEXT.pageManager().getPage(CONTAINING_PAGE);
    SlingBindings slingBindings = new SlingBindings();
    slingBindings.put(WCMBindings.CURRENT_PAGE, currentPage);
    slingBindings.put(SlingBindings.RESOURCE, resource);
    slingBindings.put(WCMBindings.PROPERTIES, resource.getValueMap());
    request.setAttribute(SlingBindings.class.getName(), slingBindings);
    return request.adaptTo(Field.class);
}
Also used : SlingBindings(org.apache.sling.api.scripting.SlingBindings) MockSlingHttpServletRequest(org.apache.sling.testing.mock.sling.servlet.MockSlingHttpServletRequest) Resource(org.apache.sling.api.resource.Resource) Page(com.day.cq.wcm.api.Page)

Example 22 with MockSlingHttpServletRequest

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

the class ContainerImplTest method testFormWithCustomAttributesAndFields.

@Test
public void testFormWithCustomAttributesAndFields() {
    Resource resource = context.currentResource(FORM1_PATH);
    when(formStructureHelperFactory.getFormStructureHelper(resource)).thenReturn(formStructureHelper);
    slingBindings.put(WCMBindings.PROPERTIES, resource.adaptTo(ValueMap.class));
    slingBindings.put(RESOURCE_PROPERTY, resource);
    MockSlingHttpServletRequest request = context.request();
    request.setRequestDispatcherFactory(requestDispatcherFactory);
    when(requestDispatcherFactory.getRequestDispatcher((Resource) any(), (RequestDispatcherOptions) any())).thenReturn(requestDispatcher);
    Container container = request.adaptTo(Container.class);
    assertEquals("my-id", container.getId());
    assertEquals("my-id", container.getName());
    assertEquals("application/x-www-form-urlencoded", container.getEnctype());
    assertEquals("GET", container.getMethod());
    assertEquals(CONTAINING_PAGE + ".html", container.getAction());
    assertEquals("core/wcm/components/form/container/v1/container/new", container.getResourceTypeForDropArea());
    assertEquals("/content/we-retail/home", container.getRedirect());
}
Also used : Container(com.adobe.cq.wcm.core.components.models.form.Container) MockSlingHttpServletRequest(org.apache.sling.testing.mock.sling.servlet.MockSlingHttpServletRequest) ValueMap(org.apache.sling.api.resource.ValueMap) Resource(org.apache.sling.api.resource.Resource) Test(org.junit.Test)

Example 23 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 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 24 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 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 25 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 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)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