PrivateKeyFilter implements EntityFilter<PrivateKey> { @Override @SuppressWarnings("squid:S1157") public List<PrivateKey> filter(String folderPath, FilterConfiguration filterConfiguration, Bundle bundle, Bundle filteredBundle) { Stream<PrivateKey> stream = DependencyUtils.filterDependencies(PrivateKey.class, bundle, filteredBundle, e -> filterConfiguration.getRequiredEntityNames(ENTITY_NAME).contains(e.getName())).stream(); if (!filterConfiguration.getRequiredEntityNames(ENTITY_NAME).contains(SSL_DEFAULT_PRIVATE_KEY.toUpperCase())) { stream = stream .filter(p -> !p.getName().equals(SSL_DEFAULT_PRIVATE_KEY.toUpperCase())); } if (!filterConfiguration.getRequiredEntityNames(ENTITY_NAME).contains(SSL_DEFAULT_PRIVATE_KEY.toLowerCase())) { stream = stream .filter(p -> !p.getName().equals(SSL_DEFAULT_PRIVATE_KEY.toLowerCase())); } List<PrivateKey> privateKeys = stream.collect(Collectors.toList()); DependencyUtils.validateEntitiesInList(privateKeys, filterConfiguration.getRequiredEntityNames(ENTITY_NAME), "Private Key(s)"); return privateKeys; }  @Override @NotNull Collection<Class<? extends EntityFilter>> getDependencyEntityFilters(); @Override @SuppressWarnings("squid:S1157") List<PrivateKey> filter(String folderPath, FilterConfiguration filterConfiguration, Bundle bundle, Bundle filteredBundle); @Override String getFilterableEntityName();  }
@Test void filterNoEntities() { PrivateKeyFilter filter = new PrivateKeyFilter(); Bundle filteredBundle = new Bundle(); Bundle bundle = FilterTestUtils.getBundle(); bundle.setDependencyMap(Collections.emptyMap()); List<PrivateKey> filteredEntities = filter.filter("/my/folder/path", new FilterConfiguration(), bundle, filteredBundle); assertEquals(0, filteredEntities.size()); }
@Test void filter() { PrivateKeyFilter filter = new PrivateKeyFilter(); Bundle filteredBundle = new Bundle(); filteredBundle.addEntity(createPolicy("my-policy", "1", "", "", null, "")); Bundle bundle = FilterTestUtils.getBundle(); bundle.setDependencyMap( ImmutableMap.of( new Dependency("1", Policy.class, "my-policy", EntityTypes.POLICY_TYPE), Arrays.asList(new Dependency("2", PrivateKey.class, "SSL", EntityTypes.PRIVATE_KEY_TYPE), new Dependency("3", PrivateKey.class, "pk3", EntityTypes.PRIVATE_KEY_TYPE)), new Dependency("2", Policy.class, "my-policy2", EntityTypes.POLICY_TYPE), Collections.singletonList(new Dependency("4", PrivateKey.class, "pk4", EntityTypes.PRIVATE_KEY_TYPE)))); bundle.addEntity(new PrivateKey.Builder().setAlias("pk1").setId("1").build()); bundle.addEntity(new PrivateKey.Builder().setAlias("SSL").setId("2").build()); bundle.addEntity(new PrivateKey.Builder().setAlias("pk3").setId("3").build()); bundle.addEntity(new PrivateKey.Builder().setAlias("pk4").setId("4").build()); bundle.addEntity(new PrivateKey.Builder().setAlias("ssl").setId("5").build()); FilterConfiguration filterConfiguration = new FilterConfiguration(); List<PrivateKey> filteredEntities = filter.filter("/my/folder/path", filterConfiguration, bundle, filteredBundle); assertEquals(1, filteredEntities.size()); assertTrue(filteredEntities.stream().anyMatch(c -> "pk3".equals(c.getName()))); filterConfiguration.getEntityFilters().put(filter.getFilterableEntityName(), new HashSet<>()); filterConfiguration.getEntityFilters().get(filter.getFilterableEntityName()).add("ssl"); filterConfiguration.getEntityFilters().get(filter.getFilterableEntityName()).add("pk1"); filteredEntities = filter.filter("/my/folder/path", filterConfiguration, bundle, filteredBundle); assertEquals(3, filteredEntities.size()); assertTrue(filteredEntities.stream().anyMatch(c -> "pk3".equals(c.getName()))); assertTrue(filteredEntities.stream().anyMatch(c -> "pk1".equals(c.getName()))); assertTrue(filteredEntities.stream().anyMatch(c -> "ssl".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")); }