ContentFragmentUtils { 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 { Resource parent = templateResource.getParent(); if (JCR_CONTENT.equals(templateResource.getName()) && parent != null) { templateResource = parent; } Resource data = fragmentResource.getChild(JCR_CONTENT + "/data"); if (data == null || data.getValueMap().get("cq:model") == null) { type = templateResource.getPath(); } else { StringBuilder prefix = new StringBuilder(); String[] segments = Text.explode(templateResource.getPath(), '/', false); for (int i = 1; i < segments.length - 5; i++) { prefix.append(segments[i]); prefix.append("/"); } type = prefix.toString() + "models/" + templateResource.getName(); } } return type; } private  ContentFragmentUtils(); static String getType(ContentFragment contentFragment); static Iterator<ContentElement> filterElements(final ContentFragment contentFragment,
                                                          final String[] elementNames); static String getEditorJSON(final ContentFragment contentFragment,
                                       final String variationName,
                                       final String[] elementNames); static String getGridResourceType(Resource fragmentResource); static String[] getItemsOrder(Map<String, ?> itemsMap); static Map<String, ComponentExporter> getComponentExporters(Iterator<Resource> resourceIterator,
                                                                       ModelFactory modelFactory,
                                                                       SlingHttpServletRequest slingHttpServletRequest);  }
@Test public void getTypeWhenContentFragmentIsNull() { String type = ContentFragmentUtils.getType(null); Assertions.assertEquals(type, ""); }
@Test public void getTypeWhenResourceIsNull() { FragmentTemplate fragmentTemplate = Mockito.mock(FragmentTemplate.class); ContentFragment contentFragment = Mockito.mock(ContentFragment.class); Mockito.when(contentFragment.getTemplate()).thenReturn(fragmentTemplate); Mockito.when(contentFragment.getName()).thenReturn("foobar"); String type = ContentFragmentUtils.getType(contentFragment); Assertions.assertEquals(type, "foobar"); }
@Test public void getTypeWhenTemplateResourceIsNotNull() { 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"); String type = ContentFragmentUtils.getType(contentFragment); Assertions.assertEquals(type, "/foo/bar/qux"); }
@Test public void getTypeWhenTemplateResourceIsNotNullButIsContentNodeFallbackToParent() { 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"); String type = ContentFragmentUtils.getType(contentFragment); Assertions.assertEquals(type, "/foo/bar"); }
@Test public void getTypeOfStructuredContentFragment() { 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"); String type = ContentFragmentUtils.getType(contentFragment); Assertions.assertEquals(type, "bar/models/waldo"); }