-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactoring: test for custom container now based on SystemUnderTest c…
…lass and configured logging
- Loading branch information
Edwin Steiner
committed
Sep 9, 2024
1 parent
3557e64
commit e9bc41a
Showing
6 changed files
with
198 additions
and
86 deletions.
There are no files selected for viewing
This file contains 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 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,48 @@ | ||
import org.junit.jupiter.api.*; | ||
import org.keycloak.admin.client.Keycloak; | ||
import org.keycloak.admin.client.KeycloakBuilder; | ||
import org.keycloak.representations.idm.RealmRepresentation; | ||
import org.testcontainers.junit.jupiter.Testcontainers; | ||
import sut.SystemUnderTest; | ||
|
||
import java.util.Optional; | ||
|
||
@Tag("integration") // to run in verify phase of mvn | ||
@Testcontainers | ||
class KeycloakCustomContainerTest { | ||
|
||
private static SystemUnderTest sut; | ||
|
||
@BeforeAll | ||
static void beforeAll() { | ||
sut = SystemUnderTest.start(); | ||
} | ||
|
||
protected SystemUnderTest sut() { | ||
return sut; | ||
} | ||
|
||
@AfterAll | ||
static void afterAll() { | ||
sut.stop(); | ||
} | ||
|
||
@Test | ||
void test_startup() { | ||
Assertions.assertTrue(sut().keycloak.isRunning()); | ||
} | ||
|
||
@Test | ||
void test_import_realm() { | ||
Keycloak keycloakAdminClient = KeycloakBuilder.builder() | ||
.serverUrl(sut().keycloak.getAuthServerUrl()) | ||
.realm("master") | ||
.clientId("admin-cli") | ||
.username(sut().keycloak.getAdminUsername()) | ||
.password(sut().keycloak.getAdminPassword()) | ||
.build(); | ||
|
||
Optional<RealmRepresentation> example1 = keycloakAdminClient.realms().findAll().stream().filter(realmRepresentation -> realmRepresentation.getRealm().equals("example1")).findFirst(); | ||
Assertions.assertTrue(example1.isPresent(), "Realm `example1` should exist. Realm import via keycloak-config-cli failed."); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains 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 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,100 @@ | ||
package sut; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.testcontainers.containers.Network; | ||
import org.testcontainers.containers.PostgreSQLContainer; | ||
import org.testcontainers.containers.output.Slf4jLogConsumer; | ||
|
||
import java.util.Map; | ||
|
||
public class SystemUnderTest { | ||
|
||
private static final Logger LOGGER = LoggerFactory.getLogger(SystemUnderTest.class); | ||
private static final String DOCKER_IMAGE_NAME_POSTGRES = "postgres:16-alpine"; | ||
private static final String NETWORK_ALIAS_POSTGRES = "postgres"; | ||
private static final String DATABASE_NAME_POSTGRES = "postgres"; | ||
private String runningKeycloakBaseUrl; | ||
|
||
private Network network; | ||
|
||
public PostgreSQLContainer postgres; | ||
public KeycloakCustomContainer keycloak; | ||
|
||
public SystemUnderTest(String runningKeycloakBaseUrl) { | ||
this.runningKeycloakBaseUrl = runningKeycloakBaseUrl; | ||
} | ||
|
||
private SystemUnderTest(Network network) { | ||
this.network = network; | ||
} | ||
|
||
public String getBaseUrl() { | ||
return runningKeycloakBaseUrl != null ? runningKeycloakBaseUrl : keycloak.getAuthServerUrl(); | ||
} | ||
|
||
public static SystemUnderTest start() { | ||
final SystemUnderTest sut = new SystemUnderTest(Network.newNetwork()); | ||
sut.startComponents(); | ||
return sut; | ||
} | ||
|
||
private void startComponents() { | ||
startPostgres(network); | ||
startKeycloak(postgres); | ||
} | ||
|
||
private PostgreSQLContainer startPostgres(Network network) { | ||
postgres = new PostgreSQLContainer<>(DOCKER_IMAGE_NAME_POSTGRES) | ||
.withLogConsumer(new Slf4jLogConsumer(LOGGER)) | ||
.withNetwork(network) | ||
.withNetworkAliases(NETWORK_ALIAS_POSTGRES) | ||
.withDatabaseName(DATABASE_NAME_POSTGRES) | ||
.withUsername("postgres") | ||
.withPassword("postgres"); | ||
try { | ||
postgres.start(); | ||
return postgres; | ||
} catch (Exception e) { | ||
System.err.println(postgres.getLogs()); | ||
throw e; | ||
} | ||
} | ||
|
||
private KeycloakCustomContainer startKeycloak(PostgreSQLContainer postgres) { | ||
final String jdbcUrl = String.format("jdbc:postgresql://%s:5432/%s?loggerLevel=OFF", NETWORK_ALIAS_POSTGRES, DATABASE_NAME_POSTGRES); | ||
keycloak = new KeycloakCustomContainer() | ||
.withLogConsumer(new Slf4jLogConsumer(LOGGER)) | ||
.withNetwork(network) | ||
.withEnv(Map.of("KC_DB", "postgres", | ||
"KC_DB_USERNAME" , postgres.getUsername(), | ||
"KC_DB_PASSWORD", postgres.getPassword(), | ||
"KC_DB_URL", jdbcUrl, | ||
"KC_LOG_LEVEL", "info")); | ||
try { | ||
keycloak.start(); | ||
return keycloak; | ||
} catch (Exception e) { | ||
System.err.println(keycloak.getLogs()); | ||
throw e; | ||
} | ||
} | ||
|
||
public void stop() { | ||
stopComponents(); | ||
} | ||
|
||
private void stopComponents() { | ||
stopKeycloak(); | ||
stopPostgres(); | ||
} | ||
|
||
private void stopKeycloak() { | ||
keycloak.start(); | ||
} | ||
|
||
private void stopPostgres() { | ||
postgres.stop(); | ||
} | ||
|
||
} |
This file contains 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