diff --git a/spring-boot-modules/spring-boot-libraries-3/src/main/java/com/baeldung/springmodulith/application/events/rewards/LoyaltyPointsService.java b/spring-boot-modules/spring-boot-libraries-3/src/main/java/com/baeldung/springmodulith/application/events/rewards/LoyaltyPointsService.java index 20af0caa6f..3febcf0d66 100644 --- a/spring-boot-modules/spring-boot-libraries-3/src/main/java/com/baeldung/springmodulith/application/events/rewards/LoyaltyPointsService.java +++ b/spring-boot-modules/spring-boot-libraries-3/src/main/java/com/baeldung/springmodulith/application/events/rewards/LoyaltyPointsService.java @@ -7,8 +7,7 @@ import org.springframework.stereotype.Service; @Service public class LoyaltyPointsService { - public static final int ORDER_COMPLETED_POINTS = 10; - public static final int SING_UP_POINTS = 50; + public static final int ORDER_COMPLETED_POINTS = 60; private final LoyalCustomersRepository loyalCustomers; public LoyaltyPointsService(LoyalCustomersRepository loyalCustomers) { @@ -17,11 +16,7 @@ public class LoyaltyPointsService { @EventListener public void onOrderCompleted(OrderCompletedEvent event) { - if (loyalCustomers.find(event.customerId()).isEmpty()) { - loyalCustomers.save(event.customerId()); - loyalCustomers.awardPoints(event.customerId(), SING_UP_POINTS); - } - + // business logic to award points to loyal customers loyalCustomers.awardPoints(event.customerId(), ORDER_COMPLETED_POINTS); } diff --git a/spring-boot-modules/spring-boot-libraries-3/src/test/java/com/baeldung/springmodulith/application/events/EventListenerUnitTest.java b/spring-boot-modules/spring-boot-libraries-3/src/test/java/com/baeldung/springmodulith/application/events/EventListenerUnitTest.java index b8ad79e6f1..104cc1690d 100644 --- a/spring-boot-modules/spring-boot-libraries-3/src/test/java/com/baeldung/springmodulith/application/events/EventListenerUnitTest.java +++ b/spring-boot-modules/spring-boot-libraries-3/src/test/java/com/baeldung/springmodulith/application/events/EventListenerUnitTest.java @@ -16,7 +16,7 @@ import com.baeldung.springmodulith.application.events.rewards.LoyalCustomersRepo @SpringBootTest @ComponentScan(basePackages = "com.baeldung.springmodulith.application.events") -public class EventListenerUnitTest { +class EventListenerUnitTest { @Autowired private LoyalCustomersRepository customers; @@ -31,9 +31,9 @@ public class EventListenerUnitTest { testEventPublisher.publishEvent(event); assertThat(customers.find("customer-1")) - .isPresent().get() - .hasFieldOrPropertyWithValue("customerId", "customer-1") - .hasFieldOrPropertyWithValue("points", 60); + .isPresent().get() + .hasFieldOrPropertyWithValue("customerId", "customer-1") + .hasFieldOrPropertyWithValue("points", 60); } } diff --git a/spring-boot-modules/spring-boot-libraries-3/src/test/java/com/baeldung/springmodulith/application/events/EventPublisherUnitTest.java b/spring-boot-modules/spring-boot-libraries-3/src/test/java/com/baeldung/springmodulith/application/events/EventPublisherUnitTest.java index 0989bb5fd3..16f20defc3 100644 --- a/spring-boot-modules/spring-boot-libraries-3/src/test/java/com/baeldung/springmodulith/application/events/EventPublisherUnitTest.java +++ b/spring-boot-modules/spring-boot-libraries-3/src/test/java/com/baeldung/springmodulith/application/events/EventPublisherUnitTest.java @@ -1,18 +1,17 @@ package com.baeldung.springmodulith.application.events; -import static org.assertj.core.api.Assertions.assertThat; - +import com.baeldung.springmodulith.application.events.orders.OrderService; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.context.annotation.ComponentScan; -import com.baeldung.springmodulith.application.events.orders.OrderService; +import static org.assertj.core.api.Assertions.assertThat; @SpringBootTest @ComponentScan(basePackages = "com.baeldung.springmodulith.application.events") -public class EventPublisherUnitTest { +class EventPublisherUnitTest { @Autowired OrderService orderService; @@ -30,10 +29,10 @@ public class EventPublisherUnitTest { orderService.placeOrder("customer1", "product1", "product2"); assertThat(testEventListener.getEvents()) - .hasSize(1).first() - .hasFieldOrPropertyWithValue("customerId", "customer1") - .hasFieldOrProperty("orderId") - .hasFieldOrProperty("timestamp"); + .hasSize(1).first() + .hasFieldOrPropertyWithValue("customerId", "customer1") + .hasFieldOrProperty("orderId") + .hasFieldOrProperty("timestamp"); } } diff --git a/spring-boot-modules/spring-boot-libraries-3/src/test/java/com/baeldung/springmodulith/application/events/SpringModulithScenarioApiUnitTest.java b/spring-boot-modules/spring-boot-libraries-3/src/test/java/com/baeldung/springmodulith/application/events/SpringModulithScenarioApiUnitTest.java index 17fe4a555c..8d262a24d0 100644 --- a/spring-boot-modules/spring-boot-libraries-3/src/test/java/com/baeldung/springmodulith/application/events/SpringModulithScenarioApiUnitTest.java +++ b/spring-boot-modules/spring-boot-libraries-3/src/test/java/com/baeldung/springmodulith/application/events/SpringModulithScenarioApiUnitTest.java @@ -1,19 +1,22 @@ package com.baeldung.springmodulith.application.events; +import com.baeldung.springmodulith.application.events.orders.OrderCompletedEvent; import com.baeldung.springmodulith.application.events.orders.OrderService; import com.baeldung.springmodulith.application.events.rewards.LoyalCustomersRepository; - import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.context.annotation.ComponentScan; import org.springframework.modulith.test.ApplicationModuleTest; +import org.springframework.modulith.test.ApplicationModuleTest.BootstrapMode; import org.springframework.modulith.test.Scenario; +import java.time.Duration; +import java.time.Instant; + +import static java.time.Duration.ofMillis; import static org.assertj.core.api.Assertions.assertThat; @ApplicationModuleTest -@ComponentScan(basePackages = "com.baeldung.springmodulith.application.events") -public class SpringModulithScenarioApiUnitTest { +class SpringModulithScenarioApiUnitTest { @Autowired OrderService orderService; @@ -22,8 +25,24 @@ public class SpringModulithScenarioApiUnitTest { LoyalCustomersRepository loyalCustomers; @Test - void test(Scenario scenario) { - + void whenPlacingOrder_thenPublishOrderCompletedEvent(Scenario scenario) { + scenario.stimulate(() -> orderService.placeOrder("customer-1", "product-1", "product-2")) + .andWaitAtMost(ofMillis(500)) + .andWaitForEventOfType(OrderCompletedEvent.class) + .toArriveAndVerify(evt -> assertThat(evt) + .hasFieldOrPropertyWithValue("customerId", "customer-1") + .hasFieldOrProperty("orderId") + .hasFieldOrProperty("timestamp")); } + @Test + void whenReceivingPublishOrderCompletedEvent_thenRewardCustomerWithLoyaltyPoints(Scenario scenario) { + scenario.publish(new OrderCompletedEvent("order-1", "customer-1", Instant.now())) + .andWaitAtMost(ofMillis(500)) + .andWaitForStateChange(() -> loyalCustomers.find("customer-1")) + .andVerify(it -> assertThat(it) + .isPresent().get() + .hasFieldOrPropertyWithValue("customerId", "customer-1") + .hasFieldOrPropertyWithValue("points", 60)); + } }