-
Notifications
You must be signed in to change notification settings - Fork 7.9k
Adding database suppliers #31073
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Adding database suppliers #31073
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
19 changes: 19 additions & 0 deletions
19
test-poc/framework/src/main/java/org/keycloak/test/framework/KeycloakTestDatabase.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; | ||
|
|
||
| } |
35 changes: 35 additions & 0 deletions
35
...ramework/src/main/java/org/keycloak/test/framework/database/AbstractDatabaseSupplier.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| 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.LifeCycle; | ||
| import org.keycloak.test.framework.injection.Registry; | ||
| import org.keycloak.test.framework.injection.Supplier; | ||
|
|
||
| public abstract class AbstractDatabaseSupplier implements Supplier<TestDatabase, KeycloakTestDatabase> { | ||
|
|
||
| @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) { | ||
| TestDatabase testDatabase = getTestDatabase(); | ||
| testDatabase.start(); | ||
| return new InstanceWrapper<>(this, annotation, testDatabase, LifeCycle.GLOBAL); | ||
| } | ||
|
|
||
| @Override | ||
| public boolean compatible(InstanceWrapper<TestDatabase, KeycloakTestDatabase> a, InstanceWrapper<TestDatabase, KeycloakTestDatabase> b) { | ||
| return true; | ||
| } | ||
|
|
||
| abstract TestDatabase getTestDatabase(); | ||
|
|
||
| } |
76 changes: 76 additions & 0 deletions
76
test-poc/framework/src/main/java/org/keycloak/test/framework/database/DatabaseConfig.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| package org.keycloak.test.framework.database; | ||
|
|
||
| import java.util.HashMap; | ||
| import java.util.Map; | ||
|
|
||
| public class DatabaseConfig { | ||
|
|
||
| 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; | ||
| } | ||
|
|
||
| public DatabaseConfig urlHost(String urlHost) { | ||
| this.urlHost = urlHost; | ||
| return this; | ||
| } | ||
|
|
||
| public String getUsername() { | ||
| return username; | ||
| } | ||
|
|
||
| public DatabaseConfig username(String username) { | ||
| this.username = username; | ||
| return this; | ||
| } | ||
|
|
||
| public String getPassword() { | ||
| return password; | ||
| } | ||
|
|
||
| public DatabaseConfig password(String password) { | ||
| this.password = password; | ||
| return this; | ||
| } | ||
|
|
||
| public Map<String, String> toConfig() { | ||
| Map<String, String> 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; | ||
| } | ||
|
|
||
| } |
11 changes: 11 additions & 0 deletions
11
...framework/src/main/java/org/keycloak/test/framework/database/DevFileDatabaseSupplier.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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); | ||
| } | ||
|
|
||
| } |
11 changes: 11 additions & 0 deletions
11
.../framework/src/main/java/org/keycloak/test/framework/database/DevMemDatabaseSupplier.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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); | ||
| } | ||
|
|
||
| } |
15 changes: 15 additions & 0 deletions
15
...ramework/src/main/java/org/keycloak/test/framework/database/PostgresDatabaseSupplier.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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); | ||
| } | ||
|
|
||
| } |
31 changes: 31 additions & 0 deletions
31
test-poc/framework/src/main/java/org/keycloak/test/framework/database/TestDatabase.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| package org.keycloak.test.framework.database; | ||
|
|
||
| import java.util.Map; | ||
|
|
||
| public class TestDatabase { | ||
|
|
||
| private DatabaseConfig databaseConfig; | ||
|
|
||
| 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 Map<String, String> getServerConfig() { | ||
| return databaseConfig.toConfig(); | ||
| } | ||
|
|
||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,27 +5,28 @@ | |
|
|
||
| import java.util.LinkedList; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.concurrent.TimeoutException; | ||
|
|
||
| public class EmbeddedKeycloakTestServer implements KeycloakTestServer { | ||
|
|
||
| private Keycloak keycloak; | ||
|
|
||
| @Override | ||
| public void start(KeycloakTestServerConfig serverConfig) { | ||
| public void start(KeycloakTestServerConfig serverConfig, Map<String, String> databaseConfig) { | ||
| 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())); | ||
| } | ||
| serverConfig.adminUserName().ifPresent(username -> rawOptions.add("--bootstrap-admin-username=" + username)); | ||
| serverConfig.adminUserPassword().ifPresent(password -> rawOptions.add("--bootstrap-admin-password=" + password)); | ||
|
|
||
| serverConfig.adminUserName().ifPresent(username -> System.setProperty("keycloakAdmin", username)); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| 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) | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 3 additions & 1 deletion
4
test-poc/framework/src/main/java/org/keycloak/test/framework/server/KeycloakTestServer.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 3 additions & 3 deletions
6
.../framework/src/main/java/org/keycloak/test/framework/server/RemoteKeycloakTestServer.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.