#33 tdd(order-service): update product - pojo

This commit is contained in:
haerong22
2023-02-09 01:13:47 +09:00
parent 6035b8ea22
commit 80c4fdb121
7 changed files with 105 additions and 5 deletions

View File

@@ -29,4 +29,12 @@ class Product {
this.discountPolicy = discountPolicy;
}
public void update(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;
}
}

View File

@@ -29,4 +29,11 @@ class ProductService {
product.getDiscountPolicy()
);
}
public void updateProduct(Long productId, UpdateProductRequest request) {
final Product product = productPort.getProduct(productId);
product.update(request.name(), request.price(), request.discountPolicy());
productPort.save(product);
}
}

View File

@@ -0,0 +1,12 @@
package com.example.productorderservice.product;
import org.springframework.util.Assert;
record UpdateProductRequest(String name, int price, DiscountPolicy discountPolicy) {
UpdateProductRequest {
Assert.hasText(name, "상품명은 필수입니다.");
Assert.isTrue(price > 0, "상품 가격은 0보다 커야 합니다.");
Assert.notNull(discountPolicy, "할인 정책은 필수입니다.");
}
}

View File

@@ -25,11 +25,7 @@ class ProductApiTest extends ApiTest {
ProductSteps.상품등록요청(ProductSteps.상품등록요청_생성());
final Long productId = 1L;
ExtractableResponse<Response> response = RestAssured.given().log().all()
.when()
.get("/products/{productId}", productId)
.then().log().all()
.extract();
final var response = ProductSteps.상품조회요청(productId);
assertThat(response.statusCode()).isEqualTo(HttpStatus.OK.value());
assertThat(response.jsonPath().getString("name")).isEqualTo("상품명");

View File

@@ -0,0 +1,50 @@
package com.example.productorderservice.product;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.junit.jupiter.MockitoExtension;
import static org.assertj.core.api.Assertions.assertThat;
@ExtendWith(MockitoExtension.class)
class ProductServicePojoTest {
private ProductService productService;
private StubProductPort productPort = new StubProductPort();
@BeforeEach
void setUp() {
productService = new ProductService(productPort);
}
@Test
void 상품수정() {
final Long productId = 1L;
final Product product = new Product("상품명", 1000, DiscountPolicy.NONE);
final UpdateProductRequest request = new UpdateProductRequest("상품수정", 2000, DiscountPolicy.NONE);
productPort.getProduct_will_return = product;
productService = new ProductService(productPort);
productService.updateProduct(productId, request);
assertThat(product.getName()).isEqualTo("상품수정");
assertThat(product.getPrice()).isEqualTo(2000);
}
private static class StubProductPort implements ProductPort {
public Product getProduct_will_return;
@Override
public void save(Product product) {
}
@Override
public Product getProduct(long productId) {
return getProduct_will_return;
}
}
}

View File

@@ -25,4 +25,12 @@ public class ProductSteps {
final AddProductRequest request = new AddProductRequest(name, price, discountPolicy);
return request;
}
public static ExtractableResponse<Response> 상품조회요청(Long productId) {
return RestAssured.given().log().all()
.when()
.get("/products/{productId}", productId)
.then().log().all()
.extract();
}
}

View File

@@ -0,0 +1,19 @@
package com.example.productorderservice.product;
import org.junit.jupiter.api.Test;
import static org.assertj.core.api.Assertions.assertThat;
class ProductTest {
@Test
void update() {
Product product = new Product("상품명", 1000, DiscountPolicy.NONE);
product.update("상품수정", 2000, DiscountPolicy.NONE);
assertThat(product.getName()).isEqualTo("상품수정");
assertThat(product.getPrice()).isEqualTo(2000);
}
}