#33 tdd(order-service): add product - pojo
This commit is contained in:
@@ -0,0 +1,12 @@
|
||||
package com.example.productorderservice.product;
|
||||
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
record AddProductRequest(String name, int price, DiscountPolicy discountPolicy) {
|
||||
AddProductRequest {
|
||||
Assert.hasText(name, "상품명은 필수입니다.");
|
||||
Assert.isTrue(price > 0, "상품 가격은 0보다 커야 합니다.");
|
||||
Assert.notNull(discountPolicy, "할인 정책은 필수입니다.");
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
package com.example.productorderservice.product;
|
||||
|
||||
enum DiscountPolicy {
|
||||
NONE
|
||||
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
package com.example.productorderservice.product;
|
||||
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
class Product {
|
||||
private Long id;
|
||||
private final String name;
|
||||
private final int price;
|
||||
private final DiscountPolicy discountPolicy;
|
||||
|
||||
public Product(String name, int price, DiscountPolicy discountPolicy) {
|
||||
Assert.hasText(name, "상품명은 필수입니다.");
|
||||
Assert.isTrue(price > 0, "상품 가격은 0보다 커야 합니다.");
|
||||
Assert.notNull(discountPolicy, "할인 정책은 필수입니다.");
|
||||
|
||||
this.name = name;
|
||||
this.price = price;
|
||||
this.discountPolicy = discountPolicy;
|
||||
}
|
||||
|
||||
public void assignId(final Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
package com.example.productorderservice.product;
|
||||
|
||||
class ProductAdapter implements ProductPort {
|
||||
|
||||
private final ProductRepository productRepository;
|
||||
|
||||
ProductAdapter(ProductRepository productRepository) {
|
||||
this.productRepository = productRepository;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void save(Product product) {
|
||||
productRepository.save(product);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
package com.example.productorderservice.product;
|
||||
|
||||
interface ProductPort {
|
||||
void save(Product product);
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
package com.example.productorderservice.product;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
class ProductRepository {
|
||||
private Map<Long, Product> persistence = new HashMap<>();
|
||||
private Long sequence = 0L;
|
||||
|
||||
public void save(final Product product) {
|
||||
product.assignId(++sequence);
|
||||
persistence.put(product.getId(), product);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
package com.example.productorderservice.product;
|
||||
|
||||
class ProductService {
|
||||
private final ProductPort productPort;
|
||||
|
||||
ProductService(ProductPort productPort) {
|
||||
this.productPort = productPort;
|
||||
}
|
||||
|
||||
|
||||
public void addProduct(AddProductRequest request) {
|
||||
final Product product = new Product(request.name(), request.price(), request.discountPolicy());
|
||||
|
||||
productPort.save(product);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
package com.example.productorderservice.product;
|
||||
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
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 AddProductRequest request = 상품등록요청_생성();
|
||||
|
||||
productService.addProduct(request);
|
||||
}
|
||||
|
||||
private static AddProductRequest 상품등록요청_생성() {
|
||||
final String name = "상품명";
|
||||
final int price = 1000;
|
||||
final DiscountPolicy discountPolicy = DiscountPolicy.NONE;
|
||||
|
||||
final AddProductRequest request = new AddProductRequest(name, price, discountPolicy);
|
||||
return request;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user