Search in sources :

Example 21 with Resource

use of org.apache.sling.api.resource.Resource in project aem-core-wcm-components by Adobe-Marketing-Cloud.

the class ListImpl method populateTagListItems.

private void populateTagListItems() {
    listItems = new ArrayList<>();
    String[] tags = properties.get(PN_TAGS, new String[0]);
    boolean matchAny = properties.get(PN_TAGS_MATCH, TAGS_MATCH_ANY_VALUE).equals(TAGS_MATCH_ANY_VALUE);
    if (ArrayUtils.isNotEmpty(tags)) {
        Page rootPage = getRootPage(PN_TAGS_PARENT_PAGE);
        if (rootPage != null) {
            TagManager tagManager = resourceResolver.adaptTo(TagManager.class);
            if (tagManager != null) {
                RangeIterator<Resource> resourceRangeIterator = tagManager.find(rootPage.getPath(), tags, matchAny);
                if (resourceRangeIterator != null) {
                    while (resourceRangeIterator.hasNext()) {
                        Page containingPage = pageManager.getContainingPage(resourceRangeIterator.next());
                        if (containingPage != null) {
                            listItems.add(containingPage);
                        }
                    }
                }
            }
        }
    }
}
Also used : TagManager(com.day.cq.tagging.TagManager) Resource(org.apache.sling.api.resource.Resource) Page(com.day.cq.wcm.api.Page)

Example 22 with Resource

use of org.apache.sling.api.resource.Resource in project aem-core-wcm-components by Adobe-Marketing-Cloud.

the class AdaptiveImageServletTest method prepareRequestResponsePair.

private Pair<MockSlingHttpServletRequest, MockSlingHttpServletResponse> prepareRequestResponsePair(String resourcePath, String selectorString, String extension) {
    final MockSlingHttpServletRequest request = new MockSlingHttpServletRequest(aemContext.resourceResolver(), aemContext.bundleContext());
    final MockSlingHttpServletResponse response = new MockSlingHttpServletResponse();
    Resource resource = resourceResolver.getResource(resourcePath);
    request.setResource(resource);
    MockRequestPathInfo requestPathInfo = (MockRequestPathInfo) request.getRequestPathInfo();
    requestPathInfo.setSelectorString(selectorString);
    requestPathInfo.setExtension(extension);
    SlingBindings bindings = new SlingBindings();
    bindings.put(SlingBindings.REQUEST, request);
    bindings.put(SlingBindings.RESPONSE, response);
    bindings.put(SlingBindings.SLING, aemContext.slingScriptHelper());
    bindings.put(SlingBindings.RESOLVER, resourceResolver);
    request.setAttribute(SlingBindings.class.getName(), bindings);
    return new Pair<MockSlingHttpServletRequest, MockSlingHttpServletResponse>() {

        @Override
        public MockSlingHttpServletRequest getLeft() {
            return request;
        }

        @Override
        public MockSlingHttpServletResponse getRight() {
            return response;
        }

        @Override
        public MockSlingHttpServletResponse setValue(MockSlingHttpServletResponse value) {
            throw new UnsupportedOperationException();
        }
    };
}
Also used : MockRequestPathInfo(org.apache.sling.testing.mock.sling.servlet.MockRequestPathInfo) SlingBindings(org.apache.sling.api.scripting.SlingBindings) MockSlingHttpServletRequest(org.apache.sling.testing.mock.sling.servlet.MockSlingHttpServletRequest) Resource(org.apache.sling.api.resource.Resource) MockSlingHttpServletResponse(org.apache.sling.testing.mock.sling.servlet.MockSlingHttpServletResponse) Pair(org.apache.commons.lang3.tuple.Pair)

Example 23 with Resource

use of org.apache.sling.api.resource.Resource in project aem-core-wcm-components by Adobe-Marketing-Cloud.

the class MockCommerceFactory method getProduct.

public static Product getProduct(Resource resource) {
    Product product = mock(Product.class);
    when(product.getPath()).thenReturn(resource.getPath());
    Resource image = resource.getChild("image");
    if (image != null) {
        ImageResource imageResource = new ImageResource(image);
        when(product.getImage()).thenReturn(imageResource);
    }
    return product;
}
Also used : ImageResource(com.day.cq.commons.ImageResource) Resource(org.apache.sling.api.resource.Resource) ImageResource(com.day.cq.commons.ImageResource) Product(com.adobe.cq.commerce.api.Product)

Example 24 with Resource

use of org.apache.sling.api.resource.Resource in project aem-core-wcm-components by Adobe-Marketing-Cloud.

the class MockXFFactory method getExperienceFragmentSocialVariation.

public static ExperienceFragmentSocialVariation getExperienceFragmentSocialVariation(Page page) {
    ExperienceFragmentSocialVariation socialVariation = mock(ExperienceFragmentSocialVariation.class);
    StringBuilder stringBuilder = new StringBuilder();
    String image = null;
    for (Resource resource : page.getContentResource().getChild("root").getChildren()) {
        if (resource.isResourceType(IMAGE_RT) && StringUtils.isEmpty(image)) {
            image = resource.getValueMap().get("fileReference", String.class);
        }
        String text = resource.getValueMap().get("text", String.class);
        if (StringUtils.isNotEmpty(text)) {
            stringBuilder.append(text);
        }
    }
    when(socialVariation.getText()).thenReturn(stringBuilder.toString());
    when(socialVariation.getImagePath()).thenReturn(image);
    return socialVariation;
}
Also used : ExperienceFragmentSocialVariation(com.adobe.cq.xf.social.ExperienceFragmentSocialVariation) Resource(org.apache.sling.api.resource.Resource)

Example 25 with Resource

use of org.apache.sling.api.resource.Resource in project aem-core-wcm-components by Adobe-Marketing-Cloud.

the class FormStructureHelperImplTest method testGetFormElements.

@Test
public void testGetFormElements() {
    Resource resource = resourceResolver.getResource(CONTAINING_PAGE + "/jcr:content/root/responsivegrid");
    Iterator<Resource> formFields = formStructureHelper.getFormElements(resource).iterator();
    assertFalse(formFields.hasNext());
    Set<String> allowedFields = new HashSet<>(4);
    allowedFields.add("text");
    allowedFields.add("hidden");
    allowedFields.add("button_button");
    allowedFields.add("text_inside_non_form_node");
    allowedFields.add("button_button_inherited");
    Set<String> returnedFormFields = new HashSet<>();
    resource = resourceResolver.getResource(CONTAINING_PAGE + "/jcr:content/root/responsivegrid/container");
    formFields = formStructureHelper.getFormElements(resource).iterator();
    //test only the allowed fields should be returned
    while (formFields.hasNext()) {
        Resource field = formFields.next();
        assertTrue(allowedFields.contains(field.getName()));
        returnedFormFields.add(field.getName());
    }
    //test all the fields preset in allowedFields should be returned
    for (String field : allowedFields) {
        assertTrue(returnedFormFields.contains(field));
    }
}
Also used : Resource(org.apache.sling.api.resource.Resource) HashSet(java.util.HashSet) Test(org.junit.Test)

Aggregations

Resource (org.apache.sling.api.resource.Resource)1569 Test (org.junit.Test)695 ValueMap (org.apache.sling.api.resource.ValueMap)338 ResourceResolver (org.apache.sling.api.resource.ResourceResolver)260 NonExistingResource (org.apache.sling.api.resource.NonExistingResource)164 HashMap (java.util.HashMap)160 Node (javax.jcr.Node)151 ArrayList (java.util.ArrayList)137 ModifiableValueMap (org.apache.sling.api.resource.ModifiableValueMap)119 PersistenceException (org.apache.sling.api.resource.PersistenceException)114 Test (org.junit.jupiter.api.Test)72 Map (java.util.Map)70 SlingHttpServletRequest (org.apache.sling.api.SlingHttpServletRequest)70 HttpServletRequest (javax.servlet.http.HttpServletRequest)62 SyntheticResource (org.apache.sling.api.resource.SyntheticResource)60 FakeSlingHttpServletRequest (org.apache.sling.launchpad.testservices.exported.FakeSlingHttpServletRequest)59 LoginException (org.apache.sling.api.resource.LoginException)58 RepositoryException (javax.jcr.RepositoryException)54 Calendar (java.util.Calendar)50 Session (javax.jcr.Session)49