PoC config (#31434)
* Adding config to provide option to select supplier types Signed-off-by: wojnarfilip <fwojnar@redhat.com> * Proposed cleanup Signed-off-by: stianst <stianst@gmail.com> --------- Signed-off-by: wojnarfilip <fwojnar@redhat.com> Signed-off-by: stianst <stianst@gmail.com> Co-authored-by: wojnarfilip <fwojnar@redhat.com>
This commit is contained in:
parent
02e3339762
commit
dc9de96f7b
2 changed files with 27 additions and 41 deletions
|
@ -1,57 +1,43 @@
|
||||||
package org.keycloak.test.framework.config;
|
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 org.keycloak.test.framework.injection.ValueTypeAlias;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.FileInputStream;
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.util.Properties;
|
|
||||||
|
|
||||||
public class Config {
|
public class Config {
|
||||||
|
|
||||||
private final static Config instance = new Config();
|
private static final SmallRyeConfig config = initConfig();
|
||||||
|
|
||||||
private Properties localEnv = new Properties();
|
public static String getSelectedSupplier(Class<?> valueType) {
|
||||||
|
return config.getOptionalValue("kc.test." + ValueTypeAlias.getAlias(valueType), String.class).orElse(null);
|
||||||
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 Config getInstance() {
|
private static SmallRyeConfig initConfig() {
|
||||||
return instance;
|
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) {
|
private static ConfigSource initTestConfigSource() {
|
||||||
return getString("kc-test-" + ValueTypeAlias.getAlias(valueType));
|
try {
|
||||||
}
|
String testConfigFile = System.getProperty("kc.test.config", System.getenv("KC_TEST_CONFIG"));
|
||||||
|
return testConfigFile != null ? new PropertiesConfigSource(new File(testConfigFile).toURI().toURL()) : null;
|
||||||
public String getString(String key) {
|
} catch (Exception e) {
|
||||||
String propKey = key.replace('-', '.');
|
throw new RuntimeException(e);
|
||||||
String envKey = key.replace('-', '_').toUpperCase();
|
|
||||||
|
|
||||||
String value = System.getProperty(propKey);
|
|
||||||
if (value != null) {
|
|
||||||
return value;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
value = System.getenv(envKey);
|
|
||||||
if (value != null) {
|
|
||||||
return value;
|
|
||||||
}
|
|
||||||
|
|
||||||
value = localEnv.getProperty(envKey);
|
|
||||||
if (value != null) {
|
|
||||||
return value;
|
|
||||||
}
|
|
||||||
|
|
||||||
return null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -251,7 +251,7 @@ public class Registry {
|
||||||
Class supplierValueType = supplier.getValueType();
|
Class supplierValueType = supplier.getValueType();
|
||||||
|
|
||||||
if (!loadedValueTypes.contains(supplierValueType)) {
|
if (!loadedValueTypes.contains(supplierValueType)) {
|
||||||
String requestedSupplier = Config.getInstance().getSelectedSupplier(supplierValueType);
|
String requestedSupplier = Config.getSelectedSupplier(supplierValueType);
|
||||||
if (requestedSupplier != null) {
|
if (requestedSupplier != null) {
|
||||||
if (requestedSupplier.equals(supplier.getAlias())) {
|
if (requestedSupplier.equals(supplier.getAlias())) {
|
||||||
shouldAdd = true;
|
shouldAdd = true;
|
||||||
|
|
Loading…
Reference in a new issue