Skip to content

Commit

Permalink
feat: PPI-8 - criando ProductRepository
Browse files Browse the repository at this point in the history
  • Loading branch information
vanderleik committed Jul 10, 2024
1 parent 0bf8981 commit 1df35a9
Show file tree
Hide file tree
Showing 4 changed files with 166 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -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<ProductEntity, UUID> {

}
5 changes: 4 additions & 1 deletion src/main/resources/application-test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,7 @@ spring:
h2:
console:
enabled: true
path: /h2-console
path: /h2-console

flyway:
locations: classpath:db/migration/test
11 changes: 11 additions & 0 deletions src/main/resources/db/migration/test/V1__create_product_table.sql
Original file line number Diff line number Diff line change
@@ -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)
);
Original file line number Diff line number Diff line change
@@ -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<ProductEntity> 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);
}
}

0 comments on commit 1df35a9

Please sign in to comment.