From dc9de96f7b82b9ca97532cf985a2fe8f9e11e7fe Mon Sep 17 00:00:00 2001 From: Stian Thorgersen Date: Fri, 19 Jul 2024 14:45:53 +0200 Subject: [PATCH] PoC config (#31434) * Adding config to provide option to select supplier types Signed-off-by: wojnarfilip * Proposed cleanup Signed-off-by: stianst --------- Signed-off-by: wojnarfilip Signed-off-by: stianst Co-authored-by: wojnarfilip --- .../test/framework/config/Config.java | 66 ++++++++----------- .../test/framework/injection/Registry.java | 2 +- 2 files changed, 27 insertions(+), 41 deletions(-) diff --git a/test-poc/framework/src/main/java/org/keycloak/test/framework/config/Config.java b/test-poc/framework/src/main/java/org/keycloak/test/framework/config/Config.java index bcc967512d..db71242bf4 100644 --- a/test-poc/framework/src/main/java/org/keycloak/test/framework/config/Config.java +++ b/test-poc/framework/src/main/java/org/keycloak/test/framework/config/Config.java @@ -1,57 +1,43 @@ package org.keycloak.test.framework.config; +import io.smallrye.config.DotEnvConfigSourceProvider; +import io.smallrye.config.PropertiesConfigSource; +import io.smallrye.config.SmallRyeConfig; +import io.smallrye.config.SmallRyeConfigBuilder; +import org.eclipse.microprofile.config.spi.ConfigSource; import org.keycloak.test.framework.injection.ValueTypeAlias; import java.io.File; -import java.io.FileInputStream; -import java.io.IOException; -import java.util.Properties; public class Config { - private final static Config instance = new Config(); + private static final SmallRyeConfig config = initConfig(); - private Properties localEnv = new Properties(); - - private Config() { - File envFile = new File(".env"); - if (envFile.isFile()) { - try { - localEnv.load(new FileInputStream(envFile)); - } catch (IOException e) { - throw new RuntimeException(e); - } - } + public static String getSelectedSupplier(Class valueType) { + return config.getOptionalValue("kc.test." + ValueTypeAlias.getAlias(valueType), String.class).orElse(null); } - public static Config getInstance() { - return instance; + private static SmallRyeConfig initConfig() { + SmallRyeConfigBuilder configBuilder = new SmallRyeConfigBuilder() + .addDefaultSources() + .addDefaultInterceptors() + .withSources(new DotEnvConfigSourceProvider()); + + ConfigSource testConfigSource = initTestConfigSource(); + if (testConfigSource != null) { + configBuilder.withSources(testConfigSource); + } + + return configBuilder.build(); } - public String getSelectedSupplier(Class valueType) { - return getString("kc-test-" + ValueTypeAlias.getAlias(valueType)); - } - - public String getString(String key) { - String propKey = key.replace('-', '.'); - String envKey = key.replace('-', '_').toUpperCase(); - - String value = System.getProperty(propKey); - if (value != null) { - return value; + private static ConfigSource initTestConfigSource() { + try { + String testConfigFile = System.getProperty("kc.test.config", System.getenv("KC_TEST_CONFIG")); + return testConfigFile != null ? new PropertiesConfigSource(new File(testConfigFile).toURI().toURL()) : null; + } catch (Exception e) { + throw new RuntimeException(e); } - - value = System.getenv(envKey); - if (value != null) { - return value; - } - - value = localEnv.getProperty(envKey); - if (value != null) { - return value; - } - - return null; } } diff --git a/test-poc/framework/src/main/java/org/keycloak/test/framework/injection/Registry.java b/test-poc/framework/src/main/java/org/keycloak/test/framework/injection/Registry.java index 23726e930e..b4f35eaef8 100644 --- a/test-poc/framework/src/main/java/org/keycloak/test/framework/injection/Registry.java +++ b/test-poc/framework/src/main/java/org/keycloak/test/framework/injection/Registry.java @@ -251,7 +251,7 @@ public class Registry { Class supplierValueType = supplier.getValueType(); if (!loadedValueTypes.contains(supplierValueType)) { - String requestedSupplier = Config.getInstance().getSelectedSupplier(supplierValueType); + String requestedSupplier = Config.getSelectedSupplier(supplierValueType); if (requestedSupplier != null) { if (requestedSupplier.equals(supplier.getAlias())) { shouldAdd = true;