FolderFilter implements EntityFilter<Folder> { @Override public List<Folder> filter(String folderPath, FilterConfiguration filterConfiguration, Bundle bundle, Bundle filteredBundle) { return bundle.getFolderTree().stream() .filter(f -> { String path = bundle.getFolderTree().getFormattedPath(f); return ("/" + path + "/").startsWith(folderPath + "/"); }).collect(Collectors.toList()); }  @Override @NotNull Collection<Class<? extends EntityFilter>> getDependencyEntityFilters(); @Override List<Folder> filter(String folderPath, FilterConfiguration filterConfiguration, Bundle bundle, Bundle filteredBundle); @SuppressWarnings("squid:S1075") static List<Folder> parentFolders(String folderPath, Bundle bundle);  }
@Test void filterEmptyBundle() { FolderFilter folderFilter = new FolderFilter(); Bundle filteredBundle = new Bundle(); List<Folder> childFolders = folderFilter.filter("/my/folder/path", new FilterConfiguration(), FilterTestUtils.getBundle(), filteredBundle); assertTrue(childFolders.isEmpty()); }
@Test void filterBundle() { Bundle bundle = FilterTestUtils.getBundle(); Folder folder1 = createFolder("my", "1", Folder.ROOT_FOLDER); Folder folder2 = createFolder("folder", "2", folder1); Folder folder3 = createFolder("path", "3", createFolder("folder", "2", folder1)); bundle.addEntity(folder1); bundle.addEntity(folder2); bundle.addEntity(createFolder("fold", "5", folder1)); bundle.addEntity(folder3); bundle.addEntity(createFolder("sub-folder", "4", folder3)); bundle.addEntity(createFolder("another-path", "6", folder2)); bundle.setFolderTree(new FolderTree(bundle.getEntities(Folder.class).values())); FolderFilter folderFilter = new FolderFilter(); Bundle filteredBundle = new Bundle(); List<Folder> childFolders = folderFilter.filter("/my/folder", new FilterConfiguration(), bundle, filteredBundle); assertEquals(4, childFolders.size()); assertTrue(childFolders.stream().anyMatch(f -> "folder".equals(f.getName()))); assertTrue(childFolders.stream().anyMatch(f -> "path".equals(f.getName()))); assertTrue(childFolders.stream().anyMatch(f -> "another-path".equals(f.getName()))); assertTrue(childFolders.stream().anyMatch(f -> "sub-folder".equals(f.getName()))); }
@Test void filterBundleWithMultipleFoldersStartingWithTheSameName() { Bundle bundle = FilterTestUtils.getBundle(); Folder folder1 = createFolder("Folder Test", "1", Folder.ROOT_FOLDER); Folder folder2 = createFolder("Folder Test 2", "2", Folder.ROOT_FOLDER); bundle.addEntity(folder1); bundle.addEntity(folder2); bundle.setFolderTree(new FolderTree(bundle.getEntities(Folder.class).values())); FolderFilter folderFilter = new FolderFilter(); Bundle filteredBundle = new Bundle(); List<Folder> childFolders = folderFilter.filter("/Folder Test", new FilterConfiguration(), bundle, filteredBundle); assertEquals(1, childFolders.size()); assertEquals("Folder Test", childFolders.iterator().next().getName()); }