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..e9d78fcd54db --- /dev/null +++ b/test-poc/framework/src/main/java/org/keycloak/test/framework/database/DatabaseConfig.java @@ -0,0 +1,31 @@ +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 ""; + } + + default boolean isExternal() { + return false; + } +} 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..10600fa72a57 --- /dev/null +++ b/test-poc/framework/src/main/java/org/keycloak/test/framework/database/DatabaseSupplier.java @@ -0,0 +1,37 @@ +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 abstract class DatabaseSupplier implements Supplier { + + protected static TestDatabase testDatabase = new TestDatabase(new DevMemDatabaseConfig()); + + @Override + public Class getAnnotationClass() { + return null; + } + + @Override + public Class getValueType() { + return TestDatabase.class; + } + + @Override + public InstanceWrapper getValue(Registry registry, Annotation annotation) { + return new InstanceWrapper<>(this, annotation, testDatabase, LifeCycle.GLOBAL); + } + + @Override + public boolean compatible(InstanceWrapper a, InstanceWrapper b) { + return true; + } + + public TestDatabase getTestDatabase() { + return testDatabase; + } +} diff --git a/test-poc/framework/src/main/java/org/keycloak/test/framework/database/DevMemDatabaseConfig.java b/test-poc/framework/src/main/java/org/keycloak/test/framework/database/DevMemDatabaseConfig.java new file mode 100644 index 000000000000..feeac6abae5d --- /dev/null +++ b/test-poc/framework/src/main/java/org/keycloak/test/framework/database/DevMemDatabaseConfig.java @@ -0,0 +1,4 @@ +package org.keycloak.test.framework.database; + +public class DevMemDatabaseConfig implements DatabaseConfig { +} 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 new file mode 100644 index 000000000000..674051a56608 --- /dev/null +++ b/test-poc/framework/src/main/java/org/keycloak/test/framework/database/DevMemDatabaseSupplier.java @@ -0,0 +1,4 @@ +package org.keycloak.test.framework.database; + +public class DevMemDatabaseSupplier extends DatabaseSupplier { +} diff --git a/test-poc/framework/src/main/java/org/keycloak/test/framework/database/ExternalDatabaseConfig.java b/test-poc/framework/src/main/java/org/keycloak/test/framework/database/ExternalDatabaseConfig.java new file mode 100644 index 000000000000..0512ec5748d0 --- /dev/null +++ b/test-poc/framework/src/main/java/org/keycloak/test/framework/database/ExternalDatabaseConfig.java @@ -0,0 +1,8 @@ +package org.keycloak.test.framework.database; + +public class ExternalDatabaseConfig implements DatabaseConfig { + @Override + public boolean isExternal() { + return true; + } +} diff --git a/test-poc/framework/src/main/java/org/keycloak/test/framework/database/TestDatabase.java b/test-poc/framework/src/main/java/org/keycloak/test/framework/database/TestDatabase.java new file mode 100644 index 000000000000..78e8f6aa18f6 --- /dev/null +++ b/test-poc/framework/src/main/java/org/keycloak/test/framework/database/TestDatabase.java @@ -0,0 +1,22 @@ +package org.keycloak.test.framework.database; + +public class TestDatabase { + + private DatabaseConfig databaseConfig; + + public TestDatabase(DatabaseConfig config) { + databaseConfig = config; + } + + public void start() { + + } + + public void stop() { + + } + + public DatabaseConfig getDatabaseConfig() { + return databaseConfig; + } +} diff --git a/test-poc/framework/src/main/java/org/keycloak/test/framework/server/AbstractKeycloakTestServerSupplier.java b/test-poc/framework/src/main/java/org/keycloak/test/framework/server/AbstractKeycloakTestServerSupplier.java index 17a9117fd6f3..a4aa3feff3b7 100644 --- a/test-poc/framework/src/main/java/org/keycloak/test/framework/server/AbstractKeycloakTestServerSupplier.java +++ b/test-poc/framework/src/main/java/org/keycloak/test/framework/server/AbstractKeycloakTestServerSupplier.java @@ -1,6 +1,8 @@ package org.keycloak.test.framework.server; import org.keycloak.test.framework.KeycloakIntegrationTest; +import org.keycloak.test.framework.database.DatabaseSupplier; +import org.keycloak.test.framework.database.DevMemDatabaseSupplier; import org.keycloak.test.framework.injection.InstanceWrapper; import org.keycloak.test.framework.injection.LifeCycle; import org.keycloak.test.framework.injection.Registry; @@ -22,10 +24,11 @@ public Class getAnnotationClass() { @Override public InstanceWrapper getValue(Registry registry, KeycloakIntegrationTest annotation) { KeycloakTestServerConfig serverConfig = SupplierHelpers.getInstance(annotation.config()); + DatabaseSupplier databaseSupplier = new DevMemDatabaseSupplier(); KeycloakTestServer keycloakTestServer = getServer(); - keycloakTestServer.start(serverConfig); + keycloakTestServer.start(serverConfig, databaseSupplier.getTestDatabase()); return new InstanceWrapper<>(this, annotation, keycloakTestServer, LifeCycle.GLOBAL); } diff --git a/test-poc/framework/src/main/java/org/keycloak/test/framework/server/DistributionKeycloakTestServer.java b/test-poc/framework/src/main/java/org/keycloak/test/framework/server/DistributionKeycloakTestServer.java index 2c6b211a3b5c..7ba3954cd459 100644 --- a/test-poc/framework/src/main/java/org/keycloak/test/framework/server/DistributionKeycloakTestServer.java +++ b/test-poc/framework/src/main/java/org/keycloak/test/framework/server/DistributionKeycloakTestServer.java @@ -1,9 +1,11 @@ package org.keycloak.test.framework.server; +import org.keycloak.test.framework.database.TestDatabase; + public class DistributionKeycloakTestServer implements KeycloakTestServer { @Override - public void start(KeycloakTestServerConfig serverConfig) { + public void start(KeycloakTestServerConfig serverConfig, TestDatabase testDatabase) { throw new RuntimeException("Method not implemented!"); } 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..e818df644172 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.TestDatabase; import java.util.LinkedList; import java.util.List; @@ -11,20 +13,34 @@ public class EmbeddedKeycloakTestServer implements KeycloakTestServer { private Keycloak keycloak; + private TestDatabase database; + @Override - public void start(KeycloakTestServerConfig serverConfig) { + public void start(KeycloakTestServerConfig serverConfig, TestDatabase testDatabase) { + database = testDatabase; 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())); } + DatabaseConfig databaseConfig = database.getDatabaseConfig(); + + //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 (databaseConfig.isExternal()) { + rawOptions.add("--db-url-host=" + databaseConfig.urlHost()); + rawOptions.add("--db-username=" + databaseConfig.username()); + rawOptions.add("--db-password=" + databaseConfig.password()); + } else { + testDatabase.start(); + } + + serverConfig.options().forEach((key, value) -> rawOptions.add("--" + key + "=" + value)); keycloak = Keycloak.builder() @@ -36,6 +52,9 @@ public void start(KeycloakTestServerConfig serverConfig) { public void stop() { try { keycloak.stop(); + if (!database.getDatabaseConfig().isExternal()) { + database.stop(); + } } catch (TimeoutException e) { throw new RuntimeException(e); } 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..14fb85c14561 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,11 @@ package org.keycloak.test.framework.server; +import org.keycloak.test.framework.database.DatabaseConfig; +import org.keycloak.test.framework.database.TestDatabase; + public interface KeycloakTestServer { - void start(KeycloakTestServerConfig serverConfig); + void start(KeycloakTestServerConfig serverConfig, TestDatabase testDatabase); void stop(); 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..62b8522c408c 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,12 @@ package org.keycloak.test.framework.server; +import org.keycloak.test.framework.database.DatabaseConfig; +import org.keycloak.test.framework.database.TestDatabase; + public class RemoteKeycloakTestServer implements KeycloakTestServer { @Override - public void start(KeycloakTestServerConfig serverConfig) { + public void start(KeycloakTestServerConfig serverConfig, TestDatabase testDatabase) { }