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;
}
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);
}
Aggregations