BAEL-7523: wip

This commit is contained in:
emanuel.trandafir 2024-02-23 20:36:07 +01:00
parent b9ee399220
commit 7e0a40fec2
4 changed files with 63 additions and 52 deletions

View File

@ -17,11 +17,22 @@
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId> <artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.springframework.kafka</groupId> <groupId>org.springframework.kafka</groupId>
<artifactId>spring-kafka</artifactId> <artifactId>spring-kafka</artifactId>
</dependency> </dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.13.1</version>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>${postgresql.version}</version>
</dependency>
<dependency> <dependency>
<groupId>org.springframework.modulith</groupId> <groupId>org.springframework.modulith</groupId>
<artifactId>spring-modulith-events-api</artifactId> <artifactId>spring-modulith-events-api</artifactId>
@ -33,13 +44,6 @@
<version>${spring-modulith-events-kafka.version}</version> <version>${spring-modulith-events-kafka.version}</version>
</dependency> </dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.13.1</version>
</dependency>
<dependency> <dependency>
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId> <artifactId>spring-boot-starter-test</artifactId>
@ -65,14 +69,14 @@
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.testcontainers</groupId> <groupId>org.testcontainers</groupId>
<artifactId>oracle-xe</artifactId> <artifactId>junit-jupiter</artifactId>
<version>${testcontainers.version}</version> <version>${testcontainers.version}</version>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.testcontainers</groupId> <groupId>org.testcontainers</groupId>
<artifactId>junit-jupiter</artifactId> <artifactId>postgresql</artifactId>
<version>${testcontainers.version}</version> <version>${testcontainers.version}</version>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
@ -92,6 +96,7 @@
<spring-modulith-events-kafka.version>1.1.2</spring-modulith-events-kafka.version> <spring-modulith-events-kafka.version>1.1.2</spring-modulith-events-kafka.version>
<testcontainers.version>1.19.3</testcontainers.version> <testcontainers.version>1.19.3</testcontainers.version>
<awaitility.version>4.2.0</awaitility.version> <awaitility.version>4.2.0</awaitility.version>
<postgresql.version>42.3.1</postgresql.version>
</properties> </properties>
</project> </project>

View File

@ -6,11 +6,9 @@ import org.springframework.transaction.annotation.Transactional;
@Service @Service
public class Baeldung { public class Baeldung {
private final ApplicationEventPublisher applicationEvents; private final ApplicationEventPublisher applicationEvents;
private final ArticleRepository articleRepository; private final ArticleRepository articleRepository;
public Baeldung(ApplicationEventPublisher applicationEvents, ArticleRepository articleRepository) { public Baeldung(ApplicationEventPublisher applicationEvents, ArticleRepository articleRepository) {
this.applicationEvents = applicationEvents; this.applicationEvents = applicationEvents;
this.articleRepository = articleRepository; this.articleRepository = articleRepository;
@ -26,7 +24,6 @@ public class Baeldung {
applicationEvents.publishEvent(new ArticlePublishedEvent(article.slug(), article.title())); applicationEvents.publishEvent(new ArticlePublishedEvent(article.slug(), article.title()));
} }
private Article addArticleTags(Article article) { private Article addArticleTags(Article article) {
return article; return article;
} }

View File

@ -1,4 +1,3 @@
logging.level.org.springframework.orm.jpa: TRACE
spring.kafka: spring.kafka:
bootstrap-servers: localhost:9092 bootstrap-servers: localhost:9092
@ -12,7 +11,18 @@ spring.kafka:
auto-offset-reset: earliest auto-offset-reset: earliest
spring.modulith: spring.modulith:
events: republish-outstanding-events-on-restart: true
republish-outstanding-events-on-startup: true events.jdbc.schema-initialization.enabled: true
jdbc.schema-initialization.enabled: true
logging.level.org.springframework.orm.jpa: TRACE
spring:
datasource:
# url: jdbc:postgresql://localhost:5432/test_db
username: test_user
password: test_pass
jpa:
properties:
hibernate:
dialect: org.hibernate.dialect.PostgreSQLDialect
hbm2ddl.auto: create

View File

@ -1,10 +1,8 @@
package com.baeldung.springmodulith.events.externalization; package com.baeldung.springmodulith.events.externalization;
import static java.time.Duration.ofMillis; import com.baeldung.springmodulith.Application;
import static java.time.Duration.ofSeconds; import com.baeldung.springmodulith.events.externalization.listener.TestKafkaListenerConfig;
import static org.assertj.core.api.Assertions.assertThat; import com.baeldung.springmodulith.events.externalization.listener.TestListener;
import static org.testcontainers.shaded.org.awaitility.Awaitility.await;
import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
@ -12,15 +10,16 @@ import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.DynamicPropertyRegistry; import org.springframework.test.context.DynamicPropertyRegistry;
import org.springframework.test.context.DynamicPropertySource; import org.springframework.test.context.DynamicPropertySource;
import org.testcontainers.containers.KafkaContainer; import org.testcontainers.containers.KafkaContainer;
import org.testcontainers.containers.OracleContainer; import org.testcontainers.containers.PostgreSQLContainer;
import org.testcontainers.junit.jupiter.Container; import org.testcontainers.junit.jupiter.Container;
import org.testcontainers.junit.jupiter.Testcontainers; import org.testcontainers.junit.jupiter.Testcontainers;
import org.testcontainers.shaded.org.awaitility.Awaitility; import org.testcontainers.shaded.org.awaitility.Awaitility;
import org.testcontainers.utility.DockerImageName; import org.testcontainers.utility.DockerImageName;
import com.baeldung.springmodulith.Application; import static java.time.Duration.ofMillis;
import com.baeldung.springmodulith.events.externalization.listener.TestKafkaListenerConfig; import static java.time.Duration.ofSeconds;
import com.baeldung.springmodulith.events.externalization.listener.TestListener; import static org.assertj.core.api.Assertions.assertThat;
import static org.testcontainers.shaded.org.awaitility.Awaitility.await;
@Testcontainers @Testcontainers
@SpringBootTest(classes = { Application.class, TestKafkaListenerConfig.class }) @SpringBootTest(classes = { Application.class, TestKafkaListenerConfig.class })
@ -37,26 +36,26 @@ class EventsExternalizationLiveTest {
static KafkaContainer kafkaContainer = new KafkaContainer(DockerImageName.parse("confluentinc/cp-kafka:latest")); static KafkaContainer kafkaContainer = new KafkaContainer(DockerImageName.parse("confluentinc/cp-kafka:latest"));
@Container @Container
static OracleContainer oracleContainer = new OracleContainer("gvenzl/oracle-xe:21-slim-faststart") public static PostgreSQLContainer postgresqlContainer = new PostgreSQLContainer()
.withDatabaseName("test") .withDatabaseName("test_db")
.withUsername("user") .withUsername("test_user")
.withPassword("pass"); .withPassword("test_pass");
@DynamicPropertySource @DynamicPropertySource
static void dynamicProperties(DynamicPropertyRegistry registry) { static void dynamicProperties(DynamicPropertyRegistry registry) {
registry.add("spring.kafka.bootstrap-servers", kafkaContainer::getBootstrapServers); registry.add("spring.kafka.bootstrap-servers", kafkaContainer::getBootstrapServers);
registry.add("spring.datasource.url", oracleContainer::getJdbcUrl); registry.add("spring.datasource.url", postgresqlContainer::getJdbcUrl);
} }
static { static {
Awaitility.setDefaultTimeout(ofSeconds(5)); Awaitility.setDefaultTimeout(ofSeconds(50));
Awaitility.setDefaultPollDelay(ofMillis(100)); Awaitility.setDefaultPollDelay(ofMillis(100));
} }
@BeforeEach @BeforeEach
void beforeEach() { void beforeEach() {
listener.reset(); listener.reset();
repository.deleteAll(); // repository.deleteAll();
} }
@Test @Test