Search in sources :

Example 6 with Style

use of com.day.cq.wcm.api.designer.Style 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)

Example 7 with Style

use of com.day.cq.wcm.api.designer.Style in project aem-core-wcm-components by Adobe-Marketing-Cloud.

the class PageImplTest method getPageUnderTest.

private Page getPageUnderTest(String pagePath, String designPath) {
    Resource resource = aemContext.currentResource(pagePath);
    com.day.cq.wcm.api.Page page = spy(aemContext.currentPage(pagePath));
    SlingBindings slingBindings = (SlingBindings) aemContext.request().getAttribute(SlingBindings.class.getName());
    Design design = mock(Design.class);
    if (designPath != null) {
        when(design.getPath()).thenReturn(designPath);
    } else {
        when(design.getPath()).thenReturn(Designer.DEFAULT_DESIGN_PATH);
    }
    Resource templateResource = aemContext.resourceResolver().getResource("/conf/coretest/settings/wcm/templates/product-page");
    Template template = mock(Template.class);
    when(template.hasStructureSupport()).thenReturn(true);
    when(template.adaptTo(Resource.class)).thenReturn(templateResource);
    when(page.getTemplate()).thenReturn(template);
    ContentPolicyMapping mapping = templateResource.getChild(POLICIES_MAPPING_PATH).adaptTo(ContentPolicyMapping.class);
    ContentPolicy contentPolicy = mapping.getPolicy();
    Style style;
    slingBindings.put(WCMBindings.CURRENT_DESIGN, design);
    if (contentPolicy != null) {
        Resource contentPolicyResource = aemContext.resourceResolver().getResource(contentPolicy.getPath());
        style = new MockStyle(contentPolicyResource, contentPolicyResource.adaptTo(ValueMap.class));
    } else {
        style = mock(Style.class);
        when(style.get(anyString(), Matchers.anyObject())).thenAnswer(invocationOnMock -> invocationOnMock.getArguments()[1]);
    }
    slingBindings.put(WCMBindings.CURRENT_STYLE, style);
    slingBindings.put(SlingBindings.RESOLVER, aemContext.request().getResourceResolver());
    slingBindings.put(WCMBindings.CURRENT_PAGE, page);
    slingBindings.put(WCMBindings.PAGE_MANAGER, aemContext.pageManager());
    slingBindings.put(SlingBindings.RESOURCE, resource);
    slingBindings.put(WCMBindings.PAGE_PROPERTIES, page.getProperties());
    MockSlingHttpServletRequest request = aemContext.request();
    request.setContextPath(CONTEXT_PATH);
    request.setResource(resource);
    return request.adaptTo(Page.class);
}
Also used : SlingBindings(org.apache.sling.api.scripting.SlingBindings) Resource(org.apache.sling.api.resource.Resource) ContentPolicy(com.day.cq.wcm.api.policies.ContentPolicy) Template(com.day.cq.wcm.api.Template) Design(com.day.cq.wcm.api.designer.Design) MockSlingHttpServletRequest(org.apache.sling.testing.mock.sling.servlet.MockSlingHttpServletRequest) ContentPolicyMapping(com.day.cq.wcm.api.policies.ContentPolicyMapping) MockStyle(com.adobe.cq.wcm.core.components.context.MockStyle) Style(com.day.cq.wcm.api.designer.Style) MockStyle(com.adobe.cq.wcm.core.components.context.MockStyle)

Example 8 with Style

use of com.day.cq.wcm.api.designer.Style 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 9 with Style

use of com.day.cq.wcm.api.designer.Style in project aem-core-wcm-components by Adobe-Marketing-Cloud.

the class Utils method getWrappedImageResourceWithInheritance.

/**
 * Wraps an image resource with the properties and child resources of the inherited featured image of either
 * the linked page or the page containing the resource.
 *
 * @param resource The image resource
 * @param linkHandler The link handler
 * @param currentStyle The style of the image resource
 * @param currentPage The page containing the image resource
 * @return The wrapped image resource augmented with inherited properties and child resource if inheritance is enabled, the plain image resource otherwise.
 */
public static Resource getWrappedImageResourceWithInheritance(Resource resource, LinkHandler linkHandler, Style currentStyle, Page currentPage) {
    if (resource == null) {
        LOGGER.error("The resource is not defined");
        return null;
    }
    if (linkHandler == null) {
        LOGGER.error("The link handler is not defined");
        return null;
    }
    ValueMap properties = resource.getValueMap();
    String fileReference = properties.get(DownloadResource.PN_REFERENCE, String.class);
    Resource fileResource = resource.getChild(DownloadResource.NN_FILE);
    boolean imageFromPageImage = properties.get(PN_IMAGE_FROM_PAGE_IMAGE, StringUtils.isEmpty(fileReference) && fileResource == null);
    boolean altValueFromPageImage = properties.get(PN_ALT_VALUE_FROM_PAGE_IMAGE, imageFromPageImage);
    if (imageFromPageImage) {
        Resource inheritedResource = null;
        String linkURL = properties.get(ImageResource.PN_LINK_URL, String.class);
        boolean actionsEnabled = (currentStyle != null) ? !currentStyle.get(Teaser.PN_ACTIONS_DISABLED, !properties.get(Teaser.PN_ACTIONS_ENABLED, true)) : properties.get(Teaser.PN_ACTIONS_ENABLED, true);
        Resource firstAction = Optional.of(resource).map(res -> res.getChild(Teaser.NN_ACTIONS)).map(actions -> actions.getChildren().iterator().next()).orElse(null);
        if (StringUtils.isNotEmpty(linkURL)) {
            // the inherited resource is the featured image of the linked page
            Optional<Link> link = linkHandler.getLink(resource);
            inheritedResource = link.map(link1 -> (Page) link1.getReference()).map(ComponentUtils::getFeaturedImage).orElse(null);
        } else if (actionsEnabled && firstAction != null) {
            // the inherited resource is the featured image of the first action's page (the resource is assumed to be a teaser)
            inheritedResource = Optional.of(linkHandler.getLink(firstAction, Teaser.PN_ACTION_LINK)).map(link1 -> {
                if (link1.isPresent()) {
                    Page linkedPage = (Page) link1.get().getReference();
                    return Optional.ofNullable(linkedPage).map(ComponentUtils::getFeaturedImage).orElse(null);
                }
                return null;
            }).orElse(null);
        } else {
            // the inherited resource is the featured image of the current page
            inheritedResource = Optional.ofNullable(currentPage).map(page -> {
                Template template = page.getTemplate();
                // make sure the resource is part of the currentPage or of its template
                if (StringUtils.startsWith(resource.getPath(), currentPage.getPath()) || (template != null && StringUtils.startsWith(resource.getPath(), template.getPath()))) {
                    return ComponentUtils.getFeaturedImage(currentPage);
                }
                return null;
            }).orElse(null);
        }
        Map<String, String> overriddenProperties = new HashMap<>();
        Map<String, Resource> overriddenChildren = new HashMap<>();
        String inheritedFileReference = null;
        Resource inheritedFileResource = null;
        String inheritedAlt = null;
        String inheritedAltValueFromDAM = null;
        if (inheritedResource != null) {
            // Define the inherited properties
            ValueMap inheritedProperties = inheritedResource.getValueMap();
            inheritedFileReference = inheritedProperties.get(DownloadResource.PN_REFERENCE, String.class);
            inheritedFileResource = inheritedResource.getChild(DownloadResource.NN_FILE);
            inheritedAlt = inheritedProperties.get(ImageResource.PN_ALT, String.class);
            inheritedAltValueFromDAM = inheritedProperties.get(PN_ALT_VALUE_FROM_DAM, String.class);
        }
        overriddenProperties.put(DownloadResource.PN_REFERENCE, inheritedFileReference);
        overriddenChildren.put(DownloadResource.NN_FILE, inheritedFileResource);
        // don't inherit the image title from the page image
        overriddenProperties.put(PN_TITLE_VALUE_FROM_DAM, "false");
        if (altValueFromPageImage) {
            overriddenProperties.put(ImageResource.PN_ALT, inheritedAlt);
            overriddenProperties.put(PN_ALT_VALUE_FROM_DAM, inheritedAltValueFromDAM);
        } else {
            overriddenProperties.put(PN_ALT_VALUE_FROM_DAM, "false");
        }
        return new CoreResourceWrapper(resource, resource.getResourceType(), null, overriddenProperties, overriddenChildren);
    }
    return resource;
}
Also used : ValueMap(org.apache.sling.api.resource.ValueMap) ModelFactory(org.apache.sling.models.factory.ModelFactory) LinkHandler(com.adobe.cq.wcm.core.components.internal.link.LinkHandler) ResourceResolver(org.apache.sling.api.resource.ResourceResolver) LoggerFactory(org.slf4j.LoggerFactory) AllowedComponentList(com.day.cq.wcm.foundation.AllowedComponentList) HashMap(java.util.HashMap) DownloadResource(com.day.cq.commons.DownloadResource) StringUtils(org.apache.commons.lang3.StringUtils) Page(com.day.cq.wcm.api.Page) SlingHttpServletRequest(org.apache.sling.api.SlingHttpServletRequest) HashSet(java.util.HashSet) JSONException(org.json.JSONException) Style(com.day.cq.wcm.api.designer.Style) JSONObject(org.json.JSONObject) Image(com.adobe.cq.wcm.core.components.models.Image) Map(java.util.Map) Link(com.adobe.cq.wcm.core.components.commons.link.Link) LinkedHashSet(java.util.LinkedHashSet) Logger(org.slf4j.Logger) ImmutableSet(com.google.common.collect.ImmutableSet) Designer(com.day.cq.wcm.api.designer.Designer) Collection(java.util.Collection) Set(java.util.Set) Resource(org.apache.sling.api.resource.Resource) ExperienceFragment(com.adobe.cq.wcm.core.components.models.ExperienceFragment) CoreResourceWrapper(com.adobe.cq.wcm.core.components.internal.resource.CoreResourceWrapper) ComponentUtils(com.adobe.cq.wcm.core.components.util.ComponentUtils) PageManager(com.day.cq.wcm.api.PageManager) Nullable(org.jetbrains.annotations.Nullable) Template(com.day.cq.wcm.api.Template) Optional(java.util.Optional) ImageResource(com.day.cq.commons.ImageResource) NotNull(org.jetbrains.annotations.NotNull) Teaser(com.adobe.cq.wcm.core.components.models.Teaser) Collections(java.util.Collections) HashMap(java.util.HashMap) ValueMap(org.apache.sling.api.resource.ValueMap) DownloadResource(com.day.cq.commons.DownloadResource) Resource(org.apache.sling.api.resource.Resource) ImageResource(com.day.cq.commons.ImageResource) CoreResourceWrapper(com.adobe.cq.wcm.core.components.internal.resource.CoreResourceWrapper) Page(com.day.cq.wcm.api.Page) Template(com.day.cq.wcm.api.Template) ComponentUtils(com.adobe.cq.wcm.core.components.util.ComponentUtils) Link(com.adobe.cq.wcm.core.components.commons.link.Link)

Example 10 with Style

use of com.day.cq.wcm.api.designer.Style in project aem-core-wcm-components by Adobe-Marketing-Cloud.

the class DownloadImplTest method testDownloadWithHiddenTitleLink.

@Test
void testDownloadWithHiddenTitleLink() {
    Resource mockResource = mock(Resource.class);
    MockValueMap mockValueMap = new MockValueMap(mockResource);
    mockValueMap.put(Download.PN_HIDE_TITLE_LINK, true);
    Style mockStyle = new MockStyle(mockResource, mockValueMap);
    Download download = getDownloadUnderTest(DOWNLOAD_1, mockStyle);
    assertTrue(download.hideTitleLink(), "Expected title link to be hidden");
}
Also used : MockValueMap(org.apache.sling.testing.resourceresolver.MockValueMap) Resource(org.apache.sling.api.resource.Resource) MockStyle(com.adobe.cq.wcm.core.components.testing.MockStyle) MockStyle(com.adobe.cq.wcm.core.components.testing.MockStyle) Style(com.day.cq.wcm.api.designer.Style) Download(com.adobe.cq.wcm.core.components.models.Download) Test(org.junit.jupiter.api.Test)

Aggregations

Style (com.day.cq.wcm.api.designer.Style)15 Resource (org.apache.sling.api.resource.Resource)10 Test (org.junit.jupiter.api.Test)8 Designer (com.day.cq.wcm.api.designer.Designer)4 ResourceResolver (org.apache.sling.api.resource.ResourceResolver)4 ValueMap (org.apache.sling.api.resource.ValueMap)4 MockStyle (com.adobe.cq.wcm.core.components.context.MockStyle)3 Embed (com.adobe.cq.wcm.core.components.models.Embed)3 Page (com.day.cq.wcm.api.Page)3 Template (com.day.cq.wcm.api.Template)3 ContentPolicy (com.day.cq.wcm.api.policies.ContentPolicy)3 LinkHandler (com.adobe.cq.wcm.core.components.internal.link.LinkHandler)2 CoreResourceWrapper (com.adobe.cq.wcm.core.components.internal.resource.CoreResourceWrapper)2 Download (com.adobe.cq.wcm.core.components.models.Download)2 Image (com.adobe.cq.wcm.core.components.models.Image)2 MockStyle (com.adobe.cq.wcm.core.components.testing.MockStyle)2 DownloadResource (com.day.cq.commons.DownloadResource)2 ImageResource (com.day.cq.commons.ImageResource)2 PageManager (com.day.cq.wcm.api.PageManager)2 ContentPolicyMapping (com.day.cq.wcm.api.policies.ContentPolicyMapping)2