Search in sources :

Example 6 with Designer

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);
}
Also used : Design(com.day.cq.wcm.api.designer.Design) Designer(com.day.cq.wcm.api.designer.Designer) ComponentContext(com.day.cq.wcm.api.components.ComponentContext) MockSlingHttpServletRequest(org.apache.sling.testing.mock.sling.servlet.MockSlingHttpServletRequest) Resource(org.apache.sling.api.resource.Resource)

Example 7 with Designer

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));
}
Also used : Designer(com.day.cq.wcm.api.designer.Designer) Resource(org.apache.sling.api.resource.Resource) ResourceResolver(org.apache.sling.api.resource.ResourceResolver) Style(com.day.cq.wcm.api.designer.Style) Test(org.junit.jupiter.api.Test)

Example 8 with Designer

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));
}
Also used : Designer(com.day.cq.wcm.api.designer.Designer) Resource(org.apache.sling.api.resource.Resource) ResourceResolver(org.apache.sling.api.resource.ResourceResolver) Style(com.day.cq.wcm.api.designer.Style) Test(org.junit.jupiter.api.Test)

Example 9 with Designer

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;
}
Also used : Designer(com.day.cq.wcm.api.designer.Designer) ValueMap(org.apache.sling.api.resource.ValueMap) Style(com.day.cq.wcm.api.designer.Style)

Example 10 with Designer

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" } }));
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) BeforeEach(org.junit.jupiter.api.BeforeEach) Assertions.assertNotNull(org.junit.jupiter.api.Assertions.assertNotNull) ResourceResolver(org.apache.sling.api.resource.ResourceResolver) Iterator(java.util.Iterator) Designer(com.day.cq.wcm.api.designer.Designer) MockStyle(com.adobe.cq.wcm.core.components.testing.MockStyle) Resource(org.apache.sling.api.resource.Resource) Value(com.adobe.granite.ui.components.Value) AemContextExtension(io.wcm.testing.mock.aem.junit5.AemContextExtension) Mockito.when(org.mockito.Mockito.when) Function(java.util.function.Function) AemContext(io.wcm.testing.mock.aem.junit5.AemContext) Objects(java.util.Objects) Test(org.junit.jupiter.api.Test) DataSource(com.adobe.granite.ui.components.ds.DataSource) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) CoreComponentTestContext(com.adobe.cq.wcm.core.components.context.CoreComponentTestContext) TextValueDataResourceSource(com.adobe.cq.wcm.core.components.internal.servlets.TextValueDataResourceSource) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) Mockito.mock(org.mockito.Mockito.mock) Designer(com.day.cq.wcm.api.designer.Designer) ResourceResolver(org.apache.sling.api.resource.ResourceResolver) Resource(org.apache.sling.api.resource.Resource) MockStyle(com.adobe.cq.wcm.core.components.testing.MockStyle) DataSource(com.adobe.granite.ui.components.ds.DataSource) Test(org.junit.jupiter.api.Test)

Aggregations

Designer (com.day.cq.wcm.api.designer.Designer)10 Resource (org.apache.sling.api.resource.Resource)7 ResourceResolver (org.apache.sling.api.resource.ResourceResolver)6 Page (com.day.cq.wcm.api.Page)3 Design (com.day.cq.wcm.api.designer.Design)3 Style (com.day.cq.wcm.api.designer.Style)3 ValueMap (org.apache.sling.api.resource.ValueMap)3 Test (org.junit.jupiter.api.Test)3 ContentPolicy (com.day.cq.wcm.api.policies.ContentPolicy)2 ContentPolicyManager (com.day.cq.wcm.api.policies.ContentPolicyManager)2 ArrayList (java.util.ArrayList)2 CoreComponentTestContext (com.adobe.cq.wcm.core.components.context.CoreComponentTestContext)1 TextValueDataResourceSource (com.adobe.cq.wcm.core.components.internal.servlets.TextValueDataResourceSource)1 MockStyle (com.adobe.cq.wcm.core.components.testing.MockStyle)1 Value (com.adobe.granite.ui.components.Value)1 DataSource (com.adobe.granite.ui.components.ds.DataSource)1 PageManager (com.day.cq.wcm.api.PageManager)1 ComponentContext (com.day.cq.wcm.api.components.ComponentContext)1 Reference (com.day.cq.wcm.api.reference.Reference)1 AemContext (io.wcm.testing.mock.aem.junit5.AemContext)1