From 3f6b9183de8f7d7810d18de42079b97f05781583 Mon Sep 17 00:00:00 2001 From: stianst Date: Mon, 15 Jul 2024 12:34:24 +0200 Subject: [PATCH] WIP --- ...ier.java => AbstractDatabaseSupplier.java} | 15 ++-- .../framework/database/DatabaseConfig.java | 73 +++++++++++++++---- .../database/DefaultDatabaseConfig.java | 4 - .../database/DefaultDatabaseSupplier.java | 4 - .../database/DevFileDatabaseSupplier.java | 11 +++ .../database/DevMemDatabaseSupplier.java | 11 +++ .../database/PostgresDatabaseSupplier.java | 15 ++++ .../test/framework/database/TestDatabase.java | 21 ++++-- .../AbstractKeycloakTestServerSupplier.java | 17 ++++- .../DistributionKeycloakTestServer.java | 5 +- ...istributionKeycloakTestServerSupplier.java | 5 ++ .../server/EmbeddedKeycloakTestServer.java | 18 ++--- .../EmbeddedKeycloakTestServerSupplier.java | 5 ++ .../framework/server/KeycloakTestServer.java | 5 +- .../server/RemoteKeycloakTestServer.java | 7 +- .../RemoteKeycloakTestServerSupplier.java | 5 ++ ...keycloak.test.framework.injection.Supplier | 4 +- 17 files changed, 162 insertions(+), 63 deletions(-) rename test-poc/framework/src/main/java/org/keycloak/test/framework/database/{DatabaseSupplier.java => AbstractDatabaseSupplier.java} (66%) delete mode 100644 test-poc/framework/src/main/java/org/keycloak/test/framework/database/DefaultDatabaseConfig.java delete mode 100644 test-poc/framework/src/main/java/org/keycloak/test/framework/database/DefaultDatabaseSupplier.java create mode 100644 test-poc/framework/src/main/java/org/keycloak/test/framework/database/DevFileDatabaseSupplier.java create mode 100644 test-poc/framework/src/main/java/org/keycloak/test/framework/database/DevMemDatabaseSupplier.java create mode 100644 test-poc/framework/src/main/java/org/keycloak/test/framework/database/PostgresDatabaseSupplier.java 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/AbstractDatabaseSupplier.java similarity index 66% rename from test-poc/framework/src/main/java/org/keycloak/test/framework/database/DatabaseSupplier.java rename to test-poc/framework/src/main/java/org/keycloak/test/framework/database/AbstractDatabaseSupplier.java index 6a9f09bfcab1..2de10d0d7051 100644 --- 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/AbstractDatabaseSupplier.java @@ -2,12 +2,11 @@ import org.keycloak.test.framework.KeycloakTestDatabase; 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; -public abstract class DatabaseSupplier implements Supplier { - - protected static TestDatabase testDatabase; +public abstract class AbstractDatabaseSupplier implements Supplier { @Override public Class getAnnotationClass() { @@ -21,10 +20,9 @@ public Class getValueType() { @Override public InstanceWrapper getValue(Registry registry, KeycloakTestDatabase annotation) { - InstanceWrapper wrapper = new InstanceWrapper<>(this, annotation); - testDatabase = registry.getDependency(TestDatabase.class, wrapper); + TestDatabase testDatabase = getTestDatabase(); testDatabase.start(); - return wrapper; + return new InstanceWrapper<>(this, annotation, testDatabase, LifeCycle.GLOBAL); } @Override @@ -32,7 +30,6 @@ public boolean compatible(InstanceWrapper a, return true; } - public TestDatabase getTestDatabase() { - return testDatabase; - } + abstract TestDatabase getTestDatabase(); + } 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 index e9d78fcd54db..f8597fa7aa77 100644 --- 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 @@ -1,31 +1,76 @@ package org.keycloak.test.framework.database; -import java.util.Collections; +import java.util.HashMap; import java.util.Map; -public interface DatabaseConfig { +public class DatabaseConfig { - default String vendor() { - return ""; + private String vendor; + private String containerImage; + private String urlHost; + private String username; + private String password; + + public String getVendor() { + return vendor; + } + + public DatabaseConfig vendor(String vendor) { + this.vendor = vendor; + return this; + } + + public String getContainerImage() { + return containerImage; + } + + public DatabaseConfig containerImage(String containerImage) { + this.containerImage = containerImage; + return this; + } + + public String getUrlHost() { + return urlHost; } - default String containerImage() { - return ""; + public DatabaseConfig urlHost(String urlHost) { + this.urlHost = urlHost; + return this; } - default String urlHost() { - return ""; + public String getUsername() { + return username; } - default String username() { - return ""; + public DatabaseConfig username(String username) { + this.username = username; + return this; } - default String password() { - return ""; + public String getPassword() { + return password; } - default boolean isExternal() { - return false; + public DatabaseConfig password(String password) { + this.password = password; + return this; } + + public Map toConfig() { + Map config = new HashMap<>(); + if (vendor != null) { + config.put("db", vendor); + } + if (urlHost != null) { + config.put("db-url-host", urlHost); + } + if (username != null) { + config.put("db-username", username); + } + if (password != null) { + config.put("db-password", password); + } + return config; + } + } 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 deleted file mode 100644 index dc6e482b8571..000000000000 --- a/test-poc/framework/src/main/java/org/keycloak/test/framework/database/DefaultDatabaseConfig.java +++ /dev/null @@ -1,4 +0,0 @@ -package org.keycloak.test.framework.database; - -public class DefaultDatabaseConfig implements DatabaseConfig { -} diff --git a/test-poc/framework/src/main/java/org/keycloak/test/framework/database/DefaultDatabaseSupplier.java b/test-poc/framework/src/main/java/org/keycloak/test/framework/database/DefaultDatabaseSupplier.java deleted file mode 100644 index 11301e35ddee..000000000000 --- a/test-poc/framework/src/main/java/org/keycloak/test/framework/database/DefaultDatabaseSupplier.java +++ /dev/null @@ -1,4 +0,0 @@ -package org.keycloak.test.framework.database; - -public class DefaultDatabaseSupplier extends DatabaseSupplier { -} 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 new file mode 100644 index 000000000000..5dcec98242d9 --- /dev/null +++ b/test-poc/framework/src/main/java/org/keycloak/test/framework/database/DevFileDatabaseSupplier.java @@ -0,0 +1,11 @@ +package org.keycloak.test.framework.database; + +public class DevFileDatabaseSupplier extends AbstractDatabaseSupplier { + + @Override + TestDatabase getTestDatabase() { + DatabaseConfig databaseConfig = new DatabaseConfig().vendor("dev-file"); + return new TestDatabase(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..9c14651b2384 --- /dev/null +++ b/test-poc/framework/src/main/java/org/keycloak/test/framework/database/DevMemDatabaseSupplier.java @@ -0,0 +1,11 @@ +package org.keycloak.test.framework.database; + +public class DevMemDatabaseSupplier extends AbstractDatabaseSupplier { + + @Override + TestDatabase getTestDatabase() { + DatabaseConfig databaseConfig = new DatabaseConfig().vendor("dev-mem"); + return new TestDatabase(databaseConfig); + } + +} 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 new file mode 100644 index 000000000000..2c7540c02f52 --- /dev/null +++ b/test-poc/framework/src/main/java/org/keycloak/test/framework/database/PostgresDatabaseSupplier.java @@ -0,0 +1,15 @@ +package org.keycloak.test.framework.database; + +public class PostgresDatabaseSupplier extends AbstractDatabaseSupplier { + + @Override + TestDatabase getTestDatabase() { + DatabaseConfig databaseConfig = new DatabaseConfig() + .vendor("postgres") + .username("keycloak") + .password("keycloak") + .containerImage("the-postgres-container:the-version"); + return new TestDatabase(databaseConfig); + } + +} 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 index 78e8f6aa18f6..3b4707b036a7 100644 --- 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 @@ -1,22 +1,31 @@ package org.keycloak.test.framework.database; +import java.util.Map; + public class TestDatabase { private DatabaseConfig databaseConfig; - public TestDatabase(DatabaseConfig config) { - databaseConfig = config; + public TestDatabase(DatabaseConfig databaseConfig) { + this.databaseConfig = databaseConfig; } public void start() { - + if (databaseConfig.getContainerImage() != null) { + // TODO Start container + } } public void stop() { - + if (databaseConfig.getContainerImage() != null) { + // TODO Stop container + } else if (databaseConfig.getVendor().equals("dev-mem")) { + // TODO Stop in-mem H2 database + } } - public DatabaseConfig getDatabaseConfig() { - return databaseConfig; + public Map getServerConfig() { + return databaseConfig.toConfig(); } + } 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 f0b4fe8303c0..ad09f71dffd2 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,7 +1,6 @@ package org.keycloak.test.framework.server; import org.keycloak.test.framework.KeycloakIntegrationTest; -import org.keycloak.test.framework.KeycloakTestDatabase; import org.keycloak.test.framework.database.TestDatabase; import org.keycloak.test.framework.injection.InstanceWrapper; import org.keycloak.test.framework.injection.LifeCycle; @@ -9,6 +8,9 @@ import org.keycloak.test.framework.injection.Supplier; import org.keycloak.test.framework.injection.SupplierHelpers; +import java.util.Collections; +import java.util.Map; + public abstract class AbstractKeycloakTestServerSupplier implements Supplier { @Override @@ -25,11 +27,18 @@ public Class getAnnotationClass() { public InstanceWrapper getValue(Registry registry, KeycloakIntegrationTest annotation) { KeycloakTestServerConfig serverConfig = SupplierHelpers.getInstance(annotation.config()); InstanceWrapper wrapper = new InstanceWrapper<>(this, annotation); - TestDatabase testDatabase = registry.getDependency(TestDatabase.class, wrapper); + + Map databaseConfig; + if (requiresDatabase()) { + TestDatabase testDatabase = registry.getDependency(TestDatabase.class, wrapper); + databaseConfig = testDatabase.getServerConfig(); + } else { + databaseConfig = Collections.emptyMap(); + } KeycloakTestServer keycloakTestServer = getServer(); + keycloakTestServer.start(serverConfig, databaseConfig); - keycloakTestServer.start(serverConfig, testDatabase.getDatabaseConfig()); wrapper.setValue(keycloakTestServer, LifeCycle.GLOBAL); return wrapper; @@ -47,4 +56,6 @@ public void close(KeycloakTestServer keycloakTestServer) { public abstract KeycloakTestServer getServer(); + public abstract boolean requiresDatabase(); + } 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 1e36ce2202d6..f0698ce87dd8 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 @@ -4,6 +4,7 @@ import java.util.LinkedList; import java.util.List; +import java.util.Map; public class DistributionKeycloakTestServer implements KeycloakTestServer { @@ -16,7 +17,7 @@ public class DistributionKeycloakTestServer implements KeycloakTestServer { private RawKeycloakDistribution keycloak; @Override - public void start(KeycloakTestServerConfig serverConfig) { + public void start(KeycloakTestServerConfig serverConfig, Map databaseConfig) { keycloak = new RawKeycloakDistribution(debug, manualStop, enableTls, reCreate, removeBuildOptionsAfterBuild, requestPort); // Set environment variables user and password for Keycloak Admin used by Keycloak instance. @@ -35,6 +36,8 @@ public void start(KeycloakTestServerConfig serverConfig) { serverConfig.options().forEach((key, value) -> rawOptions.add("--" + key + "=" + value)); + databaseConfig.forEach((key, value) -> rawOptions.add("--" + key + "=" + value)); + keycloak.run(rawOptions).assertStartedDevMode(); } 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 52e67eea5311..96a7dab29bfb 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 @@ -7,4 +7,9 @@ public KeycloakTestServer getServer() { return new DistributionKeycloakTestServer(); } + @Override + public boolean requiresDatabase() { + return true; + } + } 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 9562a2184cf6..2999f0db3356 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,10 +2,10 @@ import org.keycloak.Keycloak; import org.keycloak.common.Version; -import org.keycloak.test.framework.database.DatabaseConfig; import java.util.LinkedList; import java.util.List; +import java.util.Map; import java.util.concurrent.TimeoutException; public class EmbeddedKeycloakTestServer implements KeycloakTestServer { @@ -13,8 +13,7 @@ public class EmbeddedKeycloakTestServer implements KeycloakTestServer { private Keycloak keycloak; @Override - public void start(KeycloakTestServerConfig serverConfig, DatabaseConfig databaseConfig) { - + public void start(KeycloakTestServerConfig serverConfig, Map databaseConfig) { List rawOptions = new LinkedList<>(); rawOptions.add("start-dev"); rawOptions.add("--cache=local"); @@ -23,18 +22,11 @@ public void start(KeycloakTestServerConfig serverConfig, DatabaseConfig database rawOptions.add("--features=" + String.join(",", serverConfig.features())); } - serverConfig.adminUserName().ifPresent(username -> rawOptions.add("--bootstrap-admin-username=" + username)); - serverConfig.adminUserPassword().ifPresent(password -> rawOptions.add("--bootstrap-admin-password=" + password)); - - //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()); - } - + serverConfig.adminUserName().ifPresent(username -> System.setProperty("keycloakAdmin", username)); + serverConfig.adminUserPassword().ifPresent(password -> System.setProperty("keycloakAdminPassword", password)); serverConfig.options().forEach((key, value) -> rawOptions.add("--" + key + "=" + value)); + databaseConfig.forEach((key, value) -> rawOptions.add("--" + key + "=" + value)); keycloak = Keycloak.builder() .setVersion(Version.VERSION) 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 ef67e956447a..fc87d4e6f256 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 @@ -7,4 +7,9 @@ public KeycloakTestServer getServer() { return new EmbeddedKeycloakTestServer(); } + @Override + public boolean requiresDatabase() { + return true; + } + } 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 c14f127127da..1894cff97e11 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,11 +1,10 @@ package org.keycloak.test.framework.server; -import org.keycloak.test.framework.database.DatabaseConfig; -import org.keycloak.test.framework.database.TestDatabase; +import java.util.Map; public interface KeycloakTestServer { - void start(KeycloakTestServerConfig serverConfig, DatabaseConfig databaseConfig); + void start(KeycloakTestServerConfig serverConfig, Map databaseConfig); 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 ca0c7351d7aa..d592329316d1 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,18 +1,15 @@ package org.keycloak.test.framework.server; -import org.keycloak.test.framework.database.DatabaseConfig; -import org.keycloak.test.framework.database.TestDatabase; +import java.util.Map; public class RemoteKeycloakTestServer implements KeycloakTestServer { @Override - public void start(KeycloakTestServerConfig serverConfig, DatabaseConfig databaseConfig) { - + public void start(KeycloakTestServerConfig serverConfig, Map databaseConfig) { } @Override public void stop() { - } @Override 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 71bf4aa93abf..30d61aa7e2e8 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 @@ -7,4 +7,9 @@ public KeycloakTestServer getServer() { return new RemoteKeycloakTestServer(); } + @Override + public boolean requiresDatabase() { + return false; + } + } 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 b8cc003477ad..7dc7ba9ba5c8 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 @@ -7,4 +7,6 @@ org.keycloak.test.framework.server.DistributionKeycloakTestServerSupplier org.keycloak.test.framework.server.RemoteKeycloakTestServerSupplier org.keycloak.test.framework.webdriver.ChromeWebDriverSupplier org.keycloak.test.framework.webdriver.FirefoxWebDriverSupplier -org.keycloak.test.framework.database.DefaultDatabaseSupplier \ No newline at end of file +org.keycloak.test.framework.database.DevMemDatabaseSupplier +org.keycloak.test.framework.database.DevFileDatabaseSupplier +org.keycloak.test.framework.database.PostgresDatabaseSupplier \ No newline at end of file