Skip to content

Commit

Permalink
[Order API TDD] Move Inner class for test to Upper leve
Browse files Browse the repository at this point in the history
  • Loading branch information
jihunparkme committed Feb 27, 2024
1 parent c697d1f commit 7db7c50
Show file tree
Hide file tree
Showing 7 changed files with 108 additions and 86 deletions.
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보다 커야 합니다.");
}
}
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;
}
}
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);
}
}
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);
}
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);
}
}
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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,8 @@

import com.example.productorderservice.product.DiscountPolicy;
import com.example.productorderservice.product.Product;
import com.example.productorderservice.product.ProductRepository;
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;

class OrderServiceTest {

Expand All @@ -35,91 +30,15 @@ public void save(final Order order) {

@Test
void 상품주문() {
final long productId = 1L;
final int quantity = 2;
final CreateOrderRequest request = new CreateOrderRequest(productId, quantity);
final CreateOrderRequest request = getCreateOrderRequest();

orderService.createOrder(request);
}

private record CreateOrderRequest(long productId, int quantity) {
private CreateOrderRequest {
Assert.notNull(productId, "상품 ID는 필수입니다.");
Assert.isTrue(quantity > 0, "수량은 0보다 커야 합니다.");
}
}

private class OrderService {
private final OrderPort orderPort;

private 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);
}
}

private 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);
}
}

private 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;
}
}

private 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);
}
private static CreateOrderRequest getCreateOrderRequest() {
final long productId = 1L;
final int quantity = 2;
return new CreateOrderRequest(productId, quantity);
}

private interface OrderPort {
Product getProductById(final long productId);
void save(final Order order);
}
}

0 comments on commit 7db7c50

Please sign in to comment.