Search in sources :

Example 1 with SightlyWCMMode

use of com.adobe.cq.sightly.SightlyWCMMode in project aem-core-wcm-components by Adobe-Marketing-Cloud.

the class WCMUsePojoBaseTest method getResourceBackedBindings.

/**
     * <p>
     * Creates a {@link Bindings} map initialised with the following default bindings available to Sightly use objects based on {@link
     * WCMUsePojo}:
     * </p>
     * <ul>
     * <li>{@link SlingBindings#RESOURCE}</li>
     * <li>{@link SlingBindings#REQUEST}</li>
     * <li>{@link SlingBindings#RESPONSE}</li>
     * <li>{@link WCMBindings#PROPERTIES}</li>
     * <li>{@link WCMBindings#WCM_MODE}</li>
     * <li>{@link WCMBindings#PAGE_MANAGER}</li>
     * <li>{@link WCMBindings#RESOURCE_PAGE}</li>
     * <li>{@link WCMBindings#CURRENT_PAGE}</li>
     * <li>{@link WCMBindings#PAGE_PROPERTIES}</li>
     * </ul>
     *
     * @param resourcePath the path to a resource already loaded in the testing context
     * @return the bindings map
     */
protected Bindings getResourceBackedBindings(String resourcePath) {
    Bindings bindings = getDefaultSlingBindings();
    Resource resource = context.resourceResolver().getResource(resourcePath);
    if (resource != null) {
        ValueMap properties = resource.adaptTo(ValueMap.class);
        bindings.put(SlingBindings.RESOURCE, resource);
        bindings.put(WCMBindings.PROPERTIES, properties);
        bindings.put(WCMBindings.WCM_MODE, new SightlyWCMMode(context.request()));
        PageManager pageManager = context.pageManager();
        bindings.put(WCMBindings.PAGE_MANAGER, pageManager);
        context.request().setResource(resource);
        Page resourcePage = pageManager.getContainingPage(resource);
        if (resourcePage != null) {
            bindings.put(WCMBindings.RESOURCE_PAGE, resourcePage);
            bindings.put(WCMBindings.CURRENT_PAGE, resourcePage);
            bindings.put(WCMBindings.PAGE_PROPERTIES, properties);
        }
    } else {
        throw new IllegalArgumentException("Cannot find a resource at " + resourcePath);
    }
    return bindings;
}
Also used : PageManager(com.day.cq.wcm.api.PageManager) ValueMap(org.apache.sling.api.resource.ValueMap) Resource(org.apache.sling.api.resource.Resource) Page(com.day.cq.wcm.api.Page) SightlyWCMMode(com.adobe.cq.sightly.SightlyWCMMode) Bindings(javax.script.Bindings) SlingBindings(org.apache.sling.api.scripting.SlingBindings) SimpleBindings(javax.script.SimpleBindings) WCMBindings(com.adobe.cq.sightly.WCMBindings)

Example 2 with SightlyWCMMode

use of com.adobe.cq.sightly.SightlyWCMMode 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)

Aggregations

SightlyWCMMode (com.adobe.cq.sightly.SightlyWCMMode)2 Page (com.day.cq.wcm.api.Page)2 Resource (org.apache.sling.api.resource.Resource)2 SlingBindings (org.apache.sling.api.scripting.SlingBindings)2 WCMBindings (com.adobe.cq.sightly.WCMBindings)1 PageManager (com.day.cq.wcm.api.PageManager)1 Style (com.day.cq.wcm.api.designer.Style)1 ContentPolicy (com.day.cq.wcm.api.policies.ContentPolicy)1 ContentPolicyMapping (com.day.cq.wcm.api.policies.ContentPolicyMapping)1 Bindings (javax.script.Bindings)1 SimpleBindings (javax.script.SimpleBindings)1 ValueMap (org.apache.sling.api.resource.ValueMap)1 MockSlingHttpServletRequest (org.apache.sling.testing.mock.sling.servlet.MockSlingHttpServletRequest)1