From 56f6e276bddacade18fa46e57b7440c1c73ee0ee Mon Sep 17 00:00:00 2001 From: Roberto Cortez Date: Mon, 4 Dec 2023 14:15:43 +0000 Subject: [PATCH] Add test for expressions and factories --- .../config/ConfigSourceFactoryTest.java | 35 +++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/implementation/src/test/java/io/smallrye/config/ConfigSourceFactoryTest.java b/implementation/src/test/java/io/smallrye/config/ConfigSourceFactoryTest.java index 944ce9c47..46e7fd2ef 100644 --- a/implementation/src/test/java/io/smallrye/config/ConfigSourceFactoryTest.java +++ b/implementation/src/test/java/io/smallrye/config/ConfigSourceFactoryTest.java @@ -1,9 +1,11 @@ package io.smallrye.config; +import static io.smallrye.config.KeyValuesConfigSource.config; import static org.junit.jupiter.api.Assertions.assertEquals; import java.util.Collections; import java.util.HashMap; +import java.util.List; import java.util.Map; import org.eclipse.microprofile.config.spi.ConfigSource; @@ -40,4 +42,37 @@ public Iterable getConfigSources(final ConfigSourceContext context return Collections.singleton(new PropertiesConfigSource(properties, "", 100)); } } + + @Test + void expression() { + SmallRyeConfig config = new SmallRyeConfigBuilder() + .addDefaultInterceptors() + .withSources(new ExpressionConfigSourceFactory()) + .withSources(config("expression.value", "12${DEFAULT:}")) + .withSources(new EnvConfigSource(Map.of("DEFAULT", "34"), 100)) + .build(); + + assertEquals("1234", config.getRawValue("factory.expression")); + } + + @ConfigMapping(prefix = "expression") + interface Expression { + @WithDefault("${DEFAULT:}") + String value(); + } + + static class ExpressionConfigSourceFactory implements ConfigSourceFactory { + @Override + public Iterable getConfigSources(final ConfigSourceContext context) { + SmallRyeConfig config = new SmallRyeConfigBuilder() + .withSources(new ConfigSourceContext.ConfigSourceContextConfigSource(context)) + .withMapping(Expression.class) + .build(); + + Expression mapping = config.getConfigMapping(Expression.class); + assertEquals("1234", mapping.value()); + + return List.of(new PropertiesConfigSource(Map.of("factory.expression", mapping.value()), "", 100)); + } + } }