-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[tests] adiciona testes automatizados para CaracteristicaProdutoReque…
…st e Produto
- Loading branch information
1 parent
7a4251a
commit 95ea7e7
Showing
2 changed files
with
185 additions
and
0 deletions.
There are no files selected for viewing
90 changes: 90 additions & 0 deletions
90
...va/com/zupacademy/magno/mercadolivre/caracteristica/CaracteristicaProdutoRequestTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
package com.zupacademy.magno.mercadolivre.caracteristica; | ||
|
||
import com.zupacademy.magno.mercadolivre.categoria.Categoria; | ||
import com.zupacademy.magno.mercadolivre.produto.Produto; | ||
import com.zupacademy.magno.mercadolivre.usuario.Usuario; | ||
import com.zupacademy.magno.mercadolivre.usuario.cadastro.SenhaLimpa; | ||
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.boot.test.autoconfigure.orm.jpa.TestEntityManager; | ||
import org.springframework.test.annotation.DirtiesContext; | ||
|
||
import javax.persistence.PersistenceException; | ||
import java.math.BigDecimal; | ||
import java.util.Arrays; | ||
import java.util.HashSet; | ||
import java.util.Set; | ||
|
||
import static org.junit.jupiter.api.Assertions.*; | ||
|
||
@DataJpaTest | ||
@DirtiesContext(classMode = DirtiesContext.ClassMode.BEFORE_EACH_TEST_METHOD) | ||
class CaracteristicaProdutoRequestTest { | ||
|
||
@Autowired | ||
TestEntityManager testManager; | ||
|
||
private Categoria categoria; | ||
private Usuario usuario; | ||
|
||
@BeforeEach | ||
public void iniSetup(){ | ||
categoria = new Categoria("TECH"); | ||
usuario = new Usuario("[email protected]", new SenhaLimpa("123456")); | ||
|
||
} | ||
|
||
@Test | ||
@DisplayName("NAO deve receber caracteristicas com nomes iguais") | ||
public void test01(){ | ||
Set<CaracteristicaProdutoRequest> caracteristicas = new HashSet<>(Arrays.asList( | ||
new CaracteristicaProdutoRequest("marca", "lenovo"), | ||
new CaracteristicaProdutoRequest("marca", "lenovo"), | ||
new CaracteristicaProdutoRequest("cor", "preto"))); | ||
|
||
|
||
testManager.persistAndFlush(categoria); | ||
testManager.persistAndFlush(usuario); | ||
|
||
Produto novoProduto = new Produto( | ||
"notebook lenovo", | ||
new BigDecimal("2000.0"), | ||
1, | ||
"um notebook", | ||
categoria, | ||
caracteristicas, | ||
usuario); | ||
|
||
assertThrows(PersistenceException.class, ()-> { | ||
testManager.persist(novoProduto); | ||
}); | ||
} | ||
|
||
@Test | ||
@DisplayName("DEVE receber caracteristicas sem nomes iguais") | ||
public void test02(){ | ||
Set<CaracteristicaProdutoRequest> caracteristicas = new HashSet<>(Arrays.asList( | ||
new CaracteristicaProdutoRequest("marca", "lenovo"), | ||
new CaracteristicaProdutoRequest("peso", "200.0"), | ||
new CaracteristicaProdutoRequest("cor", "preto"))); | ||
|
||
testManager.persistAndFlush(categoria); | ||
testManager.persistAndFlush(usuario); | ||
|
||
Produto novoProduto = new Produto( | ||
"notebook lenovo", | ||
new BigDecimal("2000.0"), | ||
1, | ||
"um notebook", | ||
categoria, | ||
caracteristicas, | ||
usuario); | ||
|
||
Produto produtoSalvo = testManager.persistFlushFind(novoProduto); | ||
assertEquals(3, produtoSalvo.getCaracteristicas().size()); | ||
} | ||
|
||
} |
95 changes: 95 additions & 0 deletions
95
src/test/java/com/zupacademy/magno/mercadolivre/produto/ProdutoTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
package com.zupacademy.magno.mercadolivre.produto; | ||
|
||
import com.zupacademy.magno.mercadolivre.caracteristica.CaracteristicaProdutoRequest; | ||
import com.zupacademy.magno.mercadolivre.categoria.Categoria; | ||
import com.zupacademy.magno.mercadolivre.usuario.Usuario; | ||
import com.zupacademy.magno.mercadolivre.usuario.cadastro.SenhaLimpa; | ||
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.TestEntityManager; | ||
|
||
import java.math.BigDecimal; | ||
import java.util.Arrays; | ||
import java.util.HashSet; | ||
import java.util.Set; | ||
|
||
import static org.junit.jupiter.api.Assertions.*; | ||
|
||
class ProdutoTest { | ||
private Categoria categoria; | ||
private Usuario usuario; | ||
|
||
@BeforeEach | ||
public void iniSetup(){ | ||
categoria = new Categoria("TECH"); | ||
usuario = new Usuario("[email protected]", new SenhaLimpa("123456")); | ||
|
||
} | ||
|
||
@Test | ||
@DisplayName("NAO deve criar Produto com menos que 3 caracteristicas") | ||
public void test01(){ | ||
|
||
Set<CaracteristicaProdutoRequest> caracteristicas = new HashSet<>(Arrays.asList( | ||
new CaracteristicaProdutoRequest("marca", "lenovo"), | ||
new CaracteristicaProdutoRequest("cor", "preto"))); | ||
|
||
assertThrows(IllegalArgumentException.class, () -> { | ||
Produto novoProduto = new Produto( | ||
"notebook lenovo", | ||
new BigDecimal("2000.0"), | ||
1, | ||
"um notebook", | ||
categoria, | ||
caracteristicas, | ||
usuario); | ||
}); | ||
|
||
} | ||
|
||
@Test | ||
@DisplayName("DEVE criar Produto com 3 caracteristicas válidas") | ||
public void test02(){ | ||
|
||
Set<CaracteristicaProdutoRequest> caracteristicas = new HashSet<>(Arrays.asList( | ||
new CaracteristicaProdutoRequest("marca", "lenovo"), | ||
new CaracteristicaProdutoRequest("shape", "quadrado"), | ||
new CaracteristicaProdutoRequest("cor", "preto"))); | ||
|
||
Produto novoProduto = new Produto( | ||
"notebook lenovo", | ||
new BigDecimal("2000.0"), | ||
1, | ||
"um notebook", | ||
categoria, | ||
caracteristicas, | ||
usuario); | ||
|
||
assertEquals(3, novoProduto.getCaracteristicas().size()); | ||
} | ||
|
||
@Test | ||
@DisplayName("DEVE criar Produto com mais que 3 caracteristicas válidas") | ||
public void test03(){ | ||
|
||
Set<CaracteristicaProdutoRequest> caracteristicas = new HashSet<>(Arrays.asList( | ||
new CaracteristicaProdutoRequest("marca", "lenovo"), | ||
new CaracteristicaProdutoRequest("shape", "quadrado"), | ||
new CaracteristicaProdutoRequest("peso", "1.5kg"), | ||
new CaracteristicaProdutoRequest("cor", "preto"))); | ||
|
||
Produto novoProduto = new Produto( | ||
"notebook lenovo", | ||
new BigDecimal("2000.0"), | ||
1, | ||
"um notebook", | ||
categoria, | ||
caracteristicas, | ||
usuario); | ||
|
||
assertEquals(4, novoProduto.getCaracteristicas().size()); | ||
} | ||
|
||
} |