-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8990a65
commit c697d1f
Showing
5 changed files
with
186 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...t-order-service/src/main/java/com/example/productorderservice/product/DiscountPolicy.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
package com.example.productorderservice.product; | ||
|
||
enum DiscountPolicy { | ||
public enum DiscountPolicy { | ||
NONE | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
125 changes: 125 additions & 0 deletions
125
...t-order-service/src/test/java/com/example/productorderservice/order/OrderServiceTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
package com.example.productorderservice.order; | ||
|
||
import com.example.productorderservice.product.DiscountPolicy; | ||
import com.example.productorderservice.product.Product; | ||
import com.example.productorderservice.product.ProductRepository; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.springframework.util.Assert; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
class OrderServiceTest { | ||
|
||
private OrderService orderService; | ||
private OrderPort orderPort; | ||
private OrderRepository orderRepository; | ||
|
||
@BeforeEach | ||
void setUp() { | ||
orderRepository = new OrderRepository(); | ||
orderPort = new OrderPort() { | ||
@Override | ||
public Product getProductById(final long productId) { | ||
return new Product("상품명", 1000, DiscountPolicy.NONE); | ||
} | ||
|
||
@Override | ||
public void save(final Order order) { | ||
orderRepository.save(order); | ||
} | ||
}; | ||
orderService = new OrderService(orderPort); | ||
} | ||
|
||
@Test | ||
void 상품주문() { | ||
final long productId = 1L; | ||
final int quantity = 2; | ||
final CreateOrderRequest request = new CreateOrderRequest(productId, quantity); | ||
|
||
orderService.createOrder(request); | ||
} | ||
|
||
private record CreateOrderRequest(long productId, int quantity) { | ||
private CreateOrderRequest { | ||
Assert.notNull(productId, "상품 ID는 필수입니다."); | ||
Assert.isTrue(quantity > 0, "수량은 0보다 커야 합니다."); | ||
} | ||
} | ||
|
||
private class OrderService { | ||
private final OrderPort orderPort; | ||
|
||
private OrderService(final OrderPort orderPort) { | ||
this.orderPort = orderPort; | ||
} | ||
|
||
public void createOrder(final CreateOrderRequest request) { | ||
final Product product = orderPort.getProductById(request.productId()); | ||
|
||
final Order order = new Order(product, request.quantity()); | ||
|
||
orderPort.save(order); | ||
} | ||
} | ||
|
||
private class OrderAdapter implements OrderPort{ | ||
|
||
private final ProductRepository productRepository; | ||
private final OrderRepository orderRepository; | ||
|
||
private OrderAdapter(final ProductRepository productRepository, final OrderRepository orderRepository) { | ||
this.productRepository = productRepository; | ||
this.orderRepository = orderRepository; | ||
} | ||
|
||
@Override | ||
public Product getProductById(final long productId) { | ||
return productRepository.findById(productId) | ||
.orElseThrow(() -> new IllegalArgumentException("상품이 존재하지 않습니다.")); | ||
} | ||
|
||
@Override | ||
public void save(final Order order) { | ||
orderRepository.save(order); | ||
} | ||
} | ||
|
||
private class Order { | ||
private Long id; | ||
private final Product product; | ||
private final int quantity; | ||
|
||
public Order(final Product product, final int quantity) { | ||
Assert.notNull(product, "상품은 필수입니다."); | ||
Assert.isTrue(quantity > 0, "수량은 0보다 커야 합니다."); | ||
this.product = product; | ||
this.quantity = quantity; | ||
} | ||
|
||
public void assignId(final Long id) { | ||
this.id = id; | ||
} | ||
|
||
public Long getId() { | ||
return id; | ||
} | ||
} | ||
|
||
private class OrderRepository { | ||
private Map<Long, Order> persistence = new HashMap<>(); | ||
private Long sequance = 0L; | ||
|
||
public void save(final Order order) { | ||
order.assignId(++sequance); | ||
persistence.put(order.getId(), order); | ||
} | ||
} | ||
|
||
private interface OrderPort { | ||
Product getProductById(final long productId); | ||
void save(final Order order); | ||
} | ||
} |