diff --git a/edc-tests/edc-controlplane/catalog-tests/src/test/java/org/eclipse/tractusx/edc/tests/catalog/CatalogTest.java b/edc-tests/edc-controlplane/catalog-tests/src/test/java/org/eclipse/tractusx/edc/tests/catalog/CatalogTest.java index 6f0a04221..1040f41d4 100644 --- a/edc-tests/edc-controlplane/catalog-tests/src/test/java/org/eclipse/tractusx/edc/tests/catalog/CatalogTest.java +++ b/edc-tests/edc-controlplane/catalog-tests/src/test/java/org/eclipse/tractusx/edc/tests/catalog/CatalogTest.java @@ -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; @@ -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); } diff --git a/edc-tests/edc-controlplane/catalog-tests/src/test/java/org/eclipse/tractusx/edc/tests/catalog/FederatedCatalogTest.java b/edc-tests/edc-controlplane/catalog-tests/src/test/java/org/eclipse/tractusx/edc/tests/catalog/FederatedCatalogTest.java index 4f17996c0..e7bb401ad 100644 --- a/edc-tests/edc-controlplane/catalog-tests/src/test/java/org/eclipse/tractusx/edc/tests/catalog/FederatedCatalogTest.java +++ b/edc-tests/edc-controlplane/catalog-tests/src/test/java/org/eclipse/tractusx/edc/tests/catalog/FederatedCatalogTest.java @@ -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; @@ -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) @@ -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))); diff --git a/edc-tests/edc-controlplane/edr-api-tests/src/test/java/org/eclipse/tractusx/edc/tests/edrv2/NegotiateEdrTest.java b/edc-tests/edc-controlplane/edr-api-tests/src/test/java/org/eclipse/tractusx/edc/tests/edrv2/NegotiateEdrTest.java index 608de68da..659615325 100644 --- a/edc-tests/edc-controlplane/edr-api-tests/src/test/java/org/eclipse/tractusx/edc/tests/edrv2/NegotiateEdrTest.java +++ b/edc-tests/edc-controlplane/edr-api-tests/src/test/java/org/eclipse/tractusx/edc/tests/edrv2/NegotiateEdrTest.java @@ -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; @@ -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); } } diff --git a/edc-tests/edc-controlplane/fixtures/src/testFixtures/java/org/eclipse/tractusx/edc/tests/runtimes/BeforeInitCallback.java b/edc-tests/edc-controlplane/fixtures/src/testFixtures/java/org/eclipse/tractusx/edc/tests/runtimes/BeforeInitCallback.java deleted file mode 100644 index a4633ae15..000000000 --- a/edc-tests/edc-controlplane/fixtures/src/testFixtures/java/org/eclipse/tractusx/edc/tests/runtimes/BeforeInitCallback.java +++ /dev/null @@ -1,33 +0,0 @@ -/* - * Copyright (c) 2024 Bayerische Motoren Werke Aktiengesellschaft - * - * See the NOTICE file(s) distributed with this work for additional - * information regarding copyright ownership. - * - * This program and the accompanying materials are made available under the - * terms of the Apache License, Version 2.0 which is available at - * https://www.apache.org/licenses/LICENSE-2.0. - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT - * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the - * License for the specific language governing permissions and limitations - * under the License. - * - * SPDX-License-Identifier: Apache-2.0 - */ - -package org.eclipse.tractusx.edc.tests.runtimes; - -import org.eclipse.edc.junit.extensions.EmbeddedRuntime; -import org.eclipse.edc.junit.extensions.RuntimeExtension; - -/** - * Callback invoked before the runtime boots with extensions of {@link RuntimeExtension}. This will allow injecting - * custom mock services directly in the tests rather than extending the {@link RuntimeExtension} with custom mocks - */ -@FunctionalInterface -public interface BeforeInitCallback { - - void beforeInit(EmbeddedRuntime runtime); -} diff --git a/edc-tests/edc-controlplane/fixtures/src/testFixtures/java/org/eclipse/tractusx/edc/tests/runtimes/PgParticipantRuntime.java b/edc-tests/edc-controlplane/fixtures/src/testFixtures/java/org/eclipse/tractusx/edc/tests/runtimes/PgParticipantRuntime.java deleted file mode 100644 index 7c1b465f5..000000000 --- a/edc-tests/edc-controlplane/fixtures/src/testFixtures/java/org/eclipse/tractusx/edc/tests/runtimes/PgParticipantRuntime.java +++ /dev/null @@ -1,54 +0,0 @@ -/******************************************************************************** - * Copyright (c) 2023 Bayerische Motoren Werke Aktiengesellschaft (BMW AG) - * - * See the NOTICE file(s) distributed with this work for additional - * information regarding copyright ownership. - * - * This program and the accompanying materials are made available under the - * terms of the Apache License, Version 2.0 which is available at - * https://www.apache.org/licenses/LICENSE-2.0. - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT - * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the - * License for the specific language governing permissions and limitations - * under the License. - * - * SPDX-License-Identifier: Apache-2.0 - ********************************************************************************/ - -package org.eclipse.tractusx.edc.tests.runtimes; - -import org.eclipse.edc.boot.vault.InMemoryVault; -import org.eclipse.edc.spi.monitor.ConsoleMonitor; -import org.eclipse.edc.spi.monitor.Monitor; -import org.eclipse.edc.spi.result.Result; -import org.eclipse.edc.spi.security.Vault; - -import java.util.Map; - -class PgParticipantRuntime extends ParticipantRuntime { - - PgParticipantRuntime(String moduleName, String runtimeName, String bpn, Map properties) { - super(moduleName, runtimeName, bpn, properties); - mockVault(); - } - - protected void mockVault() { - this.registerServiceMock(Vault.class, new InMemoryVaultOverride(new ConsoleMonitor())); - } - - private static class InMemoryVaultOverride extends InMemoryVault { - - InMemoryVaultOverride(Monitor monitor) { - super(monitor); - } - - @Override - public Result deleteSecret(String s) { - super.deleteSecret(s); - return Result.success(); - } - } - -} diff --git a/edc-tests/edc-controlplane/fixtures/src/testFixtures/java/org/eclipse/tractusx/edc/tests/runtimes/PgRuntimeExtension.java b/edc-tests/edc-controlplane/fixtures/src/testFixtures/java/org/eclipse/tractusx/edc/tests/runtimes/PostgresExtension.java similarity index 67% rename from edc-tests/edc-controlplane/fixtures/src/testFixtures/java/org/eclipse/tractusx/edc/tests/runtimes/PgRuntimeExtension.java rename to edc-tests/edc-controlplane/fixtures/src/testFixtures/java/org/eclipse/tractusx/edc/tests/runtimes/PostgresExtension.java index 8e0a0186b..91ff3f6a2 100644 --- a/edc-tests/edc-controlplane/fixtures/src/testFixtures/java/org/eclipse/tractusx/edc/tests/runtimes/PgRuntimeExtension.java +++ b/edc-tests/edc-controlplane/fixtures/src/testFixtures/java/org/eclipse/tractusx/edc/tests/runtimes/PostgresExtension.java @@ -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. @@ -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; @@ -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 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 postgresqlConfiguration(String name) { - var jdbcUrl = baseJdbcUrl() + name + "?currentSchema=" + DB_SCHEMA_NAME; + public Map getConfiguration(String databaseName) { + var jdbcUrl = baseJdbcUrl() + databaseName.toLowerCase() + "?currentSchema=" + DB_SCHEMA_NAME; var group = "edc.datasource.default"; return Map.of( @@ -86,15 +82,16 @@ private Map 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); } } diff --git a/edc-tests/edc-controlplane/fixtures/src/testFixtures/java/org/eclipse/tractusx/edc/tests/runtimes/Runtimes.java b/edc-tests/edc-controlplane/fixtures/src/testFixtures/java/org/eclipse/tractusx/edc/tests/runtimes/Runtimes.java index 89d384a02..5eead9802 100644 --- a/edc-tests/edc-controlplane/fixtures/src/testFixtures/java/org/eclipse/tractusx/edc/tests/runtimes/Runtimes.java +++ b/edc-tests/edc-controlplane/fixtures/src/testFixtures/java/org/eclipse/tractusx/edc/tests/runtimes/Runtimes.java @@ -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; @@ -29,7 +30,10 @@ static RuntimeExtension memoryRuntime(String runtimeName, String bpn, Map 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); } } diff --git a/edc-tests/edc-controlplane/policy-tests/src/test/java/org/eclipse/tractusx/edc/tests/policy/PolicyMonitorEndToEndTest.java b/edc-tests/edc-controlplane/policy-tests/src/test/java/org/eclipse/tractusx/edc/tests/policy/PolicyMonitorEndToEndTest.java index c7db01938..06337f946 100644 --- a/edc-tests/edc-controlplane/policy-tests/src/test/java/org/eclipse/tractusx/edc/tests/policy/PolicyMonitorEndToEndTest.java +++ b/edc-tests/edc-controlplane/policy-tests/src/test/java/org/eclipse/tractusx/edc/tests/policy/PolicyMonitorEndToEndTest.java @@ -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; @@ -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); } } diff --git a/edc-tests/edc-controlplane/transfer-tests/src/test/java/org/eclipse/tractusx/edc/tests/transfer/TransferPullEndToEndTest.java b/edc-tests/edc-controlplane/transfer-tests/src/test/java/org/eclipse/tractusx/edc/tests/transfer/TransferPullEndToEndTest.java index 00dfda52a..fae287c3c 100644 --- a/edc-tests/edc-controlplane/transfer-tests/src/test/java/org/eclipse/tractusx/edc/tests/transfer/TransferPullEndToEndTest.java +++ b/edc-tests/edc-controlplane/transfer-tests/src/test/java/org/eclipse/tractusx/edc/tests/transfer/TransferPullEndToEndTest.java @@ -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; @@ -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); } } diff --git a/edc-tests/edc-controlplane/transfer-tests/src/test/java/org/eclipse/tractusx/edc/tests/transfer/TransferPushEndToEndTest.java b/edc-tests/edc-controlplane/transfer-tests/src/test/java/org/eclipse/tractusx/edc/tests/transfer/TransferPushEndToEndTest.java index c57ce9470..a524f1b82 100644 --- a/edc-tests/edc-controlplane/transfer-tests/src/test/java/org/eclipse/tractusx/edc/tests/transfer/TransferPushEndToEndTest.java +++ b/edc-tests/edc-controlplane/transfer-tests/src/test/java/org/eclipse/tractusx/edc/tests/transfer/TransferPushEndToEndTest.java @@ -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; @@ -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); } -} \ No newline at end of file +}