Skip to content

Commit

Permalink
refactor(test): extract PostgresExtension (#1583)
Browse files Browse the repository at this point in the history
  • Loading branch information
ndr-brt authored Sep 25, 2024
1 parent d89f6ad commit d0c2e26
Show file tree
Hide file tree
Showing 10 changed files with 78 additions and 129 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,10 @@
import org.eclipse.edc.junit.extensions.RuntimeExtension;
import org.eclipse.edc.policy.model.Operator;
import org.eclipse.tractusx.edc.tests.participant.TransferParticipant;
import org.eclipse.tractusx.edc.tests.runtimes.PostgresExtension;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Order;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

Expand Down Expand Up @@ -224,10 +226,14 @@ class InMemory extends Tests {
class Postgres extends Tests {

@RegisterExtension
protected static final RuntimeExtension CONSUMER_RUNTIME = pgRuntime(CONSUMER.getName(), CONSUMER.getBpn(), CONSUMER.getConfiguration());
@Order(0)
private static final PostgresExtension POSTGRES = new PostgresExtension(CONSUMER.getName(), PROVIDER.getName());

@RegisterExtension
protected static final RuntimeExtension PROVIDER_RUNTIME = pgRuntime(PROVIDER.getName(), PROVIDER.getBpn(), PROVIDER.getConfiguration());
protected static final RuntimeExtension CONSUMER_RUNTIME = pgRuntime(CONSUMER, POSTGRES);

@RegisterExtension
protected static final RuntimeExtension PROVIDER_RUNTIME = pgRuntime(PROVIDER, POSTGRES);

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,10 @@
import org.eclipse.edc.junit.annotations.PostgresqlIntegrationTest;
import org.eclipse.edc.junit.extensions.RuntimeExtension;
import org.eclipse.tractusx.edc.tests.participant.TransferParticipant;
import org.eclipse.tractusx.edc.tests.runtimes.PostgresExtension;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Order;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

Expand Down Expand Up @@ -54,7 +56,6 @@ public class FederatedCatalogTest {
.id(CONSUMER_BPN)
.build();


protected static final TransferParticipant PROVIDER = TransferParticipant.Builder.newInstance()
.name(PROVIDER_NAME)
.id(PROVIDER_BPN)
Expand Down Expand Up @@ -128,10 +129,14 @@ class InMemory extends Tests {
class Postgres extends Tests {

@RegisterExtension
protected static final RuntimeExtension CONSUMER_RUNTIME = pgRuntime(CONSUMER.getName(), CONSUMER.getBpn(), CONSUMER.getConfiguration());
@Order(0)
protected static final PostgresExtension POSTGRES = new PostgresExtension(CONSUMER.getName(), PROVIDER.getName());

@RegisterExtension
protected static final RuntimeExtension CONSUMER_RUNTIME = pgRuntime(CONSUMER, POSTGRES);

@RegisterExtension
protected static final RuntimeExtension PROVIDER_RUNTIME = pgRuntime(PROVIDER.getName(), PROVIDER.getBpn(), PROVIDER.getConfiguration());
protected static final RuntimeExtension PROVIDER_RUNTIME = pgRuntime(PROVIDER, POSTGRES);

static {
CONSUMER_RUNTIME.registerServiceMock(TargetNodeDirectory.class, new TestTargetNodeDirectory(List.of(PROVIDER)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,12 @@
import org.eclipse.tractusx.edc.tests.helpers.PolicyHelperFunctions;
import org.eclipse.tractusx.edc.tests.helpers.ReceivedEvent;
import org.eclipse.tractusx.edc.tests.participant.TransferParticipant;
import org.eclipse.tractusx.edc.tests.runtimes.PostgresExtension;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Order;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;
import org.mockserver.integration.ClientAndServer;
Expand Down Expand Up @@ -202,10 +204,14 @@ class InMemory extends Tests {
class Postgres extends Tests {

@RegisterExtension
protected static final RuntimeExtension CONSUMER_RUNTIME = pgRuntime(CONSUMER.getName(), CONSUMER.getBpn(), CONSUMER.getConfiguration());
@Order(0)
private static final PostgresExtension POSTGRES = new PostgresExtension(CONSUMER.getName(), PROVIDER.getName());

@RegisterExtension
protected static final RuntimeExtension PROVIDER_RUNTIME = pgRuntime(PROVIDER.getName(), PROVIDER.getBpn(), PROVIDER.getConfiguration());
protected static final RuntimeExtension CONSUMER_RUNTIME = pgRuntime(CONSUMER, POSTGRES);

@RegisterExtension
protected static final RuntimeExtension PROVIDER_RUNTIME = pgRuntime(PROVIDER, POSTGRES);

}
}

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2024 Bayerische Motoren Werke Aktiengesellschaft
* Copyright (c) 2024 Bayerische Motoren Werke Aktiengesellschaft (BMW AG)
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
Expand All @@ -19,8 +19,9 @@

package org.eclipse.tractusx.edc.tests.runtimes;

import com.github.dockerjava.api.model.ExposedPort;
import org.eclipse.edc.util.io.Ports;
import org.junit.jupiter.api.extension.AfterAllCallback;
import org.junit.jupiter.api.extension.BeforeAllCallback;
import org.junit.jupiter.api.extension.ExtensionContext;
import org.testcontainers.containers.PostgreSQLContainer;
import org.testcontainers.containers.wait.strategy.Wait;
Expand All @@ -31,51 +32,46 @@
import java.util.Map;

import static java.lang.String.format;
import static java.util.Arrays.stream;
import static java.util.stream.Collectors.joining;
import static org.eclipse.tractusx.edc.tests.TestRuntimeConfiguration.DB_SCHEMA_NAME;

/**
* Instantiates the Postgres docker container and configures the runtime accordingly
* JUnit extension that permits to spin up a PostgresSQL container
*/
public class PgRuntimeExtension extends ParticipantRuntimeExtension {
public class PostgresExtension implements BeforeAllCallback, AfterAllCallback {

private static final String POSTGRES_IMAGE_NAME = "postgres:16.4";
private static final String USER = "postgres";
private static final String PASSWORD = "password";
private final PostgreSQLContainer<?> postgreSqlContainer;
private final String dbName;
private final String[] databases;
private final int exposedPort;

public PgRuntimeExtension(String moduleName, String runtimeName, String bpn, Map<String, String> properties) {
super(moduleName, runtimeName, bpn, properties);
this.dbName = runtimeName.toLowerCase();
postgreSqlContainer = new PostgreSQLContainer<>(POSTGRES_IMAGE_NAME)
.withLabel("runtime", dbName)
public PostgresExtension(String... databases) {
this.databases = databases;
exposedPort = Ports.getFreePort();
this.postgreSqlContainer = new PostgreSQLContainer<>(POSTGRES_IMAGE_NAME)
.withUsername(USER)
.withPassword(PASSWORD)
.withDatabaseName(dbName);

postgreSqlContainer.setPortBindings(List.of("%d:5432".formatted(Ports.getFreePort())));

.withPassword(PASSWORD);
postgreSqlContainer.setPortBindings(List.of("%d:5432".formatted(exposedPort)));
}

@Override
public void beforeAll(ExtensionContext context) {

postgreSqlContainer.start();
postgreSqlContainer.waitingFor(Wait.forHealthcheck());
var config = postgresqlConfiguration(dbName);
config.forEach(System::setProperty);
createDatabase();
super.beforeAll(context);
this.createDatabases();
}

@Override
public void afterAll(ExtensionContext context) {
super.afterAll(context);
postgreSqlContainer.stop();
postgreSqlContainer.close();
}

private Map<String, String> postgresqlConfiguration(String name) {
var jdbcUrl = baseJdbcUrl() + name + "?currentSchema=" + DB_SCHEMA_NAME;
public Map<String, String> getConfiguration(String databaseName) {
var jdbcUrl = baseJdbcUrl() + databaseName.toLowerCase() + "?currentSchema=" + DB_SCHEMA_NAME;
var group = "edc.datasource.default";

return Map.of(
Expand All @@ -86,15 +82,16 @@ private Map<String, String> postgresqlConfiguration(String name) {
);
}

private void createDatabase() {
private void createDatabases() {
try (var connection = DriverManager.getConnection(baseJdbcUrl() + "postgres", postgreSqlContainer.getUsername(), postgreSqlContainer.getPassword())) {
connection.createStatement().execute(String.format("create database %s;", postgreSqlContainer.getDatabaseName()));
var command = stream(databases).map("create database %s;"::formatted).collect(joining("; "));
connection.createStatement().execute(command);
} catch (SQLException ignored) {
// ignored
}
}

private String baseJdbcUrl() {
return format("jdbc:postgresql://%s:%s/", postgreSqlContainer.getHost(), postgreSqlContainer.getFirstMappedPort());
return format("jdbc:postgresql://%s:%s/", postgreSqlContainer.getHost(), exposedPort);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
package org.eclipse.tractusx.edc.tests.runtimes;

import org.eclipse.edc.junit.extensions.RuntimeExtension;
import org.eclipse.tractusx.edc.tests.participant.TractusxParticipantBase;

import java.util.Map;

Expand All @@ -29,7 +30,10 @@ static RuntimeExtension memoryRuntime(String runtimeName, String bpn, Map<String
return new ParticipantRuntimeExtension(":edc-tests:runtime:runtime-memory", runtimeName, bpn, properties);
}

static RuntimeExtension pgRuntime(String runtimeName, String bpn, Map<String, String> properties) {
return new PgRuntimeExtension(":edc-tests:runtime:runtime-postgresql", runtimeName, bpn, properties);
static RuntimeExtension pgRuntime(TractusxParticipantBase participant, PostgresExtension postgres) {
var configuration = participant.getConfiguration();
configuration.putAll(postgres.getConfiguration(participant.getName()));
return new ParticipantRuntimeExtension(":edc-tests:runtime:runtime-postgresql",
participant.getName(), participant.getBpn(), configuration);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@
import org.eclipse.edc.junit.annotations.PostgresqlIntegrationTest;
import org.eclipse.edc.junit.extensions.RuntimeExtension;
import org.eclipse.tractusx.edc.tests.participant.TransferParticipant;
import org.eclipse.tractusx.edc.tests.runtimes.PostgresExtension;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Order;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

Expand Down Expand Up @@ -107,10 +109,14 @@ class InMemory extends Tests {
class Postgres extends Tests {

@RegisterExtension
protected static final RuntimeExtension CONSUMER_RUNTIME = pgRuntime(CONSUMER.getName(), CONSUMER.getBpn(), CONSUMER.getConfiguration());
@Order(0)
private static final PostgresExtension POSTGRES = new PostgresExtension(CONSUMER.getName(), PROVIDER.getName());

@RegisterExtension
protected static final RuntimeExtension PROVIDER_RUNTIME = pgRuntime(PROVIDER.getName(), PROVIDER.getBpn(), PROVIDER.getConfiguration());
protected static final RuntimeExtension CONSUMER_RUNTIME = pgRuntime(CONSUMER, POSTGRES);

@RegisterExtension
protected static final RuntimeExtension PROVIDER_RUNTIME = pgRuntime(PROVIDER, POSTGRES);

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@
import org.eclipse.edc.junit.extensions.RuntimeExtension;
import org.eclipse.tractusx.edc.tests.participant.TractusxParticipantBase;
import org.eclipse.tractusx.edc.tests.participant.TransferParticipant;
import org.eclipse.tractusx.edc.tests.runtimes.PostgresExtension;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Order;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;
import org.mockserver.verify.VerificationTimes;
Expand Down Expand Up @@ -187,10 +189,14 @@ class InMemory extends Tests {
class Postgres extends Tests {

@RegisterExtension
protected static final RuntimeExtension CONSUMER_RUNTIME = pgRuntime(CONSUMER.getName(), CONSUMER.getBpn(), CONSUMER.getConfiguration());
@Order(0)
private static final PostgresExtension POSTGRES = new PostgresExtension(CONSUMER.getName(), PROVIDER.getName());

@RegisterExtension
protected static final RuntimeExtension PROVIDER_RUNTIME = pgRuntime(PROVIDER.getName(), PROVIDER.getBpn(), PROVIDER.getConfiguration());
protected static final RuntimeExtension CONSUMER_RUNTIME = pgRuntime(CONSUMER, POSTGRES);

@RegisterExtension
protected static final RuntimeExtension PROVIDER_RUNTIME = pgRuntime(PROVIDER, POSTGRES);

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@
import org.eclipse.edc.junit.extensions.RuntimeExtension;
import org.eclipse.tractusx.edc.tests.participant.TractusxParticipantBase;
import org.eclipse.tractusx.edc.tests.participant.TransferParticipant;
import org.eclipse.tractusx.edc.tests.runtimes.PostgresExtension;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Order;
import org.junit.jupiter.api.extension.RegisterExtension;

import static org.eclipse.tractusx.edc.tests.TestRuntimeConfiguration.CONSUMER_BPN;
Expand Down Expand Up @@ -76,10 +78,14 @@ class InMemory extends Tests {
class Postgres extends Tests {

@RegisterExtension
protected static final RuntimeExtension CONSUMER_RUNTIME = pgRuntime(CONSUMER.getName(), CONSUMER.getBpn(), CONSUMER.getConfiguration());
@Order(0)
private static final PostgresExtension POSTGRES = new PostgresExtension(CONSUMER.getName(), PROVIDER.getName());

@RegisterExtension
protected static final RuntimeExtension PROVIDER_RUNTIME = pgRuntime(PROVIDER.getName(), PROVIDER.getBpn(), PROVIDER.getConfiguration());
protected static final RuntimeExtension CONSUMER_RUNTIME = pgRuntime(CONSUMER, POSTGRES);

@RegisterExtension
protected static final RuntimeExtension PROVIDER_RUNTIME = pgRuntime(PROVIDER, POSTGRES);

}
}
}

0 comments on commit d0c2e26

Please sign in to comment.