Skip to content

Commit

Permalink
#569 activated validation of unknown properties
Browse files Browse the repository at this point in the history
  • Loading branch information
DirkMahler committed Aug 2, 2024
1 parent ab42d94 commit 7fa00f0
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,9 @@
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.Map;

import io.smallrye.config.EnvConfigSource;
import io.smallrye.config.ExpressionConfigSourceInterceptor;
import io.smallrye.config.SmallRyeConfig;
import io.smallrye.config.SmallRyeConfigBuilder;
import io.smallrye.config.*;
import io.smallrye.config.source.yaml.YamlConfigSource;
import lombok.extern.slf4j.Slf4j;
import org.eclipse.microprofile.config.spi.ConfigSource;
Expand All @@ -25,8 +23,8 @@
import static java.nio.file.Files.walkFileTree;
import static java.util.Collections.emptyList;
import static java.util.Collections.list;
import static java.util.stream.Collectors.toList;
import static java.util.stream.Collectors.toUnmodifiableList;
import static java.util.stream.Collectors.*;
import static java.util.stream.StreamSupport.stream;

/**
* Defines the interface for loading runtime configuration.
Expand Down Expand Up @@ -197,12 +195,19 @@ public Builder<C> withProfiles(List<String> profiles) {
* @return The {@link Configuration}.
*/
public C load(ConfigSource... additionalConfigSources) {
SmallRyeConfig config = new SmallRyeConfigBuilder().withMapping(configurationMapping)
.withSources(this.configSources)
// Create intermediate configuration with applied profiles and interpolated properties (without validation)
SmallRyeConfig interpolatedConfig = new SmallRyeConfigBuilder().withSources(this.configSources)
.withSources(additionalConfigSources)
.withProfiles(this.profiles)
.withValidateUnknown(false)
.withInterceptors(new ExpressionConfigSourceInterceptor())
.withProfiles(this.profiles)
.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))
.collect(toMap(property -> property, interpolatedConfig::getRawValue));
SmallRyeConfig config = new SmallRyeConfigBuilder().withMapping(configurationMapping)
.withSources(new PropertiesConfigSource(interpolatedProperties, "Interpolated Configuration", ConfigSource.DEFAULT_ORDINAL))
.build();
C configMapping = config.getConfigMapping(configurationMapping);
if (log.isDebugEnabled()) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,24 @@
package com.buschmais.jqassistant.core.runtime.impl.configuration;

import java.io.File;
import java.net.URISyntaxException;
import java.util.List;
import java.util.Map;

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 All @@ -29,7 +33,7 @@ class ConfigurationMappingLoaderTest {
* Load all yaml/yml config files from the working directory.
*/
@Test
void loadFromDefaultConfigLocations() throws URISyntaxException {
void loadFromDefaultConfigLocations() {
TestConfiguration configuration = getConfiguration(emptyList());

assertThat(configuration).isNotNull();
Expand Down Expand Up @@ -78,6 +82,18 @@ 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
@SetEnvironmentVariable(key = "jqassistant_scan_continue_on_error", value = "false")
void overrideFromEnvVariable() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,3 @@ jqassistant:
artifact-id: full-test-plugin
type: jqp
version: 1.0.0
active: false

0 comments on commit 7fa00f0

Please sign in to comment.