Skip to content

Commit

Permalink
add testcontainers pg for E2E tests
Browse files Browse the repository at this point in the history
  • Loading branch information
wolf4ood committed Jul 19, 2023
1 parent ecbb766 commit c34ca18
Show file tree
Hide file tree
Showing 14 changed files with 191 additions and 81 deletions.
3 changes: 3 additions & 0 deletions DEPENDENCIES
Original file line number Diff line number Diff line change
Expand Up @@ -455,7 +455,10 @@ maven/mavencentral/org.slf4j/slf4j-api/1.7.36, MIT, approved, CQ13368
maven/mavencentral/org.slf4j/slf4j-api/1.7.7, MIT, approved, CQ9827
maven/mavencentral/org.slf4j/slf4j-api/2.0.5, MIT, approved, #5915
maven/mavencentral/org.slf4j/slf4j-api/2.0.7, MIT, approved, #5915
maven/mavencentral/org.testcontainers/database-commons/1.18.3, MIT, approved, clearlydefined
maven/mavencentral/org.testcontainers/jdbc/1.18.3, MIT, approved, clearlydefined
maven/mavencentral/org.testcontainers/junit-jupiter/1.18.3, MIT, approved, #7941
maven/mavencentral/org.testcontainers/postgresql/1.18.3, MIT, approved, #9332
maven/mavencentral/org.testcontainers/testcontainers/1.18.3, MIT, approved, #7938
maven/mavencentral/org.testcontainers/vault/1.18.3, MIT, approved, #7927
maven/mavencentral/org.yaml/snakeyaml/1.33, Apache-2.0, approved, clearlydefined
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ private boolean processNegotiated(EndpointDataReferenceEntry edrEntry) {
.execute("Start an EDR token renewal");
} else {
breakLease(edrEntry);
return true;
return false;
}
}

Expand All @@ -200,7 +200,7 @@ private boolean processExpired(EndpointDataReferenceEntry edrEntry) {
.execute("Start an EDR token deletion");
} else {
breakLease(edrEntry);
return true;
return false;
}
}

Expand Down
3 changes: 3 additions & 0 deletions edc-tests/e2e-tests/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ dependencies {
testCompileOnly(project(":edc-tests:runtime:runtime-memory-ssi"))
testCompileOnly(project(":edc-tests:runtime:runtime-postgresql"))
testImplementation(libs.edc.auth.oauth2.client)
testImplementation(libs.testcontainers.junit)
testImplementation(libs.testcontainers.postgres)

}

// do not publish
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/*
* Copyright (c) 2022 Bayerische Motoren Werke Aktiengesellschaft (BMW AG)
*
* 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
*
* SPDX-License-Identifier: Apache-2.0
*
* Contributors:
* Bayerische Motoren Werke Aktiengesellschaft (BMW AG) - initial API and implementation
*
*/

package org.eclipse.tractusx.edc.helpers;

import org.postgresql.ds.PGSimpleDataSource;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import javax.sql.DataSource;

import static java.lang.String.format;

@Deprecated(forRemoval = true)
public final class TxPostgresqlLocalInstance {
private final String password;
private final String jdbcUrlPrefix;
private final String username;
private final String databaseName;

public TxPostgresqlLocalInstance(String user, String password, String jdbcUrlPrefix, String db) {
username = user;
this.password = password;
this.jdbcUrlPrefix = jdbcUrlPrefix;
databaseName = db;
}

public void createDatabase() {
createDatabase(databaseName);
}

public void createDatabase(String name) {
try (var connection = DriverManager.getConnection(jdbcUrlPrefix + username, username, password)) {
connection.createStatement().execute(format("create database %s;", name));
} catch (SQLException e) {
e.printStackTrace();
// database could already exist
}
}

public Connection getTestConnection(String hostName, int port, String dbName) {
try {
return createTestDataSource(hostName, port, dbName).getConnection();
} catch (SQLException e) {
throw new RuntimeException(e);
}
}

public Connection getConnection() {
try {
return DriverManager.getConnection(jdbcUrlPrefix, username, password);
} catch (SQLException e) {
throw new RuntimeException(e);
}
}

public String getJdbcUrlPrefix() {
return jdbcUrlPrefix;
}

private DataSource createTestDataSource(String hostName, int port, String dbName) {
var dataSource = new PGSimpleDataSource();
dataSource.setServerNames(new String[]{ hostName });
dataSource.setPortNumbers(new int[]{ port });
dataSource.setUser(username);
dataSource.setPassword(password);
dataSource.setDatabaseName(dbName);
return dataSource;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,33 @@
import org.eclipse.edc.spi.system.ServiceExtension;
import org.eclipse.edc.spi.system.ServiceExtensionContext;
import org.eclipse.edc.spi.system.injection.InjectionContainer;
import org.eclipse.edc.sql.testfixtures.PostgresqlLocalInstance;
import org.eclipse.tractusx.edc.helpers.TxPostgresqlLocalInstance;
import org.eclipse.tractusx.edc.token.MockDapsService;
import org.junit.jupiter.api.extension.ExtensionContext;
import org.testcontainers.containers.PostgreSQLContainer;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static java.lang.String.format;
import static org.eclipse.tractusx.edc.lifecycle.TestRuntimeConfiguration.DB_SCHEMA_NAME;
import static org.mockito.Mockito.mock;

public class PgParticipantRuntime extends ParticipantRuntime {

private static final String POSTGRES_IMAGE_NAME = "postgres:14.2";
private static final String USER = "postgres";
private static final String PASSWORD = "password";


private final String dbName;
public PostgreSQLContainer<?> postgreSqlContainer = new PostgreSQLContainer<>(POSTGRES_IMAGE_NAME)
.withExposedPorts(5432)
.withUsername(USER)
.withPassword(PASSWORD)
.withDatabaseName("itest");
private TxPostgresqlLocalInstance helper;

public PgParticipantRuntime(String moduleName, String runtimeName, String bpn, Map<String, String> properties) {
super(moduleName, runtimeName, bpn, properties);
Expand All @@ -41,12 +57,69 @@ public PgParticipantRuntime(String moduleName, String runtimeName, String bpn, M
this.registerServiceMock(Vault.class, new InMemoryVaultOverride(mock(Monitor.class)));
}

@Override
public void beforeAll(ExtensionContext context) throws Exception {
postgreSqlContainer.start();
var config = postgresqlConfiguration(dbName);
config.forEach(System::setProperty);
super.beforeAll(context);
}

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

@Override
protected void bootExtensions(ServiceExtensionContext context, List<InjectionContainer<ServiceExtension>> serviceExtensions) {
PostgresqlLocalInstance.createDatabase(dbName);
helper = new TxPostgresqlLocalInstance(postgreSqlContainer.getUsername(), postgreSqlContainer.getPassword(), baseJdbcUrl(), postgreSqlContainer.getDatabaseName());
helper.createDatabase(dbName);
super.bootExtensions(context, serviceExtensions);
}

public Map<String, String> postgresqlConfiguration(String name) {
var jdbcUrl = jdbcUrl(name);
return new HashMap<>() {
{
put("edc.datasource.asset.name", "asset");
put("edc.datasource.asset.url", jdbcUrl);
put("edc.datasource.asset.user", USER);
put("edc.datasource.asset.password", PASSWORD);
put("edc.datasource.contractdefinition.name", "contractdefinition");
put("edc.datasource.contractdefinition.url", jdbcUrl);
put("edc.datasource.contractdefinition.user", USER);
put("edc.datasource.contractdefinition.password", PASSWORD);
put("edc.datasource.contractnegotiation.name", "contractnegotiation");
put("edc.datasource.contractnegotiation.url", jdbcUrl);
put("edc.datasource.contractnegotiation.user", USER);
put("edc.datasource.contractnegotiation.password", PASSWORD);
put("edc.datasource.policy.name", "policy");
put("edc.datasource.policy.url", jdbcUrl);
put("edc.datasource.policy.user", USER);
put("edc.datasource.policy.password", PASSWORD);
put("edc.datasource.transferprocess.name", "transferprocess");
put("edc.datasource.transferprocess.url", jdbcUrl);
put("edc.datasource.transferprocess.user", USER);
put("edc.datasource.transferprocess.password", PASSWORD);
put("edc.datasource.edr.name", "edr");
put("edc.datasource.edr.url", jdbcUrl);
put("edc.datasource.edr.user", USER);
put("edc.datasource.edr.password", PASSWORD);
// use non-default schema name to test usage of non-default schema
put("org.eclipse.tractusx.edc.postgresql.migration.schema", DB_SCHEMA_NAME);
}
};
}

public String jdbcUrl(String name) {
return baseJdbcUrl() + name + "?currentSchema=" + DB_SCHEMA_NAME;
}

public String baseJdbcUrl() {
return format("jdbc:postgresql://%s:%s/", postgreSqlContainer.getHost(), postgreSqlContainer.getFirstMappedPort());
}

private static class InMemoryVaultOverride extends InMemoryVault {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,54 +66,6 @@ public class TestRuntimeConfiguration {

static final String OAUTH_TOKEN_URL = "http://localhost:" + OAUTH_PORT;

public static Map<String, String> sokratesPostgresqlConfiguration() {
var baseConfiguration = sokratesConfiguration();
var postgresConfiguration = postgresqlConfiguration(SOKRATES_NAME.toLowerCase());
baseConfiguration.putAll(postgresConfiguration);
return baseConfiguration;
}

public static Map<String, String> platoPostgresqlConfiguration() {
var baseConfiguration = platoConfiguration();
var postgresConfiguration = postgresqlConfiguration(PLATO_NAME.toLowerCase());
baseConfiguration.putAll(postgresConfiguration);
return baseConfiguration;
}

public static Map<String, String> postgresqlConfiguration(String name) {
var jdbcUrl = jdbcUrl(name);
return new HashMap<>() {
{
put("edc.datasource.asset.name", "asset");
put("edc.datasource.asset.url", jdbcUrl);
put("edc.datasource.asset.user", PostgresqlLocalInstance.USER);
put("edc.datasource.asset.password", PostgresqlLocalInstance.PASSWORD);
put("edc.datasource.contractdefinition.name", "contractdefinition");
put("edc.datasource.contractdefinition.url", jdbcUrl);
put("edc.datasource.contractdefinition.user", PostgresqlLocalInstance.USER);
put("edc.datasource.contractdefinition.password", PostgresqlLocalInstance.PASSWORD);
put("edc.datasource.contractnegotiation.name", "contractnegotiation");
put("edc.datasource.contractnegotiation.url", jdbcUrl);
put("edc.datasource.contractnegotiation.user", PostgresqlLocalInstance.USER);
put("edc.datasource.contractnegotiation.password", PostgresqlLocalInstance.PASSWORD);
put("edc.datasource.policy.name", "policy");
put("edc.datasource.policy.url", jdbcUrl);
put("edc.datasource.policy.user", PostgresqlLocalInstance.USER);
put("edc.datasource.policy.password", PostgresqlLocalInstance.PASSWORD);
put("edc.datasource.transferprocess.name", "transferprocess");
put("edc.datasource.transferprocess.url", jdbcUrl);
put("edc.datasource.transferprocess.user", PostgresqlLocalInstance.USER);
put("edc.datasource.transferprocess.password", PostgresqlLocalInstance.PASSWORD);
put("edc.datasource.edr.name", "edr");
put("edc.datasource.edr.url", jdbcUrl);
put("edc.datasource.edr.user", PostgresqlLocalInstance.USER);
put("edc.datasource.edr.password", PostgresqlLocalInstance.PASSWORD);
// use non-default schema name to test usage of non-default schema
put("org.eclipse.tractusx.edc.postgresql.migration.schema", DB_SCHEMA_NAME);
}
};
}

public static Map<String, String> sokratesSsiConfiguration() {
var ssiConfiguration = new HashMap<String, String>() {
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@
import static org.eclipse.tractusx.edc.lifecycle.TestRuntimeConfiguration.PLATO_NAME;
import static org.eclipse.tractusx.edc.lifecycle.TestRuntimeConfiguration.SOKRATES_BPN;
import static org.eclipse.tractusx.edc.lifecycle.TestRuntimeConfiguration.SOKRATES_NAME;
import static org.eclipse.tractusx.edc.lifecycle.TestRuntimeConfiguration.platoPostgresqlConfiguration;
import static org.eclipse.tractusx.edc.lifecycle.TestRuntimeConfiguration.sokratesPostgresqlConfiguration;
import static org.eclipse.tractusx.edc.lifecycle.TestRuntimeConfiguration.platoConfiguration;
import static org.eclipse.tractusx.edc.lifecycle.TestRuntimeConfiguration.sokratesConfiguration;

@PostgresqlDbIntegrationTest
public class CatalogPostgresqlTest extends AbstractCatalogTest {
Expand All @@ -33,13 +33,13 @@ public class CatalogPostgresqlTest extends AbstractCatalogTest {
":edc-tests:runtime:runtime-postgresql",
SOKRATES_NAME,
SOKRATES_BPN,
sokratesPostgresqlConfiguration()
sokratesConfiguration()
);
@RegisterExtension
protected static final PgParticipantRuntime PLATO_RUNTIME = new PgParticipantRuntime(
":edc-tests:runtime:runtime-postgresql",
PLATO_NAME,
PLATO_BPN,
platoPostgresqlConfiguration()
platoConfiguration()
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@
import static org.eclipse.tractusx.edc.lifecycle.TestRuntimeConfiguration.PLATO_NAME;
import static org.eclipse.tractusx.edc.lifecycle.TestRuntimeConfiguration.SOKRATES_BPN;
import static org.eclipse.tractusx.edc.lifecycle.TestRuntimeConfiguration.SOKRATES_NAME;
import static org.eclipse.tractusx.edc.lifecycle.TestRuntimeConfiguration.platoPostgresqlConfiguration;
import static org.eclipse.tractusx.edc.lifecycle.TestRuntimeConfiguration.sokratesPostgresqlConfiguration;
import static org.eclipse.tractusx.edc.lifecycle.TestRuntimeConfiguration.platoConfiguration;
import static org.eclipse.tractusx.edc.lifecycle.TestRuntimeConfiguration.sokratesConfiguration;

@PostgresqlDbIntegrationTest
public class NegotiateEdrPostgresqlTest extends AbstractNegotiateEdrTest {
Expand All @@ -33,14 +33,14 @@ public class NegotiateEdrPostgresqlTest extends AbstractNegotiateEdrTest {
":edc-tests:runtime:runtime-postgresql",
SOKRATES_NAME,
SOKRATES_BPN,
sokratesPostgresqlConfiguration()
sokratesConfiguration()
);
@RegisterExtension
protected static final PgParticipantRuntime PLATO_RUNTIME = new PgParticipantRuntime(
":edc-tests:runtime:runtime-postgresql",
PLATO_NAME,
PLATO_BPN,
platoPostgresqlConfiguration()
platoConfiguration()
);

}
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,14 @@ public class RenewalEdrInMemoryTest extends AbstractRenewalEdrTest {
":edc-tests:runtime:runtime-memory",
SOKRATES_NAME,
SOKRATES_BPN,
renewalConfiguration(sokratesConfiguration(), "60")
renewalConfiguration(sokratesConfiguration())
);

@RegisterExtension
protected static final ParticipantRuntime PLATO_RUNTIME = new ParticipantRuntime(
":edc-tests:runtime:runtime-memory",
PLATO_NAME,
PLATO_BPN,
renewalConfiguration(platoConfiguration(), "60")
renewalConfiguration(platoConfiguration())
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@
import static org.eclipse.tractusx.edc.lifecycle.TestRuntimeConfiguration.PLATO_NAME;
import static org.eclipse.tractusx.edc.lifecycle.TestRuntimeConfiguration.SOKRATES_BPN;
import static org.eclipse.tractusx.edc.lifecycle.TestRuntimeConfiguration.SOKRATES_NAME;
import static org.eclipse.tractusx.edc.lifecycle.TestRuntimeConfiguration.platoPostgresqlConfiguration;
import static org.eclipse.tractusx.edc.lifecycle.TestRuntimeConfiguration.sokratesPostgresqlConfiguration;
import static org.eclipse.tractusx.edc.lifecycle.TestRuntimeConfiguration.platoConfiguration;
import static org.eclipse.tractusx.edc.lifecycle.TestRuntimeConfiguration.sokratesConfiguration;
import static org.eclipse.tractusx.edc.tests.edr.TestFunctions.renewalConfiguration;

@PostgresqlDbIntegrationTest
Expand All @@ -34,14 +34,14 @@ public class RenewalEdrPostgresqlTest extends AbstractRenewalEdrTest {
":edc-tests:runtime:runtime-postgresql",
SOKRATES_NAME,
SOKRATES_BPN,
renewalConfiguration(sokratesPostgresqlConfiguration())
renewalConfiguration(sokratesConfiguration())
);
@RegisterExtension
protected static final PgParticipantRuntime PLATO_RUNTIME = new PgParticipantRuntime(
":edc-tests:runtime:runtime-postgresql",
PLATO_NAME,
PLATO_BPN,
renewalConfiguration(platoPostgresqlConfiguration())
renewalConfiguration(platoConfiguration())
);

}
Loading

0 comments on commit c34ca18

Please sign in to comment.