IdentityProviderFilter implements EntityFilter<IdentityProvider> { @Override public List<IdentityProvider> filter(String folderPath, FilterConfiguration filterConfiguration, Bundle bundle, Bundle filteredBundle) { Stream<IdentityProvider> identityProviderEntityStream = DependencyUtils.filterDependencies(IdentityProvider.class, bundle, filteredBundle, e -> filterConfiguration.getRequiredEntityNames(ENTITY_NAME).contains(e.getName())).stream(); if (!filterConfiguration.getRequiredEntityNames(ENTITY_NAME).contains(IdentityProvider.INTERNAL_IDP_NAME)) { identityProviderEntityStream = identityProviderEntityStream .filter(idp -> !IdentityProvider.INTERNAL_IDP_ID.equals(idp.getId())); } List<IdentityProvider> identityProviders = identityProviderEntityStream.collect(Collectors.toList()); DependencyUtils.validateEntitiesInList(identityProviders, filterConfiguration.getRequiredEntityNames(ENTITY_NAME), "Identity Provider(s)"); return identityProviders; }  @Override @NotNull Collection<Class<? extends EntityFilter>> getDependencyEntityFilters(); @Override List<IdentityProvider> filter(String folderPath, FilterConfiguration filterConfiguration, Bundle bundle, Bundle filteredBundle); @Override String getFilterableEntityName();  }
@Test void filterNoEntities() { IdentityProviderFilter filter = new IdentityProviderFilter(); Bundle filteredBundle = new Bundle(); Bundle bundle = FilterTestUtils.getBundle(); bundle.setDependencyMap(Collections.emptyMap()); List<IdentityProvider> filteredEntities = filter.filter("/my/folder/path", new FilterConfiguration(), bundle, filteredBundle); assertEquals(0, filteredEntities.size()); }
@Test void filter() { IdentityProviderFilter filter = new IdentityProviderFilter(); Bundle filteredBundle = new Bundle(); filteredBundle.addEntity(TestUtils.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(IdentityProvider.INTERNAL_IDP_ID, IdentityProvider.class, "idp2", EntityTypes.ID_PROVIDER_CONFIG_TYPE), new Dependency("3", IdentityProvider.class, "idp3", EntityTypes.ID_PROVIDER_CONFIG_TYPE)), new Dependency("2", Policy.class, "my-policy2", EntityTypes.POLICY_TYPE), Collections.singletonList(new Dependency("4", IdentityProvider.class, "idp4", EntityTypes.ID_PROVIDER_CONFIG_TYPE)))); bundle.addEntity(new IdentityProvider.Builder().name("idp1").id("1").build()); bundle.addEntity(new IdentityProvider.Builder().name("idp2").id(IdentityProvider.INTERNAL_IDP_ID).build()); bundle.addEntity(new IdentityProvider.Builder().name("idp3").id("3").build()); bundle.addEntity(new IdentityProvider.Builder().name("idp4").id("4").build()); FilterConfiguration filterConfiguration = new FilterConfiguration(); List<IdentityProvider> filteredEntities = filter.filter("/my/folder/path", filterConfiguration, bundle, filteredBundle); assertEquals(1, filteredEntities.size()); assertTrue(filteredEntities.stream().anyMatch(c -> "idp3".equals(c.getName()))); filterConfiguration.getEntityFilters().put(filter.getFilterableEntityName(), new HashSet<>()); filterConfiguration.getEntityFilters().get(filter.getFilterableEntityName()).add("idp4"); filterConfiguration.getEntityFilters().get(filter.getFilterableEntityName()).add("idp1"); filteredEntities = filter.filter("/my/folder/path", filterConfiguration, bundle, filteredBundle); assertEquals(3, filteredEntities.size()); assertTrue(filteredEntities.stream().anyMatch(c -> "idp3".equals(c.getName()))); assertTrue(filteredEntities.stream().anyMatch(c -> "idp1".equals(c.getName()))); assertTrue(filteredEntities.stream().anyMatch(c -> "idp4".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")); }