KeyValueConfigSource implements ConfigSource { @Override public Map<String, ConfigProperty> loadConfig() { List<CompletableFuture<List<KeyValueConfigEntity>>> futureList = configNames.stream().map(this::loadConfig).collect(Collectors.toList()); CompletableFuture<Void> allResults = CompletableFuture.allOf(futureList.toArray(new CompletableFuture[futureList.size()])); CompletableFuture<List<List<KeyValueConfigEntity>>> joinedFuture = allResults.thenApply( input -> futureList.stream().map(CompletableFuture::join).collect(Collectors.toList())); List<List<KeyValueConfigEntity>> resultList; try { resultList = joinedFuture.get(repositoryTimeout.toMillis(), TimeUnit.MILLISECONDS); } catch (ExecutionException e) { throw ThrowableUtil.propagate(e.getCause()); } catch (TimeoutException e) { String message = String.format("TimeoutException after '%s' millis", repositoryTimeout.toMillis()); throw new ConfigSourceNotAvailableException(message, e); } catch (InterruptedException e) { Thread.interrupted(); throw ThrowableUtil.propagate(e); } return resultList .stream() .flatMap(Collection::stream) .filter(i -> !i.getDisabled()) .collect( Collector.of( (Supplier<TreeMap<String, ConfigProperty>>) TreeMap::new, (map, i) -> { String origin = i.getConfigName().getQualifiedName(); String name = i.getPropName(); String value = i.getPropValue(); map.putIfAbsent( name, LoadedConfigProperty.withNameAndValue(name, value).origin(origin).build()); }, (map1, map2) -> map1)); } private  KeyValueConfigSource(Builder builder); static Builder withRepository(KeyValueConfigRepository repository); static Builder withRepository(KeyValueConfigRepository repository, String collectionName); @Override Map<String, ConfigProperty> loadConfig();  }
@Test void testKeyValueLoadConfig() throws Exception { KeyValueConfigName n1 = new KeyValueConfigName(g1, collectionName); KeyValueConfigName n2 = new KeyValueConfigName(g2, collectionName); KeyValueConfigName root = new KeyValueConfigName(null, collectionName); KeyValueConfigEntity entity1 = new KeyValueConfigEntity("p1", "v1", n1); KeyValueConfigEntity entity2 = new KeyValueConfigEntity("p2", "v2", n2); KeyValueConfigEntity entity3 = new KeyValueConfigEntity("p1", "v1", root); KeyValueConfigEntity entity4 = new KeyValueConfigEntity("p2", "v2", root); KeyValueConfigEntity entity5 = new KeyValueConfigEntity("p42", "v42", root); doReturn(ImmutableList.of(entity1)).when(repository).findAll(n1); doReturn(ImmutableList.of(entity2)).when(repository).findAll(n2); doReturn(ImmutableList.of(entity3, entity4, entity5)).when(repository).findAll(root); Map<String, ConfigProperty> config = configSource.loadConfig(); assertEquals(3, config.size()); assertEquals("v1", config.get("p1").valueAsString().get()); assertEquals("v2", config.get("p2").valueAsString().get()); assertEquals("v42", config.get("p42").valueAsString().get()); }
@Test void testKeyValueLoadConfigFindAllThrowsException() throws Exception { KeyValueConfigName n1 = new KeyValueConfigName(g1, collectionName); KeyValueConfigName n2 = new KeyValueConfigName(g2, collectionName); KeyValueConfigName root = new KeyValueConfigName(null, collectionName); KeyValueConfigEntity entity1 = new KeyValueConfigEntity("p1", "v1", n1); KeyValueConfigEntity entity2 = new KeyValueConfigEntity("p2", "v2", n2); doReturn(ImmutableList.of(entity1)).when(repository).findAll(n1); doReturn(ImmutableList.of(entity2)).when(repository).findAll(n2); doThrow(new RuntimeException("some exception")).when(repository).findAll(root); Map<String, ConfigProperty> config = configSource.loadConfig(); assertEquals(2, config.size()); assertEquals("v1", config.get("p1").valueAsString().get()); assertEquals("v2", config.get("p2").valueAsString().get()); }
@Test void testKeyValueLoadConfigFindAllGettingLong() throws Exception { KeyValueConfigName n1 = new KeyValueConfigName(g1, collectionName); KeyValueConfigName n2 = new KeyValueConfigName(g2, collectionName); KeyValueConfigName root = new KeyValueConfigName(null, collectionName); KeyValueConfigEntity entity1 = new KeyValueConfigEntity("p1", "v1", n1); KeyValueConfigEntity entity2 = new KeyValueConfigEntity("p2", "v2", n2); doAnswer( (Answer<ImmutableList<KeyValueConfigEntity>>) invocation -> { Thread.sleep(100); return ImmutableList.of(entity1); }) .when(repository) .findAll(n1); doAnswer( (Answer<ImmutableList<KeyValueConfigEntity>>) invocation -> { Thread.sleep(100); return ImmutableList.of(entity2); }) .when(repository) .findAll(n2); doThrow(new RuntimeException("some exception")).when(repository).findAll(root); Map<String, ConfigProperty> config = configSource.loadConfig(); assertEquals(2, config.size()); assertEquals("v1", config.get("p1").valueAsString().get()); assertEquals("v2", config.get("p2").valueAsString().get()); }