ContentFragmentUtils { public static Iterator<ContentElement> filterElements(final ContentFragment contentFragment, final String[] elementNames) { if (ArrayUtils.isNotEmpty(elementNames)) { List<ContentElement> elements = new LinkedList<>(); for (String name : elementNames) { if (!contentFragment.hasElement(name)) { LOG.warn("Skipping non-existing element '{}'", name); continue; } elements.add(contentFragment.getElement(name)); } return elements.iterator(); } return contentFragment.getElements(); } 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 filterEmptyElementNamesReturnsOriginalList() { ContentFragment contentFragment = Mockito.mock(ContentFragment.class); Iterator<ContentElement> contentElementIterator = Mockito.mock(Iterator.class); Mockito.when(contentFragment.getElements()).thenReturn(contentElementIterator); Iterator<ContentElement> elementIterator = ContentFragmentUtils.filterElements(contentFragment, null); Assertions.assertEquals(elementIterator, contentElementIterator); }
@Test public void filterElementNamesReturnsAppropriateElementsOnly() { 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); Iterator<ContentElement> elementIterator = ContentFragmentUtils.filterElements(contentFragment, new String[]{"foo", "bar", "qux"}); MatcherAssert.assertThat(() -> elementIterator, IsIterableContainingInOrder.contains(foo, qux)); }