#33 tdd(order-service): order - jpa

This commit is contained in:
haerong22
2023-02-14 00:25:31 +09:00
parent 66c8feb24b
commit 7a7fbefb95
5 changed files with 45 additions and 80 deletions

View File

@@ -1,12 +1,26 @@
package com.example.productorderservice.order;
import com.example.productorderservice.product.Product;
import lombok.AccessLevel;
import lombok.Getter;
import lombok.NoArgsConstructor;
import org.springframework.util.Assert;
import javax.persistence.*;
@Entity
@Table(name = "orders")
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@Getter
class Order {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private final Product product;
private final int quantity;
@OneToOne
private Product product;
private int quantity;
public Order(Product product, int quantity) {
Assert.notNull(product, "상품은 필수입니다.");
@@ -15,11 +29,4 @@ class Order {
this.quantity = quantity;
}
public void assignId(Long id) {
this.id = id;
}
public Long getId() {
return this.id;
}
}

View File

@@ -1,18 +1,8 @@
package com.example.productorderservice.order;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import java.util.HashMap;
import java.util.Map;
@Repository
class OrderRepository {
private final Map<Long, Order> persistence = new HashMap<>();
private Long sequence = 0L;
public void save(Order order) {
order.assignId(++sequence);
persistence.put(order.getId(), order);
}
public interface OrderRepository extends JpaRepository<Order, Long> {
}

View File

@@ -16,23 +16,11 @@ class OrderApiTest extends ApiTest {
@Test
void 상품주문() {
ProductSteps.상품등록요청(ProductSteps.상품등록요청_생성());
final CreateOrderRequest request = 상품주문요청_생성();
final CreateOrderRequest request = OrderSteps.상품주문요청_생성();
ExtractableResponse<Response> response = RestAssured.given().log().all()
.contentType(MediaType.APPLICATION_JSON_VALUE)
.body(request)
.when()
.post("/orders")
.then()
.log().all().extract();
final var response = OrderSteps.상품주문요청(request);
assertThat(response.statusCode()).isEqualTo(HttpStatus.CREATED.value());
}
private static CreateOrderRequest 상품주문요청_생성() {
final Long productId = 1L;
final int quantity = 2;
return new CreateOrderRequest(productId, quantity);
}
}

View File

@@ -1,45 +0,0 @@
package com.example.productorderservice.order;
import com.example.productorderservice.product.DiscountPolicy;
import com.example.productorderservice.product.Product;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
class OrderServiceUnitTest {
private OrderService orderService;
private OrderPort orderPort;
private OrderRepository orderRepository;
@BeforeEach
void setUp() {
orderRepository = new OrderRepository();
orderPort = new OrderPort() {
@Override
public Product getProductById(Long productId) {
return new Product("상품명", 1000, DiscountPolicy.NONE);
}
@Override
public void save(Order order) {
orderRepository.save(order);
}
};
orderService = new OrderService(orderPort);
}
@Test
void 상품주문() {
final CreateOrderRequest request = 상품주문요청_생성();
orderService.createOrder(request);
}
private static CreateOrderRequest 상품주문요청_생성() {
final Long productId = 1L;
final int quantity = 2;
return new CreateOrderRequest(productId, quantity);
}
}

View File

@@ -0,0 +1,25 @@
package com.example.productorderservice.order;
import io.restassured.RestAssured;
import io.restassured.response.ExtractableResponse;
import io.restassured.response.Response;
import org.springframework.http.MediaType;
public class OrderSteps {
public static ExtractableResponse<Response> 상품주문요청(CreateOrderRequest request) {
return RestAssured.given().log().all()
.contentType(MediaType.APPLICATION_JSON_VALUE)
.body(request)
.when()
.post("/orders")
.then()
.log().all().extract();
}
public static CreateOrderRequest 상품주문요청_생성() {
final Long productId = 1L;
final int quantity = 2;
return new CreateOrderRequest(productId, quantity);
}
}