BAEL-7547: testing spring events

This commit is contained in:
emanuel.trandafir 2024-03-21 18:07:54 +01:00
parent 40879b5237
commit 4a95314449
7 changed files with 179 additions and 0 deletions

View File

@ -0,0 +1,12 @@
package com.baeldung.springmodulith.application.events.orders;
import java.time.Instant;
import java.util.List;
record Order(String id, String customerId, List<String> productIds, Instant timestamp) {
public Order(String customerId, List<String> productIds) {
this(null, customerId, productIds, Instant.now());
}
}

View File

@ -0,0 +1,6 @@
package com.baeldung.springmodulith.application.events.orders;
import java.time.Instant;
public record OrderCompletedEvent(String orderId, String customerId, Instant time) {
}

View File

@ -0,0 +1,25 @@
package com.baeldung.springmodulith.application.events.orders;
import org.springframework.stereotype.Component;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
@Component
class OrderRepository {
private final List<Order> orders = new ArrayList<>();
public Order save(Order order) {
order = new Order(UUID.randomUUID().toString(), order.customerId(), order.productIds(), order.timestamp());
orders.add(order);
return order;
}
public List<Order> ordersByCustomer(String customerId) {
return orders.stream()
.filter(it -> it.customerId().equals(customerId))
.toList();
}
}

View File

@ -0,0 +1,29 @@
package com.baeldung.springmodulith.application.events.orders;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.stereotype.Service;
import java.util.Arrays;
@Service
public class OrderService {
private final OrderRepository repository;
private final ApplicationEventPublisher eventPublisher;
public OrderService(OrderRepository orders, ApplicationEventPublisher eventsPublisher) {
this.repository = orders;
this.eventPublisher = eventsPublisher;
}
public void placeOrder(String customerId, String... productIds) {
Order order = new Order(customerId, Arrays.asList(productIds));
// business logic to validate and place the order
Order savedOrder = repository.save(order);
OrderCompletedEvent event = new OrderCompletedEvent(savedOrder.id(), savedOrder.customerId(), savedOrder.timestamp());
eventPublisher.publishEvent(event);
}
}

View File

@ -0,0 +1,41 @@
package com.baeldung.springmodulith.application.events.rewards;
import org.springframework.stereotype.Component;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
@Component
public class LoyalCustomersRepository {
private List<LoyalCustomer> customers = new ArrayList<>();
public Optional<LoyalCustomer> find(String customerId) {
return customers.stream()
.filter(it -> it.customerId().equals(customerId))
.findFirst();
}
public void awardPoints(String customerId, int points) {
var customer = find(customerId).orElseThrow();
customers.remove(customer);
customers.add(customer.addPoints(points));
}
public void save(String customerId) {
customers.add(new LoyalCustomer(customerId, 0));
}
public boolean isLoyalCustomer(String customerId) {
return find(customerId).isPresent();
}
public record LoyalCustomer(String customerId, int points) {
LoyalCustomer addPoints(int points) {
return new LoyalCustomer(customerId, this.points() + points);
}
}
}

View File

@ -0,0 +1,28 @@
package com.baeldung.springmodulith.application.events.rewards;
import com.baeldung.springmodulith.application.events.orders.OrderCompletedEvent;
import org.springframework.context.event.EventListener;
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;
private final LoyalCustomersRepository loyalCustomers;
public LoyaltyPointsService(LoyalCustomersRepository loyalCustomers) {
this.loyalCustomers = loyalCustomers;
}
@EventListener
public void onOrderCompleted(OrderCompletedEvent event) {
if (loyalCustomers.find(event.customerId()).isEmpty()) {
loyalCustomers.save(event.customerId());
loyalCustomers.awardPoints(event.customerId(), SING_UP_POINTS);
}
loyalCustomers.awardPoints(event.customerId(), ORDER_COMPLETED_POINTS);
}
}

View File

@ -0,0 +1,38 @@
package com.baeldung.springmodulith.application.events;
import com.baeldung.springmodulith.application.events.orders.OrderService;
import com.baeldung.springmodulith.application.events.rewards.LoyalCustomersRepository;
import com.baeldung.springmodulith.application.events.rewards.LoyalCustomersRepository.LoyalCustomer;
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 java.util.Optional;
import static org.assertj.core.api.Assertions.assertThat;
@SpringBootTest
@ComponentScan(basePackages = "com.baeldung.springmodulith.application.events")
public class ApplicationEventsTest {
@Autowired
OrderService orderService;
@Autowired
LoyalCustomersRepository loyalCustomers;
@Test
void whenNewCustomerCompletesAnOrder_thenHeReceivesSingUpPointsPlusOrderPoints() {
orderService.placeOrder("customer1", "product1", "product2");
Optional<LoyalCustomer> customer = loyalCustomers.find("customer1");
assertThat(customer).isPresent()
.get()
.extracting(LoyalCustomer::points)
.isEqualTo(50 + 10);
}
}