ContentFragmentUtils { public static String getEditorJSON(final ContentFragment contentFragment, final String variationName, final String[] elementNames) { JsonObjectBuilder jsonObjectBuilder = Json.createObjectBuilder(); jsonObjectBuilder.add("title", contentFragment.getTitle()); Resource contentFragmentResource = contentFragment.adaptTo(Resource.class); if (contentFragmentResource != null) { jsonObjectBuilder.add("path", contentFragmentResource.getPath()); } if (variationName != null) { jsonObjectBuilder.add("variation", variationName); } if (elementNames != null) { JsonArrayBuilder arrayBuilder = Json.createArrayBuilder(); for (String elementName : elementNames) { arrayBuilder.add(elementName); } jsonObjectBuilder.add("elements", arrayBuilder); } Iterator<Resource> associatedContentIterator = contentFragment.getAssociatedContent(); if (associatedContentIterator.hasNext()) { JsonArrayBuilder associatedContentArray = Json.createArrayBuilder(); while (associatedContentIterator.hasNext()) { Resource resource = associatedContentIterator.next(); ValueMap valueMap = resource.adaptTo(ValueMap.class); JsonObjectBuilder contentObject = Json.createObjectBuilder(); if (valueMap != null && valueMap.containsKey(JCR_TITLE)) { contentObject.add("title", valueMap.get(JCR_TITLE, String.class)); } contentObject.add("path", resource.getPath()); associatedContentArray.add(contentObject); } jsonObjectBuilder.add("associatedContent", associatedContentArray); } return jsonObjectBuilder.build().toString(); } 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 getEditorJsonOutputOfContentFragment() { 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); String json = ContentFragmentUtils.getEditorJSON(contentFragment, "slave", new String[]{"foo", "bar"}); JsonReader expected = Json.createReader(this.getClass().getResourceAsStream("expectedJson.json")); JsonReader actual = Json.createReader(new StringReader(json)); assertEquals(expected.read(), actual.read()); }