diff --git a/src/main/java/org/store/clothstar/order/controller/OrderController.java b/src/main/java/org/store/clothstar/order/controller/OrderController.java index cfe92724..3c203640 100644 --- a/src/main/java/org/store/clothstar/order/controller/OrderController.java +++ b/src/main/java/org/store/clothstar/order/controller/OrderController.java @@ -2,7 +2,6 @@ import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.tags.Tag; -import jakarta.annotation.security.PermitAll; import lombok.RequiredArgsConstructor; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; @@ -29,40 +28,26 @@ public class OrderController { @Operation(summary = "단일 주문 조회", description = "단일 주문의 정보를 조회한다.") @GetMapping("/{orderId}") - public ResponseEntity getOrder(@PathVariable("orderId") Long orderId) { - + public ResponseEntity getOrder(@PathVariable Long orderId) { OrderResponse orderResponse = orderService.getOrder(orderId); - - return ResponseEntity.ok().body(orderResponse); + return ResponseEntity.ok(orderResponse); } - @PermitAll @Operation(summary = "주문 생성", description = "단일 주문을 생성한다.") @PostMapping - public ResponseEntity saveOrder( - @RequestBody @Validated OrderRequestWrapper orderRequestWrapper) { - + public ResponseEntity saveOrder(@RequestBody @Validated OrderRequestWrapper orderRequestWrapper) { Long orderId = orderApplicationService.saveOrderWithTransaction(orderRequestWrapper); - URI location = URIBuilder.buildURI(orderId); - return ResponseEntity.created(location).build(); } @Operation(summary = "구매 확정", description = "구매자가 구매 확정 시, 주문상태가 '구매확정'으로 변경된다.") @PatchMapping("/{orderId}") - public ResponseEntity deliveredToConfirmOrder(@PathVariable("orderId") Long orderId) { - + public ResponseEntity deliveredToConfirmOrder(@PathVariable Long orderId) { orderService.deliveredToConfirmOrder(orderId); - - return ResponseEntity.ok() - .body(new MessageDTO( - HttpStatus.OK.value(), - "주문이 정상적으로 구매 확정 되었습니다.")); + return ResponseEntity.ok(new MessageDTO(HttpStatus.OK.value(), "주문이 정상적으로 구매 확정 되었습니다.")); } } - - diff --git a/src/main/java/org/store/clothstar/order/controller/OrderSellerController.java b/src/main/java/org/store/clothstar/order/controller/OrderSellerController.java index 7df83881..b87b6f06 100644 --- a/src/main/java/org/store/clothstar/order/controller/OrderSellerController.java +++ b/src/main/java/org/store/clothstar/order/controller/OrderSellerController.java @@ -24,20 +24,21 @@ public class OrderSellerController { @Operation(summary = "(판매자) WAITING 주문 리스트 조회", description = "(판매자) 주문상태가 '승인대기'인 주문 리스트를 조회한다.") @GetMapping public ResponseEntity> getWaitingOrder() { - List orderResponseList = orderSellerService.getWaitingOrder(); - return ResponseEntity.ok(orderResponseList); } - @Operation(summary = "(판매자) 주문 승인 또는 취소", description = "(판매자) 주문을 승인 또는 취소한다.") - @PatchMapping("/{orderId}") - public ResponseEntity cancelOrApproveOrder( - @PathVariable("orderId") Long orderId, - @RequestBody @Validated OrderSellerRequest orderSellerRequest) { - - MessageDTO messageDTO = orderSellerService.cancelOrApproveOrder(orderId, orderSellerRequest); + @Operation(summary = "(판매자) 주문 승인", description = "(판매자) 주문을 승인한다.") + @PatchMapping("/{orderId}/approve") + public ResponseEntity approveOrder(@PathVariable Long orderId) { + MessageDTO messageDTO = orderSellerService.approveOrder(orderId); + return ResponseEntity.ok(messageDTO); + } - return ResponseEntity.ok().body(messageDTO); + @Operation(summary = "(판매자) 주문 취소", description = "(판매자) 주문을 취소한다.") + @PatchMapping("/{orderId}/cancel") + public ResponseEntity cancelOrder(@PathVariable Long orderId) { + MessageDTO messageDTO = orderSellerService.cancelOrder(orderId); + return ResponseEntity.ok(messageDTO); } } diff --git a/src/main/java/org/store/clothstar/order/repository/orderSeller/JpaOrderSellerRepository.java b/src/main/java/org/store/clothstar/order/repository/orderSeller/JpaOrderSellerRepository.java index cded4802..79cc1824 100644 --- a/src/main/java/org/store/clothstar/order/repository/orderSeller/JpaOrderSellerRepository.java +++ b/src/main/java/org/store/clothstar/order/repository/orderSeller/JpaOrderSellerRepository.java @@ -7,7 +7,12 @@ import org.springframework.transaction.annotation.Transactional; import org.store.clothstar.order.entity.OrderEntity; +import java.util.List; + public interface JpaOrderSellerRepository extends JpaRepository { + @Query("SELECT o FROM orders o WHERE o.status = 'WAITING'") + List findWaitingOrders(); + @Transactional @Modifying @Query("UPDATE orders o SET o.status = 'APPROVE' WHERE o.orderId = :orderId") diff --git a/src/main/java/org/store/clothstar/order/repository/orderSeller/JpaOrderSellerRepositoryAdapter.java b/src/main/java/org/store/clothstar/order/repository/orderSeller/JpaOrderSellerRepositoryAdapter.java index 8350d55d..b56f6cd2 100644 --- a/src/main/java/org/store/clothstar/order/repository/orderSeller/JpaOrderSellerRepositoryAdapter.java +++ b/src/main/java/org/store/clothstar/order/repository/orderSeller/JpaOrderSellerRepositoryAdapter.java @@ -2,7 +2,6 @@ import org.springframework.stereotype.Repository; import org.store.clothstar.order.domain.Order; -import org.store.clothstar.order.domain.type.Status; import org.store.clothstar.order.entity.OrderEntity; import java.util.List; @@ -18,10 +17,8 @@ public class JpaOrderSellerRepositoryAdapter implements UpperOrderSellerReposito @Override public List SelectWaitingOrders() { - List orderEntityList = jpaOrderSellerRepository.findAll(); - + List orderEntityList = jpaOrderSellerRepository.findWaitingOrders(); return orderEntityList.stream() - .filter(orderEntity -> orderEntity.getStatus() == Status.WAITING) .map(Order::new) .collect(Collectors.toList()); } diff --git a/src/main/java/org/store/clothstar/order/service/OrderSellerService.java b/src/main/java/org/store/clothstar/order/service/OrderSellerService.java index 3cae1718..2f1ec274 100644 --- a/src/main/java/org/store/clothstar/order/service/OrderSellerService.java +++ b/src/main/java/org/store/clothstar/order/service/OrderSellerService.java @@ -10,8 +10,9 @@ import org.store.clothstar.order.domain.type.Status; import org.store.clothstar.order.dto.reponse.OrderResponse; import org.store.clothstar.order.dto.request.OrderSellerRequest; -import org.store.clothstar.order.repository.order.MybatisOrderRepository; +import org.store.clothstar.order.repository.order.UpperOrderRepository; import org.store.clothstar.order.repository.orderSeller.UpperOrderSellerRepository; +import org.store.clothstar.orderDetail.service.OrderDetailService; import java.util.List; import java.util.stream.Collectors; @@ -20,15 +21,19 @@ public class OrderSellerService { private final UpperOrderSellerRepository upperOrderSellerRepository; - private final MybatisOrderRepository orderRepository; + private final UpperOrderRepository upperOrderRepository; + private final OrderDetailService orderDetailService; public OrderSellerService( - @Qualifier("jpaOrderSellerRepositoryAdapter") UpperOrderSellerRepository upperOrderSellerRepository + @Qualifier("jpaOrderSellerRepositoryAdapter") UpperOrderSellerRepository upperOrderSellerRepository, + @Qualifier("jpaOrderRepositoryAdapter") UpperOrderRepository upperOrderRepository // @Qualifier("mybatisOrderSellerRepository") UpperOrderSellerRepository upperOrderSellerRepository - , MybatisOrderRepository orderRepository +// @Qualifier("mybatisOrderRepository") UpperOrderRepository upperOrderRepository + ,OrderDetailService orderDetailService ) { this.upperOrderSellerRepository = upperOrderSellerRepository; - this.orderRepository = orderRepository; + this.upperOrderRepository = upperOrderRepository; + this.orderDetailService=orderDetailService; } @Transactional(readOnly = true) @@ -40,31 +45,38 @@ public List getWaitingOrder() { } @Transactional - public MessageDTO cancelOrApproveOrder(Long orderId, OrderSellerRequest orderSellerRequest) { + public MessageDTO approveOrder(Long orderId) { + MessageDTO messageDTO; // 주문 유효성 검사 - orderRepository.getOrder(orderId) + upperOrderRepository.getOrder(orderId) .filter(Order -> Order.getStatus() == Status.WAITING) .orElseThrow(() -> new ResponseStatusException(HttpStatus.BAD_REQUEST, "주문이 존재하지 않거나 상태가 'WAITING'이 아니어서 처리할 수 없습니다.")); - return processOrder(orderId, orderSellerRequest); + upperOrderSellerRepository.approveOrder(orderId); + messageDTO = new MessageDTO(HttpStatus.OK.value(), "주문이 정상적으로 승인 되었습니다."); + + upperOrderRepository.getOrder(orderId) + .map(OrderResponse::fromOrder) + .orElseThrow(() -> new ResponseStatusException(HttpStatus.INTERNAL_SERVER_ERROR, "처리 후 주문 정보를 찾을 수 없습니다.")); + + return messageDTO; } - // 주문 처리 @Transactional - public MessageDTO processOrder(Long orderId, OrderSellerRequest orderSellerRequest) { + public MessageDTO cancelOrder(Long orderId) { + MessageDTO messageDTO; - MessageDTO messageDTO = null; + // 주문 유효성 검사 + upperOrderRepository.getOrder(orderId) + .filter(Order -> Order.getStatus() == Status.WAITING) + .orElseThrow(() -> new ResponseStatusException(HttpStatus.BAD_REQUEST, "주문이 존재하지 않거나 상태가 'WAITING'이 아니어서 처리할 수 없습니다.")); - if (orderSellerRequest.getApprovalStatus() == ApprovalStatus.APPROVE) { - upperOrderSellerRepository.approveOrder(orderId); - messageDTO = new MessageDTO(HttpStatus.OK.value(), "주문이 정상적으로 승인 되었습니다."); - } else if (orderSellerRequest.getApprovalStatus() == ApprovalStatus.CANCEL) { - upperOrderSellerRepository.cancelOrder(orderId); - messageDTO = new MessageDTO(HttpStatus.OK.value(), "주문이 정상적으로 취소 되었습니다."); - } + upperOrderSellerRepository.cancelOrder(orderId); + orderDetailService.restoreStockByOrder(orderId); + messageDTO = new MessageDTO(HttpStatus.OK.value(), "주문이 정상적으로 취소 되었습니다."); - orderRepository.getOrder(orderId) + upperOrderRepository.getOrder(orderId) .map(OrderResponse::fromOrder) .orElseThrow(() -> new ResponseStatusException(HttpStatus.INTERNAL_SERVER_ERROR, "처리 후 주문 정보를 찾을 수 없습니다.")); diff --git a/src/main/java/org/store/clothstar/order/service/OrderService.java b/src/main/java/org/store/clothstar/order/service/OrderService.java index 6e53ae17..9823b943 100644 --- a/src/main/java/org/store/clothstar/order/service/OrderService.java +++ b/src/main/java/org/store/clothstar/order/service/OrderService.java @@ -24,19 +24,17 @@ public class OrderService { private final UpperOrderRepository upperOrderRepository; private final MemberRepository memberRepository; private final AddressRepository addressRepository; - private final OrderDetailService orderDetailService; public OrderService( @Qualifier("jpaOrderRepositoryAdapter") UpperOrderRepository upperOrderRepository + ,@Qualifier("memberJpaRepository") MemberRepository memberRepository + ,@Qualifier("addressJpaRepository") AddressRepository addressRepository // @Qualifier("mybatisOrderRepository") UpperOrderRepository upperOrderRepository - , MemberRepository memberRepository - , AddressRepository addressRepository , OrderDetailService orderDetailService ) { this.upperOrderRepository = upperOrderRepository; this.memberRepository = memberRepository; this.addressRepository = addressRepository; - this.orderDetailService = orderDetailService; } @Transactional(readOnly = true) diff --git a/src/main/java/org/store/clothstar/orderDetail/controller/OrderDetailController.java b/src/main/java/org/store/clothstar/orderDetail/controller/OrderDetailController.java index 5f67d248..ed250551 100644 --- a/src/main/java/org/store/clothstar/orderDetail/controller/OrderDetailController.java +++ b/src/main/java/org/store/clothstar/orderDetail/controller/OrderDetailController.java @@ -25,13 +25,9 @@ public class OrderDetailController { @Operation(summary = "주문상세 추가 저장", description = "개별 상품에 대한 주문상세(상품명, 가격, 개수...)를 특정 주문에 추가 저장한다.") @PostMapping - public ResponseEntity addOrderDetail( - @RequestBody @Validated AddOrderDetailRequest addOrderDetailRequest) { - + public ResponseEntity addOrderDetail(@RequestBody @Validated AddOrderDetailRequest addOrderDetailRequest) { Long orderDetailId = orderdetailService.addOrderDetail(addOrderDetailRequest); - URI location = URIBuilder.buildURI(orderDetailId); - return ResponseEntity.created(location).build(); } } diff --git a/src/main/java/org/store/clothstar/orderDetail/service/OrderDetailService.java b/src/main/java/org/store/clothstar/orderDetail/service/OrderDetailService.java index 4385f7cd..c21d5422 100644 --- a/src/main/java/org/store/clothstar/orderDetail/service/OrderDetailService.java +++ b/src/main/java/org/store/clothstar/orderDetail/service/OrderDetailService.java @@ -1,6 +1,5 @@ package org.store.clothstar.orderDetail.service; -import jakarta.annotation.security.PermitAll; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.http.HttpStatus; @@ -70,14 +69,12 @@ public void saveOrderDetailWithOrder(CreateOrderDetailRequest createOrderDetailR order.getTotalProductsPrice() + order.getTotalShippingPrice() + orderDetail.getOneKindTotalPrice(); order.updatePrices(newTotalProductsPrice, newTotalPaymentPrice); - log.info("총 주문 가격 =" + order.getTotalPaymentPrice()); upperOrderRepository.updateOrderPrices(order); // 주문 수량만큼 상품 재고 차감 updateProductStock(product, orderDetail.getQuantity()); } - @PermitAll // 주문 상세 추가 생성 @Transactional public Long addOrderDetail(AddOrderDetailRequest addOrderDetailRequest) { diff --git a/src/test/java/org/store/clothstar/order/service/OrderSellerServiceApplicationTest.java b/src/test/java/org/store/clothstar/order/service/OrderSellerServiceApplicationTest.java index b4d529de..2e96b025 100644 --- a/src/test/java/org/store/clothstar/order/service/OrderSellerServiceApplicationTest.java +++ b/src/test/java/org/store/clothstar/order/service/OrderSellerServiceApplicationTest.java @@ -11,12 +11,11 @@ import org.springframework.web.server.ResponseStatusException; import org.store.clothstar.common.dto.MessageDTO; import org.store.clothstar.order.domain.Order; -import org.store.clothstar.order.domain.type.ApprovalStatus; import org.store.clothstar.order.domain.type.Status; import org.store.clothstar.order.dto.reponse.OrderResponse; -import org.store.clothstar.order.dto.request.OrderSellerRequest; -import org.store.clothstar.order.repository.order.MybatisOrderRepository; +import org.store.clothstar.order.repository.order.UpperOrderRepository; import org.store.clothstar.order.repository.orderSeller.UpperOrderSellerRepository; +import org.store.clothstar.orderDetail.service.OrderDetailService; import java.time.LocalDateTime; import java.util.List; @@ -38,13 +37,13 @@ class OrderSellerServiceApplicationTest { private Order mockOrder; @Mock - private OrderSellerRequest mockOrderSellerRequest; + private UpperOrderSellerRepository upperOrderSellerRepository; @Mock - private UpperOrderSellerRepository upperOrderSellerRepository; + private UpperOrderRepository upperOrderRepository; @Mock - private MybatisOrderRepository orderRepository; + private OrderDetailService orderDetailService; @Test @DisplayName("getWaitingOrders: '승인대기' 주문 조회 - 메서드 호출 & 반환값 테스트") @@ -73,78 +72,59 @@ void getWaitingOrder_test() { } @Test - @DisplayName("cancelOrApproveOrder: 판매자 주문 승인 - 메서드 호출 & 반환값 테스트") + @DisplayName("approveOrder: 판매자 주문 승인 - 메서드 호출 & 반환값 테스트") void approveOrder_verify_test() { //given Long orderId = 1L; given(mockOrder.getStatus()).willReturn(Status.WAITING); given(mockOrder.getCreatedAt()).willReturn(LocalDateTime.now()); - given(mockOrderSellerRequest.getApprovalStatus()).willReturn(ApprovalStatus.APPROVE); - given(orderRepository.getOrder(orderId)).willReturn(Optional.of(mockOrder)); + given(upperOrderRepository.getOrder(orderId)).willReturn(Optional.of(mockOrder)); //when - MessageDTO messageDTO = orderSellerService.cancelOrApproveOrder(orderId, mockOrderSellerRequest); + MessageDTO messageDTO = orderSellerService.approveOrder(orderId); //then then(upperOrderSellerRepository).should(times(1)).approveOrder(orderId); - then(orderRepository).should(times(2)).getOrder(orderId); + then(upperOrderRepository).should(times(2)).getOrder(orderId); assertThat(messageDTO.getStatusCode()).isEqualTo(HttpStatus.OK.value()); assertThat(messageDTO.getMessage()).isEqualTo("주문이 정상적으로 승인 되었습니다."); } @Test - @DisplayName("cancelOrApproveOrder: 판매자 주문 취소 - 메서드 호출 & 반환값 테스트") - void cancelOrApproveOrder_verify_test() { + @DisplayName("cancelOrder: 판매자 주문 취소 - 메서드 호출 & 반환값 테스트") + void cancelOrder_verify_test() { // given Long orderId = mockOrder.getOrderId(); given(mockOrder.getOrderId()).willReturn(1L); given(mockOrder.getStatus()).willReturn(Status.WAITING); given(mockOrder.getCreatedAt()).willReturn(LocalDateTime.now()); - given(mockOrderSellerRequest.getApprovalStatus()).willReturn(ApprovalStatus.CANCEL); - given(orderRepository.getOrder(orderId)).willReturn(Optional.of(mockOrder)); + given(upperOrderRepository.getOrder(orderId)).willReturn(Optional.of(mockOrder)); //when - MessageDTO messageDTO = orderSellerService.cancelOrApproveOrder(orderId, mockOrderSellerRequest); + MessageDTO messageDTO = orderSellerService.cancelOrder(orderId); //then then(upperOrderSellerRepository).should(times(1)).cancelOrder(orderId); - then(orderRepository).should(times(2)).getOrder(orderId); + then(upperOrderRepository).should(times(2)).getOrder(orderId); assertThat(messageDTO.getStatusCode()).isEqualTo(HttpStatus.OK.value()); assertThat(messageDTO.getMessage()).isEqualTo("주문이 정상적으로 취소 되었습니다."); } @Test - @DisplayName("cancelOrApproveOrder: 주문상태가 '승인대기'가 아닐 때 예외처리 테스트") + @DisplayName("approveOrder: 주문상태가 '승인대기'가 아닐 때 예외처리 테스트") void cancelOrApproveOrder_NotWAITING_exception_test() { //given Long orderId = 1L; - given(orderRepository.getOrder(orderId)).willReturn(Optional.of(mockOrder)); + given(upperOrderRepository.getOrder(orderId)).willReturn(Optional.of(mockOrder)); given(mockOrder.getStatus()).willReturn(Status.DELIVERED); //when ResponseStatusException thrown = assertThrows(ResponseStatusException.class, () -> { - orderSellerService.cancelOrApproveOrder(orderId, mockOrderSellerRequest); + orderSellerService.approveOrder(orderId); }); //then assertEquals("400 BAD_REQUEST \"주문이 존재하지 않거나 상태가 'WAITING'이 아니어서 처리할 수 없습니다.\"", thrown.getMessage()); } - - @Test - @DisplayName("cancelOrApproveOrder: 판매자 주문 관리 처리 후, 주문 정보를 찾을 수 없을 때 예외처리 테스트") - void cancelOrApproveOrder_after_cannotFindOrder_exception_test() { - //given - Long orderId = 1L; - given(mockOrderSellerRequest.getApprovalStatus()).willReturn(ApprovalStatus.APPROVE); - given(orderRepository.getOrder(orderId)).willReturn(Optional.empty()); - - //when - ResponseStatusException thrown = assertThrows(ResponseStatusException.class, () -> { - orderSellerService.processOrder(orderId, mockOrderSellerRequest); - }); - - //then - assertEquals("500 INTERNAL_SERVER_ERROR \"처리 후 주문 정보를 찾을 수 없습니다.\"", thrown.getMessage()); - } } \ No newline at end of file diff --git a/src/test/java/org/store/clothstar/order/service/OrderServiceTest.java b/src/test/java/org/store/clothstar/order/service/OrderServiceTest.java index 6ee6f228..395a63e5 100644 --- a/src/test/java/org/store/clothstar/order/service/OrderServiceTest.java +++ b/src/test/java/org/store/clothstar/order/service/OrderServiceTest.java @@ -19,7 +19,6 @@ import org.store.clothstar.order.dto.request.CreateOrderRequest; import org.store.clothstar.order.dto.request.OrderRequestWrapper; import org.store.clothstar.order.repository.order.UpperOrderRepository; -import org.store.clothstar.orderDetail.service.OrderDetailService; import java.time.LocalDateTime; import java.util.Optional; @@ -44,9 +43,6 @@ class OrderServiceTest { @Mock private AddressRepository addressRepository; - @Mock - private OrderDetailService orderDetailService; - @Test @DisplayName("getOrder: 주문 조회 - 메서드 호출 & 반환값 테스트") void getOrder_test() { diff --git a/src/test/java/org/store/clothstar/orderDetail/service/OrderDetailServiceTest.java b/src/test/java/org/store/clothstar/orderDetail/service/OrderDetailServiceTest.java index 49e8445f..319e30ec 100644 --- a/src/test/java/org/store/clothstar/orderDetail/service/OrderDetailServiceTest.java +++ b/src/test/java/org/store/clothstar/orderDetail/service/OrderDetailServiceTest.java @@ -8,11 +8,11 @@ import org.mockito.junit.jupiter.MockitoExtension; import org.springframework.web.server.ResponseStatusException; import org.store.clothstar.order.domain.Order; -import org.store.clothstar.order.repository.order.MybatisOrderRepository; +import org.store.clothstar.order.repository.order.UpperOrderRepository; import org.store.clothstar.orderDetail.domain.OrderDetail; import org.store.clothstar.orderDetail.dto.request.AddOrderDetailRequest; import org.store.clothstar.orderDetail.dto.request.CreateOrderDetailRequest; -import org.store.clothstar.orderDetail.repository.MybatisOrderDetailRepository; +import org.store.clothstar.orderDetail.repository.UpperOrderDetailRepository; import org.store.clothstar.product.domain.Product; import org.store.clothstar.product.repository.ProductRepository; import org.store.clothstar.productLine.domain.ProductLine; @@ -32,16 +32,16 @@ class OrderDetailServiceTest { private OrderDetailService orderDetailService; @Mock - private MybatisOrderRepository orderRepository; + private UpperOrderRepository upperOrderRepository; @Mock - private ProductLineMybatisRepository productLineMybatisRepository; + private UpperOrderDetailRepository upperOrderDetailRepository; @Mock - private ProductRepository productRepository; + private ProductLineMybatisRepository productLineMybatisRepository; @Mock - private MybatisOrderDetailRepository orderDetailRepository; + private ProductRepository productRepository; @DisplayName("saveOrderDetailWithOrder: 주문상세 생성 - 메서드 호출 테스트") @Test @@ -54,7 +54,7 @@ void saveOrderDetailWithOrder_verify_test() { Product mockProduct = mock(Product.class); Order mockOrder = mock(Order.class); - given(orderRepository.getOrder(orderId)).willReturn(Optional.of(mockOrder)); + given(upperOrderRepository.getOrder(orderId)).willReturn(Optional.of(mockOrder)); given(productLineMybatisRepository.selectByProductLineId(mockRequest.getProductLineId())).willReturn(Optional.of(mockProductLine)); given(productRepository.selectByProductId(mockRequest.getProductId())).willReturn(Optional.of(mockProduct)); given(mockRequest.toOrderDetail(orderId, mockProductLine, mockProduct)).willReturn(mockOrderDetail); @@ -63,9 +63,11 @@ void saveOrderDetailWithOrder_verify_test() { orderDetailService.saveOrderDetailWithOrder(mockRequest, orderId); //then - then(orderRepository).should().getOrder(orderId); - then(productLineMybatisRepository).should().selectByProductLineId(mockRequest.getProductLineId()); - then(productRepository).should().selectByProductId(mockRequest.getProductId()); + then(upperOrderRepository).should(times(1)).getOrder(orderId); + then(productLineMybatisRepository).should(times(1)).selectByProductLineId(mockRequest.getProductLineId()); + then(productRepository).should(times(1)).selectByProductId(mockRequest.getProductId()); + then(upperOrderDetailRepository).should(times(1)).saveOrderDetail(mockOrderDetail); + then(upperOrderRepository).should(times(1)).updateOrderPrices(mockOrder); } @DisplayName("addOrderDetail: 주문상세 추가 - 반환값 테스트") @@ -79,7 +81,7 @@ void addOrderDetail_test() { Order mockOrder = mock(Order.class); given(mockOrderDetail.getOrderDetailId()).willReturn(1L); - given(orderRepository.getOrder(mockRequest.getOrderId())).willReturn(Optional.of(mockOrder)); + given(upperOrderRepository.getOrder(mockRequest.getOrderId())).willReturn(Optional.of(mockOrder)); given(productLineMybatisRepository.selectByProductLineId(mockRequest.getProductLineId())).willReturn(Optional.of(mockProductLine)); given(productRepository.selectByProductId(mockRequest.getProductId())).willReturn(Optional.of(mockProduct)); given(mockRequest.toOrderDetail(mockOrder, mockProductLine, mockProduct)).willReturn(mockOrderDetail); @@ -101,7 +103,7 @@ void addOrderDetail_verify_test() { Product mockProduct = mock(Product.class); Order mockOrder = mock(Order.class); - given(orderRepository.getOrder(mockRequest.getOrderId())).willReturn(Optional.of(mockOrder)); + given(upperOrderRepository.getOrder(mockRequest.getOrderId())).willReturn(Optional.of(mockOrder)); given(productLineMybatisRepository.selectByProductLineId(mockRequest.getProductLineId())).willReturn(Optional.of(mockProductLine)); given(productRepository.selectByProductId(mockRequest.getProductId())).willReturn(Optional.of(mockProduct)); given(mockRequest.toOrderDetail(mockOrder, mockProductLine, mockProduct)).willReturn(mockOrderDetail); @@ -110,9 +112,12 @@ void addOrderDetail_verify_test() { orderDetailService.addOrderDetail(mockRequest); //then - then(orderRepository).should().getOrder(mockRequest.getOrderId()); - then(productLineMybatisRepository).should().selectByProductLineId(mockRequest.getProductLineId()); - then(productRepository).should().selectByProductId(mockRequest.getProductId()); + then(upperOrderRepository).should(times(1)).getOrder(mockRequest.getOrderId()); + then(productLineMybatisRepository).should(times(1)).selectByProductLineId(mockRequest.getProductLineId()); + then(productRepository).should(times(1)).selectByProductId(mockRequest.getProductId()); + then(upperOrderDetailRepository).should(times(1)).saveOrderDetail(mockOrderDetail); + then(upperOrderRepository).should(times(1)).updateOrderPrices(mockOrder); + then(productRepository).should(times(1)).updateProduct(mockProduct); } @DisplayName("addOrderDetail: 주문상세 추가 - 주문 유효성 검사 예외처리 테스트") @@ -124,7 +129,7 @@ void getOrderDetail_quantityZero_exception_test() { ProductLine mockProductLine = mock(ProductLine.class); Product mockProduct = mock(Product.class); - given(orderRepository.getOrder(mockRequest.getOrderId())).willReturn(Optional.of(mockOrder)); + given(upperOrderRepository.getOrder(mockRequest.getOrderId())).willReturn(Optional.of(mockOrder)); given(productLineMybatisRepository.selectByProductLineId(mockRequest.getProductLineId())).willReturn(Optional.of(mockProductLine)); given(productRepository.selectByProductId(mockRequest.getProductId())).willReturn(Optional.of(mockProduct)); given(mockRequest.getQuantity()).willReturn(10); @@ -139,30 +144,6 @@ void getOrderDetail_quantityZero_exception_test() { assertEquals("400 BAD_REQUEST \"주문 개수가 재고보다 더 많습니다.\"", thrown.getMessage()); } - @Test - @DisplayName("addOrderDetail: 주문생성시, 상품재고를 차감하는 메서드(updateProduct())가 호출되는지 테스트") - void product_stock_subtract() { - //given - AddOrderDetailRequest mockRequest = mock(AddOrderDetailRequest.class); - Order mockOrder = mock(Order.class); - ProductLine mockProductLine = mock(ProductLine.class); - Product mockProduct = mock(Product.class); - OrderDetail mockOrderDetail = mock(OrderDetail.class); - - given(orderRepository.getOrder(mockRequest.getOrderId())).willReturn(Optional.of(mockOrder)); - given(productLineMybatisRepository.selectByProductLineId(mockRequest.getProductLineId())).willReturn(Optional.of(mockProductLine)); - given(productRepository.selectByProductId(mockRequest.getProductId())).willReturn(Optional.of(mockProduct)); - given(mockRequest.getQuantity()).willReturn(1); - given(mockProduct.getStock()).willReturn(10L); - given(mockRequest.toOrderDetail(mockOrder, mockProductLine, mockProduct)).willReturn(mockOrderDetail); - - //when - orderDetailService.addOrderDetail(mockRequest); - - //then - verify(productRepository).updateProduct(mockProduct); - } - @Test @DisplayName("restoreStockByOrder: 주문 취소시, 상품 재고 반환 - 메서드 호출 테스트") void restoreStockByOrder_verify_test() { @@ -173,17 +154,16 @@ void restoreStockByOrder_verify_test() { OrderDetail mockOrderDetail3 = mock(OrderDetail.class); Product mockProduct = mock(Product.class); List orderDetails = List.of(mockOrderDetail1, mockOrderDetail2, mockOrderDetail3); - given(orderDetailRepository.findByOrderId(orderId)).willReturn(orderDetails); + given(upperOrderDetailRepository.findByOrderId(orderId)).willReturn(orderDetails); given(productRepository.selectByProductId(mockOrderDetail1.getProductId())).willReturn(Optional.of(mockProduct)); //when orderDetailService.restoreStockByOrder(orderId); //then - then(orderDetailRepository).should(times(1)).findByOrderId(orderId); + then(upperOrderDetailRepository).should(times(1)).findByOrderId(orderId); then(productRepository).should(times(3)).selectByProductId(mockProduct.getProductId()); then(productRepository).should(times(3)).updateProduct(mockProduct); - } @Test @@ -195,7 +175,7 @@ void restoreStockByOrder_product_null_exception_test() { OrderDetail mockOrderDetail2 = mock(OrderDetail.class); OrderDetail mockOrderDetail3 = mock(OrderDetail.class); List orderDetails = List.of(mockOrderDetail1, mockOrderDetail2, mockOrderDetail3); - given(orderDetailRepository.findByOrderId(orderId)).willReturn(orderDetails); + given(upperOrderDetailRepository.findByOrderId(orderId)).willReturn(orderDetails); given(productRepository.selectByProductId(mockOrderDetail1.getProductId())).willReturn(Optional.empty()); //when