Skip to content

Commit

Permalink
Add test for expressions and factories
Browse files Browse the repository at this point in the history
  • Loading branch information
radcortez committed Dec 4, 2023
1 parent 5a96de2 commit 56f6e27
Showing 1 changed file with 35 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -40,4 +42,37 @@ public Iterable<ConfigSource> 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<ConfigSource> 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));
}
}
}

0 comments on commit 56f6e27

Please sign in to comment.