-
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.
[Order API TDD] Move Inner class for test to Upper leve
- Loading branch information
1 parent
c697d1f
commit 7db7c50
Showing
7 changed files
with
108 additions
and
86 deletions.
There are no files selected for viewing
10 changes: 10 additions & 0 deletions
10
...order-service/src/main/java/com/example/productorderservice/order/CreateOrderRequest.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,10 @@ | ||
package com.example.productorderservice.order; | ||
|
||
import org.springframework.util.Assert; | ||
|
||
record CreateOrderRequest(long productId, int quantity) { | ||
CreateOrderRequest { | ||
Assert.notNull(productId, "상품 ID는 필수입니다."); | ||
Assert.isTrue(quantity > 0, "수량은 0보다 커야 합니다."); | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
product-order-service/src/main/java/com/example/productorderservice/order/Order.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,25 @@ | ||
package com.example.productorderservice.order; | ||
|
||
import com.example.productorderservice.product.Product; | ||
import org.springframework.util.Assert; | ||
|
||
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; | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
product-order-service/src/main/java/com/example/productorderservice/order/OrderAdapter.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,26 @@ | ||
package com.example.productorderservice.order; | ||
|
||
import com.example.productorderservice.product.Product; | ||
import com.example.productorderservice.product.ProductRepository; | ||
|
||
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); | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
product-order-service/src/main/java/com/example/productorderservice/order/OrderPort.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,9 @@ | ||
package com.example.productorderservice.order; | ||
|
||
import com.example.productorderservice.product.Product; | ||
|
||
interface OrderPort { | ||
Product getProductById(final long productId); | ||
|
||
void save(final Order order); | ||
} |
14 changes: 14 additions & 0 deletions
14
...ct-order-service/src/main/java/com/example/productorderservice/order/OrderRepository.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,14 @@ | ||
package com.example.productorderservice.order; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
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); | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
product-order-service/src/main/java/com/example/productorderservice/order/OrderService.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,19 @@ | ||
package com.example.productorderservice.order; | ||
|
||
import com.example.productorderservice.product.Product; | ||
|
||
class OrderService { | ||
private final OrderPort orderPort; | ||
|
||
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); | ||
} | ||
} |
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