tdd : picking system - order validation

This commit is contained in:
haerong22
2021-08-03 19:29:37 +09:00
parent e6f2115892
commit e330fca802
4 changed files with 65 additions and 10 deletions

View File

@@ -4,5 +4,5 @@ import com.example.pickingtdd.entity.Order;
public interface OrderService {
Order createOrder(Order order);
Order createOrder(Order order) throws Exception;
}

View File

@@ -7,7 +7,21 @@ import org.springframework.stereotype.Service;
public class OrderServiceImpl implements OrderService {
@Override
public Order createOrder(Order order) {
return order;
public Order createOrder(Order order) throws Exception {
if (orderValidation(order)) {
return order;
} else {
throw new Exception("order validation fail");
}
}
private boolean orderValidation(Order order) {
if (order.getOrderId() == null) {
return false;
}
if (order.getState() == null || order.getState().length() == 0) {
return false;
}
return true;
}
}

View File

@@ -2,25 +2,65 @@ package com.example.pickingtdd.service;
import com.example.pickingtdd.entity.Order;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import static org.junit.jupiter.api.Assertions.*;
@SpringBootTest
public class OrderServiceTests {
@Autowired
OrderService orderService;
Order orderSuccess;
Order orderFail;
@BeforeEach
void init() {
orderSuccess = new Order();
orderSuccess.setOrderId(1L);
orderSuccess.setState("Ordered");
orderFail = new Order();
orderFail.setOrderId(null);
orderFail.setState("");
}
@Test
void create_order_success() {
void createOrder_success() {
Order order = new Order();
order.setOrderId(1L);
order.setState("Ordered");
try {
order = orderService.createOrder(orderSuccess);
} catch (Exception e) {
// do nothing
}
order = orderService.createOrder(order);
assertEquals(1L, order.getOrderId());
assertEquals("Ordered", order.getState());
}
Assertions.assertEquals(1L, order.getOrderId());
Assertions.assertEquals("Ordered", order.getState());
@Test
void orderValidation_success() {
Order order = new Order();
try {
order = orderService.createOrder(orderSuccess);
} catch (Exception e) {
fail("should not throw exception");
}
assertEquals(1L, order.getOrderId());
assertEquals("Ordered", order.getState());
}
@Test
void orderValidation_fail() {
Exception e = assertThrows(Exception.class, () -> {
orderService.createOrder(orderFail);
});
assertEquals("order validation fail", e.getMessage());
}
}

View File

@@ -8,6 +8,7 @@ SKU
주문 -> 피킹리스트 -> 피커 -> 피킹완료
------- TO-DO -------
[] ORDER 검증
[] Order.state => enum 변경
[v] ORDER 검증
[v] ORDER 생성