From 7fa00f0cf9e68a971325cfaa9e9320f230d4cd53 Mon Sep 17 00:00:00 2001 From: Dirk Mahler Date: Fri, 2 Aug 2024 13:09:37 +0200 Subject: [PATCH] #569 activated validation of unknown properties --- .../ConfigurationMappingLoader.java | 23 +++++++++++-------- .../ConfigurationMappingLoaderTest.java | 20 ++++++++++++++-- .../.jqassistant/plugins.yml | 1 - 3 files changed, 32 insertions(+), 12 deletions(-) diff --git a/core/runtime/src/main/java/com/buschmais/jqassistant/core/runtime/api/configuration/ConfigurationMappingLoader.java b/core/runtime/src/main/java/com/buschmais/jqassistant/core/runtime/api/configuration/ConfigurationMappingLoader.java index b1701c7c7a..4417fe2489 100644 --- a/core/runtime/src/main/java/com/buschmais/jqassistant/core/runtime/api/configuration/ConfigurationMappingLoader.java +++ b/core/runtime/src/main/java/com/buschmais/jqassistant/core/runtime/api/configuration/ConfigurationMappingLoader.java @@ -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; @@ -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. @@ -197,12 +195,19 @@ public Builder withProfiles(List 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 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()) { diff --git a/core/runtime/src/test/java/com/buschmais/jqassistant/core/runtime/impl/configuration/ConfigurationMappingLoaderTest.java b/core/runtime/src/test/java/com/buschmais/jqassistant/core/runtime/impl/configuration/ConfigurationMappingLoaderTest.java index a3d456a104..22e959360b 100644 --- a/core/runtime/src/test/java/com/buschmais/jqassistant/core/runtime/impl/configuration/ConfigurationMappingLoaderTest.java +++ b/core/runtime/src/test/java/com/buschmais/jqassistant/core/runtime/impl/configuration/ConfigurationMappingLoaderTest.java @@ -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}. @@ -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(); @@ -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() { diff --git a/core/runtime/src/test/resources/working directory/.jqassistant/plugins.yml b/core/runtime/src/test/resources/working directory/.jqassistant/plugins.yml index 17c0fed78b..a3272708f8 100644 --- a/core/runtime/src/test/resources/working directory/.jqassistant/plugins.yml +++ b/core/runtime/src/test/resources/working directory/.jqassistant/plugins.yml @@ -7,4 +7,3 @@ jqassistant: artifact-id: full-test-plugin type: jqp version: 1.0.0 - active: false