Search in sources :

Example 1 with SmallRyeConfigBuilder

use of io.smallrye.config.SmallRyeConfigBuilder in project smallrye-open-api by smallrye.

the class DeploymentProcessor method config.

/**
 * Creates the config from the microprofile-config.properties file in the application. The spec defines that the
 * config file may be present in two locations.
 */
private static OpenApiConfig config(final WebArchive war) {
    Optional<Node> microprofileConfig = Stream.of(ofNullable(war.get("/META-INF/microprofile-config.properties")), ofNullable(war.get("/WEB-INF/classes/META-INF/microprofile-config.properties"))).filter(Optional::isPresent).findFirst().flatMap(node -> node);
    if (!microprofileConfig.isPresent()) {
        return new OpenApiConfigImpl(ConfigProvider.getConfig());
    }
    Properties properties = new Properties();
    try (InputStreamReader reader = new InputStreamReader(microprofileConfig.get().getAsset().openStream(), UTF_8)) {
        properties.load(reader);
    } catch (IOException e) {
        e.printStackTrace();
    }
    SmallRyeConfig config = new SmallRyeConfigBuilder().addDefaultSources().addDefaultInterceptors().withSources(new PropertiesConfigSource(properties, "microprofile-config.properties")).build();
    return new OpenApiConfigImpl(config);
}
Also used : SmallRyeConfigBuilder(io.smallrye.config.SmallRyeConfigBuilder) PropertiesConfigSource(io.smallrye.config.PropertiesConfigSource) OpenApiConfigImpl(io.smallrye.openapi.api.OpenApiConfigImpl) Optional(java.util.Optional) InputStreamReader(java.io.InputStreamReader) SmallRyeConfig(io.smallrye.config.SmallRyeConfig) Node(org.jboss.shrinkwrap.api.Node) IOException(java.io.IOException) Properties(java.util.Properties)

Example 2 with SmallRyeConfigBuilder

use of io.smallrye.config.SmallRyeConfigBuilder in project smallrye-reactive-messaging by smallrye.

the class KafkaConfigMergeTest method testConfigMerge.

@Test
public void testConfigMerge() {
    Map<String, Object> globalMap = new HashMap<>();
    globalMap.put("a", "string");
    globalMap.put("b", 23);
    globalMap.put("c", "some-value");
    Map<String, String> conf = new HashMap<>();
    globalMap.put("d", "string");
    globalMap.put("e", 23);
    globalMap.put("c", "some-value-from-conf");
    SmallRyeConfig config = new SmallRyeConfigBuilder().addDefaultSources().withSources(new ConfigSource() {

        @Override
        public Set<String> getPropertyNames() {
            return conf.keySet();
        }

        @Override
        public String getValue(String propertyName) {
            return conf.get(propertyName);
        }

        @Override
        public String getName() {
            return "internal";
        }
    }).build();
    Config merged = ConfigHelper.merge(config, globalMap);
    assertThat(merged.getValue("a", String.class)).isEqualTo("string");
    assertThat(merged.getOptionalValue("a", String.class)).contains("string");
    assertThat(merged.getValue("b", Integer.class)).isEqualTo(23);
    assertThat(merged.getOptionalValue("b", Integer.class)).contains(23);
    assertThat(merged.getValue("d", String.class)).isEqualTo("string");
    assertThat(merged.getOptionalValue("d", String.class)).contains("string");
    assertThat(merged.getValue("e", Integer.class)).isEqualTo(23);
    assertThat(merged.getOptionalValue("e", Integer.class)).contains(23);
    assertThat(merged.getValue("c", String.class)).isEqualTo("some-value-from-conf");
    assertThat(merged.getOptionalValue("c", String.class)).contains("some-value-from-conf");
    assertThatThrownBy(() -> merged.getValue("missing", String.class)).isInstanceOf(NoSuchElementException.class);
    assertThatThrownBy(() -> merged.getValue("missing", Integer.class)).isInstanceOf(NoSuchElementException.class);
    assertThat(merged.getOptionalValue("missing", String.class)).isEmpty();
    assertThat(merged.getOptionalValue("missing", Integer.class)).isEmpty();
    assertThatThrownBy(() -> merged.getValue("a", Integer.class)).isInstanceOf(IllegalArgumentException.class);
    assertThatThrownBy(() -> merged.getValue("d", Integer.class)).isInstanceOf(IllegalArgumentException.class);
}
Also used : ConfigSource(org.eclipse.microprofile.config.spi.ConfigSource) SmallRyeConfigBuilder(io.smallrye.config.SmallRyeConfigBuilder) HashMap(java.util.HashMap) SmallRyeConfig(io.smallrye.config.SmallRyeConfig) Config(org.eclipse.microprofile.config.Config) SmallRyeConfig(io.smallrye.config.SmallRyeConfig) Test(org.junit.jupiter.api.Test)

Example 3 with SmallRyeConfigBuilder

use of io.smallrye.config.SmallRyeConfigBuilder in project smallrye-reactive-messaging by smallrye.

the class JsonHelperTest method createTestConfig.

private ConnectorConfig createTestConfig(ConfigSource... extraSources) {
    Map<String, String> channelConnector = Collections.singletonMap("mp.messaging.incoming.testChannel.connector", "smallrye-kafka");
    SmallRyeConfig config = new SmallRyeConfigBuilder().addDefaultSources().withSources(getConfigSourceFromMap(channelConnector, 0)).withSources(extraSources).build();
    return new TestConnectorConfig(config);
}
Also used : SmallRyeConfigBuilder(io.smallrye.config.SmallRyeConfigBuilder) SmallRyeConfig(io.smallrye.config.SmallRyeConfig)

Example 4 with SmallRyeConfigBuilder

use of io.smallrye.config.SmallRyeConfigBuilder in project jqa-core-framework by buschmais.

the class ConfigurationLoaderImpl method load.

@Override
public Configuration load(File configurationDirectory, ConfigSource... configSources) {
    List<ConfigSource> yamlConfigSources = getYamlConfigSources(configurationDirectory);
    SmallRyeConfig config = new SmallRyeConfigBuilder().withMapping(Configuration.class).addDefaultSources().withSources(yamlConfigSources).withSources(configSources).withValidateUnknown(false).build();
    return config.getConfigMapping(Configuration.class);
}
Also used : ConfigSource(org.eclipse.microprofile.config.spi.ConfigSource) YamlConfigSource(io.smallrye.config.source.yaml.YamlConfigSource) SmallRyeConfigBuilder(io.smallrye.config.SmallRyeConfigBuilder) Configuration(com.buschmais.jqassistant.core.configuration.api.Configuration) SmallRyeConfig(io.smallrye.config.SmallRyeConfig)

Example 5 with SmallRyeConfigBuilder

use of io.smallrye.config.SmallRyeConfigBuilder in project smallrye-reactive-messaging by smallrye.

the class ConfigHelperTest method testMergedConfig.

@Test
public void testMergedConfig() {
    Map<String, String> cfg = new HashMap<>();
    cfg.put("test", "test");
    cfg.put("ov-2", "ov-cfg");
    Map<String, Object> channelSpecific = new HashMap<>();
    channelSpecific.put("spec-1", "spec");
    channelSpecific.put("spec-2", 2);
    channelSpecific.put("ov", "ov-spec");
    channelSpecific.put("ov-2", "ov-spec");
    Map<String, Object> global = new HashMap<>();
    global.put("global-1", "global");
    global.put("global-2", 3);
    global.put("ov", "ov-global");
    global.put("ov-2", "ov-global");
    SmallRyeConfigBuilder test = new SmallRyeConfigBuilder().withSources(new PropertiesConfigSource(cfg, "test", 500));
    Config config = ConfigHelper.merge(test.build(), channelSpecific, global);
    assertThat(config.getValue("test", String.class)).isEqualTo("test");
    Assertions.assertThatThrownBy(() -> config.getValue("test", Double.class)).isInstanceOf(IllegalArgumentException.class);
    assertThat(config.getOptionalValue("test", String.class)).hasValue("test");
    assertThat(config.getOptionalValue("spec-1", String.class)).hasValue("spec");
    assertThat(config.getValue("spec-1", String.class)).isEqualTo("spec");
    Assertions.assertThatThrownBy(() -> config.getValue("spec-1", Double.class)).isInstanceOf(IllegalArgumentException.class);
    assertThat(config.getValue("spec-2", Integer.class)).isEqualTo(2);
    assertThat(config.getOptionalValue("global-1", String.class)).hasValue("global");
    assertThat(config.getValue("global-1", String.class)).isEqualTo("global");
    Assertions.assertThatThrownBy(() -> config.getValue("global-1", Double.class)).isInstanceOf(IllegalArgumentException.class);
    assertThat(config.getValue("global-2", Integer.class)).isEqualTo(3);
    assertThat(config.getOptionalValue("ov", String.class)).hasValue("ov-spec");
    assertThat(config.getOptionalValue("ov-2", String.class)).hasValue("ov-cfg");
    assertThat(config.getOptionalValue("missing", String.class)).isEmpty();
    assertThatThrownBy(() -> config.getValue("missing", Integer.class)).isInstanceOf(NoSuchElementException.class);
    assertThat(config.getConfigValue("test").getValue()).isEqualTo("test");
    assertThat(config.getConfigValue("global-1").getValue()).isNull();
    assertThat(config.getPropertyNames()).containsExactlyInAnyOrder("test", "ov", "ov-2", "global-1", "global-2", "spec-1", "spec-2");
    assertThat(config.getConfigSources()).hasSize(2);
}
Also used : SmallRyeConfigBuilder(io.smallrye.config.SmallRyeConfigBuilder) PropertiesConfigSource(io.smallrye.config.PropertiesConfigSource) HashMap(java.util.HashMap) Config(org.eclipse.microprofile.config.Config) Test(org.junit.jupiter.api.Test)

Aggregations

SmallRyeConfigBuilder (io.smallrye.config.SmallRyeConfigBuilder)7 SmallRyeConfig (io.smallrye.config.SmallRyeConfig)4 HashMap (java.util.HashMap)4 PropertiesConfigSource (io.smallrye.config.PropertiesConfigSource)3 Config (org.eclipse.microprofile.config.Config)3 ConfigSource (org.eclipse.microprofile.config.spi.ConfigSource)3 Test (org.junit.jupiter.api.Test)3 Configuration (com.buschmais.jqassistant.core.configuration.api.Configuration)1 YamlConfigSource (io.smallrye.config.source.yaml.YamlConfigSource)1 OpenApiConfigImpl (io.smallrye.openapi.api.OpenApiConfigImpl)1 IOException (java.io.IOException)1 InputStreamReader (java.io.InputStreamReader)1 Map (java.util.Map)1 Optional (java.util.Optional)1 Properties (java.util.Properties)1 Set (java.util.Set)1 Node (org.jboss.shrinkwrap.api.Node)1 BeforeEach (org.junit.jupiter.api.BeforeEach)1