Skip to content

Commit

Permalink
#569 validate configuration properties (#576)
Browse files Browse the repository at this point in the history
  • Loading branch information
DirkMahler authored Aug 8, 2024
1 parent 172f71e commit f4a0249
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

import io.smallrye.config.*;
import io.smallrye.config.source.yaml.YamlConfigSource;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.eclipse.microprofile.config.spi.ConfigSource;

Expand All @@ -22,12 +23,14 @@
import static java.util.Collections.list;
import static java.util.stream.Collectors.*;
import static java.util.stream.StreamSupport.stream;
import static lombok.AccessLevel.PRIVATE;

/**
* Defines the interface for loading runtime configuration.
* <p>
* The mechanism is based on Eclipse Micro Profile configuration.
*/
@NoArgsConstructor(access = PRIVATE)
@Slf4j
public class ConfigurationMappingLoader {

Expand Down Expand Up @@ -207,35 +210,52 @@ public Builder<C> withIgnoreProperties(Collection<String> ignoreProperties) {
*/
public C load(ConfigSource... additionalConfigSources) {
// Create intermediate configuration with applied profiles and interpolated properties (without validation)
SmallRyeConfig interpolatedConfig = new SmallRyeConfigBuilder().withSources(this.configSources)
SmallRyeConfig config = new SmallRyeConfigBuilder().withSources(this.configSources)
.withSources(additionalConfigSources)
.withProfiles(this.profiles)
.withInterceptors(new ExpressionConfigSourceInterceptor())
.withMapping(configurationMapping)
.withValidateUnknown(false)
.build();
// Create final config including validation, including only jqassistant properties
Map<String, String> interpolatedProperties = stream(interpolatedConfig.getPropertyNames()
.spliterator(), false).filter(property -> property.startsWith(Configuration.PREFIX))
.filter(property -> !ignoreProperties.contains(property))
.collect(toMap(property -> property, interpolatedConfig::getRawValue));
SmallRyeConfig config = new SmallRyeConfigBuilder().withMapping(configurationMapping)
.withSources(new PropertiesConfigSource(interpolatedProperties, "jQAssistant Configuration", ConfigSource.DEFAULT_ORDINAL))
.build();
if (log.isDebugEnabled()) {
logConfigProblems(config);
}
C configMapping = config.getConfigMapping(configurationMapping);
if (log.isDebugEnabled()) {
log.debug("Loaded configuration from {} config sources:\n{}", additionalConfigSources.length, configurationSerializer.toYaml(configMapping));
}
return configMapping;
}

private void logConfigProblems(SmallRyeConfig interpolatedConfig) {
Map<String, String> filteredProperties = stream(interpolatedConfig.getPropertyNames()
.spliterator(), false).filter(property -> property.startsWith(Configuration.PREFIX))
.filter(property -> !ignoreProperties.contains(property))
.collect(toMap(property -> property, interpolatedConfig::getRawValue, (s1, s2) -> null, () -> new TreeMap<>()));
log.debug("jQAssistant config properties:");
for (Map.Entry<String, String> entry : filteredProperties.entrySet()) {
log.debug("\t{}={}", entry.getKey(), entry.getValue());
}
try {
new SmallRyeConfigBuilder().withMapping(configurationMapping)
.withSources(new PropertiesConfigSource(filteredProperties, "jQAssistant Configuration", ConfigSource.DEFAULT_ORDINAL))
.build();
} catch (ConfigValidationException configValidationException) {
for (int i = 0; i < configValidationException.getProblemCount(); i++) {
log.debug(configValidationException.getProblem(i)
.getMessage());
}
}
}

private List<ConfigSource> getExternalYamlConfigSources(File directory, List<Path> configLocations, int ordinal) {
List<ConfigSource> configSources = new ArrayList<>();
List<ConfigSource> yamlConfigSources = new ArrayList<>();
for (Path configLocation : configLocations) {
Path path = directory.toPath()
.resolve(configLocation);
configSources.addAll(getExternalYamlConfigSources(path, ordinal));
yamlConfigSources.addAll(getExternalYamlConfigSources(path, ordinal));
}
return configSources;
return yamlConfigSources;
}

private List<ConfigSource> getExternalYamlConfigSources(Path configLocationPath, int ordinal) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import com.buschmais.jqassistant.core.scanner.api.configuration.Scan;
import com.buschmais.jqassistant.core.shared.configuration.Plugin;

import io.smallrye.config.ConfigValidationException;
import io.smallrye.config.PropertiesConfigSource;
import io.smallrye.config.SysPropConfigSource;
import org.eclipse.microprofile.config.spi.ConfigSource;
Expand All @@ -19,7 +18,6 @@
import static java.util.Collections.emptyList;
import static java.util.Collections.singletonList;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;

/**
* Tests for the {@link ConfigurationMappingLoader}.
Expand Down Expand Up @@ -83,18 +81,6 @@ void profile() {
.properties()).containsEntry("profile-user-value", "test-value");
}

@Test
void unknownProperty() {
String unknownProperty = "jqassistant.unknown";
assertThatExceptionOfType(ConfigValidationException.class).isThrownBy(() -> {
ConfigurationMappingLoader.builder(TestConfiguration.class, emptyList())
.withUserHome(USER_HOME)
.withWorkingDirectory(WORKING_DIRECTORY)
.load(new PropertiesConfigSource(Map.of(unknownProperty, "test value"), "Test", ConfigSource.DEFAULT_ORDINAL));
})
.withMessageContaining(unknownProperty);
}

@Test
void ignoreProperty() {
String unknownProperty = "jqassistant.unknown";
Expand Down

0 comments on commit f4a0249

Please sign in to comment.