Skip to content

Commit

Permalink
Closes keycloak#30616. Added database suppliers.
Browse files Browse the repository at this point in the history
Signed-off-by: Miquel Simon <[email protected]>
  • Loading branch information
miquelsi committed Jul 11, 2024
1 parent 2bd17cd commit 9869df8
Show file tree
Hide file tree
Showing 11 changed files with 142 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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<TestDatabase, Annotation> {

protected static TestDatabase testDatabase = new TestDatabase(new DevMemDatabaseConfig());

@Override
public Class<Annotation> getAnnotationClass() {
return null;
}

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

@Override
public InstanceWrapper<TestDatabase, Annotation> getValue(Registry registry, Annotation annotation) {
return new InstanceWrapper<>(this, annotation, testDatabase, LifeCycle.GLOBAL);
}

@Override
public boolean compatible(InstanceWrapper<TestDatabase, Annotation> a, InstanceWrapper<TestDatabase, Annotation> b) {
return true;
}

public TestDatabase getTestDatabase() {
return testDatabase;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package org.keycloak.test.framework.database;

public class DevMemDatabaseConfig implements DatabaseConfig {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package org.keycloak.test.framework.database;

public class DevMemDatabaseSupplier extends DatabaseSupplier {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package org.keycloak.test.framework.database;

public class ExternalDatabaseConfig implements DatabaseConfig {
@Override
public boolean isExternal() {
return true;
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -22,10 +24,11 @@ public Class<KeycloakIntegrationTest> getAnnotationClass() {
@Override
public InstanceWrapper<KeycloakTestServer, KeycloakIntegrationTest> 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);
}
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.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!");
}

Expand Down
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.TestDatabase;

import java.util.LinkedList;
import java.util.List;
Expand All @@ -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<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()));
}

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()
Expand All @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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();

Expand Down
Original file line number Diff line number Diff line change
@@ -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) {

}

Expand Down

0 comments on commit 9869df8

Please sign in to comment.