use of com.day.cq.wcm.api.designer.Designer in project aem-core-wcm-components by Adobe-Marketing-Cloud.
the class PageImplTest method getPageUnderTest.
protected Page getPageUnderTest(String pagePath, Object... properties) {
Utils.enableDataLayer(context, true);
Map<String, Object> propertyMap = MapUtil.toMap(properties);
Resource resource = context.currentResource(pagePath + "/" + JcrConstants.JCR_CONTENT);
MockSlingHttpServletRequest request = context.request();
context.request().setContextPath("/core");
if (resource != null && !propertyMap.isEmpty()) {
if (propertyMap.containsKey(DESIGN_PATH_KEY)) {
Designer designer = context.resourceResolver().adaptTo(Designer.class);
if (designer != null) {
Design design = designer.getDesign(pagePath);
Design spyDesign = Mockito.spy(design);
Mockito.doReturn(propertyMap.get(DESIGN_PATH_KEY)).when(spyDesign).getPath();
request.setAttribute(DESIGN_CACHE_KEY, spyDesign);
}
}
if (propertyMap.containsKey(CSS_CLASS_NAMES_KEY)) {
ComponentContext spyComponentContext = Mockito.spy((ComponentContext) request.getAttribute(ComponentContext.CONTEXT_ATTR_NAME));
Mockito.doReturn(Sets.newLinkedHashSet(Arrays.asList((String[]) propertyMap.get(CSS_CLASS_NAMES_KEY)))).when(spyComponentContext).getCssClassNames();
request.setAttribute(ComponentContext.CONTEXT_ATTR_NAME, spyComponentContext);
}
context.contentPolicyMapping(resource.getResourceType(), properties);
}
return request.adaptTo(Page.class);
}
use of com.day.cq.wcm.api.designer.Designer in project aem-core-wcm-components by Adobe-Marketing-Cloud.
the class UtilsTest method testGetPropertyOrStyleReturnsStyle.
@Test
public void testGetPropertyOrStyleReturnsStyle() {
Resource resource = mock(Resource.class);
ResourceResolver resourceResolver = mock(ResourceResolver.class);
Designer designer = mock(Designer.class);
Style style = mock(Style.class);
when(resource.getValueMap()).thenReturn(ValueMap.EMPTY);
when(resource.getResourceResolver()).thenReturn(resourceResolver);
when(resourceResolver.adaptTo(Designer.class)).thenReturn(designer);
when(designer.getStyle(resource)).thenReturn(style);
when(style.get("foo", String.class)).thenReturn("bar");
assertEquals("bar", Utils.getPropertyOrStyle(resource, "foo", String.class));
}
use of com.day.cq.wcm.api.designer.Designer in project aem-core-wcm-components by Adobe-Marketing-Cloud.
the class UtilsTest method testGetPropertyOrStyleReturnsNothingWhenStyleMissing.
@Test
public void testGetPropertyOrStyleReturnsNothingWhenStyleMissing() {
Resource resource = mock(Resource.class);
ResourceResolver resourceResolver = mock(ResourceResolver.class);
Designer designer = mock(Designer.class);
Style style = mock(Style.class);
when(resource.getValueMap()).thenReturn(ValueMap.EMPTY);
when(resource.getResourceResolver()).thenReturn(resourceResolver);
// Designer missing
assertNull(Utils.getPropertyOrStyle(resource, "foo", String.class));
// Style missing
when(resourceResolver.adaptTo(Designer.class)).thenReturn(designer);
assertNull(Utils.getPropertyOrStyle(resource, "foo", String.class));
// Value missing
when(designer.getStyle(resource)).thenReturn(style);
assertNull(Utils.getPropertyOrStyle(resource, "foo", String.class));
}
use of com.day.cq.wcm.api.designer.Designer in project aem-core-wcm-components by Adobe-Marketing-Cloud.
the class Utils method getPropertyOrStyle.
/**
* Returns the property from the given {@link Resource} if it exists and is convertible to the requested type. If not it tries to get
* the property from the {@link Resource}'s {@link Style}.
*
* @param resource the {@link Resource}
* @param property the property name
* @param type the class of the requested type
* @param <T> the type of the expected return value
* @return the return value converted to the requested type, or null of not found in either of {@link Resource} properties or{@link Style}
*/
public static <T> T getPropertyOrStyle(Resource resource, String property, Class<T> type) {
ValueMap properties = resource.getValueMap();
T value = properties.get(property, type);
if (value == null) {
Designer designer = resource.getResourceResolver().adaptTo(Designer.class);
Style style = designer != null ? designer.getStyle(resource) : null;
if (style != null) {
value = style.get(property, type);
}
}
return value;
}
use of com.day.cq.wcm.api.designer.Designer in project aem-core-wcm-components by Adobe-Marketing-Cloud.
the class AllowedEmbeddablesDataSourceServletTest method testAllowedEmbeddablesDesignDataSourceServlet.
@Test
public void testAllowedEmbeddablesDesignDataSourceServlet() {
Designer designer = mock(Designer.class);
context.registerAdapter(ResourceResolver.class, Designer.class, (Function<ResourceResolver, Designer>) input -> designer);
Resource styleResource = context.resourceResolver().getResource("/apps/etc/designs/embed");
MockStyle mockStyle = new MockStyle(styleResource, styleResource.getValueMap());
when(designer.getStyle(any(Resource.class))).thenReturn(mockStyle);
context.request().setAttribute(Value.CONTENTPATH_ATTRIBUTE, CURRENT_PATH);
dataSourceServlet.doGet(context.request(), context.response());
DataSource dataSource = (DataSource) context.request().getAttribute(DataSource.class.getName());
assertNotNull(dataSource);
validateAllowedEmbeddables(dataSource, getExpectedAllowedEmbeddables(new String[][] { { "Select", "" }, { "Chatbot", "/apps/my-app/chatbot" }, { "Social", "/apps/my-app/social" } }));
}
Aggregations