Skip to content

Commit

Permalink
test : OrderService-updateDeleteAt & OrderDetailService-updateDelete…
Browse files Browse the repository at this point in the history
…At, restoreStockByOrderDetail 테스트 코드 추가 & warning 메세지 해결
  • Loading branch information
axhtl committed Jul 10, 2024
1 parent e6419e5 commit c3d6424
Show file tree
Hide file tree
Showing 4 changed files with 193 additions and 31 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package org.store.clothstar.order.service;

import lombok.extern.slf4j.Slf4j;
import org.hibernate.query.Order;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
Expand Down Expand Up @@ -93,15 +92,15 @@ public void deliveredToConfirmOrder(Long orderId) {
@Transactional
public void updateDeleteAt(Long orderId) {
OrderEntity orderEntity = orderRepository.findById(orderId)
.orElseThrow(() -> new IllegalArgumentException("주문 번호를 찾을 수 없습니다."));

List<OrderDetailEntity> orderDetailList = orderDetailRepository.findOrderDetailListByOrderId(orderId);
orderDetailList.forEach(OrderDetailEntity::updateDeletedAt);
.orElseThrow(() -> new ResponseStatusException(HttpStatus.NOT_FOUND, "주문 번호를 찾을 수 없습니다."));

if(orderEntity.getDeletedAt() != null){
throw new IllegalArgumentException("이미 삭제된 주문입니다.");
throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "이미 삭제된 주문입니다.");
}

List<OrderDetailEntity> orderDetailList = orderDetailRepository.findOrderDetailListByOrderId(orderId);
orderDetailList.forEach(OrderDetailEntity::updateDeletedAt);

orderEntity.updateDeletedAt();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -113,10 +113,10 @@ public Long addOrderDetail(AddOrderDetailRequest addOrderDetailRequest) {
@Transactional
public void updateDeleteAt(Long orderDetailId) {
OrderDetailEntity orderDetailEntity = orderDetailRepository.findById(orderDetailId)
.orElseThrow(() -> new IllegalArgumentException("주문상세 번호를 찾을 수 없습니다."));
.orElseThrow(() -> new ResponseStatusException(HttpStatus.NOT_FOUND, "주문상세 번호를 찾을 수 없습니다."));

if(orderDetailEntity.getDeletedAt() != null){
throw new IllegalArgumentException("이미 삭제된 주문입니다.");
throw new ResponseStatusException(HttpStatus.NOT_FOUND, "이미 삭제된 주문입니다.");
}

restoreStockByOrderDetail(orderDetailId);
Expand All @@ -138,7 +138,7 @@ public void restoreStockByOrder(Long orderId) {
@Transactional
public void restoreStockByOrderDetail(Long orderDetailId) {
OrderDetailEntity orderDetailEntity = orderDetailRepository.findById(orderDetailId)
.orElseThrow(() -> new IllegalArgumentException("주문상세 번호를 찾을 수 없습니다."));
.orElseThrow(() -> new ResponseStatusException(HttpStatus.NOT_FOUND, "주문상세 번호를 찾을 수 없습니다."));
productService.restoreProductStockByOrderDetail(orderDetailEntity);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Slice;
import org.springframework.web.server.ResponseStatusException;
import org.store.clothstar.member.domain.Address;
import org.store.clothstar.member.domain.Member;
Expand All @@ -22,7 +20,11 @@
import org.store.clothstar.order.entity.OrderEntity;
import org.store.clothstar.order.repository.order.OrderRepository;
import org.store.clothstar.order.type.Status;
import org.store.clothstar.orderDetail.entity.OrderDetailEntity;
import org.store.clothstar.orderDetail.repository.OrderDetailRepository;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Optional;

import static org.assertj.core.api.Assertions.assertThat;
Expand All @@ -39,12 +41,18 @@ class OrderServiceTest {
@Mock
private OrderRepository orderRepository;

@Mock
private OrderDetailRepository orderDetailRepository;

@Mock
private MemberRepository memberRepository;

@Mock
private AddressRepository addressRepository;

@Mock
private OrderDetailEntity orderDetailEntity;

@Test
@DisplayName("getOrder: 주문 조회 - 메서드 호출 & 반환값 테스트")
void getOrder_test() {
Expand Down Expand Up @@ -159,9 +167,8 @@ void saveOrder_member_exception_test() {
given(memberRepository.findById(1L)).willReturn(Optional.empty());

//when
ResponseStatusException thrown = assertThrows(ResponseStatusException.class, () -> {
orderService.saveOrder(orderRequestWrapper.getCreateOrderRequest());
});
ResponseStatusException thrown = assertThrows(ResponseStatusException.class, () ->
orderService.saveOrder(orderRequestWrapper.getCreateOrderRequest()));

//then
assertEquals("400 BAD_REQUEST \"회원 정보를 찾을 수 없습니다.\"", thrown.getMessage());
Expand All @@ -184,9 +191,8 @@ void saveOrder_address_exception_test() {
given(addressRepository.findById(2L)).willReturn(Optional.empty());

//when
ResponseStatusException thrown = assertThrows(ResponseStatusException.class, () -> {
orderService.saveOrder(orderRequestWrapper.getCreateOrderRequest());
});
ResponseStatusException thrown = assertThrows(ResponseStatusException.class, () ->
orderService.saveOrder(orderRequestWrapper.getCreateOrderRequest()));

//then
assertEquals("400 BAD_REQUEST \"배송지 정보를 찾을 수 없습니다.\"", thrown.getMessage());
Expand Down Expand Up @@ -222,11 +228,67 @@ void deliveredToConfirmOrder_fail_exception_test() {
given(orderRepository.findById(orderId)).willReturn(Optional.of(mockOrder));

//when
ResponseStatusException thrown = assertThrows(ResponseStatusException.class, () -> {
orderService.deliveredToConfirmOrder(orderId);
});
ResponseStatusException thrown = assertThrows(ResponseStatusException.class, () ->
orderService.deliveredToConfirmOrder(orderId));

//then
assertEquals("400 BAD_REQUEST \"주문 상태가 '배송완료'가 아니기 때문에 주문확정이 불가능합니다.\"", thrown.getMessage());
}

@Test
@DisplayName("updateDeleteAt: 주문 삭제 - 메서드 호출 테스트")
void updateDeleteAt_verify_test() {
//given
Long orderId = 1L;
OrderEntity orderEntity = mock(OrderEntity.class);
OrderDetailEntity mockOrderDetail1 = mock(OrderDetailEntity.class);
OrderDetailEntity mockOrderDetail2 = mock(OrderDetailEntity.class);
OrderDetailEntity mockOrderDetail3 = mock(OrderDetailEntity.class);
List<OrderDetailEntity> orderDetailList = List.of(mockOrderDetail1, mockOrderDetail2, mockOrderDetail3);
given(orderRepository.findById(1L)).willReturn(Optional.of(orderEntity));
given(orderDetailRepository.findOrderDetailListByOrderId(orderId)).willReturn(orderDetailList);

//when
orderService.updateDeleteAt(orderId);

//then
verify(mockOrderDetail1, times(1)).updateDeletedAt();
verify(mockOrderDetail2, times(1)).updateDeletedAt();
verify(mockOrderDetail3, times(1)).updateDeletedAt();
then(orderRepository).should(times(1)).findById(orderId);
then(orderDetailRepository).should().findOrderDetailListByOrderId(orderId);
then(orderEntity).should(times(1)).updateDeletedAt();
}

@Test
@DisplayName("updateDeleteAt: 주문 삭제 - orderEntity null 예외처리 테스트")
void updateDeleteAt_orderEntityNull_exception_test() {
//given
Long orderId = 1L;
given(orderRepository.findById(1L)).willReturn(Optional.empty());

//when
ResponseStatusException thrown = assertThrows(ResponseStatusException.class, () ->
orderService.updateDeleteAt(orderId));

//then
assertEquals("404 NOT_FOUND \"주문 번호를 찾을 수 없습니다.\"", thrown.getMessage());
}

@Test
@DisplayName("updateDeleteAt: 주문 삭제 - 이미 삭제된 경우 예외처리 테스트")
void updateDeleteAt_alreadyDelete_exception_test() {
//given
Long orderId = 1L;
OrderEntity orderEntity = mock(OrderEntity.class);
given(orderRepository.findById(1L)).willReturn(Optional.of(orderEntity));
given(orderEntity.getDeletedAt()).willReturn(LocalDateTime.now());

//when
ResponseStatusException thrown = assertThrows(ResponseStatusException.class, () ->
orderService.updateDeleteAt(orderId));

//then
assertEquals("400 BAD_REQUEST \"이미 삭제된 주문입니다.\"", thrown.getMessage());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import org.springframework.web.server.ResponseStatusException;
import org.store.clothstar.order.entity.OrderEntity;
import org.store.clothstar.order.repository.order.OrderRepository;
import org.store.clothstar.order.type.Status;
import org.store.clothstar.orderDetail.dto.request.AddOrderDetailRequest;
import org.store.clothstar.orderDetail.dto.request.CreateOrderDetailRequest;
import org.store.clothstar.orderDetail.entity.OrderDetailEntity;
Expand All @@ -19,6 +20,7 @@
import org.store.clothstar.productLine.entity.ProductLineEntity;
import org.store.clothstar.productLine.repository.ProductLineJPARepository;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Optional;

Expand Down Expand Up @@ -47,6 +49,9 @@ class OrderDetailServiceTest {
@Mock
private ProductJPARepository productJPARepository;

@Mock
private OrderDetailEntity orderDetailEntity;

@DisplayName("saveOrderDetailWithOrder: 주문상세 생성 - 메서드 호출 테스트")
@Test
void saveOrderDetailWithOrder_verify_test() {
Expand Down Expand Up @@ -91,9 +96,8 @@ void saveOrderDetailWithOrder_exception_test() {
given(mockProduct.getStock()).willReturn(1L);

//when
ResponseStatusException thrown = assertThrows(ResponseStatusException.class, () -> {
orderDetailService.saveOrderDetailWithOrder(mockRequest,orderId);
});
ResponseStatusException thrown = assertThrows(ResponseStatusException.class, () ->
orderDetailService.saveOrderDetailWithOrder(mockRequest,orderId));

//then
assertEquals("400 BAD_REQUEST \"주문 개수가 재고보다 더 많습니다.\"", thrown.getMessage());
Expand All @@ -115,9 +119,8 @@ void getOrderDetail_quantityZero_exception_test() {
given(mockProduct.getStock()).willReturn(1L);

//when
ResponseStatusException thrown = assertThrows(ResponseStatusException.class, () -> {
orderDetailService.addOrderDetail(mockRequest);
});
ResponseStatusException thrown = assertThrows(ResponseStatusException.class, () ->
orderDetailService.addOrderDetail(mockRequest));

//then
assertEquals("400 BAD_REQUEST \"주문 개수가 재고보다 더 많습니다.\"", thrown.getMessage());
Expand All @@ -135,6 +138,7 @@ void addOrderDetail_test() {
OrderEntity mockOrder = mock(OrderEntity.class);

given(mockOrderDetail.getOrderDetailId()).willReturn(1L);
given(mockOrder.getStatus()).willReturn(Status.WAITING);
given(orderRepository.findById(mockRequest.getOrderId())).willReturn(Optional.of(mockOrder));
given(productLineJPARepository.findById(mockRequest.getProductLineId())).willReturn(Optional.of(mockProductLine));
given(productJPARepository.findById(mockRequest.getProductId())).willReturn(Optional.of(mockProduct));
Expand All @@ -157,6 +161,7 @@ void addOrderDetail_verify_test() {
ProductEntity mockProduct = mock(ProductEntity.class);
OrderEntity mockOrder = mock(OrderEntity.class);

given(mockOrder.getStatus()).willReturn(Status.WAITING);
given(orderRepository.findById(mockRequest.getOrderId())).willReturn(Optional.of(mockOrder));
given(productLineJPARepository.findById(mockRequest.getProductLineId())).willReturn(Optional.of(mockProductLine));
given(productJPARepository.findById(mockRequest.getProductId())).willReturn(Optional.of(mockProduct));
Expand All @@ -170,8 +175,6 @@ void addOrderDetail_verify_test() {
then(productLineJPARepository).should(times(1)).findById(mockRequest.getProductLineId());
then(productJPARepository).should(times(1)).findById(mockRequest.getProductId());
then(orderDetailRepository).should(times(1)).save(mockOrderDetail);
// then(orderRepository).should(times(1)).updateOrderPrices(mockOrder);
// then(productJPARepository).should(times(1)).updateProduct(mockProduct);
}

@DisplayName("addOrderDetail: 주문상세 추가 - 주문 유효성 검사 예외처리 테스트")
Expand All @@ -190,14 +193,82 @@ void addOrderDetail_quantityZero_exception_test() {
given(mockProduct.getStock()).willReturn(1L);

//when
ResponseStatusException thrown = assertThrows(ResponseStatusException.class, () -> {
orderDetailService.addOrderDetail(mockRequest);
});
ResponseStatusException thrown = assertThrows(ResponseStatusException.class, () ->
orderDetailService.addOrderDetail(mockRequest));

//then
assertEquals("400 BAD_REQUEST \"주문 개수가 재고보다 더 많습니다.\"", thrown.getMessage());
}

@DisplayName("addOrderDetail: 주문상세 추가 - 주문 상태 검사 예외처리 테스트")
@Test
void addOrderDetail_noWAITING_exception_test() {
//given
AddOrderDetailRequest mockRequest = mock(AddOrderDetailRequest.class);
ProductLineEntity mockProductLine = mock(ProductLineEntity.class);
ProductEntity mockProduct = mock(ProductEntity.class);
OrderEntity mockOrder = mock(OrderEntity.class);

given(mockOrder.getStatus()).willReturn(Status.CANCEL);
given(orderRepository.findById(mockRequest.getOrderId())).willReturn(Optional.of(mockOrder));
given(productLineJPARepository.findById(mockRequest.getProductLineId())).willReturn(Optional.of(mockProductLine));
given(productJPARepository.findById(mockRequest.getProductId())).willReturn(Optional.of(mockProduct));

//when
ResponseStatusException thrown = assertThrows(ResponseStatusException.class, () ->
orderDetailService.addOrderDetail(mockRequest));

//then
assertEquals("400 BAD_REQUEST \"주문이 이미 처리된 상태에서는 추가 주문이 불가능합니다.\"", thrown.getMessage());
}

@Test
@DisplayName("updateDeleteAt: 주문 상세 삭제 - 메서드 호출 테스트")
void updateDeleteAt_verify_test() {
//given
long orderDetailId = 1L;
given(orderDetailRepository.findById(orderDetailId)).willReturn(Optional.of(orderDetailEntity));

//when
orderDetailService.updateDeleteAt(orderDetailId);

//then
then(orderDetailRepository).should(times(2)).findById(orderDetailId);
then(productService).should(times(1)).restoreProductStockByOrderDetail(orderDetailEntity);
then(orderDetailEntity).should(times(1)).updateDeletedAt();
}

@Test
@DisplayName("updateDeleteAt: 주문 상세 삭제 - OrderDetail null 예외처리 테스트")
void updateDeleteAt_null_exception_test() {
//given
long orderDetailId = 1L;
given(orderDetailRepository.findById(orderDetailId)).willReturn(Optional.empty());

//when
ResponseStatusException thrown = assertThrows(ResponseStatusException.class, () ->
orderDetailService.updateDeleteAt(orderDetailId));

//then
assertEquals("404 NOT_FOUND \"주문상세 번호를 찾을 수 없습니다.\"", thrown.getMessage());
}

@Test
@DisplayName("updateDeleteAt: 주문 상세 삭제 - 이미 삭제된 경우 예외처리 테스트")
void updateDeleteAt_alreadyDelete_exception_test() {
//given
long orderDetailId = 1L;
given(orderDetailRepository.findById(orderDetailId)).willReturn(Optional.of(orderDetailEntity));
given(orderDetailEntity.getDeletedAt()).willReturn(LocalDateTime.now());

//when
ResponseStatusException thrown = assertThrows(ResponseStatusException.class, () ->
orderDetailService.updateDeleteAt(orderDetailId));

//then
assertEquals("404 NOT_FOUND \"이미 삭제된 주문입니다.\"", thrown.getMessage());
}

@Test
@DisplayName("restoreStockByOrder: 주문 취소시, 상품 재고 반환 - 메서드 호출 테스트")
void restoreStockByOrder_verify_test() {
Expand All @@ -215,4 +286,34 @@ void restoreStockByOrder_verify_test() {
//then
then(productService).should(times(1)).restoreProductStockByOrder(orderDetailList);
}

@Test
@DisplayName("restoreStockByOrderDetail: 주문 상세 삭제시, 상품 재고 반환 - 메서드 호출 테스트")
void restoreStockByOrderDetail_verify_test() {
//given
long orderDetailId = 1L;
OrderDetailEntity mockOrderDetail = mock(OrderDetailEntity.class);
given(orderDetailRepository.findById(orderDetailId)).willReturn(Optional.of(mockOrderDetail));

//when
orderDetailService.restoreStockByOrderDetail(orderDetailId);

//then
then(productService).should(times(1)).restoreProductStockByOrderDetail(mockOrderDetail);
}

@Test
@DisplayName("restoreStockByOrderDetail: 주문 상세 삭제시, 상품 재고 반환 - orderDetail null 예외처리 테스트")
void restoreStockByOrderDetail_null_exception_test() {
//given
long orderDetailId = 1L;
given(orderDetailRepository.findById(orderDetailId)).willReturn(Optional.empty());

//when
ResponseStatusException thrown = assertThrows(ResponseStatusException.class, () ->
orderDetailService.restoreStockByOrderDetail(orderDetailId));

//then
assertEquals("404 NOT_FOUND \"주문상세 번호를 찾을 수 없습니다.\"", thrown.getMessage());
}
}

0 comments on commit c3d6424

Please sign in to comment.