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

View File

@ -6,32 +6,29 @@ import org.springframework.transaction.annotation.Transactional;
@Service
public class Baeldung {
private final ApplicationEventPublisher applicationEvents;
private final ArticleRepository articleRepository;
private final ApplicationEventPublisher applicationEvents;
private final ArticleRepository articleRepository;
public Baeldung(ApplicationEventPublisher applicationEvents, ArticleRepository articleRepository) {
this.applicationEvents = applicationEvents;
this.articleRepository = articleRepository;
}
@Transactional
public void createArticle(Article article) {
// ... business logic
validateArticle(article);
article = addArticleTags(article);
article = articleRepository.save(article);
public Baeldung(ApplicationEventPublisher applicationEvents, ArticleRepository articleRepository) {
this.applicationEvents = applicationEvents;
this.articleRepository = articleRepository;
}
applicationEvents.publishEvent(new ArticlePublishedEvent(article.slug(), article.title()));
}
@Transactional
public void createArticle(Article article) {
// ... business logic
validateArticle(article);
article = addArticleTags(article);
article = articleRepository.save(article);
private Article addArticleTags(Article article) {
return article;
}
applicationEvents.publishEvent(new ArticlePublishedEvent(article.slug(), article.title()));
}
private Article addArticleTags(Article article) {
return article;
}
private void validateArticle(Article article) {
}
private void validateArticle(Article article) {
}
}

View File

@ -1,4 +1,3 @@
logging.level.org.springframework.orm.jpa: TRACE
spring.kafka:
bootstrap-servers: localhost:9092
@ -12,7 +11,18 @@ spring.kafka:
auto-offset-reset: earliest
spring.modulith:
events:
republish-outstanding-events-on-startup: true
jdbc.schema-initialization.enabled: true
republish-outstanding-events-on-restart: true
events.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;
import static java.time.Duration.ofMillis;
import static java.time.Duration.ofSeconds;
import static org.assertj.core.api.Assertions.assertThat;
import static org.testcontainers.shaded.org.awaitility.Awaitility.await;
import com.baeldung.springmodulith.Application;
import com.baeldung.springmodulith.events.externalization.listener.TestKafkaListenerConfig;
import com.baeldung.springmodulith.events.externalization.listener.TestListener;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
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.DynamicPropertySource;
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.Testcontainers;
import org.testcontainers.shaded.org.awaitility.Awaitility;
import org.testcontainers.utility.DockerImageName;
import com.baeldung.springmodulith.Application;
import com.baeldung.springmodulith.events.externalization.listener.TestKafkaListenerConfig;
import com.baeldung.springmodulith.events.externalization.listener.TestListener;
import static java.time.Duration.ofMillis;
import static java.time.Duration.ofSeconds;
import static org.assertj.core.api.Assertions.assertThat;
import static org.testcontainers.shaded.org.awaitility.Awaitility.await;
@Testcontainers
@SpringBootTest(classes = { Application.class, TestKafkaListenerConfig.class })
@ -37,26 +36,26 @@ class EventsExternalizationLiveTest {
static KafkaContainer kafkaContainer = new KafkaContainer(DockerImageName.parse("confluentinc/cp-kafka:latest"));
@Container
static OracleContainer oracleContainer = new OracleContainer("gvenzl/oracle-xe:21-slim-faststart")
.withDatabaseName("test")
.withUsername("user")
.withPassword("pass");
public static PostgreSQLContainer postgresqlContainer = new PostgreSQLContainer()
.withDatabaseName("test_db")
.withUsername("test_user")
.withPassword("test_pass");
@DynamicPropertySource
static void dynamicProperties(DynamicPropertyRegistry registry) {
registry.add("spring.kafka.bootstrap-servers", kafkaContainer::getBootstrapServers);
registry.add("spring.datasource.url", oracleContainer::getJdbcUrl);
registry.add("spring.datasource.url", postgresqlContainer::getJdbcUrl);
}
static {
Awaitility.setDefaultTimeout(ofSeconds(5));
Awaitility.setDefaultTimeout(ofSeconds(50));
Awaitility.setDefaultPollDelay(ofMillis(100));
}
@BeforeEach
void beforeEach() {
listener.reset();
repository.deleteAll();
// repository.deleteAll();
}
@Test
@ -94,4 +93,4 @@ class EventsExternalizationLiveTest {
.extracting(Article::title, Article::author)
.containsExactly("Introduction to Spring Boot", "John Doe");
}
}
}