Skip to content

Commit

Permalink
feat(test): convert all postgres tests to use testcontainers (eclipse…
Browse files Browse the repository at this point in the history
…-edc#3268)

feat: consolidate tractus-x HashiCorp Vault impl
  • Loading branch information
paullatzelsperger authored and bjungs committed Aug 18, 2023
1 parent bc10bd1 commit 5b69dc0
Show file tree
Hide file tree
Showing 22 changed files with 186 additions and 222 deletions.
1 change: 1 addition & 0 deletions core/common/junit/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ dependencies {
runtimeOnly(libs.junit.jupiter.engine)

implementation(libs.junit.pioneer)
implementation(libs.testcontainers.junit)
}


Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package org.eclipse.edc.junit.annotations;

import org.junit.jupiter.api.Tag;
import org.testcontainers.junit.jupiter.Testcontainers;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
Expand All @@ -28,6 +29,7 @@
@Target({ ElementType.TYPE })
@Retention(RetentionPolicy.RUNTIME)
@IntegrationTest
@Testcontainers
@Tag("PostgresqlIntegrationTest")
public @interface PostgresqlDbIntegrationTest {
}
3 changes: 3 additions & 0 deletions extensions/common/sql/sql-core/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ dependencies {
testFixturesImplementation(project(":spi:common:transaction-datasource-spi"))
testFixturesImplementation(libs.mockito.core)

testFixturesImplementation(libs.testcontainers.junit)
testFixturesImplementation(libs.testcontainers.postgres)

}


Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

package org.eclipse.edc.sql;

import org.eclipse.edc.junit.annotations.PostgresqlDbIntegrationTest;
import org.eclipse.edc.junit.annotations.ComponentTest;
import org.eclipse.edc.spi.persistence.EdcPersistenceException;
import org.eclipse.edc.sql.testfixtures.PostgresqlStoreSetupExtension;
import org.jetbrains.annotations.NotNull;
Expand All @@ -35,7 +35,7 @@
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;

@PostgresqlDbIntegrationTest
@ComponentTest
@ExtendWith(PostgresqlStoreSetupExtension.class)
public class SqlQueryExecutorIntegrationTest {

Expand Down Expand Up @@ -114,5 +114,6 @@ private KeyValue insertRow(Connection connection) {
return keyValue;
}

private record KeyValue(String key, String value) { }
private record KeyValue(String key, String value) {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,40 +24,57 @@
import static java.lang.String.format;

public final class PostgresqlLocalInstance {
public static final String USER = "postgres";
public static final String PASSWORD = "password";
public static final String JDBC_URL_PREFIX = "jdbc:postgresql://localhost:5432/";
private static final String TEST_DATABASE = "itest";
private final String password;
private final String jdbcUrlPrefix;
private final String username;
private final String databaseName;

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

public static void createTestDatabase() {
createDatabase(TEST_DATABASE);
public void createDatabase() {
createDatabase(databaseName);
}

public static void createDatabase(String name) {
try (var connection = DriverManager.getConnection(JDBC_URL_PREFIX + USER, USER, PASSWORD)) {
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) {
// database could already exist
}
}

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

private static DataSource createTestDataSource() {
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[]{ "localhost" });
dataSource.setPortNumbers(new int[]{ 5432 });
dataSource.setUser(USER);
dataSource.setPassword(PASSWORD);
dataSource.setDatabaseName(TEST_DATABASE);
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 @@ -19,19 +19,22 @@
import org.eclipse.edc.transaction.datasource.spi.DataSourceRegistry;
import org.eclipse.edc.transaction.spi.NoopTransactionContext;
import org.eclipse.edc.transaction.spi.TransactionContext;
import org.junit.jupiter.api.extension.AfterAllCallback;
import org.junit.jupiter.api.extension.AfterEachCallback;
import org.junit.jupiter.api.extension.BeforeAllCallback;
import org.junit.jupiter.api.extension.BeforeEachCallback;
import org.junit.jupiter.api.extension.ExtensionContext;
import org.junit.jupiter.api.extension.ParameterContext;
import org.junit.jupiter.api.extension.ParameterResolutionException;
import org.junit.jupiter.api.extension.ParameterResolver;
import org.testcontainers.containers.PostgreSQLContainer;

import java.sql.Connection;
import java.util.List;
import java.util.UUID;
import javax.sql.DataSource;

import static java.lang.String.format;
import static org.mockito.Mockito.doCallRealMethod;
import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.mock;
Expand All @@ -42,14 +45,21 @@
* Extension for running PG SQL store implementation. It automatically creates a test database and provided all the base data structure
* for a SQL store to run such as {@link DataSourceRegistry}, {@link TransactionContext} and data source name which is automatically generated
*/
public class PostgresqlStoreSetupExtension implements BeforeEachCallback, AfterEachCallback, BeforeAllCallback, ParameterResolver {

public class PostgresqlStoreSetupExtension implements BeforeEachCallback, AfterEachCallback, BeforeAllCallback, AfterAllCallback, ParameterResolver {

public static final String POSTGRES_IMAGE_NAME = "postgres:14.2";
public static PostgreSQLContainer<?> postgreSQLContainer = new PostgreSQLContainer<>(POSTGRES_IMAGE_NAME)
.withExposedPorts(5432)
.withUsername("postgres")
.withPassword("password")
.withDatabaseName("itest");
private final String datasourceName;
private final QueryExecutor queryExecutor = new SqlQueryExecutor();
private DataSourceRegistry dataSourceRegistry = null;
private DataSource dataSource = null;
private Connection connection = null;
private TransactionContext transactionContext = null;
private final QueryExecutor queryExecutor = new SqlQueryExecutor();
private PostgresqlLocalInstance helper;

@SuppressWarnings("unused")
public PostgresqlStoreSetupExtension() {
Expand Down Expand Up @@ -89,7 +99,7 @@ public void beforeEach(ExtensionContext context) throws Exception {
transactionContext = new NoopTransactionContext();
dataSourceRegistry = mock(DataSourceRegistry.class);
dataSource = mock(DataSource.class);
connection = spy(PostgresqlLocalInstance.getTestConnection());
connection = spy(helper.getTestConnection(postgreSQLContainer.getHost(), postgreSQLContainer.getFirstMappedPort(), postgreSQLContainer.getDatabaseName()));

when(dataSourceRegistry.resolve(datasourceName)).thenReturn(dataSource);
when(dataSource.getConnection()).thenReturn(connection);
Expand All @@ -104,25 +114,35 @@ public void afterEach(ExtensionContext context) throws Exception {

@Override
public void beforeAll(ExtensionContext context) {
PostgresqlLocalInstance.createTestDatabase();
postgreSQLContainer.start();
var jdbcUrlPrefix = format("jdbc:postgresql://%s:%s/", postgreSQLContainer.getHost(), postgreSQLContainer.getFirstMappedPort());
helper = new PostgresqlLocalInstance(postgreSQLContainer.getUsername(), postgreSQLContainer.getPassword(), jdbcUrlPrefix, postgreSQLContainer.getDatabaseName());
helper.createDatabase();
}

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

@Override
public boolean supportsParameter(ParameterContext parameterContext, ExtensionContext extensionContext) throws ParameterResolutionException {
var type = parameterContext.getParameter().getParameterizedType();
return List.of(PostgresqlStoreSetupExtension.class, Connection.class, QueryExecutor.class).contains(type);
return List.of(PostgresqlStoreSetupExtension.class, Connection.class, QueryExecutor.class, PostgresqlLocalInstance.class).contains(type);
}

@Override
public Object resolveParameter(ParameterContext parameterContext, ExtensionContext extensionContext) throws
ParameterResolutionException {
public Object resolveParameter(ParameterContext parameterContext, ExtensionContext extensionContext) throws ParameterResolutionException {
var type = parameterContext.getParameter().getParameterizedType();
if (type.equals(PostgresqlStoreSetupExtension.class)) {
return this;
} else if (type.equals(Connection.class)) {
return connection;
} else if (type.equals(QueryExecutor.class)) {
return queryExecutor;
} else if (type.equals(PostgresqlLocalInstance.class)) {
return helper;
}
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

package org.eclipse.edc.sql.lease;

import org.eclipse.edc.junit.annotations.PostgresqlDbIntegrationTest;
import org.eclipse.edc.junit.annotations.ComponentTest;
import org.eclipse.edc.sql.ResultSetMapper;
import org.eclipse.edc.sql.SqlQueryExecutor;
import org.eclipse.edc.sql.testfixtures.PostgresqlLocalInstance;
Expand Down Expand Up @@ -48,7 +48,7 @@
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

@PostgresqlDbIntegrationTest
@ComponentTest
@ExtendWith(PostgresqlStoreSetupExtension.class)
class PostgresLeaseContextTest {

Expand All @@ -57,13 +57,13 @@ class PostgresLeaseContextTest {

private final TransactionContext transactionContext = new NoopTransactionContext();
private final TestEntityLeaseStatements dialect = new TestEntityLeaseStatements();
private final SqlQueryExecutor queryExecutor = new SqlQueryExecutor();
private SqlLeaseContextBuilder builder;
private SqlLeaseContext leaseContext;
private final SqlQueryExecutor queryExecutor = new SqlQueryExecutor();

@BeforeAll
static void prepare() {
PostgresqlLocalInstance.createTestDatabase();
static void prepare(PostgresqlLocalInstance postgres) {
postgres.createDatabase();
}

@BeforeEach
Expand Down
Loading

0 comments on commit 5b69dc0

Please sign in to comment.