CassandraConnectionFilter implements EntityFilter<CassandraConnection> { @Override public List<CassandraConnection> filter(String folderPath, FilterConfiguration filterConfiguration, Bundle bundle, Bundle filteredBundle) { List<CassandraConnection> cassandraConnections = DependencyUtils.filterDependencies(CassandraConnection.class, bundle, filteredBundle, e -> filterConfiguration.getRequiredEntityNames(ENTITY_NAME).contains(e.getName())); DependencyUtils.validateEntitiesInList(cassandraConnections, filterConfiguration.getRequiredEntityNames(ENTITY_NAME), "Cassandra Connection(s)"); return cassandraConnections; }  @Override String getFilterableEntityName(); @Override @NotNull Collection<Class<? extends EntityFilter>> getDependencyEntityFilters(); @Override List<CassandraConnection> filter(String folderPath, FilterConfiguration filterConfiguration, Bundle bundle, Bundle filteredBundle);  }
@Test void filterNoEntities() { CassandraConnectionFilter filter = new CassandraConnectionFilter(); Bundle filteredBundle = new Bundle(); Bundle bundle = FilterTestUtils.getBundle(); bundle.setDependencyMap(Collections.emptyMap()); List<CassandraConnection> filteredEntities = filter.filter("/my/folder/path", new FilterConfiguration(), bundle, filteredBundle); assertEquals(0, filteredEntities.size()); }
@Test void filter() { CassandraConnectionFilter filter = new CassandraConnectionFilter(); Bundle filteredBundle = new Bundle(); final Policy policy = TestUtils.createPolicy("my-policy", "1", "", "", null, ""); filteredBundle.getPolicies().put(policy.getId(), policy); Bundle bundle = FilterTestUtils.getBundle(); bundle.setDependencyMap( ImmutableMap.of( new Dependency("1", Policy.class, "my-policy", EntityTypes.POLICY_TYPE), Arrays.asList(new Dependency("2", CassandraConnection.class, "cassandra2", EntityTypes.CASSANDRA_CONNECTION_TYPE), new Dependency("3", CassandraConnection.class, "cassandra3", EntityTypes.CASSANDRA_CONNECTION_TYPE)), new Dependency("2", Policy.class, "policy2", EntityTypes.POLICY_TYPE), Collections.singletonList(new Dependency("4", CassandraConnection.class, "cassandra4", EntityTypes.CASSANDRA_CONNECTION_TYPE)))); bundle.addEntity(TestUtils.createCassandraConnection("cassandra1", "1")); bundle.addEntity(TestUtils.createCassandraConnection("cassandra2", "2")); bundle.addEntity(TestUtils.createCassandraConnection("cassandra3", "3")); bundle.addEntity(TestUtils.createCassandraConnection("cassandra4", "4")); FilterConfiguration filterConfiguration = new FilterConfiguration(); List<CassandraConnection> filteredEntities = filter.filter("/my/folder/path", filterConfiguration, bundle, filteredBundle); assertEquals(2, filteredEntities.size()); assertTrue(filteredEntities.stream().anyMatch(c -> "cassandra2".equals(c.getName()))); assertTrue(filteredEntities.stream().anyMatch(c -> "cassandra3".equals(c.getName()))); filterConfiguration.getEntityFilters().put(filter.getFilterableEntityName(), new HashSet<>()); filterConfiguration.getEntityFilters().get(filter.getFilterableEntityName()).add("cassandra1"); filteredEntities = filter.filter("/my/folder/path", filterConfiguration, bundle, filteredBundle); assertEquals(3, filteredEntities.size()); assertTrue(filteredEntities.stream().anyMatch(c -> "cassandra2".equals(c.getName()))); assertTrue(filteredEntities.stream().anyMatch(c -> "cassandra3".equals(c.getName()))); assertTrue(filteredEntities.stream().anyMatch(c -> "cassandra1".equals(c.getName()))); filterConfiguration.getEntityFilters().get(filter.getFilterableEntityName()).add("non-existing-entity"); EntityFilterException entityFilterException = assertThrows(EntityFilterException.class, () -> filter.filter("/my/folder/path", filterConfiguration, bundle, filteredBundle)); assertTrue(entityFilterException.getMessage().contains("non-existing-entity")); }