AggregatingMetadataStatementsProvider implements MetadataStatementsProvider { @SuppressWarnings("Duplicates") @Override public Map<AAGUID, Set<MetadataStatement>> provide() { return metadataStatementsProviders.stream() .flatMap(provider -> { try { return provider.provide().entrySet().stream(); } catch (RuntimeException e) { logger.warn("Failed to load metadata from one of metadataStatementsProviders", e); return null; } }) .filter(Objects::nonNull) .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue)); } AggregatingMetadataStatementsProvider(List<MetadataStatementsProvider> metadataStatementsProviders); @SuppressWarnings("Duplicates") @Override Map<AAGUID, Set<MetadataStatement>> provide();  }
@Test void provide_test() { MetadataStatementsProvider providerA = mock(MetadataStatementsProvider.class); Map<AAGUID, Set<MetadataStatement>> mapA = new HashMap<>(); mapA.put(new AAGUID("df495bdc-223a-429d-9f0e-ebfa29155812"), new HashSet<>()); when(providerA.provide()).thenReturn(mapA); MetadataStatementsProvider providerB = mock(MetadataStatementsProvider.class); Map<AAGUID, Set<MetadataStatement>> mapB = new HashMap<>(); mapB.put(new AAGUID("d075c221-6a37-4c61-80c7-11254460d5bb"), new HashSet<>()); when(providerB.provide()).thenReturn(mapB); AggregatingMetadataStatementsProvider target = new AggregatingMetadataStatementsProvider(Arrays.asList(providerA, providerB)); assertThat(target.provide()).containsOnlyKeys(new AAGUID("df495bdc-223a-429d-9f0e-ebfa29155812"), new AAGUID("d075c221-6a37-4c61-80c7-11254460d5bb")); }
@Test void provide_with_one_of_provider_throws_exception_test() { MetadataStatementsProvider providerA = mock(MetadataStatementsProvider.class); Map<AAGUID, Set<MetadataStatement>> mapA = new HashMap<>(); mapA.put(new AAGUID("df495bdc-223a-429d-9f0e-ebfa29155812"), new HashSet<>()); when(providerA.provide()).thenReturn(mapA); MetadataStatementsProvider providerB = mock(MetadataStatementsProvider.class); when(providerB.provide()).thenThrow(new RuntimeException("unexpected error")); AggregatingMetadataStatementsProvider target = new AggregatingMetadataStatementsProvider(Arrays.asList(providerA, providerB)); assertThat(target.provide()).containsOnlyKeys(new AAGUID("df495bdc-223a-429d-9f0e-ebfa29155812")); }