Skip to content

Commit

Permalink
Closes keycloak#30616. Added database supplier.
Browse files Browse the repository at this point in the history
Signed-off-by: Miquel Simon <[email protected]>
  • Loading branch information
miquelsi committed Jul 8, 2024
1 parent bf26d33 commit 93f632e
Show file tree
Hide file tree
Showing 8 changed files with 101 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -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 "";
}
}
Original file line number Diff line number Diff line change
@@ -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<DatabaseConfig, Annotation> {
@Override
public Class<Annotation> getAnnotationClass() {
return null;
}

@Override
public Class<DatabaseConfig> getValueType() {
return DatabaseConfig.class;
}

@Override
public InstanceWrapper<DatabaseConfig, Annotation> 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<DatabaseConfig, Annotation> a, InstanceWrapper<DatabaseConfig, Annotation> b) {
return true;
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<String> 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()
Expand Down
Original file line number Diff line number Diff line change
@@ -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();

Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -22,11 +25,13 @@ public Class<KeycloakIntegrationTest> getAnnotationClass() {
@Override
public InstanceWrapper<KeycloakTestServer, KeycloakIntegrationTest> 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);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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) {

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
org.keycloak.test.framework.webdriver.FirefoxWebDriverSupplier
org.keycloak.test.framework.database.DatabaseSupplier

0 comments on commit 93f632e

Please sign in to comment.