Skip to content

Commit

Permalink
Add getConfigProperties implementation to DefaultConfigProperties
Browse files Browse the repository at this point in the history
  • Loading branch information
jack-berg committed Oct 12, 2023
1 parent af278ba commit 9d00503
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,16 @@ public Map<String, String> getMap(String name) {
Map.Entry::getKey, Map.Entry::getValue, (first, next) -> next, LinkedHashMap::new));
}

@Nullable
@Override
public ExtendedConfigProperties getConfigProperties(String name) {
Map<String, String> mapProperties = getMap(name);
if (mapProperties.isEmpty()) {
return null;
}
return DefaultConfigProperties.createFromMap(mapProperties);
}

/**
* Return a new {@link DefaultConfigProperties} by overriding the {@code previousProperties} with
* the {@code overrides}.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,24 @@ class ConfigPropertiesTest {
void allValid() {
Map<String, String> properties = makeTestProps();

ConfigProperties config = DefaultConfigProperties.createFromMap(properties);
DefaultConfigProperties config = DefaultConfigProperties.createFromMap(properties);
assertThat(config.getString("test.string")).isEqualTo("str");
assertThat(config.getInt("test.int")).isEqualTo(10);
assertThat(config.getLong("test.long")).isEqualTo(20);
assertThat(config.getDouble("test.double")).isEqualTo(5.4);
assertThat(config.getList("test.list")).containsExactly("cat", "dog", "bear");
assertThat(config.getMap("test.map"))
.containsExactly(entry("cat", "meow"), entry("dog", "bark"), entry("bear", "growl"));
.containsExactly(
entry("cat", "meow"),
entry("dog", "bark"),
entry("bear", "growl"),
entry("number", "1"));
ExtendedConfigProperties testMapConfig = config.getConfigProperties("test.map");
assertThat(testMapConfig.getString("cat")).isEqualTo("meow");
assertThat(testMapConfig.getString("dog")).isEqualTo("bark");
assertThat(testMapConfig.getString("bear")).isEqualTo("growl");
assertThat(testMapConfig.getString("number")).isEqualTo("1");
assertThat(testMapConfig.getInt("number")).isEqualTo(1);
assertThat(config.getDuration("test.duration")).isEqualTo(Duration.ofSeconds(1));
}

Expand All @@ -48,19 +58,24 @@ void allValidUsingHyphens() {
assertThat(config.getDouble("test-double")).isEqualTo(5.4);
assertThat(config.getList("test-list")).containsExactly("cat", "dog", "bear");
assertThat(config.getMap("test-map"))
.containsExactly(entry("cat", "meow"), entry("dog", "bark"), entry("bear", "growl"));
.containsExactly(
entry("cat", "meow"),
entry("dog", "bark"),
entry("bear", "growl"),
entry("number", "1"));
assertThat(config.getDuration("test-duration")).isEqualTo(Duration.ofSeconds(1));
}

@Test
void allMissing() {
ConfigProperties config = DefaultConfigProperties.createFromMap(emptyMap());
DefaultConfigProperties config = DefaultConfigProperties.createFromMap(emptyMap());
assertThat(config.getString("test.string")).isNull();
assertThat(config.getInt("test.int")).isNull();
assertThat(config.getLong("test.long")).isNull();
assertThat(config.getDouble("test.double")).isNull();
assertThat(config.getList("test.list")).isEmpty();
assertThat(config.getMap("test.map")).isEmpty();
assertThat(config.getConfigProperties("test.map")).isNull();
assertThat(config.getDuration("test.duration")).isNull();
}

Expand All @@ -75,13 +90,14 @@ void allEmpty() {
properties.put("test.map", "");
properties.put("test.duration", "");

ConfigProperties config = DefaultConfigProperties.createFromMap(properties);
DefaultConfigProperties config = DefaultConfigProperties.createFromMap(properties);
assertThat(config.getString("test.string")).isEmpty();
assertThat(config.getInt("test.int")).isNull();
assertThat(config.getLong("test.long")).isNull();
assertThat(config.getDouble("test.double")).isNull();
assertThat(config.getList("test.list")).isEmpty();
assertThat(config.getMap("test.map")).isEmpty();
assertThat(config.getConfigProperties("test.map")).isNull();
assertThat(config.getDuration("test.duration")).isNull();
}

Expand Down Expand Up @@ -275,7 +291,7 @@ private static Map<String, String> makeTestProps() {
properties.put("test.double", "5.4");
properties.put("test.boolean", "true");
properties.put("test.list", "cat,dog,bear");
properties.put("test.map", "cat=meow,dog=bark,bear=growl,bird=");
properties.put("test.map", "cat=meow,dog=bark,bear=growl,bird=,number=1");
properties.put("test.duration", "1s");
return properties;
}
Expand Down

0 comments on commit 9d00503

Please sign in to comment.