From c2207e65ba0c9385b33d8a0bfb1ff92e61e4ac3b Mon Sep 17 00:00:00 2001 From: Miquel Simon Date: Fri, 5 Jul 2024 12:20:01 +0200 Subject: [PATCH] DRAFT. Adding database configuration. Signed-off-by: Miquel Simon --- .../framework/KeycloakIntegrationTest.java | 3 +++ .../framework/database/DatabaseConfig.java | 27 +++++++++++++++++++ .../database/DefaultDatabaseConfig.java | 11 ++++++++ .../server/EmbeddedKeycloakTestServer.java | 15 +++++++++-- .../framework/server/KeycloakTestServer.java | 4 ++- .../server/KeycloakTestServerSupplier.java | 5 +++- .../server/RemoteKeycloakTestServer.java | 4 ++- 7 files changed, 64 insertions(+), 5 deletions(-) create mode 100644 test-poc/framework/src/main/java/org/keycloak/test/framework/database/DatabaseConfig.java create mode 100644 test-poc/framework/src/main/java/org/keycloak/test/framework/database/DefaultDatabaseConfig.java diff --git a/test-poc/framework/src/main/java/org/keycloak/test/framework/KeycloakIntegrationTest.java b/test-poc/framework/src/main/java/org/keycloak/test/framework/KeycloakIntegrationTest.java index f43bca5dde0b..6b2ec6a0b71f 100644 --- a/test-poc/framework/src/main/java/org/keycloak/test/framework/KeycloakIntegrationTest.java +++ b/test-poc/framework/src/main/java/org/keycloak/test/framework/KeycloakIntegrationTest.java @@ -1,5 +1,7 @@ package org.keycloak.test.framework; +import org.keycloak.test.framework.database.DatabaseConfig; +import org.keycloak.test.framework.database.DefaultDatabaseConfig; import org.keycloak.test.framework.server.DefaultKeycloakTestServerConfig; import org.keycloak.test.framework.server.KeycloakTestServerConfig; @@ -13,5 +15,6 @@ public @interface KeycloakIntegrationTest { Class config() default DefaultKeycloakTestServerConfig.class; + Class dbconfig() default DefaultDatabaseConfig.class; } 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/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..08d7a0576655 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,8 @@ 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.DefaultDatabaseConfig; import org.keycloak.test.framework.injection.InstanceWrapper; import org.keycloak.test.framework.injection.LifeCycle; import org.keycloak.test.framework.injection.Registry; @@ -22,11 +24,12 @@ public Class getAnnotationClass() { @Override public InstanceWrapper getValue(Registry registry, KeycloakIntegrationTest annotation) { KeycloakTestServerConfig serverConfig = SupplierHelpers.getInstance(annotation.config()); + DatabaseConfig databaseConfig = annotation.dbconfig() != null ? SupplierHelpers.getInstance(annotation.dbconfig()) : new DefaultDatabaseConfig(); // 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) { }