Skip to content

Commit

Permalink
Fix config sources ordering (#171)
Browse files Browse the repository at this point in the history
* Fix config sources ordering

* Codestyle

---------

Co-authored-by: Clement BALAY <[email protected]>
Co-authored-by: Roberto Cortez <[email protected]>
  • Loading branch information
3 people authored Sep 21, 2023
1 parent 457c5ea commit 416c805
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,22 +45,25 @@ Iterable<ConfigSource> getConfigSources(ConsulConfig config, ConsulConfigGateway
return Collections.emptyList();
}

List<ConfigSource> result = new ArrayList<>(keys.size());
Map<String, ConfigSource> results = new LinkedHashMap<>(keys.size());

List<Uni<?>> allUnis = new ArrayList<>();

for (Map.Entry<String, ValueType> entry : keys.entrySet()) {
String fullKey = config.prefix().isPresent() ? config.prefix().get() + "/" + entry.getKey() : entry.getKey();
results.put(fullKey, null);
allUnis.add(consulConfigGateway.getValue(fullKey).invoke(new Consumer<Response>() {
@Override
public void accept(Response response) {
if (response != null) {
result.add(ResponseConfigSourceUtil.toConfigSource(response, entry.getValue(), config.prefix()));
results.put(response.getKey(),
ResponseConfigSourceUtil.toConfigSource(response, entry.getValue(), config.prefix()));
} else {
String message = "Key '" + fullKey + "' not found in Consul.";
if (config.failOnMissingKey()) {
throw new RuntimeException(message);
} else {
results.remove(fullKey);
log.info(message);
}
}
Expand All @@ -81,6 +84,6 @@ public void accept(Response response) {
consulConfigGateway.close();
}

return result;
return results.values();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Optional;

import org.eclipse.microprofile.config.spi.ConfigSource;
Expand Down Expand Up @@ -207,6 +208,35 @@ void testPropertiesKeysWithPrefix() {
verify(mockGateway, times(1)).getValue("config/second");
}

@Test
void testConfigSourcesOrdered() {
ConsulConfig config = mock(ConsulConfig.class);
// We want this order: first > second > third (first overrides second which overrides third)
when(config.rawValueKeys()).thenReturn(keyValues("some/first", "some/second", "some/third"));
AgentConfig agentConfig = mock(AgentConfig.class);
when(config.agent()).thenReturn(agentConfig);

ConsulConfigGateway mockGateway = mock(ConsulConfigGateway.class);
// make sure the third property is received first
when(mockGateway.getValue("some/first")).thenReturn(validResponse("some/third", "third"));
// make sure the first property is received in second
when(mockGateway.getValue("some/second")).thenReturn(validResponse("some/first", "first"));
// make sure the second property is received in third
when(mockGateway.getValue("some/third")).thenReturn(validResponse("some/second", "second"));

Iterable<ConfigSource> configSources = new ConsulConfigSourceFactory().getConfigSources(config, mockGateway);
assertThat(configSources).hasSize(3);
assertThat(configSources).extracting(ConfigSource::getProperties).containsExactly(
Map.of("some.first", "first"),
Map.of("some.second", "second"),
Map.of("some.third", "third"));

//all keys should have been resolved because we resolve keys in the order they were given by the user
verify(mockGateway, times(1)).getValue("some/first");
verify(mockGateway, times(1)).getValue("some/second");
verify(mockGateway, times(1)).getValue("some/third");
}

private Optional<List<String>> keyValues(String... keys) {
return Optional.of(Arrays.asList(keys));
}
Expand Down

0 comments on commit 416c805

Please sign in to comment.