Search in sources :

Example 51 with SlingBindings

use of org.apache.sling.api.scripting.SlingBindings in project aem-core-wcm-components by Adobe-Marketing-Cloud.

the class ComponentFilesImplTest method getComponentFilesUnderTest.

private ComponentFiles getComponentFilesUnderTest(String path, Map<String, Object> attributes) {
    Resource resource = context.currentResource(path);
    if (resource != null) {
        if (attributes != null) {
            SlingBindings slingBindings = (SlingBindings) context.request().getAttribute(SlingBindings.class.getName());
            for (Map.Entry<String, Object> entry : attributes.entrySet()) {
                slingBindings.put(entry.getKey(), entry.getValue());
            }
            context.request().setAttribute(SlingBindings.class.getName(), slingBindings);
        }
        context.request().setResource(resource);
        return context.request().adaptTo(ComponentFiles.class);
    }
    return null;
}
Also used : SlingBindings(org.apache.sling.api.scripting.SlingBindings) Resource(org.apache.sling.api.resource.Resource) HashMap(java.util.HashMap) ArgumentMatchers.anyMap(org.mockito.ArgumentMatchers.anyMap) Map(java.util.Map)

Example 52 with SlingBindings

use of org.apache.sling.api.scripting.SlingBindings in project aem-core-wcm-components by Adobe-Marketing-Cloud.

the class NavigationImplTest method getNavigationUnderTest.

protected Navigation getNavigationUnderTest(String resourcePath) {
    Utils.enableDataLayer(context, true);
    context.currentResource(resourcePath);
    MockSlingHttpServletRequest request = context.request();
    request.setContextPath("/core");
    Component component = mock(Component.class);
    when(component.getResourceType()).thenReturn(resourceType);
    SlingBindings slingBindings = (SlingBindings) request.getAttribute(SlingBindings.class.getName());
    slingBindings.put(WCMBindings.COMPONENT, component);
    request.setAttribute(SlingBindings.class.getName(), slingBindings);
    return request.adaptTo(Navigation.class);
}
Also used : SlingBindings(org.apache.sling.api.scripting.SlingBindings) MockSlingHttpServletRequest(org.apache.sling.testing.mock.sling.servlet.MockSlingHttpServletRequest) Component(com.day.cq.wcm.api.components.Component)

Example 53 with SlingBindings

use of org.apache.sling.api.scripting.SlingBindings in project aem-core-wcm-components by Adobe-Marketing-Cloud.

the class OptionsImplTest method getOptionsUnderTest.

private Options getOptionsUnderTest(String resourcePath) {
    Resource resource = context.resourceResolver().getResource(resourcePath);
    if (resource == null) {
        throw new IllegalStateException("Did you forget to define test resource " + resourcePath + "?");
    }
    MockSlingHttpServletRequest request = new MockSlingHttpServletRequest(context.resourceResolver(), context.bundleContext());
    MockSlingHttpServletResponse response = new MockSlingHttpServletResponse();
    request.setResource(resource);
    SlingBindings bindings = new SlingBindings();
    bindings.put(SlingBindings.RESOURCE, resource);
    bindings.put(SlingBindings.REQUEST, request);
    bindings.put(SlingBindings.RESOLVER, context.resourceResolver());
    bindings.put(SlingBindings.RESPONSE, response);
    bindings.put(WCMBindings.PROPERTIES, resource.getValueMap());
    request.setAttribute(SlingBindings.class.getName(), bindings);
    request.setRequestDispatcherFactory(new MockRequestDispatcherFactory() {

        @Override
        public RequestDispatcher getRequestDispatcher(String s, RequestDispatcherOptions requestDispatcherOptions) {
            return null;
        }

        @Override
        public RequestDispatcher getRequestDispatcher(Resource resource, RequestDispatcherOptions requestDispatcherOptions) {
            return new MockRequestDispatcher(requestDispatcherOptions);
        }
    });
    return request.adaptTo(Options.class);
}
Also used : SlingBindings(org.apache.sling.api.scripting.SlingBindings) RequestDispatcherOptions(org.apache.sling.api.request.RequestDispatcherOptions) 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) MockRequestDispatcherFactory(org.apache.sling.servlethelpers.MockRequestDispatcherFactory) RequestDispatcher(javax.servlet.RequestDispatcher)

Example 54 with SlingBindings

use of org.apache.sling.api.scripting.SlingBindings in project aem-core-wcm-components by Adobe-Marketing-Cloud.

the class ButtonImplTest method getButtonUnderTest.

private Button getButtonUnderTest(String resourcePath) {
    Resource resource = context.resourceResolver().getResource(resourcePath);
    MockSlingHttpServletRequest request = new MockSlingHttpServletRequest(context.resourceResolver(), context.bundleContext());
    request.setResource(resource);
    SlingBindings bindings = new SlingBindings();
    bindings.put(SlingBindings.RESOURCE, resource);
    request.setAttribute(SlingBindings.class.getName(), bindings);
    return request.adaptTo(Button.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 55 with SlingBindings

use of org.apache.sling.api.scripting.SlingBindings 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)

Aggregations

SlingBindings (org.apache.sling.api.scripting.SlingBindings)57 Resource (org.apache.sling.api.resource.Resource)26 MockSlingHttpServletRequest (org.apache.sling.testing.mock.sling.servlet.MockSlingHttpServletRequest)19 ResourceResolver (org.apache.sling.api.resource.ResourceResolver)12 Page (com.day.cq.wcm.api.Page)11 SlingScriptHelper (org.apache.sling.api.scripting.SlingScriptHelper)10 SlingHttpServletRequest (org.apache.sling.api.SlingHttpServletRequest)9 Before (org.junit.Before)8 Bindings (javax.script.Bindings)7 IOException (java.io.IOException)6 SimpleBindings (javax.script.SimpleBindings)6 Map (java.util.Map)5 MockRequestPathInfo (org.apache.sling.testing.mock.sling.servlet.MockRequestPathInfo)5 MockSlingHttpServletResponse (org.apache.sling.testing.mock.sling.servlet.MockSlingHttpServletResponse)5 PrintWriter (java.io.PrintWriter)4 HashMap (java.util.HashMap)4 ScriptException (javax.script.ScriptException)4 SlingHttpServletResponse (org.apache.sling.api.SlingHttpServletResponse)4 Servlet (javax.servlet.Servlet)3 ServletException (javax.servlet.ServletException)3