Skip to content

Commit

Permalink
NCL-6462: Sort dumpCurrentConfig output by key
Browse files Browse the repository at this point in the history
  • Loading branch information
dwalluck committed Apr 24, 2021
1 parent 5154b3e commit 77112d3
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 12 deletions.
30 changes: 19 additions & 11 deletions common/src/main/java/org/jboss/gm/common/Configuration.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
package org.jboss.gm.common;

import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Properties;
import java.util.stream.Collectors;

import org.aeonbits.owner.Accessible;
import org.aeonbits.owner.Config;
Expand Down Expand Up @@ -224,20 +228,24 @@ public Map<String, String> convert(Method method, String input) {

/**
* Return the current configuration as a formatted string.
*
* @return the current configuration as a formatted string
*/
default String dumpCurrentConfig() {

StringBuilder currentProperties = new StringBuilder();
for (Method method : Configuration.class.getMethods()) {
if (method.isAnnotationPresent(Config.Key.class)) {
Config.Key val = method.getAnnotation(Config.Key.class);
currentProperties.append(System.lineSeparator());
currentProperties.append('\t');
currentProperties.append(val.value());
currentProperties.append(" : ");
currentProperties.append(this.getProperties().get(val.value()));
}
final List<String> values = Arrays.stream(Configuration.class.getMethods())
.map(method -> method.getAnnotation(Key.class)).filter(Objects::nonNull).map(Key::value).sorted()
.collect(Collectors.toList());
final StringBuilder currentProperties = new StringBuilder();
currentProperties.append(System.lineSeparator());

for (String value : values) {
currentProperties.append('\t');
currentProperties.append(value);
currentProperties.append(" : ");
currentProperties.append(this.getProperties().get(value));
currentProperties.append(System.lineSeparator());
}

return currentProperties.toString();
}
}
19 changes: 18 additions & 1 deletion common/src/test/java/org/jboss/gm/common/ConfigurationTest.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package org.jboss.gm.common;

import java.util.Arrays;
import java.util.Map;
import java.util.regex.Pattern;

import org.aeonbits.owner.ConfigFactory;
import org.commonjava.maven.ext.io.rest.Translator;
Expand All @@ -11,6 +13,7 @@
import org.junit.contrib.java.lang.system.SystemOutRule;
import org.junit.rules.TestRule;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
Expand Down Expand Up @@ -57,7 +60,7 @@ public void verifyEnv() {

assertEquals(deploy, c.deployUrl());

assertEquals(c.restRetryDuration(), Translator.RETRY_DURATION_SEC);
assertEquals(Translator.RETRY_DURATION_SEC, c.restRetryDuration());
}

@Test
Expand All @@ -78,4 +81,18 @@ public void verifyDefaultState() {
Configuration c = ConfigFactory.create(Configuration.class);
assertNull(c.overrideTransitive());
}

@Test
public void verifyDumpCurrentConfig() {
final Configuration c = ConfigFactory.create(Configuration.class);
final String currentConfig = c.dumpCurrentConfig();
final String lineEnding = "(\\u000D\\u000A|[\\u000A\\u000D])";
final String keyValue = "(\\t(.*) : (.*))" + lineEnding;
final Pattern p = Pattern.compile("^" + lineEnding + "(" + keyValue + ")*$");
assertThat(currentConfig).matches(p);
final String[] lines = currentConfig.split(System.lineSeparator());
assertThat(lines).isNotEmpty();
final String[] keys = Arrays.stream(lines).map(line -> line.split(":")[0].trim()).toArray(String[]::new);
assertThat(keys).hasSize(lines.length).isSorted();
}
}

0 comments on commit 77112d3

Please sign in to comment.