Search in sources :

Example 31 with MockSlingHttpServletRequest

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

the class SocialMediaHelperImplTest method getSocialMediaHelperUnderTest.

private SocialMediaHelper getSocialMediaHelperUnderTest(String pagePath) {
    Resource currentResource = CONTEXT.resourceResolver().getResource(pagePath);
    Page currentPage = currentResource.adaptTo(Page.class);
    MockSlingHttpServletRequest request = new MockSlingHttpServletRequest(CONTEXT.resourceResolver(), CONTEXT.bundleContext());
    MockSlingHttpServletResponse response = new MockSlingHttpServletResponse();
    request.setContextPath(CONTEXT_PATH);
    request.setResource(currentResource);
    MockRequestPathInfo requestPathInfo = (MockRequestPathInfo) request.getRequestPathInfo();
    requestPathInfo.setExtension(EXTENSION);
    requestPathInfo.setResourcePath(currentResource.getPath());
    SlingBindings slingBindings = new SlingBindings();
    slingBindings.put(WCMBindings.CURRENT_PAGE, currentPage);
    slingBindings.put(SlingBindings.RESOLVER, CONTEXT.resourceResolver());
    slingBindings.put(SlingBindings.RESPONSE, response);
    request.setAttribute(SlingBindings.class.getName(), slingBindings);
    return request.adaptTo(SocialMediaHelper.class);
}
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) Page(com.day.cq.wcm.api.Page)

Example 32 with MockSlingHttpServletRequest

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

the class ImageImplTest method getImageUnderTest.

private Image getImageUnderTest(String resourcePath, WCMMode wcmMode) {
    Resource resource = aemContext.resourceResolver().getResource(resourcePath);
    ContentPolicyMapping mapping = resource.adaptTo(ContentPolicyMapping.class);
    ContentPolicy contentPolicy = mapping.getPolicy();
    SlingBindings slingBindings = new SlingBindings();
    if (contentPolicy != null) {
        when(contentPolicyManager.getPolicy(resource)).thenReturn(contentPolicy);
    }
    slingBindings.put(SlingBindings.RESOURCE, resource);
    final MockSlingHttpServletRequest request = new MockSlingHttpServletRequest(aemContext.resourceResolver(), aemContext.bundleContext());
    request.setContextPath(CONTEXT_PATH);
    request.setResource(resource);
    Page page = aemContext.pageManager().getPage(PAGE);
    slingBindings.put(WCMBindings.CURRENT_PAGE, page);
    if (wcmMode != null) {
        request.setAttribute(WCMMode.REQUEST_ATTRIBUTE_NAME, wcmMode);
    }
    slingBindings.put(WCMBindings.WCM_MODE, new SightlyWCMMode(request));
    slingBindings.put(WCMBindings.PAGE_MANAGER, aemContext.pageManager());
    Style style = mock(Style.class);
    when(style.get(anyString(), (Object) Matchers.anyObject())).thenAnswer(invocationOnMock -> invocationOnMock.getArguments()[1]);
    slingBindings.put(WCMBindings.CURRENT_STYLE, style);
    request.setAttribute(SlingBindings.class.getName(), slingBindings);
    return request.adaptTo(Image.class);
}
Also used : SlingBindings(org.apache.sling.api.scripting.SlingBindings) MockSlingHttpServletRequest(org.apache.sling.testing.mock.sling.servlet.MockSlingHttpServletRequest) ContentPolicyMapping(com.day.cq.wcm.api.policies.ContentPolicyMapping) Resource(org.apache.sling.api.resource.Resource) ContentPolicy(com.day.cq.wcm.api.policies.ContentPolicy) Style(com.day.cq.wcm.api.designer.Style) Page(com.day.cq.wcm.api.Page) SightlyWCMMode(com.adobe.cq.sightly.SightlyWCMMode)

Example 33 with MockSlingHttpServletRequest

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

the class PageImplTest method getPageUnderTest.

private Page getPageUnderTest(String pagePath, String designPath) {
    Resource resource = aemContext.currentResource(pagePath);
    com.day.cq.wcm.api.Page page = spy(aemContext.currentPage(pagePath));
    SlingBindings slingBindings = (SlingBindings) aemContext.request().getAttribute(SlingBindings.class.getName());
    Design design = mock(Design.class);
    if (designPath != null) {
        when(design.getPath()).thenReturn(designPath);
    } else {
        when(design.getPath()).thenReturn(Designer.DEFAULT_DESIGN_PATH);
    }
    Resource templateResource = aemContext.resourceResolver().getResource("/conf/coretest/settings/wcm/templates/product-page");
    Template template = mock(Template.class);
    when(template.hasStructureSupport()).thenReturn(true);
    when(template.adaptTo(Resource.class)).thenReturn(templateResource);
    when(page.getTemplate()).thenReturn(template);
    ContentPolicyMapping mapping = templateResource.getChild(POLICIES_MAPPING_PATH).adaptTo(ContentPolicyMapping.class);
    ContentPolicy contentPolicy = mapping.getPolicy();
    Style style;
    slingBindings.put(WCMBindings.CURRENT_DESIGN, design);
    if (contentPolicy != null) {
        Resource contentPolicyResource = aemContext.resourceResolver().getResource(contentPolicy.getPath());
        style = new MockStyle(contentPolicyResource, contentPolicyResource.adaptTo(ValueMap.class));
    } else {
        style = mock(Style.class);
        when(style.get(anyString(), Matchers.anyObject())).thenAnswer(invocationOnMock -> invocationOnMock.getArguments()[1]);
    }
    slingBindings.put(WCMBindings.CURRENT_STYLE, style);
    slingBindings.put(SlingBindings.RESOLVER, aemContext.request().getResourceResolver());
    slingBindings.put(WCMBindings.CURRENT_PAGE, page);
    slingBindings.put(WCMBindings.PAGE_MANAGER, aemContext.pageManager());
    slingBindings.put(SlingBindings.RESOURCE, resource);
    slingBindings.put(WCMBindings.PAGE_PROPERTIES, page.getProperties());
    MockSlingHttpServletRequest request = aemContext.request();
    request.setContextPath(CONTEXT_PATH);
    request.setResource(resource);
    return request.adaptTo(Page.class);
}
Also used : SlingBindings(org.apache.sling.api.scripting.SlingBindings) Resource(org.apache.sling.api.resource.Resource) ContentPolicy(com.day.cq.wcm.api.policies.ContentPolicy) Template(com.day.cq.wcm.api.Template) Design(com.day.cq.wcm.api.designer.Design) MockSlingHttpServletRequest(org.apache.sling.testing.mock.sling.servlet.MockSlingHttpServletRequest) ContentPolicyMapping(com.day.cq.wcm.api.policies.ContentPolicyMapping) MockStyle(com.adobe.cq.wcm.core.components.context.MockStyle) Style(com.day.cq.wcm.api.designer.Style) MockStyle(com.adobe.cq.wcm.core.components.context.MockStyle)

Example 34 with MockSlingHttpServletRequest

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

the class TextImplTest method getTestedText.

private Text getTestedText(String path) {
    Resource resource = context.currentResource(ROOT + "/" + path);
    MockSlingHttpServletRequest request = context.request();
    SlingBindings bindings = (SlingBindings) request.getAttribute(SlingBindings.class.getName());
    bindings.put(SlingBindings.RESOURCE, resource);
    return request.adaptTo(Text.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)

Example 35 with MockSlingHttpServletRequest

use of org.apache.sling.testing.mock.sling.servlet.MockSlingHttpServletRequest in project sling by apache.

the class LogTracerTest method recordingWithTracing.

@Test
public void recordingWithTracing() throws Exception {
    activateTracerAndServlet();
    MockSlingHttpServletRequest request = new MockSlingHttpServletRequest(context.bundleContext()) {

        @Override
        public RequestProgressTracker getRequestProgressTracker() {
            return createTracker("x", "y");
        }

        @Override
        public String getRequestURI() {
            return "foo";
        }
    };
    request.setHeader(TracerLogServlet.HEADER_TRACER_RECORDING, "true");
    request.setHeader(LogTracer.HEADER_TRACER_CONFIG, "a.b.c;level=trace,a.b;level=debug");
    HttpServletResponse response = mock(HttpServletResponse.class);
    FilterChain chain = new FilterChain() {

        @Override
        public void doFilter(ServletRequest request, ServletResponse response) throws IOException, ServletException {
            assertNotNull(context.getService(TurboFilter.class));
            getLogContext().addTurboFilter(context.getService(TurboFilter.class));
            getLogger("a.b").info("a.b-info");
        }
    };
    prepareChain(chain).doFilter(request, response);
    String requestId = getRequestId(response);
    assertNotNull(requestId);
    Recording r = ((TracerLogServlet) context.getService(Servlet.class)).getRecording(requestId);
    assertTrue(r instanceof JSONRecording);
    JSONRecording jr = (JSONRecording) r;
    StringWriter sw = new StringWriter();
    jr.render(sw);
    JsonObject json = Json.createReader(new StringReader(sw.toString())).readObject();
    assertEquals(2, json.getJsonArray("requestProgressLogs").size());
    assertEquals(1, json.getJsonArray("logs").size());
}
Also used : SlingHttpServletRequest(org.apache.sling.api.SlingHttpServletRequest) HttpServletRequest(javax.servlet.http.HttpServletRequest) MockSlingHttpServletRequest(org.apache.sling.testing.mock.sling.servlet.MockSlingHttpServletRequest) ServletRequest(javax.servlet.ServletRequest) HttpServletResponse(javax.servlet.http.HttpServletResponse) ServletResponse(javax.servlet.ServletResponse) FilterChain(javax.servlet.FilterChain) HttpServletResponse(javax.servlet.http.HttpServletResponse) JsonObject(javax.json.JsonObject) StringWriter(java.io.StringWriter) MockSlingHttpServletRequest(org.apache.sling.testing.mock.sling.servlet.MockSlingHttpServletRequest) StringReader(java.io.StringReader) Servlet(javax.servlet.Servlet) TurboFilter(ch.qos.logback.classic.turbo.TurboFilter) 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