-
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 test: ProductService addProduct test
- Loading branch information
1 parent
fe97b0f
commit cd9618b
Showing
1 changed file
with
116 additions
and
0 deletions.
There are no files selected for viewing
116 changes: 116 additions & 0 deletions
116
...der-service/src/test/java/com/example/productorderservice/product/ProductServiceTest.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,116 @@ | ||
package com.example.productorderservice.product; | ||
|
||
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; | ||
|
||
public class ProductServiceTest { | ||
|
||
private ProductService productService; | ||
private ProductPort productPort; | ||
private ProductRepository productRepository; | ||
|
||
@BeforeEach | ||
void setUp() { | ||
productRepository = new ProductRepository(); | ||
productPort = new ProductAdapter(productRepository); | ||
productService = new ProductService(productPort); | ||
} | ||
|
||
@Test | ||
void 상품등록() { | ||
final String name = "상품명"; | ||
final int price = 1000; | ||
final DiscountPolicy discountPolicy = DiscountPolicy.NONE; | ||
final AddProductRequest request = new AddProductRequest(name, price, discountPolicy); // 02. Request 클래스 생성 | ||
|
||
productService.addProduct(request); // 01. 상품 등록 서비스 호출 | ||
} | ||
|
||
/** | ||
* 03. Request 는 record 로 사용 가능 | ||
*/ | ||
private record AddProductRequest(String name, int price, DiscountPolicy discountPolicy) { | ||
private AddProductRequest { | ||
// 03-1. Assert 를 활용해서 파라미터 체크 | ||
Assert.hasText(name, "상품명은 필수입니다."); | ||
Assert.isTrue(price > 0, "상품 가격은 0보다 커야 합니다."); | ||
Assert.notNull(discountPolicy, "할인 정책은 필수입니다."); | ||
} | ||
} | ||
|
||
private enum DiscountPolicy { | ||
NONE | ||
} | ||
|
||
/** | ||
* 04. ProductService 메서드 구현 | ||
*/ | ||
private class ProductService { | ||
private final ProductPort productPort; | ||
|
||
private ProductService(final ProductPort productPort) { | ||
this.productPort = productPort; | ||
} | ||
|
||
public void addProduct(final AddProductRequest request) { | ||
final Product product = new Product(request.name(), request.price(), request.discountPolicy()); // 05. Product 클래스 생성 | ||
|
||
productPort.save(product); | ||
} | ||
} | ||
|
||
private class Product { | ||
private Long id; | ||
private final String name; | ||
private final int price; | ||
private final DiscountPolicy discountPolicy; | ||
|
||
public Product(final String name, final int price, final DiscountPolicy discountPolicy) { | ||
Assert.hasText(name, "상품명은 필수입니다."); | ||
Assert.isTrue(price > 0, "상품 가격은 0보다 커야 합니다."); | ||
Assert.notNull(discountPolicy, "할인 정책은 필수입니다."); | ||
this.name = name; | ||
this.price = price; | ||
this.discountPolicy = discountPolicy; | ||
} | ||
|
||
public void assigId(final Long id) { | ||
this.id = id; | ||
} | ||
|
||
public Long getId() { | ||
return id; | ||
} | ||
} | ||
|
||
private interface ProductPort { | ||
void save(final Product product); | ||
} | ||
|
||
private class ProductAdapter implements ProductPort { | ||
private final ProductRepository productRepository; | ||
|
||
private ProductAdapter(final ProductRepository productRepository) { | ||
this.productRepository = productRepository; | ||
} | ||
|
||
@Override | ||
public void save(final Product product) { | ||
productRepository.save(product); | ||
} | ||
} | ||
|
||
private class ProductRepository { | ||
private Map<Long, Product> persistence = new HashMap<>(); | ||
private Long sequence = 0L; | ||
|
||
public void save(final Product product) { | ||
product.assigId(++sequence); | ||
persistence.put(product.getId(), product); | ||
} | ||
} | ||
} |