Search in sources :

Example 26 with MockSlingHttpServletResponse

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

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

the class MockSling method newSlingScriptHelper.

/**
     * Creates a new sling script helper instance using
     * {@link #DEFAULT_RESOURCERESOLVER_TYPE} for the resource resolver.
     * @param bundleContext Bundle context
     * @return Sling script helper instance
     */
public static SlingScriptHelper newSlingScriptHelper(BundleContext bundleContext) {
    SlingHttpServletRequest request = new MockSlingHttpServletRequest(newResourceResolver(bundleContext), bundleContext);
    SlingHttpServletResponse response = new MockSlingHttpServletResponse();
    return newSlingScriptHelper(request, response, bundleContext);
}
Also used : MockSlingHttpServletResponse(org.apache.sling.testing.mock.sling.servlet.MockSlingHttpServletResponse) SlingHttpServletResponse(org.apache.sling.api.SlingHttpServletResponse) MockSlingHttpServletRequest(org.apache.sling.testing.mock.sling.servlet.MockSlingHttpServletRequest) MockSlingHttpServletResponse(org.apache.sling.testing.mock.sling.servlet.MockSlingHttpServletResponse) MockSlingHttpServletRequest(org.apache.sling.testing.mock.sling.servlet.MockSlingHttpServletRequest) SlingHttpServletRequest(org.apache.sling.api.SlingHttpServletRequest)

Example 28 with MockSlingHttpServletResponse

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

the class MockSlingScriptHelperTest method setUp.

@Before
public void setUp() throws Exception {
    this.bundleContext = MockOsgi.newBundleContext();
    this.resourceResolver = MockSling.newResourceResolver(bundleContext);
    this.request = new MockSlingHttpServletRequest(this.resourceResolver, bundleContext);
    this.response = new MockSlingHttpServletResponse();
    this.scriptHelper = MockSling.newSlingScriptHelper(this.request, this.response, this.bundleContext);
}
Also used : MockSlingHttpServletRequest(org.apache.sling.testing.mock.sling.servlet.MockSlingHttpServletRequest) MockSlingHttpServletResponse(org.apache.sling.testing.mock.sling.servlet.MockSlingHttpServletResponse) Before(org.junit.Before)

Example 29 with MockSlingHttpServletResponse

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

the class RatingPostServletTest method successfulSave.

//    @Test
public void successfulSave() throws Exception {
    Map<String, Object> params = new HashMap<String, Object>();
    params.put(RatingsUtil.PROPERTY_RATING, "5");
    context.registerService(RatingsService.class, Mockito.mock(RatingsService.class));
    RatingPostServlet servlet = context.registerInjectActivateService(new RatingPostServlet());
    MockSlingHttpServletRequest request = context.request();
    request.setRemoteUser("admin");
    request.setParameterMap(params);
    request.setResource(context.create().resource(SlingshotConstants.APP_ROOT_PATH + "/content/admin/travel"));
    MockSlingHttpServletResponse response = new MockSlingHttpServletResponse();
    servlet.doPost(request, response);
    assertThat(response.getStatus(), Matchers.equalTo(SC_OK));
    String output = response.getOutputAsString();
    assertThat(output, equalTo("{  \"rating\" : 0}"));
}
Also used : HashMap(java.util.HashMap) MockSlingHttpServletRequest(org.apache.sling.testing.mock.sling.servlet.MockSlingHttpServletRequest) RatingsService(org.apache.sling.sample.slingshot.ratings.RatingsService) MockSlingHttpServletResponse(org.apache.sling.testing.mock.sling.servlet.MockSlingHttpServletResponse)

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