From 1df35a91968c8f39bfb57571eeb08431a9475f3e Mon Sep 17 00:00:00 2001 From: vanderleik Date: Wed, 10 Jul 2024 10:28:44 -0300 Subject: [PATCH] feat: PPI-8 - criando ProductRepository --- .../repositories/ProductRepository.java | 10 ++ src/main/resources/application-test.yaml | 5 +- .../test/V1__create_product_table.sql | 11 ++ .../repositories/ProductRepositoryTest.java | 141 ++++++++++++++++++ 4 files changed, 166 insertions(+), 1 deletion(-) create mode 100644 src/main/java/com/produtopedidoitens/api/adapters/persistence/repositories/ProductRepository.java create mode 100644 src/main/resources/db/migration/test/V1__create_product_table.sql create mode 100644 src/test/java/com/produtopedidoitens/api/adapters/persistence/repositories/ProductRepositoryTest.java diff --git a/src/main/java/com/produtopedidoitens/api/adapters/persistence/repositories/ProductRepository.java b/src/main/java/com/produtopedidoitens/api/adapters/persistence/repositories/ProductRepository.java new file mode 100644 index 0000000..8c2bd77 --- /dev/null +++ b/src/main/java/com/produtopedidoitens/api/adapters/persistence/repositories/ProductRepository.java @@ -0,0 +1,10 @@ +package com.produtopedidoitens.api.adapters.persistence.repositories; + +import com.produtopedidoitens.api.application.domain.entities.ProductEntity; +import org.springframework.data.jpa.repository.JpaRepository; + +import java.util.UUID; + +public interface ProductRepository extends JpaRepository { + +} diff --git a/src/main/resources/application-test.yaml b/src/main/resources/application-test.yaml index eb4a3a4..8463abe 100644 --- a/src/main/resources/application-test.yaml +++ b/src/main/resources/application-test.yaml @@ -15,4 +15,7 @@ spring: h2: console: enabled: true - path: /h2-console \ No newline at end of file + path: /h2-console + + flyway: + locations: classpath:db/migration/test \ No newline at end of file diff --git a/src/main/resources/db/migration/test/V1__create_product_table.sql b/src/main/resources/db/migration/test/V1__create_product_table.sql new file mode 100644 index 0000000..714d09d --- /dev/null +++ b/src/main/resources/db/migration/test/V1__create_product_table.sql @@ -0,0 +1,11 @@ +CREATE TABLE tbproduct ( + idproduct UUID, + productname VARCHAR(60) NOT NULL, + price NUMERIC(19,2) NOT NULL, + type VARCHAR(255) NOT NULL, + active BOOLEAN NOT NULL, + dthreg TIMESTAMP DEFAULT CURRENT_TIMESTAMP, + dthalt TIMESTAMP DEFAULT CURRENT_TIMESTAMP, + version BIGINT, + CONSTRAINT tbwine_pk PRIMARY KEY (idproduct) +); diff --git a/src/test/java/com/produtopedidoitens/api/adapters/persistence/repositories/ProductRepositoryTest.java b/src/test/java/com/produtopedidoitens/api/adapters/persistence/repositories/ProductRepositoryTest.java new file mode 100644 index 0000000..c85ba6f --- /dev/null +++ b/src/test/java/com/produtopedidoitens/api/adapters/persistence/repositories/ProductRepositoryTest.java @@ -0,0 +1,141 @@ +package com.produtopedidoitens.api.adapters.persistence.repositories; + +import com.produtopedidoitens.api.application.domain.entities.ProductEntity; +import com.produtopedidoitens.api.application.domain.enums.EnumProductType; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest; +import org.springframework.test.context.ActiveProfiles; + +import java.math.BigDecimal; +import java.util.List; + +import static org.junit.jupiter.api.Assertions.*; + +@DataJpaTest +@ActiveProfiles("test") +class ProductRepositoryTest { + + @Autowired + ProductRepository productRepository; + + private ProductEntity productEntity; + private ProductEntity serviceEntity; + + @BeforeEach + void setUp() { + productEntity = ProductEntity.builder() + .productName("Café") + .price(BigDecimal.valueOf(21.90)) + .type(EnumProductType.PRODUCT) + .active(true) + .build(); + serviceEntity = ProductEntity.builder() + .productName("Manutenção") + .price(BigDecimal.valueOf(100.00)) + .type(EnumProductType.SERVICE) + .active(true) + .build(); + } + + @Test + @DisplayName("Deve persistir um produto no banco de dados") + void testSaveProduct() { + ProductEntity productSaved = assertDoesNotThrow(() -> productRepository.save(productEntity)); + + assertNotNull(productSaved); + assertEquals(productEntity.getProductName(), productSaved.getProductName()); + assertEquals(productEntity.getPrice(), productSaved.getPrice()); + assertEquals(productEntity.getType(), productSaved.getType()); + assertEquals(productEntity.getActive(), productSaved.getActive()); + } + + @Test + @DisplayName("Deve persistir um serviço no banco de dados") + void testSaveService() { + ProductEntity serviceSaved = assertDoesNotThrow(() -> productRepository.save(serviceEntity)); + + assertNotNull(serviceSaved); + assertEquals(serviceEntity.getProductName(), serviceSaved.getProductName()); + assertEquals(serviceEntity.getPrice(), serviceSaved.getPrice()); + assertEquals(serviceEntity.getType(), serviceSaved.getType()); + assertEquals(serviceEntity.getActive(), serviceSaved.getActive()); + } + + @Test + @DisplayName("Deve retornar uma lista de produtos e serviços cadastrados no banco de dados") + void testList() { + ProductEntity productSaved = assertDoesNotThrow(() -> productRepository.save(productEntity)); + ProductEntity serviceSaved = assertDoesNotThrow(() -> productRepository.save(serviceEntity)); + + List list = assertDoesNotThrow(() -> productRepository.findAll()); + assertNotNull(list); + assertFalse(list.isEmpty()); + assertEquals(2, list.size()); + assertTrue(list.contains(productSaved)); + assertTrue(list.contains(serviceSaved)); + } + + @Test + @DisplayName("Deve retornar um produto ou serviço cadastrado no banco de dados") + void testFindById() { + ProductEntity productSaved = assertDoesNotThrow(() -> productRepository.save(productEntity)); + ProductEntity serviceSaved = assertDoesNotThrow(() -> productRepository.save(serviceEntity)); + + ProductEntity product = assertDoesNotThrow(() -> productRepository.findById(productSaved.getId()).orElse(null)); + assertNotNull(product); + assertEquals(productSaved, product); + + ProductEntity service = assertDoesNotThrow(() -> productRepository.findById(serviceSaved.getId()).orElse(null)); + assertNotNull(service); + assertEquals(serviceSaved, service); + } + + @Test + @DisplayName("Deve atualizar um produto cadastrado no banco de dados") + void testUpdateProduct() { + ProductEntity productSaved = assertDoesNotThrow(() -> productRepository.save(productEntity)); + + productSaved.setProductName("Café com leite"); + + ProductEntity productUpdated = assertDoesNotThrow(() -> productRepository.save(productSaved)); + assertNotNull(productUpdated); + assertEquals(productSaved.getProductName(), productUpdated.getProductName()); + } + + @Test + @DisplayName("Deve atualizar um serviço cadastrado no banco de dados") + void testUpdateService() { + ProductEntity serviceSaved = assertDoesNotThrow(() -> productRepository.save(serviceEntity)); + + serviceSaved.setProductName("Limpeza e conservação"); + + ProductEntity serviceUpdated = assertDoesNotThrow(() -> productRepository.save(serviceSaved)); + assertNotNull(serviceUpdated); + assertEquals(serviceSaved.getProductName(), serviceUpdated.getProductName()); + } + + @Test + @DisplayName("Deve deletar um produto cadastrado no banco de dados") + void testDeleteProduct() { + ProductEntity productSaved = assertDoesNotThrow(() -> productRepository.save(productEntity)); + + assertDoesNotThrow(() -> productRepository.delete(productSaved)); + + ProductEntity product = assertDoesNotThrow(() -> productRepository.findById(productSaved.getId()).orElse(null)); + assertNull(product); + } + + @Test + @DisplayName("Deve deletar um serviço cadastrado no banco de dados") + void testDeleteService() { + ProductEntity serviceSaved = assertDoesNotThrow(() -> productRepository.save(serviceEntity)); + + assertDoesNotThrow(() -> productRepository.delete(serviceSaved)); + + ProductEntity service = assertDoesNotThrow(() -> productRepository.findById(serviceSaved.getId()).orElse(null)); + assertNull(service); + } +} \ No newline at end of file