From 7e0a40fec298fb151ecb6a505a237fb245f50cec Mon Sep 17 00:00:00 2001 From: "emanuel.trandafir" Date: Fri, 23 Feb 2024 20:36:07 +0100 Subject: [PATCH] BAEL-7523: wip --- .../spring-boot-libraries-3/pom.xml | 23 +++++++---- .../events/externalization/Baeldung.java | 41 +++++++++---------- .../src/main/resources/application.yml | 18 ++++++-- .../EventsExternalizationLiveTest.java | 33 ++++++++------- 4 files changed, 63 insertions(+), 52 deletions(-) diff --git a/spring-boot-modules/spring-boot-libraries-3/pom.xml b/spring-boot-modules/spring-boot-libraries-3/pom.xml index 3b5176a99a..f84951f450 100644 --- a/spring-boot-modules/spring-boot-libraries-3/pom.xml +++ b/spring-boot-modules/spring-boot-libraries-3/pom.xml @@ -17,11 +17,22 @@ org.springframework.boot spring-boot-starter-data-jpa + org.springframework.kafka spring-kafka + + com.fasterxml.jackson.core + jackson-databind + 2.13.1 + + + org.postgresql + postgresql + ${postgresql.version} + org.springframework.modulith spring-modulith-events-api @@ -33,13 +44,6 @@ ${spring-modulith-events-kafka.version} - - - com.fasterxml.jackson.core - jackson-databind - 2.13.1 - - org.springframework.boot spring-boot-starter-test @@ -65,14 +69,14 @@ org.testcontainers - oracle-xe + junit-jupiter ${testcontainers.version} test org.testcontainers - junit-jupiter + postgresql ${testcontainers.version} test @@ -92,6 +96,7 @@ 1.1.2 1.19.3 4.2.0 + 42.3.1 diff --git a/spring-boot-modules/spring-boot-libraries-3/src/main/java/com/baeldung/springmodulith/events/externalization/Baeldung.java b/spring-boot-modules/spring-boot-libraries-3/src/main/java/com/baeldung/springmodulith/events/externalization/Baeldung.java index 1d309a8653..4b861a49c8 100644 --- a/spring-boot-modules/spring-boot-libraries-3/src/main/java/com/baeldung/springmodulith/events/externalization/Baeldung.java +++ b/spring-boot-modules/spring-boot-libraries-3/src/main/java/com/baeldung/springmodulith/events/externalization/Baeldung.java @@ -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) { + } } diff --git a/spring-boot-modules/spring-boot-libraries-3/src/main/resources/application.yml b/spring-boot-modules/spring-boot-libraries-3/src/main/resources/application.yml index c1c9db1489..9885be68b4 100644 --- a/spring-boot-modules/spring-boot-libraries-3/src/main/resources/application.yml +++ b/spring-boot-modules/spring-boot-libraries-3/src/main/resources/application.yml @@ -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 diff --git a/spring-boot-modules/spring-boot-libraries-3/src/test/java/com/baeldung/springmodulith/events/externalization/EventsExternalizationLiveTest.java b/spring-boot-modules/spring-boot-libraries-3/src/test/java/com/baeldung/springmodulith/events/externalization/EventsExternalizationLiveTest.java index 0ce33f6bd8..4cb94bbe1a 100644 --- a/spring-boot-modules/spring-boot-libraries-3/src/test/java/com/baeldung/springmodulith/events/externalization/EventsExternalizationLiveTest.java +++ b/spring-boot-modules/spring-boot-libraries-3/src/test/java/com/baeldung/springmodulith/events/externalization/EventsExternalizationLiveTest.java @@ -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"); } -} \ No newline at end of file +}