Skip to content

Commit

Permalink
#569 validate unknown properties only when debug is active
Browse files Browse the repository at this point in the history
  • Loading branch information
DirkMahler committed Aug 7, 2024
1 parent acbe1cf commit a4c9859
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -207,25 +207,39 @@ 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();
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) {
// 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();
C configMapping = config.getConfigMapping(configurationMapping);
if (log.isDebugEnabled()) {
log.debug("Loaded configuration from {} config sources:\n{}", additionalConfigSources.length, configurationSerializer.toYaml(configMapping));
try {
new SmallRyeConfigBuilder().withMapping(configurationMapping)
.withSources(new PropertiesConfigSource(interpolatedProperties, "jQAssistant Configuration", ConfigSource.DEFAULT_ORDINAL))
.build();
} catch (ConfigValidationException configValidationException) {
for (int i = 0; i < configValidationException.getProblemCount(); i++) {
log.debug(configValidationException.getProblem(i)
.getMessage());
}
}
return configMapping;
}

private List<ConfigSource> getExternalYamlConfigSources(File directory, List<Path> configLocations, int ordinal) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,18 @@

import java.io.File;
import java.util.List;
import java.util.Map;
import java.util.Set;

import com.buschmais.jqassistant.core.runtime.api.configuration.ConfigurationMappingLoader;
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;
import org.junit.jupiter.api.Test;
import org.junitpioneer.jupiter.SetEnvironmentVariable;

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,31 +77,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";

TestConfiguration configuration = ConfigurationMappingLoader.builder(TestConfiguration.class, emptyList())
.withUserHome(USER_HOME)
.withWorkingDirectory(WORKING_DIRECTORY)
.withIgnoreProperties(Set.of(unknownProperty))
.load(new PropertiesConfigSource(Map.of(unknownProperty, "test value"), "Test", ConfigSource.DEFAULT_ORDINAL));

assertThat(configuration).isNotNull();
}

@Test
@SetEnvironmentVariable(key = "jqassistant_scan_continue_on_error", value = "false")
void overrideFromEnvVariable() {
Expand Down

0 comments on commit a4c9859

Please sign in to comment.