|
| 1 | +package br.com.grupo63.serviceorder.product; |
| 2 | + |
| 3 | +import br.com.grupo63.serviceorder.api.controller.product.ProductAPIController; |
| 4 | +import br.com.grupo63.serviceorder.controller.ProductController; |
| 5 | +import br.com.grupo63.serviceorder.controller.dto.ProductControllerDTO; |
| 6 | +import br.com.grupo63.serviceorder.entity.product.Category; |
| 7 | +import br.com.grupo63.serviceorder.entity.product.Product; |
| 8 | +import br.com.grupo63.serviceorder.gateway.product.ProductJpaAdapter; |
| 9 | +import br.com.grupo63.serviceorder.gateway.product.ProductJpaRepository; |
| 10 | +import br.com.grupo63.serviceorder.gateway.product.entity.ProductPersistenceEntity; |
| 11 | +import br.com.grupo63.serviceorder.usecase.product.ProductUseCase; |
| 12 | +import br.com.grupo63.techchallenge.common.api.controller.dto.DefaultResponseDTO; |
| 13 | +import lombok.SneakyThrows; |
| 14 | +import org.junit.jupiter.api.BeforeEach; |
| 15 | +import org.junit.jupiter.api.Test; |
| 16 | +import org.mockito.InjectMocks; |
| 17 | +import org.mockito.Mock; |
| 18 | +import org.mockito.MockitoAnnotations; |
| 19 | +import org.springframework.http.HttpStatus; |
| 20 | +import org.springframework.http.ResponseEntity; |
| 21 | + |
| 22 | +import java.util.*; |
| 23 | + |
| 24 | +import static org.junit.jupiter.api.Assertions.*; |
| 25 | +import static org.mockito.ArgumentMatchers.any; |
| 26 | +import static org.mockito.Mockito.*; |
| 27 | +import static org.mockito.Mockito.times; |
| 28 | + |
| 29 | +public class ProductIntegrationTest { |
| 30 | + |
| 31 | + @Mock |
| 32 | + private ProductJpaRepository productJpaRepository; |
| 33 | + @InjectMocks |
| 34 | + private ProductJpaAdapter productJpaAdapter; |
| 35 | + private ProductUseCase productUseCase; |
| 36 | + private ProductController productController; |
| 37 | + private ProductAPIController productAPIController; |
| 38 | + |
| 39 | + private final Category defaultCategory = new Category(1L, false, "category"); |
| 40 | + private final Product defaultProduct = new Product(1L, false, "product", 10.0, defaultCategory); |
| 41 | + private final ProductPersistenceEntity defaultProductPersistenceEntity = new ProductPersistenceEntity(defaultProduct); |
| 42 | + private final ProductControllerDTO defaultProductControllerDTO = new ProductControllerDTO("product", 10.0, 1L); |
| 43 | + |
| 44 | + @BeforeEach |
| 45 | + void setUp() { |
| 46 | + MockitoAnnotations.openMocks(this); |
| 47 | + productUseCase = new ProductUseCase(); |
| 48 | + productController = new ProductController(productUseCase, productJpaAdapter); |
| 49 | + productAPIController = new ProductAPIController(productController); |
| 50 | + } |
| 51 | + |
| 52 | + @SneakyThrows |
| 53 | + @Test |
| 54 | + void testRead_EndToEnd() { |
| 55 | + when(productJpaRepository.findByIdAndDeletedFalse(1L)).thenReturn(Optional.of(defaultProductPersistenceEntity)); |
| 56 | + |
| 57 | + ResponseEntity<ProductControllerDTO> response = productAPIController.read(1L); |
| 58 | + assertEquals(HttpStatus.OK, response.getStatusCode()); |
| 59 | + assertEquals(Objects.requireNonNull(response.getBody()).getId(), 1L); |
| 60 | + } |
| 61 | + |
| 62 | + @SneakyThrows |
| 63 | + @Test |
| 64 | + void testList_EndToEnd() { |
| 65 | + when(productJpaRepository.findByDeletedFalse()).thenReturn(List.of(defaultProductPersistenceEntity)); |
| 66 | + |
| 67 | + ResponseEntity<List<ProductControllerDTO>> response = productAPIController.list(); |
| 68 | + assertEquals(HttpStatus.OK, response.getStatusCode()); |
| 69 | + assertEquals(Objects.requireNonNull(response.getBody()).get(0).getId(), 1L); |
| 70 | + } |
| 71 | + |
| 72 | + @SneakyThrows |
| 73 | + @Test |
| 74 | + void testListByCategory_EndToEnd() { |
| 75 | + when(productJpaRepository.findByDeletedFalseAndCategory_Name("category")).thenReturn(List.of(defaultProductPersistenceEntity)); |
| 76 | + |
| 77 | + ResponseEntity<List<ProductControllerDTO>> response = productAPIController.listByCategoryName("category"); |
| 78 | + assertEquals(HttpStatus.OK, response.getStatusCode()); |
| 79 | + assertEquals(Objects.requireNonNull(response.getBody()).get(0).getId(), 1L); |
| 80 | + } |
| 81 | + |
| 82 | + @SneakyThrows |
| 83 | + @Test |
| 84 | + void testCreate_EndToEnd() { |
| 85 | + when(productJpaRepository.saveAndFlush(any())).thenReturn(defaultProductPersistenceEntity); |
| 86 | + |
| 87 | + ResponseEntity<ProductControllerDTO> response = productAPIController.create(defaultProductControllerDTO); |
| 88 | + assertEquals(HttpStatus.OK, response.getStatusCode()); |
| 89 | + assertNotNull(response.getBody()); |
| 90 | + assertEquals(response.getBody().getId(), 1L); |
| 91 | + verify(productJpaRepository, times(1)).saveAndFlush(any()); |
| 92 | + } |
| 93 | + |
| 94 | + @SneakyThrows |
| 95 | + @Test |
| 96 | + public void testDelete_EndToEnd() { |
| 97 | + when(productJpaRepository.findByIdAndDeletedFalse(defaultProductPersistenceEntity.getId())).thenReturn(Optional.of(defaultProductPersistenceEntity)); |
| 98 | + when(productJpaRepository.saveAndFlush(any())).thenReturn(new ProductPersistenceEntity(defaultProductPersistenceEntity.getName(), defaultProductPersistenceEntity.getPrice(), defaultProductPersistenceEntity.getCategory())); |
| 99 | + |
| 100 | + ResponseEntity<DefaultResponseDTO> response = productAPIController.delete(defaultProductPersistenceEntity.getId()); |
| 101 | + |
| 102 | + assertEquals(HttpStatus.OK, response.getStatusCode()); |
| 103 | + assertNull(response.getBody()); |
| 104 | + verify(productJpaRepository, times(1)).findByIdAndDeletedFalse(any()); |
| 105 | + verify(productJpaRepository, times(1)).saveAndFlush(any()); |
| 106 | + } |
| 107 | + |
| 108 | +} |
0 commit comments