Search in sources :

Example 1 with ContentElement

use of com.adobe.cq.dam.cfm.ContentElement in project aem-core-wcm-components by Adobe-Marketing-Cloud.

the class ContentFragmentMockAdapter method apply.

@Nullable
@Override
public ContentFragment apply(@Nullable Resource resource) {
    // check if the resource is valid and an asset
    if (resource == null || !resource.isResourceType(NT_DAM_ASSET)) {
        return null;
    }
    // check if the resource is a content fragment
    Resource content = resource.getChild(JCR_CONTENT);
    ValueMap contentProperties = content.getValueMap();
    if (!contentProperties.get(PN_CONTENT_FRAGMENT, Boolean.FALSE)) {
        return null;
    }
    // check if the content fragment is text-only or structured
    Resource data = resource.getChild(PATH_DATA);
    boolean isStructured = data != null;
    /* get content fragment properties, model and elements */
    String title = contentProperties.get(JCR_TITLE, String.class);
    String description = contentProperties.get(JCR_DESCRIPTION, String.class);
    String cfName = resource.getName();
    Resource model;
    Resource modelAdaptee;
    List<ContentElement> elements = new LinkedList<>();
    if (isStructured) {
        // get the model (referenced in the property)
        model = resource.getResourceResolver().getResource(data.getValueMap().get(PN_MODEL, String.class));
        // for the 'adaptTo' mock below we use the jcr:content child to mimick the real behavior
        modelAdaptee = model.getChild(JCR_CONTENT);
        // create an element mock for each property on the master node
        Resource master = resource.getChild(PATH_MASTER);
        for (String name : master.getValueMap().keySet()) {
            // skip the primary type and content type properties
            if (JcrConstants.JCR_PRIMARYTYPE.equals(name) || name.endsWith("@ContentType")) {
                continue;
            }
            elements.add(getMockElement(resource, name, model));
        }
    } else {
        // get the model (stored in the fragment itself)
        model = resource.getChild(PATH_MODEL);
        modelAdaptee = model;
        // add the "main" element to the list
        elements.add(getMockElement(resource, null, null));
        // create an element mock for each subasset
        Resource subassets = resource.getChild("subassets");
        if (subassets != null) {
            for (Resource subasset : subassets.getChildren()) {
                elements.add(getMockElement(resource, subasset.getName(), null));
            }
        }
    }
    /* create mock objects */
    ContentFragment fragment = mock(ContentFragment.class, withSettings().lenient());
    when(fragment.getTitle()).thenReturn(title);
    when(fragment.getDescription()).thenReturn(description);
    when(fragment.getName()).thenReturn(cfName);
    when(fragment.adaptTo(Resource.class)).thenReturn(resource);
    when(fragment.getElement(isNull())).thenAnswer(invocation -> {
        String name = invocation.getArgument(0);
        return getMockElement(resource, name, isStructured ? model : null);
    });
    when(fragment.getElement(any(String.class))).thenAnswer(invocation -> {
        String name = invocation.getArgument(0);
        return getMockElement(resource, name, isStructured ? model : null);
    });
    when(fragment.hasElement(any(String.class))).thenAnswer(invocation -> {
        String name = invocation.getArgument(0);
        return fragment.getElement(name) != null;
    });
    when(fragment.getElements()).thenReturn(elements.iterator());
    List<VariationDef> variations = new LinkedList<>();
    ContentElement main = fragment.getElement(null);
    Iterator<ContentVariation> iterator = main.getVariations();
    while (iterator.hasNext()) {
        ContentVariation variation = iterator.next();
        variations.add(new VariationDef() {

            @Override
            public String getName() {
                return variation.getName();
            }

            @Override
            public String getTitle() {
                return variation.getTitle();
            }

            @Override
            public String getDescription() {
                return variation.getDescription();
            }
        });
    }
    when(fragment.listAllVariations()).thenReturn(variations.iterator());
    FragmentTemplate template = mock(FragmentTemplate.class, withSettings().lenient());
    when(template.adaptTo(Resource.class)).thenReturn(modelAdaptee);
    when(fragment.getTemplate()).thenReturn(template);
    Iterator<Resource> associatedContent = getAssociatedContent(resource);
    when(fragment.getAssociatedContent()).thenReturn(associatedContent);
    return fragment;
}
Also used : ContentFragment(com.adobe.cq.dam.cfm.ContentFragment) ValueMap(org.apache.sling.api.resource.ValueMap) Resource(org.apache.sling.api.resource.Resource) LinkedList(java.util.LinkedList) VariationDef(com.adobe.cq.dam.cfm.VariationDef) ContentElement(com.adobe.cq.dam.cfm.ContentElement) ContentVariation(com.adobe.cq.dam.cfm.ContentVariation) FragmentTemplate(com.adobe.cq.dam.cfm.FragmentTemplate) Nullable(javax.annotation.Nullable)

Example 2 with ContentElement

use of com.adobe.cq.dam.cfm.ContentElement in project aem-core-wcm-components by Adobe-Marketing-Cloud.

the class ContentFragmentMockAdapter method getMockElement.

/**
 * Creates a mock of a content element for a text-only (if {@code model} is {@code null}) or structured
 * (if {@code model} is not {@code null}) content fragment.
 */
private ContentElement getMockElement(Resource resource, String name, Resource model) {
    // get the respective element
    MockElement element;
    if (model == null) {
        element = getTextOnlyElement(resource, name);
    } else {
        element = getStructuredElement(resource, model, name);
    }
    if (element == null) {
        return null;
    }
    /* create mock objects */
    // mock data type
    DataType dataType = mock(DataType.class, withSettings().lenient());
    when(dataType.isMultiValue()).thenReturn(element.isMultiValued);
    when(dataType.getTypeString()).thenReturn(element.typeString);
    // mock fragment data
    FragmentData data = mock(FragmentData.class, withSettings().lenient());
    when(data.getValue()).thenReturn(element.isMultiValued ? element.values : element.values[0]);
    when(data.getValue(String.class)).thenReturn(element.values[0]);
    when(data.getValue(String[].class)).thenReturn(element.values);
    when(data.getContentType()).thenReturn(element.contentType);
    when(data.getDataType()).thenReturn(dataType);
    // mock content element
    ContentElement contentElement = mock(ContentElement.class, withSettings().lenient());
    when(contentElement.getName()).thenReturn(element.name);
    when(contentElement.getTitle()).thenReturn(element.title);
    when(contentElement.getContent()).thenReturn(element.values[0]);
    when(contentElement.getContentType()).thenReturn(element.contentType);
    when(contentElement.getValue()).thenReturn(data);
    // mock variations
    Map<String, ContentVariation> variations = new LinkedHashMap<>();
    for (MockVariation variation : element.variations.values()) {
        FragmentData variationData = mock(FragmentData.class, withSettings().lenient());
        when(variationData.getValue()).thenReturn(element.isMultiValued ? variation.values : variation.values[0]);
        when(variationData.getValue(String.class)).thenReturn(variation.values[0]);
        when(variationData.getValue(String[].class)).thenReturn(variation.values);
        when(variationData.getContentType()).thenReturn(variation.contentType);
        when(variationData.getDataType()).thenReturn(dataType);
        ContentVariation contentVariation = mock(ContentVariation.class, withSettings().lenient());
        when(contentVariation.getName()).thenReturn(variation.name);
        when(contentVariation.getTitle()).thenReturn(variation.title);
        when(contentVariation.getContent()).thenReturn(variation.values[0]);
        when(contentVariation.getContentType()).thenReturn(variation.contentType);
        when(contentVariation.getValue()).thenReturn(variationData);
        variations.put(variation.name, contentVariation);
    }
    when(contentElement.getVariations()).thenReturn(variations.values().iterator());
    when(contentElement.getVariation(any(String.class))).thenAnswer(invocation -> {
        String variationName = invocation.getArgument(0);
        return variations.get(variationName);
    });
    return contentElement;
}
Also used : ContentElement(com.adobe.cq.dam.cfm.ContentElement) ContentVariation(com.adobe.cq.dam.cfm.ContentVariation) DataType(com.adobe.cq.dam.cfm.DataType) FragmentData(com.adobe.cq.dam.cfm.FragmentData) LinkedHashMap(java.util.LinkedHashMap)

Example 3 with ContentElement

use of com.adobe.cq.dam.cfm.ContentElement in project aem-core-wcm-components by Adobe-Marketing-Cloud.

the class ContentFragmentUtilsTest method filterEmptyElementNamesReturnsOriginalList.

@Test
public void filterEmptyElementNamesReturnsOriginalList() {
    // GIVEN
    ContentFragment contentFragment = Mockito.mock(ContentFragment.class);
    Iterator<ContentElement> contentElementIterator = Mockito.mock(Iterator.class);
    Mockito.when(contentFragment.getElements()).thenReturn(contentElementIterator);
    // WHEN
    Iterator<ContentElement> elementIterator = ContentFragmentUtils.filterElements(contentFragment, null);
    // THEN
    Assertions.assertEquals(elementIterator, contentElementIterator);
}
Also used : ContentFragment(com.adobe.cq.dam.cfm.ContentFragment) ContentElement(com.adobe.cq.dam.cfm.ContentElement) Test(org.junit.jupiter.api.Test)

Example 4 with ContentElement

use of com.adobe.cq.dam.cfm.ContentElement in project aem-core-wcm-components by Adobe-Marketing-Cloud.

the class ContentFragmentUtilsTest method filterElementNamesReturnsAppropriateElementsOnly.

@Test
public void filterElementNamesReturnsAppropriateElementsOnly() {
    // GIVEN
    ContentElement foo = Mockito.mock(ContentElement.class);
    ContentElement qux = Mockito.mock(ContentElement.class);
    ContentFragment contentFragment = Mockito.mock(ContentFragment.class);
    Mockito.when(contentFragment.hasElement(Mockito.eq("foo"))).thenReturn(true);
    Mockito.when(contentFragment.hasElement(Mockito.eq("bar"))).thenReturn(false);
    Mockito.when(contentFragment.hasElement(Mockito.eq("qux"))).thenReturn(true);
    Mockito.when(contentFragment.getElement(Mockito.eq("foo"))).thenReturn(foo);
    Mockito.when(contentFragment.getElement(Mockito.eq("qux"))).thenReturn(qux);
    // WHEN
    Iterator<ContentElement> elementIterator = ContentFragmentUtils.filterElements(contentFragment, new String[] { "foo", "bar", "qux" });
    // THEN
    MatcherAssert.assertThat(() -> elementIterator, IsIterableContainingInOrder.contains(foo, qux));
}
Also used : ContentFragment(com.adobe.cq.dam.cfm.ContentFragment) ContentElement(com.adobe.cq.dam.cfm.ContentElement) Test(org.junit.jupiter.api.Test)

Example 5 with ContentElement

use of com.adobe.cq.dam.cfm.ContentElement in project aem-core-wcm-components by Adobe-Marketing-Cloud.

the class ElementsDataSourceServlet method getItems.

@NotNull
@Override
protected List<ContentElement> getItems(@NotNull ContentFragment fragment, @NotNull SlingHttpServletRequest request) {
    Config config = getConfig(request);
    ValueMap map = getComponentValueMap(config, request);
    String textOnlyParam = request.getParameter(PARAM_AND_PN_DISPLAY_MODE);
    boolean textOnly = map != null && map.containsKey(PARAM_AND_PN_DISPLAY_MODE) && map.get(PARAM_AND_PN_DISPLAY_MODE, "multi").equals(SINGLE_TEXT);
    if (textOnlyParam != null) {
        textOnly = textOnlyParam.equals(SINGLE_TEXT);
    }
    if (textOnly) {
        Iterator<ContentElement> elementIterator = fragment.getElements();
        List<ContentElement> elementList = new ArrayList<ContentElement>();
        while (elementIterator.hasNext()) {
            ContentElement element = elementIterator.next();
            String contentType = element.getValue().getContentType();
            if (contentType == null) {
                contentType = element.getContentType();
            }
            if (contentType != null && contentType.startsWith("text/") && !element.getValue().getDataType().isMultiValue()) {
                elementList.add(element);
            }
        }
        return elementList;
    }
    return IteratorUtils.toList(fragment.getElements());
}
Also used : ContentElement(com.adobe.cq.dam.cfm.ContentElement) Config(com.adobe.granite.ui.components.Config) ValueMap(org.apache.sling.api.resource.ValueMap) ArrayList(java.util.ArrayList) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

ContentElement (com.adobe.cq.dam.cfm.ContentElement)5 ContentFragment (com.adobe.cq.dam.cfm.ContentFragment)3 ContentVariation (com.adobe.cq.dam.cfm.ContentVariation)2 ValueMap (org.apache.sling.api.resource.ValueMap)2 Test (org.junit.jupiter.api.Test)2 DataType (com.adobe.cq.dam.cfm.DataType)1 FragmentData (com.adobe.cq.dam.cfm.FragmentData)1 FragmentTemplate (com.adobe.cq.dam.cfm.FragmentTemplate)1 VariationDef (com.adobe.cq.dam.cfm.VariationDef)1 Config (com.adobe.granite.ui.components.Config)1 ArrayList (java.util.ArrayList)1 LinkedHashMap (java.util.LinkedHashMap)1 LinkedList (java.util.LinkedList)1 Nullable (javax.annotation.Nullable)1 Resource (org.apache.sling.api.resource.Resource)1 NotNull (org.jetbrains.annotations.NotNull)1