Search in sources :

Example 1 with FragmentTemplate

use of com.adobe.cq.dam.cfm.FragmentTemplate 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 FragmentTemplate

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

the class ContentFragmentUtils method getType.

/**
 * Returns the type of a {@link ContentFragment content fragment}. The type is a string that uniquely identifies the
 * model or template of the content fragment (CF) (e.g. <code>my-project/models/my-model</code> for a structured CF
 * or <code>/content/dam/my-cf/jcr:content/model</code> for a text-only CF).
 *
 * @param contentFragment the content fragment
 * @return the type of the content fragment
 */
public static String getType(ContentFragment contentFragment) {
    String type = "";
    if (contentFragment == null) {
        return type;
    }
    Resource fragmentResource = contentFragment.adaptTo(Resource.class);
    FragmentTemplate fragmentTemplate = contentFragment.getTemplate();
    if (fragmentTemplate == null) {
        return type;
    }
    Resource templateResource = fragmentTemplate.adaptTo(Resource.class);
    if (fragmentResource == null || templateResource == null) {
        LOG.warn("Unable to return type: fragment or template resource is null");
        type = contentFragment.getName();
    } else {
        // use the parent if the template resource is the jcr:content child
        Resource parent = templateResource.getParent();
        if (JCR_CONTENT.equals(templateResource.getName()) && parent != null) {
            templateResource = parent;
        }
        // get data node to check if this is a text-only or structured content fragment
        Resource data = fragmentResource.getChild(JCR_CONTENT + "/data");
        if (data == null || data.getValueMap().get("cq:model") == null) {
            // this is a text-only content fragment, for which we use the model path as the type
            type = templateResource.getPath();
        } else {
            // this is a structured content fragment, assemble type string (e.g. "my-project/models/my-model" or
            // "my-project/nested/models/my-model")
            StringBuilder prefix = new StringBuilder();
            String[] segments = Text.explode(templateResource.getPath(), '/', false);
            // get the configuration names (e.g. for "my-project/" or "my-project/nested/")
            for (int i = 1; i < segments.length - 5; i++) {
                prefix.append(segments[i]);
                prefix.append("/");
            }
            type = prefix.toString() + "models/" + templateResource.getName();
        }
    }
    return type;
}
Also used : FragmentTemplate(com.adobe.cq.dam.cfm.FragmentTemplate) Resource(org.apache.sling.api.resource.Resource)

Example 3 with FragmentTemplate

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

the class ContentFragmentUtilsTest method getTypeWhenTemplateResourceIsNotNullButIsContentNodeFallbackToParent.

@Test
public void getTypeWhenTemplateResourceIsNotNullButIsContentNodeFallbackToParent() {
    // GIVEN
    Resource fragmentResource = Mockito.mock(Resource.class);
    Resource templateResourceParent = Mockito.mock(Resource.class);
    Resource templateResource = Mockito.mock(Resource.class);
    FragmentTemplate fragmentTemplate = Mockito.mock(FragmentTemplate.class);
    ContentFragment contentFragment = Mockito.mock(ContentFragment.class);
    Mockito.when(contentFragment.getTemplate()).thenReturn(fragmentTemplate);
    Mockito.when(contentFragment.adaptTo(Mockito.eq(Resource.class))).thenReturn(fragmentResource);
    Mockito.when(fragmentTemplate.adaptTo(Mockito.eq(Resource.class))).thenReturn(templateResource);
    Mockito.when(templateResource.getName()).thenReturn(JCR_CONTENT);
    Mockito.when(templateResource.getParent()).thenReturn(templateResourceParent);
    Mockito.when(templateResourceParent.getPath()).thenReturn("/foo/bar");
    // WHEN
    String type = ContentFragmentUtils.getType(contentFragment);
    // THEN
    Assertions.assertEquals(type, "/foo/bar");
}
Also used : ContentFragment(com.adobe.cq.dam.cfm.ContentFragment) FragmentTemplate(com.adobe.cq.dam.cfm.FragmentTemplate) Resource(org.apache.sling.api.resource.Resource) Test(org.junit.jupiter.api.Test)

Example 4 with FragmentTemplate

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

the class ContentFragmentUtilsTest method getTypeWhenTemplateResourceIsNotNull.

@Test
public void getTypeWhenTemplateResourceIsNotNull() {
    // GIVEN
    Resource fragmentResource = Mockito.mock(Resource.class);
    Resource templateResource = Mockito.mock(Resource.class);
    FragmentTemplate fragmentTemplate = Mockito.mock(FragmentTemplate.class);
    ContentFragment contentFragment = Mockito.mock(ContentFragment.class);
    Mockito.when(contentFragment.getTemplate()).thenReturn(fragmentTemplate);
    Mockito.when(contentFragment.adaptTo(Mockito.eq(Resource.class))).thenReturn(fragmentResource);
    Mockito.when(fragmentTemplate.adaptTo(Mockito.eq(Resource.class))).thenReturn(templateResource);
    Mockito.when(templateResource.getPath()).thenReturn("/foo/bar/qux");
    // WHEN
    String type = ContentFragmentUtils.getType(contentFragment);
    // THEN
    Assertions.assertEquals(type, "/foo/bar/qux");
}
Also used : ContentFragment(com.adobe.cq.dam.cfm.ContentFragment) FragmentTemplate(com.adobe.cq.dam.cfm.FragmentTemplate) Resource(org.apache.sling.api.resource.Resource) Test(org.junit.jupiter.api.Test)

Example 5 with FragmentTemplate

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

the class ContentFragmentUtilsTest method getTypeOfStructuredContentFragment.

@Test
public void getTypeOfStructuredContentFragment() {
    // GIVEN
    Resource fragmentResource = Mockito.mock(Resource.class);
    Resource fragmentDataResource = Mockito.mock(Resource.class);
    Resource templateResource = Mockito.mock(Resource.class);
    FragmentTemplate fragmentTemplate = Mockito.mock(FragmentTemplate.class);
    ContentFragment contentFragment = Mockito.mock(ContentFragment.class);
    ValueMap valueMap = new MockValueMap(fragmentDataResource);
    valueMap.put("cq:model", "foo.bar.QuxModel");
    Mockito.when(contentFragment.getTemplate()).thenReturn(fragmentTemplate);
    Mockito.when(contentFragment.adaptTo(Mockito.eq(Resource.class))).thenReturn(fragmentResource);
    Mockito.when(fragmentResource.getChild(Mockito.eq(JCR_CONTENT + "/data"))).thenReturn(fragmentDataResource);
    Mockito.when(fragmentDataResource.getValueMap()).thenReturn(valueMap);
    Mockito.when(fragmentTemplate.adaptTo(Mockito.eq(Resource.class))).thenReturn(templateResource);
    Mockito.when(templateResource.getPath()).thenReturn("/foo/bar/qux/quux/corge/grault/garply");
    Mockito.when(templateResource.getName()).thenReturn("waldo");
    // WHEN
    String type = ContentFragmentUtils.getType(contentFragment);
    // THEN
    Assertions.assertEquals(type, "bar/models/waldo");
}
Also used : ContentFragment(com.adobe.cq.dam.cfm.ContentFragment) MockValueMap(org.apache.sling.testing.resourceresolver.MockValueMap) FragmentTemplate(com.adobe.cq.dam.cfm.FragmentTemplate) ValueMap(org.apache.sling.api.resource.ValueMap) MockValueMap(org.apache.sling.testing.resourceresolver.MockValueMap) Resource(org.apache.sling.api.resource.Resource) Test(org.junit.jupiter.api.Test)

Aggregations

FragmentTemplate (com.adobe.cq.dam.cfm.FragmentTemplate)6 ContentFragment (com.adobe.cq.dam.cfm.ContentFragment)5 Resource (org.apache.sling.api.resource.Resource)5 Test (org.junit.jupiter.api.Test)4 ValueMap (org.apache.sling.api.resource.ValueMap)2 ContentElement (com.adobe.cq.dam.cfm.ContentElement)1 ContentVariation (com.adobe.cq.dam.cfm.ContentVariation)1 VariationDef (com.adobe.cq.dam.cfm.VariationDef)1 LinkedList (java.util.LinkedList)1 Nullable (javax.annotation.Nullable)1 MockValueMap (org.apache.sling.testing.resourceresolver.MockValueMap)1