GH-17 - Bootstrap PostgreSQL in a container and execute integration tests against it

Signed-off-by: Björn Kieling <bkieling@vmware.com>
This commit is contained in:
Dmitry Belyaev
2022-07-25 18:53:46 +02:00
parent 32de1846de
commit ce1ef8c540
9 changed files with 64 additions and 23 deletions

View File

@@ -13,8 +13,21 @@
<properties>
<module.name>org.springframework.modulith.events.jdbc</module.name>
<testcontainers.version>1.17.3</testcontainers.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>testcontainers-bom</artifactId>
<version>${testcontainers.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
@@ -41,6 +54,30 @@
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
<scope>test</scope>
</dependency>
<!-- Test Containers -->
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>testcontainers</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>junit-jupiter</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>postgresql</artifactId>
<scope>test</scope>
</dependency>
<!-- -->
<dependency>
<groupId>org.hsqldb</groupId>
<artifactId>hsqldb</artifactId>
@@ -60,5 +97,4 @@
</dependency>
</dependencies>
</project>

View File

@@ -31,7 +31,7 @@ import org.springframework.modulith.events.config.EventPublicationConfigurationE
class JdbcEventPublicationAutoConfiguration implements EventPublicationConfigurationExtension {
@Bean
JdbcEventPublicationRepository jpaEventPublicationRepository(JdbcTemplate jdbcTemplate, EventSerializer serializer) {
JdbcEventPublicationRepository jdbcEventPublicationRepository(JdbcTemplate jdbcTemplate, EventSerializer serializer) {
return new JdbcEventPublicationRepository(jdbcTemplate, serializer);
}

View File

@@ -22,6 +22,7 @@ import lombok.extern.slf4j.Slf4j;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.time.Instant;
import java.util.ArrayList;
import java.util.List;
@@ -81,7 +82,7 @@ class JdbcEventPublicationRepository implements EventPublicationRepository {
UUID.randomUUID(), //
publication.getEvent().getClass().getName(), //
publication.getTargetIdentifier().getValue(), //
publication.getPublicationDate(), //
Timestamp.from(publication.getPublicationDate()), //
serializeEvent(publication.getEvent()));
return publication;
@@ -96,7 +97,10 @@ class JdbcEventPublicationRepository implements EventPublicationRepository {
(rs, rowNum) -> rs.getObject("ID", UUID.class), serializedEvent, publication.getTargetIdentifier().getValue());
if (!results.isEmpty()) {
operations.update(SQL_STATEMENT_UPDATE, publication.getCompletionDate().orElse(null), results.get(0));
operations.update(
SQL_STATEMENT_UPDATE,
publication.getCompletionDate().map(Timestamp::from).orElse(null),
results.get(0));
}
return publication;

View File

@@ -1,10 +1,10 @@
CREATE TABLE IF NOT EXISTS EVENT_PUBLICATION
(
ID UUID NOT NULL,
COMPLETION_DATE TIMESTAMP(9) WITH TIME ZONE,
EVENT_TYPE VARCHAR(512) NOT NULL,
LISTENER_ID VARCHAR(512) NOT NULL,
PUBLICATION_DATE TIMESTAMP(9) WITH TIME ZONE NOT NULL,
EVENT_TYPE VARCHAR(512) NOT NULL,
SERIALIZED_EVENT VARCHAR(4000) NOT NULL,
PUBLICATION_DATE TIMESTAMP(9) WITH TIME ZONE NOT NULL,
COMPLETION_DATE TIMESTAMP(9) WITH TIME ZONE,
PRIMARY KEY (ID)
)

View File

@@ -21,7 +21,6 @@ import static org.mockito.Mockito.*;
import java.util.Optional;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
@@ -44,10 +43,10 @@ class DatabaseSchemaInitializerIntegrationTests {
private static final String COUNT_PUBLICATIONS = "SELECT COUNT(*) FROM EVENT_PUBLICATION";
@JdbcTest
@ImportAutoConfiguration(JdbcEventPublicationAutoConfiguration.class)
@ContextConfiguration(classes = TestApplication.class)
static class TestBase {
@MockBean EventSerializer serializer;
}
@@ -88,6 +87,7 @@ class DatabaseSchemaInitializerIntegrationTests {
}
@Nested
@JdbcTest
class InitializationDisabledByDefault extends TestBase {
@SpyBean JdbcOperations operations;
@@ -106,16 +106,13 @@ class DatabaseSchemaInitializerIntegrationTests {
@Nested
@ActiveProfiles("hsqldb")
class HSQLDB extends WithInitEnabled {
}
class HSQLDB extends WithInitEnabled {}
@Nested
@ActiveProfiles("h2")
class H2 extends WithInitEnabled {}
@Nested
@Disabled
@ActiveProfiles("postgres")
class Postgres extends WithInitEnabled {}
}

View File

@@ -21,7 +21,6 @@ import static org.mockito.Mockito.*;
import lombok.Value;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
@@ -37,6 +36,8 @@ import org.springframework.modulith.testapp.TestApplication;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.ContextConfiguration;
import java.time.temporal.ChronoUnit;
/**
* Integration tests for {@link JdbcEventPublicationRepository}.
*
@@ -161,7 +162,8 @@ class JdbcEventPublicationRepositoryIntegrationTests {
var actual = repository.findByEventAndTargetIdentifier(testEvent, TARGET_IDENTIFIER);
assertThat(actual).hasValueSatisfying(it -> {
assertThat(it.getPublicationDate()).isEqualTo(publicationOld.getPublicationDate());
assertThat(it.getPublicationDate()) //
.isCloseTo(publicationOld.getPublicationDate(), within(1, ChronoUnit.MILLIS));
});
}
@@ -194,7 +196,6 @@ class JdbcEventPublicationRepositoryIntegrationTests {
class H2 extends TestBase {}
@Nested
@Disabled
@ActiveProfiles("postgres")
class Postgres extends TestBase {}

View File

@@ -1,4 +1,6 @@
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.url=jdbc:h2:mem:testdb;DB_CLOSE_DELAY=-1
#
spring.test.database.replace=NONE
#
spring.modulith.events.schema-initialization.enabled=true

View File

@@ -1,4 +1,6 @@
spring.datasource.driverClassName=org.hsqldb.jdbc.JDBCDriver
spring.datasource.url=jdbc:hsqldb:mem:testdb;DB_CLOSE_DELAY=-1
#
spring.test.database.replace=NONE
#
spring.modulith.events.schema-initialization.enabled=true

View File

@@ -1,7 +1,6 @@
spring.datasource.driverClassName=org.postgresql.Driver
spring.datasource.url=jdbc:postgresql://localhost:5432/postgres
spring.datasource.username=username
spring.datasource.password=password
spring.datasource.url=jdbc:tc:postgresql:13.2:////postgres
spring.datasource.driverClassName=org.testcontainers.jdbc.ContainerDatabaseDriver
#
spring.test.database.replace=NONE
#
spring.modulith.events.schema-initialization.enabled=true