Skip to content

Commit

Permalink
feat(test): convert all Postgres-tests to use Testcontainers
Browse files Browse the repository at this point in the history
  • Loading branch information
paullatzelsperger committed Jul 5, 2023
1 parent 09d1568 commit a50fca9
Show file tree
Hide file tree
Showing 19 changed files with 135 additions and 66 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
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ dependencies {
testImplementation(testFixtures(project(":extensions:common:sql:sql-core")))
testImplementation(libs.postgres)

testImplementation(libs.testcontainers.junit)
testImplementation(libs.testcontainers.postgres)
}


Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import com.fasterxml.jackson.databind.ObjectMapper;
import org.eclipse.edc.connector.store.sql.assetindex.schema.BaseSqlDialectStatements;
import org.eclipse.edc.connector.store.sql.assetindex.schema.postgres.PostgresDialectStatements;
import org.eclipse.edc.junit.annotations.PostgresqlDbIntegrationTest;
import org.eclipse.edc.junit.annotations.ComponentTest;
import org.eclipse.edc.policy.model.PolicyRegistrationTypes;
import org.eclipse.edc.spi.testfixtures.asset.AssetIndexTestBase;
import org.eclipse.edc.spi.types.TypeManager;
Expand All @@ -32,7 +32,7 @@
import java.nio.file.Files;
import java.nio.file.Paths;

@PostgresqlDbIntegrationTest
@ComponentTest
@ExtendWith(PostgresqlStoreSetupExtension.class)
class PostgresAssetIndexTest extends AssetIndexTestBase {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import org.eclipse.edc.connector.contract.spi.testfixtures.offer.store.TestFunctions;
import org.eclipse.edc.connector.store.sql.contractdefinition.schema.BaseSqlDialectStatements;
import org.eclipse.edc.connector.store.sql.contractdefinition.schema.postgres.PostgresDialectStatements;
import org.eclipse.edc.junit.annotations.PostgresqlDbIntegrationTest;
import org.eclipse.edc.junit.annotations.ComponentTest;
import org.eclipse.edc.policy.model.PolicyRegistrationTypes;
import org.eclipse.edc.spi.query.Criterion;
import org.eclipse.edc.spi.query.QuerySpec;
Expand All @@ -42,7 +42,7 @@
import static java.util.stream.IntStream.range;
import static org.assertj.core.api.Assertions.assertThatThrownBy;

@PostgresqlDbIntegrationTest
@ComponentTest
@ExtendWith(PostgresqlStoreSetupExtension.class)
class PostgresContractDefinitionStoreTest extends ContractDefinitionStoreTestBase {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import org.eclipse.edc.connector.contract.spi.ContractId;
import org.eclipse.edc.connector.contract.spi.testfixtures.negotiation.store.ContractNegotiationStoreTestBase;
import org.eclipse.edc.connector.store.sql.contractnegotiation.store.schema.postgres.PostgresDialectStatements;
import org.eclipse.edc.junit.annotations.PostgresqlDbIntegrationTest;
import org.eclipse.edc.junit.annotations.ComponentTest;
import org.eclipse.edc.policy.model.Action;
import org.eclipse.edc.policy.model.AtomicConstraint;
import org.eclipse.edc.policy.model.LiteralExpression;
Expand Down Expand Up @@ -62,14 +62,14 @@
* This test aims to verify those parts of the contract negotiation store, that are specific to Postgres, e.g. JSON
* query operators.
*/
@PostgresqlDbIntegrationTest
@ComponentTest
@ExtendWith(PostgresqlStoreSetupExtension.class)
class PostgresContractNegotiationStoreTest extends ContractNegotiationStoreTestBase {

private static final String TEST_ASSET_ID = "test-asset-id";
private final Clock clock = Clock.systemUTC();
private SqlContractNegotiationStore store;
private LeaseUtil leaseUtil;
private final Clock clock = Clock.systemUTC();

@BeforeEach
void setUp(PostgresqlStoreSetupExtension extension, QueryExecutor queryExecutor) throws IOException {
Expand Down Expand Up @@ -257,7 +257,7 @@ void nextNotLeased_typeFilter() {
.state(REQUESTED.code())
.type(CONSUMER)
.build()).forEach(store::save);
var criteria = new Criterion[]{hasState(REQUESTED.code()), new Criterion("type", "=", "CONSUMER")};
var criteria = new Criterion[]{ hasState(REQUESTED.code()), new Criterion("type", "=", "CONSUMER") };

var result = store.nextNotLeased(10, criteria);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import org.eclipse.edc.connector.policy.spi.testfixtures.store.PolicyDefinitionStoreTestBase;
import org.eclipse.edc.connector.store.sql.policydefinition.store.SqlPolicyDefinitionStore;
import org.eclipse.edc.connector.store.sql.policydefinition.store.schema.postgres.PostgresDialectStatements;
import org.eclipse.edc.junit.annotations.PostgresqlDbIntegrationTest;
import org.eclipse.edc.junit.annotations.ComponentTest;
import org.eclipse.edc.policy.model.PolicyRegistrationTypes;
import org.eclipse.edc.spi.query.QuerySpec;
import org.eclipse.edc.spi.query.SortOrder;
Expand All @@ -44,7 +44,7 @@
* This test aims to verify those parts of the policy definition store, that are specific to Postgres, e.g. JSON query
* operators.
*/
@PostgresqlDbIntegrationTest
@ComponentTest
@ExtendWith(PostgresqlStoreSetupExtension.class)
class PostgresPolicyDefinitionStoreTest extends PolicyDefinitionStoreTestBase {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import org.eclipse.edc.connector.transfer.spi.testfixtures.store.TransferProcessStoreTestBase;
import org.eclipse.edc.connector.transfer.spi.types.ProvisionedResourceSet;
import org.eclipse.edc.connector.transfer.spi.types.ResourceManifest;
import org.eclipse.edc.junit.annotations.PostgresqlDbIntegrationTest;
import org.eclipse.edc.junit.annotations.ComponentTest;
import org.eclipse.edc.policy.model.PolicyRegistrationTypes;
import org.eclipse.edc.spi.query.Criterion;
import org.eclipse.edc.spi.query.QuerySpec;
Expand Down Expand Up @@ -49,7 +49,7 @@
import static org.eclipse.edc.connector.transfer.spi.testfixtures.store.TestFunctions.createTransferProcess;
import static org.eclipse.edc.connector.transfer.spi.testfixtures.store.TestFunctions.createTransferProcessBuilder;

@PostgresqlDbIntegrationTest
@ComponentTest
@ExtendWith(PostgresqlStoreSetupExtension.class)
class PostgresTransferProcessStoreTest extends TransferProcessStoreTestBase {

Expand Down
Loading

0 comments on commit a50fca9

Please sign in to comment.