Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added tests to Config, to ensure backwards compatibility. #10615

Merged
merged 2 commits into from
Jul 15, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion bom/runtime/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
<microprofile-opentracing-api.version>1.3.3</microprofile-opentracing-api.version>
<microprofile-reactive-streams-operators.version>1.0.1</microprofile-reactive-streams-operators.version>
<microprofile-rest-client.version>1.4.1</microprofile-rest-client.version>
<smallrye-config.version>1.8.1</smallrye-config.version>
<smallrye-config.version>1.8.4</smallrye-config.version>
<smallrye-health.version>2.2.2</smallrye-health.version>
<smallrye-metrics.version>2.4.2</smallrye-metrics.version>
<smallrye-open-api.version>2.0.2</smallrye-open-api.version>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public static SmallRyeConfigBuilder configBuilder(final boolean runTime, final b
final ApplicationPropertiesConfigSource.InJar inJar = new ApplicationPropertiesConfigSource.InJar();
final ApplicationPropertiesConfigSource.MpConfigInJar mpConfig = new ApplicationPropertiesConfigSource.MpConfigInJar();
builder.withSources(inFileSystem, inJar, mpConfig, new DotEnvConfigSource());
builder.withInterceptors(new QuarkusProfileConfigSourceInterceptor(ProfileManager.getActiveProfile()));
builder.withProfile(ProfileManager.getActiveProfile());
builder.addDefaultInterceptors();
final ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
if (runTime) {
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,18 +1,23 @@
package io.quarkus.runtime.configuration;

import static java.util.Collections.singletonMap;
import static java.util.stream.Collectors.toList;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.StreamSupport;

import org.eclipse.microprofile.config.Config;
import org.eclipse.microprofile.config.spi.ConfigProviderResolver;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;

import io.smallrye.config.ExpressionConfigSourceInterceptor;
import io.smallrye.config.ProfileConfigSourceInterceptor;
import io.smallrye.config.PropertiesConfigSource;
import io.smallrye.config.SmallRyeConfig;
import io.smallrye.config.SmallRyeConfigBuilder;
Expand All @@ -21,7 +26,6 @@ public class ConfigProfileTestCase {

static ClassLoader classLoader;
static ConfigProviderResolver cpr;
Config config;

@BeforeAll
public static void initConfig() {
Expand All @@ -38,46 +42,54 @@ public void doAfter() {
}
}

private SmallRyeConfig buildConfig(Map<String, String> configMap) {
final SmallRyeConfigBuilder builder = new SmallRyeConfigBuilder();
builder.withInterceptors(new QuarkusProfileConfigSourceInterceptor(ProfileManager.getActiveProfile()));
builder.withSources(new PropertiesConfigSource(configMap, "test input", 500));
final SmallRyeConfig config = (SmallRyeConfig) builder.build();
private SmallRyeConfigBuilder configBuilder(String... keyValues) {
return new SmallRyeConfigBuilder()
.addDefaultInterceptors()
.withSources(new PropertiesConfigSource(maps(keyValues), "test input", 500))
.withProfile(ProfileManager.getActiveProfile());
}

private SmallRyeConfig buildConfig(String... keyValues) {
final SmallRyeConfig config = configBuilder(keyValues).build();
cpr.registerConfig(config, classLoader);
this.config = config;
return config;
}

private Map<String, String> maps(Map... maps) {
Map<String, String> out = new HashMap<>();
for (Map map : maps) {
out.putAll(map);
private Map<String, String> maps(String... keyValues) {
if (keyValues.length % 2 != 0) {
throw new IllegalArgumentException("keyValues array must be a multiple of 2");
}

Map<String, String> props = new HashMap<>();
for (int i = 0; i < keyValues.length; i += 2) {
props.put(keyValues[i], keyValues[i + 1]);
}
return out;

return props;
}

@Test
public void testDefaultProfile() {
final SmallRyeConfig config = buildConfig(maps(
singletonMap("foo.one", "v1"),
singletonMap("foo.two", "v2"),
singletonMap("%foo.foo.three", "f1"),
singletonMap("%prod.foo.four", "v4")));
void defaultProfile() {
final SmallRyeConfig config = buildConfig(
"foo.one", "v1",
"foo.two", "v2",
"%foo.foo.three", "f1",
"%prod.foo.four", "v4");
assertEquals("v1", config.getValue("foo.one", String.class));
assertEquals("v2", config.getValue("foo.two", String.class));
assertFalse(config.getOptionalValue("foo.three", String.class).isPresent());
assertEquals("v4", config.getValue("foo.four", String.class));
}

@Test
public void testOverridenProfile() {
void overriddenProfile() {
System.setProperty("quarkus.profile", "foo");
try {
final SmallRyeConfig config = buildConfig(maps(
singletonMap("foo.one", "v1"),
singletonMap("foo.two", "v2"),
singletonMap("%foo.foo.three", "f1"),
singletonMap("%prod.foo.four", "v4")));
final SmallRyeConfig config = buildConfig(
"foo.one", "v1",
"foo.two", "v2",
"%foo.foo.three", "f1",
"%prod.foo.four", "v4");
assertEquals("v1", config.getValue("foo.one", String.class));
assertEquals("v2", config.getValue("foo.two", String.class));
assertEquals("f1", config.getValue("foo.three", String.class));
Expand All @@ -88,43 +100,195 @@ public void testOverridenProfile() {
}

@Test
public void testBackwardCompatibleOverridenProfile() {
void overriddenProfileHigherOrdinal() {
System.setProperty("quarkus-profile", "foo");
try {
final SmallRyeConfig config = buildConfig(maps(
singletonMap("foo.one", "v1"),
singletonMap("foo.two", "v2"),
singletonMap("%foo.foo.three", "f1"),
singletonMap("%prod.foo.four", "v4")));
assertEquals("v1", config.getValue("foo.one", String.class));
assertEquals("v2", config.getValue("foo.two", String.class));
assertEquals("f1", config.getValue("foo.three", String.class));
assertFalse(config.getOptionalValue("foo.four", String.class).isPresent());
final SmallRyeConfig config = configBuilder()
.withSources(new PropertiesConfigSource(maps("foo", "default"), "source", 1000))
.withSources(new PropertiesConfigSource(maps("%foo.foo", "profile"), "source", 100))
.build();

assertEquals("default", config.getRawValue("foo"));
} finally {
System.clearProperty("quarkus-profile");
}
}

@Test
public void testBackwardCompatibleOrdinalProfile() {
void profileNoErrorOnExpansion() {
System.setProperty("quarkus-profile", "foo");
try {
final SmallRyeConfigBuilder builder = new SmallRyeConfigBuilder();
builder.withInterceptors(new QuarkusProfileConfigSourceInterceptor(ProfileManager.getActiveProfile()));
builder.withSources(new PropertiesConfigSource(new HashMap<String, String>() {
{
put("foo", "default");
}
}, "source", Integer.MAX_VALUE));
builder.withSources(new PropertiesConfigSource(new HashMap<String, String>() {
{
put("%foo.foo", "profile");
}
}, "source", Integer.MIN_VALUE));
final SmallRyeConfig config = builder.build();
cpr.registerConfig(config, classLoader);

assertEquals("profile", config.getValue("foo", String.class));
final SmallRyeConfig config = configBuilder("foo", "${noExpansionAvailable}", "%foo.foo", "profile").build();

assertEquals("profile", config.getRawValue("foo"));
} finally {
System.clearProperty("quarkus-profile");
}
}

@Test
public void profile() {
System.setProperty("quarkus-profile", "prof");
try {
final SmallRyeConfig config = buildConfig("my.prop", "1", "%prof.my.prop", "2");

assertEquals("2", config.getValue("my.prop", String.class));

assertEquals("my.prop", config.getConfigValue("my.prop").getName());
assertEquals("my.prop", config.getConfigValue("%prof.my.prop").getName());
} finally {
System.clearProperty("quarkus-profile");
}
}

@Test
public void profileOnly() {
System.setProperty("quarkus-profile", "prof");
try {
final SmallRyeConfig config = buildConfig("my.prop", "1", "%prof.my.prop", "2");

assertEquals("2", config.getRawValue("my.prop"));
} finally {
System.clearProperty("quarkus-profile");
}
}

@Test
public void fallback() {
System.setProperty("quarkus-profile", "prof");
try {
final SmallRyeConfig config = buildConfig("my.prop", "1");

assertEquals("1", config.getRawValue("my.prop"));
} finally {
System.clearProperty("quarkus-profile");
}
}

@Test
public void expressions() {
System.setProperty("quarkus-profile", "prof");
System.setProperty("my.prop", "1");
try {
final SmallRyeConfig config = buildConfig("my.prop", "1", "%prof.my.prop", "${my.prop}");

assertThrows(IllegalArgumentException.class, () -> config.getRawValue("my.prop"));
} finally {
System.clearProperty("quarkus-profile");
System.clearProperty("my.prop");
}
}

@Test
public void profileExpressions() {
System.setProperty("quarkus-profile", "prof");
System.setProperty("%prof.my.prop.profile", "2");
try {
final SmallRyeConfig config = buildConfig("my.prop", "1",
"%prof.my.prop", "${%prof.my.prop.profile}",
"%prof.my.prop.profile", "2");

assertEquals("2", config.getRawValue("my.prop"));
} finally {
System.clearProperty("quarkus-profile");
System.clearProperty("%prof.my.prop.profile");
}
}

@Test
public void customConfigProfile() {
System.setProperty("quarkus-profile", "prof");
try {
final SmallRyeConfig config = configBuilder()
.addDefaultSources()
.withSources(new PropertiesConfigSource(maps("my.prop", "1", "%prof.my.prop", "2"), "test", 100))
.build();

assertEquals("2", config.getValue("my.prop", String.class));
} finally {
System.clearProperty("quarkus-profile");
}
}

@Test
public void noConfigProfile() {
final SmallRyeConfig config = configBuilder()
.addDefaultSources()
.withSources(new PropertiesConfigSource(maps("my.prop", "1", "%prof.my.prop", "2"), "test", 100))
.withInterceptors(
new ProfileConfigSourceInterceptor("prof"),
new ExpressionConfigSourceInterceptor())
.build();

assertEquals("2", config.getRawValue("my.prop"));
}

@Test
public void priorityProfile() {
System.setProperty("quarkus-profile", "prof");
try {
final SmallRyeConfig config = configBuilder()
.addDefaultSources()
.withSources(new PropertiesConfigSource(maps("%prof.my.prop", "higher-profile"), "higher", 200))
.withSources(new PropertiesConfigSource(maps("my.prop", "lower", "%prof.my.prop", "lower-profile"), "lower",
100))
.build();

assertEquals("higher-profile", config.getRawValue("my.prop"));
} finally {
System.clearProperty("quarkus-profile");
}
}

@Test
public void priorityOverrideProfile() {
System.setProperty("quarkus-profile", "prof");
try {
final SmallRyeConfig config = new SmallRyeConfigBuilder()
.addDefaultSources()
.withSources(new PropertiesConfigSource(maps("my.prop", "higher"), "higher", 200))
.withSources(new PropertiesConfigSource(maps("my.prop", "lower", "%prof.my.prop", "lower-profile"), "lower",
100))
.build();

assertEquals("higher", config.getRawValue("my.prop"));
} finally {
System.clearProperty("quarkus-profile");
}
}

@Test
public void priorityProfileOverOriginal() {
System.setProperty("quarkus-profile", "prof");
try {
final SmallRyeConfig config = configBuilder()
.addDefaultSources()
.withSources(new PropertiesConfigSource(maps("my.prop", "higher", "%prof.my.prop", "higher-profile"),
"higher", 200))
.withSources(new PropertiesConfigSource(maps("my.prop", "lower", "%prof.my.prop", "lower-profile"), "lower",
100))
.build();

assertEquals("higher-profile", config.getRawValue("my.prop"));
} finally {
System.clearProperty("quarkus-profile");
}
}

@Test
public void propertyNames() {
System.setProperty("quarkus-profile", "prof");
try {
final SmallRyeConfig config = buildConfig("my.prop", "1", "%prof.my.prop", "2", "%prof.prof.only", "1");

assertEquals("2", config.getRawValue("my.prop"));
assertEquals("1", config.getRawValue("prof.only"));

final List<String> properties = StreamSupport.stream(config.getPropertyNames().spliterator(), false)
.collect(toList());
assertFalse(properties.contains("%prof.my.prop")); // We are removing profile properties in SmallRyeConfig and keep only the main name.
assertTrue(properties.contains("my.prop"));
assertTrue(properties.contains("prof.only"));
} finally {
System.clearProperty("quarkus-profile");
}
Expand Down