diff --git a/.gitignore b/.gitignore index 6756eda69d..82b47094e9 100644 --- a/.gitignore +++ b/.gitignore @@ -112,3 +112,5 @@ node_modules # JENV .java-version + +.env 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 new file mode 100644 index 0000000000..bcc967512d --- /dev/null +++ b/test-poc/framework/src/main/java/org/keycloak/test/framework/config/Config.java @@ -0,0 +1,57 @@ +package org.keycloak.test.framework.config; + +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 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 Config getInstance() { + return instance; + } + + 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; + } + + 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/database/DevFileDatabaseSupplier.java b/test-poc/framework/src/main/java/org/keycloak/test/framework/database/DevFileDatabaseSupplier.java index 5dcec98242..5ac8ac6540 100644 --- a/test-poc/framework/src/main/java/org/keycloak/test/framework/database/DevFileDatabaseSupplier.java +++ b/test-poc/framework/src/main/java/org/keycloak/test/framework/database/DevFileDatabaseSupplier.java @@ -2,10 +2,16 @@ package org.keycloak.test.framework.database; public class DevFileDatabaseSupplier extends AbstractDatabaseSupplier { + public static final String VENDOR = "dev-file"; + @Override TestDatabase getTestDatabase() { - DatabaseConfig databaseConfig = new DatabaseConfig().vendor("dev-file"); + DatabaseConfig databaseConfig = new DatabaseConfig().vendor(VENDOR); return new TestDatabase(databaseConfig); } + @Override + public String getAlias() { + return VENDOR; + } } diff --git a/test-poc/framework/src/main/java/org/keycloak/test/framework/database/DevMemDatabaseSupplier.java b/test-poc/framework/src/main/java/org/keycloak/test/framework/database/DevMemDatabaseSupplier.java index 9c14651b23..063f7cb319 100644 --- a/test-poc/framework/src/main/java/org/keycloak/test/framework/database/DevMemDatabaseSupplier.java +++ b/test-poc/framework/src/main/java/org/keycloak/test/framework/database/DevMemDatabaseSupplier.java @@ -2,10 +2,16 @@ package org.keycloak.test.framework.database; public class DevMemDatabaseSupplier extends AbstractDatabaseSupplier { + public static final String VENDOR = "dev-mem"; + @Override TestDatabase getTestDatabase() { - DatabaseConfig databaseConfig = new DatabaseConfig().vendor("dev-mem"); + DatabaseConfig databaseConfig = new DatabaseConfig().vendor(VENDOR); return new TestDatabase(databaseConfig); } + @Override + public String getAlias() { + return VENDOR; + } } diff --git a/test-poc/framework/src/main/java/org/keycloak/test/framework/database/PostgresDatabaseSupplier.java b/test-poc/framework/src/main/java/org/keycloak/test/framework/database/PostgresDatabaseSupplier.java index 2c7540c02f..afc5135b83 100644 --- a/test-poc/framework/src/main/java/org/keycloak/test/framework/database/PostgresDatabaseSupplier.java +++ b/test-poc/framework/src/main/java/org/keycloak/test/framework/database/PostgresDatabaseSupplier.java @@ -2,14 +2,20 @@ package org.keycloak.test.framework.database; public class PostgresDatabaseSupplier extends AbstractDatabaseSupplier { + public static final String VENDOR = "postgres"; + @Override TestDatabase getTestDatabase() { DatabaseConfig databaseConfig = new DatabaseConfig() - .vendor("postgres") + .vendor(VENDOR) .username("keycloak") .password("keycloak") .containerImage("the-postgres-container:the-version"); return new TestDatabase(databaseConfig); } + @Override + public String getAlias() { + return VENDOR; + } } 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 076cdb5ede..c0ab240370 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 @@ -2,9 +2,11 @@ package org.keycloak.test.framework.injection; import org.jboss.logging.Logger; import org.junit.jupiter.api.extension.ExtensionContext; +import org.keycloak.test.framework.config.Config; import java.lang.annotation.Annotation; import java.lang.reflect.Field; +import java.util.HashSet; import java.util.Iterator; import java.util.LinkedList; import java.util.List; @@ -209,10 +211,48 @@ public class Registry { } private void loadSuppliers() { - ServiceLoader.load(Supplier.class).iterator().forEachRemaining(suppliers::add); + Iterator supplierIterator = ServiceLoader.load(Supplier.class).iterator(); + Set loadedValueTypes = new HashSet<>(); + Set skippedSuppliers = new HashSet<>(); + + while (supplierIterator.hasNext()) { + Supplier supplier = supplierIterator.next(); + boolean shouldAdd = false; + Class supplierValueType = supplier.getValueType(); + + if (!loadedValueTypes.contains(supplierValueType)) { + String requestedSupplier = Config.getInstance().getSelectedSupplier(supplierValueType); + if (requestedSupplier != null) { + if (requestedSupplier.equals(supplier.getAlias())) { + shouldAdd = true; + } + } else { + shouldAdd = true; + } + } + + if (shouldAdd) { + suppliers.add(supplier); + loadedValueTypes.add(supplierValueType); + } else { + skippedSuppliers.add(supplier); + } + } if (LOGGER.isTraceEnabled()) { - LOGGER.tracev("Suppliers: {0}", suppliers.stream().map(s -> s.getClass().getSimpleName()).collect(Collectors.joining(", "))); + StringBuilder loaded = new StringBuilder(); + loaded.append("Loaded suppliers:"); + for (Supplier s : suppliers) { + loaded.append("\n - " + ValueTypeAlias.getAlias(s.getValueType()) + " --> " + s.getAlias()); + } + LOGGER.trace(loaded.toString()); + + StringBuilder skipped = new StringBuilder(); + skipped.append("Skipped suppliers:"); + for (Supplier s : skippedSuppliers) { + skipped.append("\n - " + ValueTypeAlias.getAlias(s.getValueType()) + " --> " + s.getAlias()); + } + LOGGER.trace(skipped.toString()); } } diff --git a/test-poc/framework/src/main/java/org/keycloak/test/framework/injection/Supplier.java b/test-poc/framework/src/main/java/org/keycloak/test/framework/injection/Supplier.java index 8280413e2b..acc194b996 100644 --- a/test-poc/framework/src/main/java/org/keycloak/test/framework/injection/Supplier.java +++ b/test-poc/framework/src/main/java/org/keycloak/test/framework/injection/Supplier.java @@ -14,4 +14,8 @@ public interface Supplier { default void close(T instance) {} + default String getAlias() { + return getClass().getSimpleName(); + } + } diff --git a/test-poc/framework/src/main/java/org/keycloak/test/framework/injection/ValueTypeAlias.java b/test-poc/framework/src/main/java/org/keycloak/test/framework/injection/ValueTypeAlias.java new file mode 100644 index 0000000000..01e2713338 --- /dev/null +++ b/test-poc/framework/src/main/java/org/keycloak/test/framework/injection/ValueTypeAlias.java @@ -0,0 +1,25 @@ +package org.keycloak.test.framework.injection; + +import org.keycloak.test.framework.database.TestDatabase; +import org.keycloak.test.framework.server.KeycloakTestServer; +import org.openqa.selenium.WebDriver; + +import java.util.Map; + +public class ValueTypeAlias { + + private static final Map aliases = Map.of( + WebDriver.class, "browser", + KeycloakTestServer.class, "server", + TestDatabase.class, "database" + ); + + public static String getAlias(Class clazz) { + String alias = aliases.get(clazz); + if (alias == null) { + alias = clazz.getSimpleName(); + } + return alias; + } + +} diff --git a/test-poc/framework/src/main/java/org/keycloak/test/framework/server/DistributionKeycloakTestServerSupplier.java b/test-poc/framework/src/main/java/org/keycloak/test/framework/server/DistributionKeycloakTestServerSupplier.java index 96a7dab29b..e12fe2f86f 100644 --- a/test-poc/framework/src/main/java/org/keycloak/test/framework/server/DistributionKeycloakTestServerSupplier.java +++ b/test-poc/framework/src/main/java/org/keycloak/test/framework/server/DistributionKeycloakTestServerSupplier.java @@ -12,4 +12,8 @@ public class DistributionKeycloakTestServerSupplier extends AbstractKeycloakTest return true; } + @Override + public String getAlias() { + return "distribution"; + } } diff --git a/test-poc/framework/src/main/java/org/keycloak/test/framework/server/EmbeddedKeycloakTestServerSupplier.java b/test-poc/framework/src/main/java/org/keycloak/test/framework/server/EmbeddedKeycloakTestServerSupplier.java index fc87d4e6f2..6939c89789 100644 --- a/test-poc/framework/src/main/java/org/keycloak/test/framework/server/EmbeddedKeycloakTestServerSupplier.java +++ b/test-poc/framework/src/main/java/org/keycloak/test/framework/server/EmbeddedKeycloakTestServerSupplier.java @@ -12,4 +12,8 @@ public class EmbeddedKeycloakTestServerSupplier extends AbstractKeycloakTestServ return true; } + @Override + public String getAlias() { + return "embedded"; + } } diff --git a/test-poc/framework/src/main/java/org/keycloak/test/framework/server/RemoteKeycloakTestServerSupplier.java b/test-poc/framework/src/main/java/org/keycloak/test/framework/server/RemoteKeycloakTestServerSupplier.java index 30d61aa7e2..44bfba9907 100644 --- a/test-poc/framework/src/main/java/org/keycloak/test/framework/server/RemoteKeycloakTestServerSupplier.java +++ b/test-poc/framework/src/main/java/org/keycloak/test/framework/server/RemoteKeycloakTestServerSupplier.java @@ -12,4 +12,8 @@ public class RemoteKeycloakTestServerSupplier extends AbstractKeycloakTestServer return false; } + @Override + public String getAlias() { + return "remote"; + } } diff --git a/test-poc/framework/src/main/java/org/keycloak/test/framework/webdriver/ChromeWebDriverSupplier.java b/test-poc/framework/src/main/java/org/keycloak/test/framework/webdriver/ChromeWebDriverSupplier.java index 76b4f8ccd4..2d88def2d5 100644 --- a/test-poc/framework/src/main/java/org/keycloak/test/framework/webdriver/ChromeWebDriverSupplier.java +++ b/test-poc/framework/src/main/java/org/keycloak/test/framework/webdriver/ChromeWebDriverSupplier.java @@ -34,4 +34,10 @@ public class ChromeWebDriverSupplier implements Supplier