|
| 1 | +package br.com.grupo63.serviceorder.order; |
| 2 | + |
| 3 | +import br.com.grupo63.serviceorder.api.controller.order.OrderAPIController; |
| 4 | +import br.com.grupo63.serviceorder.api.controller.order.dto.CreateOrderRequestDTO; |
| 5 | +import br.com.grupo63.serviceorder.controller.OrderController; |
| 6 | +import br.com.grupo63.serviceorder.controller.dto.OrderControllerDTO; |
| 7 | +import br.com.grupo63.serviceorder.entity.order.Order; |
| 8 | +import br.com.grupo63.serviceorder.entity.order.OrderItem; |
| 9 | +import br.com.grupo63.serviceorder.entity.product.Category; |
| 10 | +import br.com.grupo63.serviceorder.entity.product.Product; |
| 11 | +import br.com.grupo63.serviceorder.gateway.identification.IIdentificationGateway; |
| 12 | +import br.com.grupo63.serviceorder.gateway.identification.dto.ClientDTO; |
| 13 | +import br.com.grupo63.serviceorder.gateway.order.OrderJpaAdapter; |
| 14 | +import br.com.grupo63.serviceorder.gateway.order.OrderJpaRepository; |
| 15 | +import br.com.grupo63.serviceorder.gateway.order.entity.OrderPersistenceEntity; |
| 16 | +import br.com.grupo63.serviceorder.gateway.product.ProductJpaAdapter; |
| 17 | +import br.com.grupo63.serviceorder.gateway.product.ProductJpaRepository; |
| 18 | +import br.com.grupo63.serviceorder.gateway.product.entity.ProductPersistenceEntity; |
| 19 | +import br.com.grupo63.serviceorder.usecase.order.OrderUseCase; |
| 20 | +import jakarta.servlet.http.HttpServletRequest; |
| 21 | +import lombok.SneakyThrows; |
| 22 | +import org.junit.jupiter.api.BeforeEach; |
| 23 | +import org.junit.jupiter.api.Test; |
| 24 | +import org.mockito.InjectMocks; |
| 25 | +import org.mockito.Mock; |
| 26 | +import org.mockito.MockitoAnnotations; |
| 27 | +import org.springframework.http.HttpStatus; |
| 28 | +import org.springframework.http.ResponseEntity; |
| 29 | +import org.springframework.mock.web.MockHttpServletRequest; |
| 30 | + |
| 31 | +import java.util.*; |
| 32 | + |
| 33 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 34 | +import static org.junit.jupiter.api.Assertions.assertNotNull; |
| 35 | +import static org.mockito.ArgumentMatchers.any; |
| 36 | +import static org.mockito.Mockito.*; |
| 37 | +import static org.mockito.Mockito.times; |
| 38 | + |
| 39 | +public class OrderIntegrationTest { |
| 40 | + |
| 41 | + @Mock |
| 42 | + private ProductJpaRepository productJpaRepository; |
| 43 | + @Mock |
| 44 | + private OrderJpaRepository orderJpaRepository; |
| 45 | + @Mock |
| 46 | + private IIdentificationGateway identificationGateway; |
| 47 | + @InjectMocks |
| 48 | + private ProductJpaAdapter productJpaAdapter; |
| 49 | + @InjectMocks |
| 50 | + private OrderJpaAdapter orderJpaAdapter; |
| 51 | + private OrderUseCase orderUseCase; |
| 52 | + private OrderController orderController; |
| 53 | + private OrderAPIController orderAPIController; |
| 54 | + |
| 55 | + private final Category defaultCategory = new Category(1L, false, "category"); |
| 56 | + private final Product defaultProduct = new Product(1L, false, "product", 10.0, defaultCategory); |
| 57 | + private final CreateOrderRequestDTO.Item defaultItem = new CreateOrderRequestDTO.Item(1L, 1L); |
| 58 | + private final Order defaultOrder = new Order(1L, false, 10.0, 1L, List.of()); |
| 59 | + |
| 60 | + private final OrderPersistenceEntity defaultOrderPersistenceEntity = |
| 61 | + new OrderPersistenceEntity(defaultOrder); |
| 62 | + private final ProductPersistenceEntity defaultProductPersistenceEntity = new ProductPersistenceEntity(defaultProduct); |
| 63 | + |
| 64 | + private final CreateOrderRequestDTO defaultCreateOrderRequestDTO = new CreateOrderRequestDTO(List.of(defaultItem)); |
| 65 | + private final ClientDTO defaultClientDTO = new ClientDTO(); |
| 66 | + |
| 67 | + @BeforeEach |
| 68 | + void setUp() { |
| 69 | + MockitoAnnotations.openMocks(this); |
| 70 | + orderUseCase = new OrderUseCase(productJpaAdapter, orderJpaAdapter, identificationGateway); |
| 71 | + orderController = new OrderController(orderUseCase); |
| 72 | + orderAPIController = new OrderAPIController(orderController); |
| 73 | + } |
| 74 | + |
| 75 | + @SneakyThrows |
| 76 | + @Test |
| 77 | + void testRead_EndToEnd() { |
| 78 | + when(orderJpaRepository.findByIdAndDeletedFalse(1L)).thenReturn(Optional.of(defaultOrderPersistenceEntity)); |
| 79 | + |
| 80 | + ResponseEntity<OrderControllerDTO> response = orderAPIController.read(1L); |
| 81 | + assertEquals(HttpStatus.OK, response.getStatusCode()); |
| 82 | + assertEquals(Objects.requireNonNull(response.getBody()).getId(), 1L); |
| 83 | + } |
| 84 | + |
| 85 | + @SneakyThrows |
| 86 | + @Test |
| 87 | + void testList_EndToEnd() { |
| 88 | + when(orderJpaRepository.findByDeletedFalse()).thenReturn(List.of(defaultOrderPersistenceEntity)); |
| 89 | + |
| 90 | + ResponseEntity<List<OrderControllerDTO>> response = orderAPIController.list(); |
| 91 | + assertEquals(HttpStatus.OK, response.getStatusCode()); |
| 92 | + assertEquals(Objects.requireNonNull(response.getBody()).get(0).getId(), 1L); |
| 93 | + } |
| 94 | + |
| 95 | + @SneakyThrows |
| 96 | + @Test |
| 97 | + void testCreate_EndToEnd() { |
| 98 | + HttpServletRequest mockedRequest = new MockHttpServletRequest(); |
| 99 | + mockedRequest.setAttribute("clientId", Long.toString(1L)); |
| 100 | + |
| 101 | + when(identificationGateway.getById(1L)).thenReturn(Optional.of(defaultClientDTO)); |
| 102 | + when(productJpaRepository.findByIdAndDeletedFalse(1L)).thenReturn(Optional.of(defaultProductPersistenceEntity)); |
| 103 | + when(orderJpaRepository.saveAndFlush(any())).thenReturn(defaultOrderPersistenceEntity); |
| 104 | + |
| 105 | + ResponseEntity<OrderControllerDTO> response = orderAPIController.create(defaultCreateOrderRequestDTO, mockedRequest); |
| 106 | + assertEquals(HttpStatus.OK, response.getStatusCode()); |
| 107 | + assertNotNull(response.getBody()); |
| 108 | + assertEquals(response.getBody().getId(), 1L); |
| 109 | + verify(orderJpaRepository, times(1)).saveAndFlush(any()); |
| 110 | + } |
| 111 | + |
| 112 | +} |
0 commit comments