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 12, 2024
1 parent 2bd17cd commit 173da6f
Show file tree
Hide file tree
Showing 12 changed files with 149 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package org.keycloak.test.framework;

import org.keycloak.test.framework.database.DatabaseConfig;
import org.keycloak.test.framework.injection.LifeCycle;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface KeycloakTestDatabase {

Class<? extends DatabaseConfig> config() default DatabaseConfig.class;

LifeCycle lifecycle() default LifeCycle.GLOBAL;

}
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,38 @@
package org.keycloak.test.framework.database;

import org.keycloak.test.framework.KeycloakTestDatabase;
import org.keycloak.test.framework.injection.InstanceWrapper;
import org.keycloak.test.framework.injection.Registry;
import org.keycloak.test.framework.injection.Supplier;

public abstract class DatabaseSupplier implements Supplier<TestDatabase, KeycloakTestDatabase> {

protected static TestDatabase testDatabase;

@Override
public Class<KeycloakTestDatabase> getAnnotationClass() {
return KeycloakTestDatabase.class;
}

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

@Override
public InstanceWrapper<TestDatabase, KeycloakTestDatabase> getValue(Registry registry, KeycloakTestDatabase annotation) {
InstanceWrapper<TestDatabase, KeycloakTestDatabase> wrapper = new InstanceWrapper<>(this, annotation);
testDatabase = registry.getDependency(TestDatabase.class, wrapper);
testDatabase.start();
return wrapper;
}

@Override
public boolean compatible(InstanceWrapper<TestDatabase, KeycloakTestDatabase> a, InstanceWrapper<TestDatabase, KeycloakTestDatabase> 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 DefaultDatabaseConfig implements DatabaseConfig {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package org.keycloak.test.framework.database;

public class DefaultDatabaseSupplier extends DatabaseSupplier {
}
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.KeycloakTestDatabase;
import org.keycloak.test.framework.database.TestDatabase;
import org.keycloak.test.framework.injection.InstanceWrapper;
import org.keycloak.test.framework.injection.LifeCycle;
import org.keycloak.test.framework.injection.Registry;
Expand All @@ -22,12 +24,15 @@ public Class<KeycloakIntegrationTest> getAnnotationClass() {
@Override
public InstanceWrapper<KeycloakTestServer, KeycloakIntegrationTest> getValue(Registry registry, KeycloakIntegrationTest annotation) {
KeycloakTestServerConfig serverConfig = SupplierHelpers.getInstance(annotation.config());
InstanceWrapper<KeycloakTestServer, KeycloakIntegrationTest> wrapper = new InstanceWrapper<>(this, annotation);
TestDatabase testDatabase = registry.getDependency(TestDatabase.class, wrapper);

KeycloakTestServer keycloakTestServer = getServer();

keycloakTestServer.start(serverConfig);
keycloakTestServer.start(serverConfig, testDatabase.getDatabaseConfig());
wrapper.setValue(keycloakTestServer, LifeCycle.GLOBAL);

return new InstanceWrapper<>(this, annotation, keycloakTestServer, LifeCycle.GLOBAL);
return wrapper;
}

@Override
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 DistributionKeycloakTestServer implements KeycloakTestServer {

@Override
public void start(KeycloakTestServerConfig serverConfig) {
public void start(KeycloakTestServerConfig serverConfig, DatabaseConfig databaseConfig) {
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 @@ -12,19 +14,26 @@ 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 (databaseConfig.isExternal()) {
rawOptions.add("--db-url-host=" + databaseConfig.urlHost());
rawOptions.add("--db-username=" + databaseConfig.username());
rawOptions.add("--db-password=" + databaseConfig.password());
}


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,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, DatabaseConfig databaseConfig);

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, DatabaseConfig databaseConfig) {

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ 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

0 comments on commit 173da6f

Please sign in to comment.