diff --git a/test-poc/framework/src/main/java/org/keycloak/test/framework/database/DatabaseConfig.java b/test-poc/framework/src/main/java/org/keycloak/test/framework/database/DatabaseConfig.java new file mode 100644 index 000000000000..9b3b2497941a --- /dev/null +++ b/test-poc/framework/src/main/java/org/keycloak/test/framework/database/DatabaseConfig.java @@ -0,0 +1,27 @@ +package org.keycloak.test.framework.database; + +import java.util.Collections; +import java.util.Map; + +public interface DatabaseConfig { + + default String vendor() { + return ""; + } + + default String containerImage() { + return ""; + } + + default String urlHost() { + return ""; + } + + default String username() { + return ""; + } + + default String password() { + return ""; + } +} diff --git a/test-poc/framework/src/main/java/org/keycloak/test/framework/database/DatabaseSupplier.java b/test-poc/framework/src/main/java/org/keycloak/test/framework/database/DatabaseSupplier.java new file mode 100644 index 000000000000..6e99041647b8 --- /dev/null +++ b/test-poc/framework/src/main/java/org/keycloak/test/framework/database/DatabaseSupplier.java @@ -0,0 +1,36 @@ +package org.keycloak.test.framework.database; + +import org.keycloak.test.framework.injection.InstanceWrapper; +import org.keycloak.test.framework.injection.LifeCycle; +import org.keycloak.test.framework.injection.Registry; +import org.keycloak.test.framework.injection.Supplier; + +import java.lang.annotation.Annotation; + +public class DatabaseSupplier implements Supplier { + @Override + public Class getAnnotationClass() { + return null; + } + + @Override + public Class getValueType() { + return DatabaseConfig.class; + } + + @Override + public InstanceWrapper getValue(Registry registry, Annotation annotation) { + DatabaseConfig databaseConfig = new DefaultDatabaseConfig(); // TODO: Get config from system property + return new InstanceWrapper<>(this, annotation, databaseConfig); + } + + @Override + public LifeCycle getLifeCycle() { + return LifeCycle.GLOBAL; + } + + @Override + public boolean compatible(InstanceWrapper a, InstanceWrapper b) { + return true; + } +} diff --git a/test-poc/framework/src/main/java/org/keycloak/test/framework/database/DefaultDatabaseConfig.java b/test-poc/framework/src/main/java/org/keycloak/test/framework/database/DefaultDatabaseConfig.java new file mode 100644 index 000000000000..cedf0d7c7840 --- /dev/null +++ b/test-poc/framework/src/main/java/org/keycloak/test/framework/database/DefaultDatabaseConfig.java @@ -0,0 +1,11 @@ +package org.keycloak.test.framework.database; + +public class DefaultDatabaseConfig implements DatabaseConfig { + + public static final String DEFAULT_DB_VENDOR = "dev-mem"; + + @Override + public String vendor() { + return DEFAULT_DB_VENDOR; + } +} diff --git a/test-poc/framework/src/main/java/org/keycloak/test/framework/server/EmbeddedKeycloakTestServer.java b/test-poc/framework/src/main/java/org/keycloak/test/framework/server/EmbeddedKeycloakTestServer.java index 98f54299bfea..90da9d6208a1 100644 --- a/test-poc/framework/src/main/java/org/keycloak/test/framework/server/EmbeddedKeycloakTestServer.java +++ b/test-poc/framework/src/main/java/org/keycloak/test/framework/server/EmbeddedKeycloakTestServer.java @@ -2,6 +2,8 @@ import org.keycloak.Keycloak; import org.keycloak.common.Version; +import org.keycloak.test.framework.database.DatabaseConfig; +import org.keycloak.test.framework.database.DefaultDatabaseConfig; import java.util.LinkedList; import java.util.List; @@ -12,19 +14,28 @@ public class EmbeddedKeycloakTestServer implements KeycloakTestServer { private Keycloak keycloak; @Override - public void start(KeycloakTestServerConfig serverConfig) { + public void start(KeycloakTestServerConfig serverConfig, DatabaseConfig databaseConfig) { serverConfig.adminUserName().ifPresent(username -> System.setProperty("keycloakAdmin", username)); serverConfig.adminUserPassword().ifPresent(password -> System.setProperty("keycloakAdminPassword", password)); List rawOptions = new LinkedList<>(); rawOptions.add("start-dev"); -// rawOptions.add("--db=dev-mem"); // TODO With dev-mem there's an issue as the H2 DB isn't stopped when restarting embedded server rawOptions.add("--cache=local"); if (!serverConfig.features().isEmpty()) { rawOptions.add("--features=" + String.join(",", serverConfig.features())); } + //rawOptions.add("--db=" + databaseConfig.vendor()); // TODO With dev-mem there's an issue as the H2 DB isn't stopped when restarting embedded server + if (!DefaultDatabaseConfig.DEFAULT_DB_VENDOR.equals(databaseConfig.vendor())) { + rawOptions.add("--db-url-host=" + databaseConfig.urlHost()); + rawOptions.add("--db-username=" + databaseConfig.username()); + rawOptions.add("--db-password=" + databaseConfig.password()); + + // Start database with testcontainers + } + + serverConfig.options().forEach((key, value) -> rawOptions.add("--" + key + "=" + value)); keycloak = Keycloak.builder() diff --git a/test-poc/framework/src/main/java/org/keycloak/test/framework/server/KeycloakTestServer.java b/test-poc/framework/src/main/java/org/keycloak/test/framework/server/KeycloakTestServer.java index e0bff4573738..e105cac2a9f8 100644 --- a/test-poc/framework/src/main/java/org/keycloak/test/framework/server/KeycloakTestServer.java +++ b/test-poc/framework/src/main/java/org/keycloak/test/framework/server/KeycloakTestServer.java @@ -1,8 +1,10 @@ package org.keycloak.test.framework.server; +import org.keycloak.test.framework.database.DatabaseConfig; + public interface KeycloakTestServer { - void start(KeycloakTestServerConfig serverConfig); + void start(KeycloakTestServerConfig serverConfig, DatabaseConfig databaseConfig); void stop(); diff --git a/test-poc/framework/src/main/java/org/keycloak/test/framework/server/KeycloakTestServerSupplier.java b/test-poc/framework/src/main/java/org/keycloak/test/framework/server/KeycloakTestServerSupplier.java index d82b2446cde8..bc2f436d1659 100644 --- a/test-poc/framework/src/main/java/org/keycloak/test/framework/server/KeycloakTestServerSupplier.java +++ b/test-poc/framework/src/main/java/org/keycloak/test/framework/server/KeycloakTestServerSupplier.java @@ -1,6 +1,9 @@ package org.keycloak.test.framework.server; import org.keycloak.test.framework.KeycloakIntegrationTest; +import org.keycloak.test.framework.database.DatabaseConfig; +import org.keycloak.test.framework.database.DatabaseSupplier; +import org.keycloak.test.framework.database.DefaultDatabaseConfig; import org.keycloak.test.framework.injection.InstanceWrapper; import org.keycloak.test.framework.injection.LifeCycle; import org.keycloak.test.framework.injection.Registry; @@ -22,11 +25,13 @@ public Class getAnnotationClass() { @Override public InstanceWrapper getValue(Registry registry, KeycloakIntegrationTest annotation) { KeycloakTestServerConfig serverConfig = SupplierHelpers.getInstance(annotation.config()); + DatabaseSupplier databaseSupplier = new DatabaseSupplier(); + DatabaseConfig databaseConfig = databaseSupplier.getValue(registry, annotation).getValue(); // RemoteKeycloakTestServer keycloakTestServer = new RemoteKeycloakTestServer(); EmbeddedKeycloakTestServer keycloakTestServer = new EmbeddedKeycloakTestServer(); - keycloakTestServer.start(serverConfig); + keycloakTestServer.start(serverConfig, databaseConfig); return new InstanceWrapper<>(this, annotation, keycloakTestServer); } diff --git a/test-poc/framework/src/main/java/org/keycloak/test/framework/server/RemoteKeycloakTestServer.java b/test-poc/framework/src/main/java/org/keycloak/test/framework/server/RemoteKeycloakTestServer.java index 62d5550227cd..78d2d2c5a37d 100644 --- a/test-poc/framework/src/main/java/org/keycloak/test/framework/server/RemoteKeycloakTestServer.java +++ b/test-poc/framework/src/main/java/org/keycloak/test/framework/server/RemoteKeycloakTestServer.java @@ -1,9 +1,11 @@ package org.keycloak.test.framework.server; +import org.keycloak.test.framework.database.DatabaseConfig; + public class RemoteKeycloakTestServer implements KeycloakTestServer { @Override - public void start(KeycloakTestServerConfig serverConfig) { + public void start(KeycloakTestServerConfig serverConfig, DatabaseConfig databaseConfig) { } diff --git a/test-poc/framework/src/main/resources/META-INF/services/org.keycloak.test.framework.injection.Supplier b/test-poc/framework/src/main/resources/META-INF/services/org.keycloak.test.framework.injection.Supplier index eb9d46618bb1..5caea55e6e01 100644 --- a/test-poc/framework/src/main/resources/META-INF/services/org.keycloak.test.framework.injection.Supplier +++ b/test-poc/framework/src/main/resources/META-INF/services/org.keycloak.test.framework.injection.Supplier @@ -2,4 +2,5 @@ org.keycloak.test.framework.admin.KeycloakAdminClientSupplier org.keycloak.test.framework.realm.ClientSupplier org.keycloak.test.framework.realm.RealmSupplier org.keycloak.test.framework.server.KeycloakTestServerSupplier -org.keycloak.test.framework.webdriver.FirefoxWebDriverSupplier \ No newline at end of file +org.keycloak.test.framework.webdriver.FirefoxWebDriverSupplier +org.keycloak.test.framework.database.DatabaseSupplier \ No newline at end of file