BAEL-7547: formatting & code review
This commit is contained in:
		
							parent
							
								
									9502473ac2
								
							
						
					
					
						commit
						7d14d37fda
					
				| @ -17,7 +17,6 @@ | ||||
|             <groupId>org.springframework.boot</groupId> | ||||
|             <artifactId>spring-boot-starter-data-jpa</artifactId> | ||||
|         </dependency> | ||||
| 
 | ||||
|         <dependency> | ||||
|             <groupId>org.springframework.kafka</groupId> | ||||
|             <artifactId>spring-kafka</artifactId> | ||||
| @ -28,20 +27,27 @@ | ||||
|             <artifactId>postgresql</artifactId> | ||||
|             <version>${postgresql.version}</version> | ||||
|         </dependency> | ||||
| 
 | ||||
|         <dependency> | ||||
|             <groupId>org.springframework.modulith</groupId> | ||||
|             <artifactId>spring-modulith-events-api</artifactId> | ||||
|             <version>${spring-modulith-events-kafka.version}</version> | ||||
|             <version>${spring-modulith.version}</version> | ||||
|         </dependency> | ||||
|         <dependency> | ||||
|             <groupId>org.springframework.modulith</groupId> | ||||
|             <artifactId>spring-modulith-events-kafka</artifactId> | ||||
|             <version>${spring-modulith-events-kafka.version}</version> | ||||
|             <version>${spring-modulith.version}</version> | ||||
|         </dependency> | ||||
|         <dependency> | ||||
|             <groupId>org.springframework.modulith</groupId> | ||||
|             <artifactId>spring-modulith-starter-jpa</artifactId> | ||||
|             <version>${spring-modulith-events-kafka.version}</version> | ||||
|             <version>${spring-modulith.version}</version> | ||||
|         </dependency> | ||||
|         <dependency> | ||||
|             <groupId>org.springframework.modulith</groupId> | ||||
|             <artifactId>spring-modulith-starter-test</artifactId> | ||||
|             <version>${spring-modulith.version}</version> | ||||
|             <scope>test</scope> | ||||
|         </dependency> | ||||
| 
 | ||||
|         <dependency> | ||||
| @ -54,20 +60,6 @@ | ||||
|             <artifactId>spring-boot-testcontainers</artifactId> | ||||
|             <scope>test</scope> | ||||
|         </dependency> | ||||
| 
 | ||||
|         <dependency> | ||||
|             <groupId>org.springframework.modulith</groupId> | ||||
|             <artifactId>spring-modulith-starter-test</artifactId> | ||||
|             <version>1.1.3</version> | ||||
|         </dependency> | ||||
| 
 | ||||
|         <dependency> | ||||
|             <groupId>com.h2database</groupId> | ||||
|             <artifactId>h2</artifactId> | ||||
|             <version>2.2.224</version> | ||||
|             <scope>test</scope> | ||||
|         </dependency> | ||||
| 
 | ||||
|         <dependency> | ||||
|             <groupId>org.testcontainers</groupId> | ||||
|             <artifactId>kafka</artifactId> | ||||
| @ -86,7 +78,6 @@ | ||||
|             <version>${testcontainers.version}</version> | ||||
|             <scope>test</scope> | ||||
|         </dependency> | ||||
| 
 | ||||
|         <dependency> | ||||
|             <groupId>org.testcontainers</groupId> | ||||
|             <artifactId>postgresql</artifactId> | ||||
| @ -100,16 +91,23 @@ | ||||
|             <version>${awaitility.version}</version> | ||||
|             <scope>test</scope> | ||||
|         </dependency> | ||||
|         <dependency> | ||||
|             <groupId>com.h2database</groupId> | ||||
|             <artifactId>h2</artifactId> | ||||
|             <version>${h2.version}</version> | ||||
|             <scope>test</scope> | ||||
|         </dependency> | ||||
|     </dependencies> | ||||
| 
 | ||||
| 
 | ||||
|     <properties> | ||||
|         <java.version>17</java.version> | ||||
|         <spring-boot.version>3.1.5</spring-boot.version> | ||||
|         <spring-modulith-events-kafka.version>1.1.2</spring-modulith-events-kafka.version> | ||||
|         <spring-modulith.version>1.1.3</spring-modulith.version> | ||||
|         <testcontainers.version>1.19.3</testcontainers.version> | ||||
|         <awaitility.version>4.2.0</awaitility.version> | ||||
|         <postgresql.version>42.3.1</postgresql.version> | ||||
|         <h2.version>2.2.224</h2.version> | ||||
|     </properties> | ||||
| 
 | ||||
| </project> | ||||
|  | ||||
| @ -5,8 +5,8 @@ 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()); | ||||
| 	} | ||||
|     public Order(String customerId, List<String> productIds) { | ||||
|         this(null, customerId, productIds, Instant.now()); | ||||
|     } | ||||
| 
 | ||||
| } | ||||
|  | ||||
| @ -1,25 +1,27 @@ | ||||
| package com.baeldung.springmodulith.application.events.orders; | ||||
| 
 | ||||
| import org.springframework.stereotype.Component; | ||||
| 
 | ||||
| import java.util.ArrayList; | ||||
| import java.util.List; | ||||
| import java.util.UUID; | ||||
| 
 | ||||
| import org.springframework.stereotype.Component; | ||||
| 
 | ||||
| @Component | ||||
| class OrderRepository { | ||||
| 	private final List<Order> orders = new ArrayList<>(); | ||||
|     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 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(); | ||||
| 	} | ||||
|     public List<Order> ordersByCustomer(String customerId) { | ||||
|         return orders.stream() | ||||
|             .filter(it -> it.customerId() | ||||
|                 .equals(customerId)) | ||||
|             .toList(); | ||||
|     } | ||||
| 
 | ||||
| } | ||||
|  | ||||
| @ -1,29 +1,29 @@ | ||||
| package com.baeldung.springmodulith.application.events.orders; | ||||
| 
 | ||||
| import java.util.Arrays; | ||||
| 
 | ||||
| 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; | ||||
|     private final OrderRepository repository; | ||||
|     private final ApplicationEventPublisher eventPublisher; | ||||
| 
 | ||||
| 	public OrderService(OrderRepository orders, ApplicationEventPublisher eventsPublisher) { | ||||
| 		this.repository = orders; | ||||
| 		this.eventPublisher = eventsPublisher; | ||||
| 	} | ||||
|     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 | ||||
|     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); | ||||
|         Order savedOrder = repository.save(order); | ||||
| 
 | ||||
| 		OrderCompletedEvent event = new OrderCompletedEvent(savedOrder.id(), savedOrder.customerId(), savedOrder.timestamp()); | ||||
| 		eventPublisher.publishEvent(event); | ||||
| 	} | ||||
|         OrderCompletedEvent event = new OrderCompletedEvent(savedOrder.id(), savedOrder.customerId(), savedOrder.timestamp()); | ||||
|         eventPublisher.publishEvent(event); | ||||
|     } | ||||
| 
 | ||||
| } | ||||
|  | ||||
| @ -1,44 +1,44 @@ | ||||
| package com.baeldung.springmodulith.application.events.rewards; | ||||
| 
 | ||||
| import org.springframework.stereotype.Component; | ||||
| 
 | ||||
| import java.util.ArrayList; | ||||
| import java.util.List; | ||||
| import java.util.Optional; | ||||
| 
 | ||||
| import org.springframework.stereotype.Component; | ||||
| 
 | ||||
| @Component | ||||
| public class LoyalCustomersRepository { | ||||
| 
 | ||||
| 	private List<LoyalCustomer> customers = new ArrayList<>(); | ||||
|     private List<LoyalCustomer> customers = new ArrayList<>(); | ||||
| 
 | ||||
| 	public Optional<LoyalCustomer> find(String customerId) { | ||||
| 		return customers.stream() | ||||
| 		  .filter(it -> it.customerId().equals(customerId)) | ||||
| 		  .findFirst(); | ||||
| 	} | ||||
|     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) | ||||
| 		  .orElseGet(() -> save(new LoyalCustomer(customerId, 0))); | ||||
|     public void awardPoints(String customerId, int points) { | ||||
|         var customer = find(customerId).orElseGet(() -> save(new LoyalCustomer(customerId, 0))); | ||||
| 
 | ||||
| 		customers.remove(customer); | ||||
| 		customers.add(customer.addPoints(points)); | ||||
| 	} | ||||
|         customers.remove(customer); | ||||
|         customers.add(customer.addPoints(points)); | ||||
|     } | ||||
| 
 | ||||
| 	public LoyalCustomer save(LoyalCustomer customer) { | ||||
| 		customers.add(customer); | ||||
| 		return customer; | ||||
| 	} | ||||
|     public LoyalCustomer save(LoyalCustomer customer) { | ||||
|         customers.add(customer); | ||||
|         return customer; | ||||
|     } | ||||
| 
 | ||||
| 	public boolean isLoyalCustomer(String customerId) { | ||||
| 		return find(customerId).isPresent(); | ||||
| 	} | ||||
|     public boolean isLoyalCustomer(String customerId) { | ||||
|         return find(customerId).isPresent(); | ||||
|     } | ||||
| 
 | ||||
| 	public record LoyalCustomer(String customerId, int points) { | ||||
|     public record LoyalCustomer(String customerId, int points) { | ||||
| 
 | ||||
| 		LoyalCustomer addPoints(int points) { | ||||
| 			return new LoyalCustomer(customerId, this.points() + points); | ||||
| 		} | ||||
| 	} | ||||
|         LoyalCustomer addPoints(int points) { | ||||
|             return new LoyalCustomer(customerId, this.points() + points); | ||||
|         } | ||||
|     } | ||||
| 
 | ||||
| } | ||||
|  | ||||
| @ -1,23 +1,24 @@ | ||||
| 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; | ||||
| 
 | ||||
| import com.baeldung.springmodulith.application.events.orders.OrderCompletedEvent; | ||||
| 
 | ||||
| @Service | ||||
| public class LoyaltyPointsService { | ||||
| 
 | ||||
| 	public static final int ORDER_COMPLETED_POINTS = 60; | ||||
| 	private final LoyalCustomersRepository loyalCustomers; | ||||
|     public static final int ORDER_COMPLETED_POINTS = 60; | ||||
|     private final LoyalCustomersRepository loyalCustomers; | ||||
| 
 | ||||
| 	public LoyaltyPointsService(LoyalCustomersRepository loyalCustomers) { | ||||
| 		this.loyalCustomers = loyalCustomers; | ||||
| 	} | ||||
|     public LoyaltyPointsService(LoyalCustomersRepository loyalCustomers) { | ||||
|         this.loyalCustomers = loyalCustomers; | ||||
|     } | ||||
| 
 | ||||
| 	@EventListener | ||||
| 	public void onOrderCompleted(OrderCompletedEvent event) { | ||||
| 		// business logic to award points to loyal customers | ||||
| 		loyalCustomers.awardPoints(event.customerId(), ORDER_COMPLETED_POINTS); | ||||
| 	} | ||||
|     @EventListener | ||||
|     public void onOrderCompleted(OrderCompletedEvent event) { | ||||
|         // business logic to award points to loyal customers | ||||
|         loyalCustomers.awardPoints(event.customerId(), ORDER_COMPLETED_POINTS); | ||||
|     } | ||||
| 
 | ||||
| } | ||||
|  | ||||
| @ -4,12 +4,10 @@ import static org.assertj.core.api.Assertions.assertThat; | ||||
| 
 | ||||
| import java.time.Instant; | ||||
| 
 | ||||
| import org.assertj.core.api.Assertions; | ||||
| import org.junit.jupiter.api.Test; | ||||
| import org.springframework.beans.factory.annotation.Autowired; | ||||
| import org.springframework.boot.test.context.SpringBootTest; | ||||
| import org.springframework.context.ApplicationEventPublisher; | ||||
| import org.springframework.context.annotation.ComponentScan; | ||||
| 
 | ||||
| import com.baeldung.springmodulith.application.events.orders.OrderCompletedEvent; | ||||
| import com.baeldung.springmodulith.application.events.rewards.LoyalCustomersRepository; | ||||
| @ -17,22 +15,22 @@ import com.baeldung.springmodulith.application.events.rewards.LoyalCustomersRepo | ||||
| @SpringBootTest | ||||
| class EventListenerUnitTest { | ||||
| 
 | ||||
| 	@Autowired | ||||
| 	private LoyalCustomersRepository customers; | ||||
|     @Autowired | ||||
|     private LoyalCustomersRepository customers; | ||||
| 
 | ||||
| 	@Autowired | ||||
| 	private ApplicationEventPublisher testEventPublisher; | ||||
|     @Autowired | ||||
|     private ApplicationEventPublisher testEventPublisher; | ||||
| 
 | ||||
| 	@Test | ||||
| 	void whenPublishingOrderCompletedEvent_thenRewardCustomerWithLoyaltyPoints() { | ||||
| 		OrderCompletedEvent event = new OrderCompletedEvent("order-1", "customer-1", Instant.now()); | ||||
|     @Test | ||||
|     void whenPublishingOrderCompletedEvent_thenRewardCustomerWithLoyaltyPoints() { | ||||
|         OrderCompletedEvent event = new OrderCompletedEvent("order-1", "customer-1", Instant.now()); | ||||
| 
 | ||||
| 		testEventPublisher.publishEvent(event); | ||||
|         testEventPublisher.publishEvent(event); | ||||
| 
 | ||||
| 		assertThat(customers.find("customer-1")) | ||||
| 		  .isPresent().get() | ||||
| 		  .hasFieldOrPropertyWithValue("customerId", "customer-1") | ||||
| 		  .hasFieldOrPropertyWithValue("points", 60); | ||||
| 	} | ||||
|         assertThat(customers.find("customer-1")) | ||||
|           .isPresent().get() | ||||
|           .hasFieldOrPropertyWithValue("customerId", "customer-1") | ||||
|           .hasFieldOrPropertyWithValue("points", 60); | ||||
|     } | ||||
| 
 | ||||
| } | ||||
|  | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user