Search in sources :

Example 6 with ContentFragment

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

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

Example 8 with ContentFragment

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

the class ContentFragmentUtilsTest method getEditorJsonOutputOfContentFragment.

@Test
public void getEditorJsonOutputOfContentFragment() {
    // GIVEN
    Resource contentFragmentResource = Mockito.mock(Resource.class);
    ContentFragment contentFragment = Mockito.mock(ContentFragment.class);
    Iterator<Resource> associatedContentResourceIterator = Mockito.mock(Iterator.class);
    Resource firstAndOnlyAssociatedContent = Mockito.mock(Resource.class);
    ValueMap associatedContentValueMap = new MockValueMap(firstAndOnlyAssociatedContent);
    associatedContentValueMap.put(JCR_TITLE, "associatedContentTitle");
    Mockito.when(contentFragment.getTitle()).thenReturn("titleOfTheContentFragment");
    Mockito.when(contentFragment.getAssociatedContent()).thenReturn(associatedContentResourceIterator);
    Mockito.when(contentFragment.adaptTo(Mockito.eq(Resource.class))).thenReturn(contentFragmentResource);
    Mockito.when(contentFragmentResource.getPath()).thenReturn("/path/to/the/content/fragment");
    Mockito.when(associatedContentResourceIterator.hasNext()).thenReturn(true, true, false);
    Mockito.when(associatedContentResourceIterator.next()).thenReturn(firstAndOnlyAssociatedContent);
    Mockito.when(firstAndOnlyAssociatedContent.getPath()).thenReturn("/path/to/the/associated/content");
    Mockito.when(firstAndOnlyAssociatedContent.adaptTo(ValueMap.class)).thenReturn(associatedContentValueMap);
    // WHEN
    String json = ContentFragmentUtils.getEditorJSON(contentFragment, "slave", new String[] { "foo", "bar" });
    // THEN
    JsonReader expected = Json.createReader(this.getClass().getResourceAsStream("expectedJson.json"));
    JsonReader actual = Json.createReader(new StringReader(json));
    assertEquals(expected.read(), actual.read());
}
Also used : ContentFragment(com.adobe.cq.dam.cfm.ContentFragment) MockValueMap(org.apache.sling.testing.resourceresolver.MockValueMap) ValueMap(org.apache.sling.api.resource.ValueMap) MockValueMap(org.apache.sling.testing.resourceresolver.MockValueMap) Resource(org.apache.sling.api.resource.Resource) StringReader(java.io.StringReader) JsonReader(javax.json.JsonReader) Test(org.junit.jupiter.api.Test)

Example 9 with ContentFragment

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

the class ContentFragmentUtilsTest method getTypeWhenResourceIsNull.

@Test
public void getTypeWhenResourceIsNull() {
    // GIVEN
    FragmentTemplate fragmentTemplate = Mockito.mock(FragmentTemplate.class);
    ContentFragment contentFragment = Mockito.mock(ContentFragment.class);
    Mockito.when(contentFragment.getTemplate()).thenReturn(fragmentTemplate);
    Mockito.when(contentFragment.getName()).thenReturn("foobar");
    // WHEN
    String type = ContentFragmentUtils.getType(contentFragment);
    // THEN
    Assertions.assertEquals(type, "foobar");
}
Also used : ContentFragment(com.adobe.cq.dam.cfm.ContentFragment) FragmentTemplate(com.adobe.cq.dam.cfm.FragmentTemplate) Test(org.junit.jupiter.api.Test)

Aggregations

ContentFragment (com.adobe.cq.dam.cfm.ContentFragment)9 Test (org.junit.jupiter.api.Test)7 Resource (org.apache.sling.api.resource.Resource)6 FragmentTemplate (com.adobe.cq.dam.cfm.FragmentTemplate)5 ContentElement (com.adobe.cq.dam.cfm.ContentElement)3 ValueMap (org.apache.sling.api.resource.ValueMap)3 LinkedList (java.util.LinkedList)2 MockValueMap (org.apache.sling.testing.resourceresolver.MockValueMap)2 ContentVariation (com.adobe.cq.dam.cfm.ContentVariation)1 VariationDef (com.adobe.cq.dam.cfm.VariationDef)1 DataSource (com.adobe.granite.ui.components.ds.DataSource)1 EmptyDataSource (com.adobe.granite.ui.components.ds.EmptyDataSource)1 SimpleDataSource (com.adobe.granite.ui.components.ds.SimpleDataSource)1 StringReader (java.io.StringReader)1 Nullable (javax.annotation.Nullable)1 JsonReader (javax.json.JsonReader)1 ResourceResolver (org.apache.sling.api.resource.ResourceResolver)1