1+ package br .com .alura .marketplace .iandt ;
2+
3+ import br .com .alura .marketplace .application .Application ;
4+ import br .com .alura .marketplace .domain .repository .ProdutoRepository ;
5+ import br .com .alura .marketplace .iandt .setup .LocalStackSetup ;
6+ import br .com .alura .marketplace .iandt .setup .PostgresSetup ;
7+ import br .com .alura .marketplace .iandt .setup .RedisSetup ;
8+ import io .restassured .RestAssured ;
9+ import org .junit .jupiter .api .*;
10+ import org .springframework .beans .factory .annotation .Autowired ;
11+ import org .springframework .boot .test .context .SpringBootTest ;
12+ import org .springframework .boot .test .web .server .LocalServerPort ;
13+ import org .springframework .test .context .ActiveProfiles ;
14+ import org .springframework .test .context .ContextConfiguration ;
15+ import org .testcontainers .junit .jupiter .Testcontainers ;
16+
17+ import static br .com .alura .marketplace .domain .entity .ProdutoFactory .criarProduto ;
18+ import static io .restassured .RestAssured .given ;
19+ import static org .assertj .core .api .Assertions .assertThat ;
20+ import static org .springframework .boot .test .context .SpringBootTest .WebEnvironment .RANDOM_PORT ;
21+
22+ @ ActiveProfiles ("test" )
23+ @ SpringBootTest (webEnvironment = RANDOM_PORT )
24+ @ ContextConfiguration (classes = Application .class )
25+ @ Testcontainers
26+ class ConsultaProdutoTest implements PostgresSetup , LocalStackSetup , RedisSetup {
27+
28+ @ LocalServerPort
29+ Integer port ;
30+
31+ @ Autowired
32+ ProdutoRepository produtoRepository ;
33+
34+ @ BeforeEach
35+ void beforeEach () {
36+ RestAssured .baseURI = String .format ("http://localhost:%s/api" , port );
37+ }
38+
39+ @ AfterEach
40+ void afterEach () {
41+ produtoRepository .deleteAll ();
42+ }
43+
44+ @ DisplayName ("Quando consultar um produto" )
45+ @ Nested
46+ class ConsultarProduto {
47+
48+ @ DisplayName ("Então deve executar com sucesso" )
49+ @ Nested
50+ class Sucesso {
51+
52+ @ DisplayName ("Dado um produto com todos os campos salvo na base" )
53+ @ Test
54+ void teste1 () {
55+ // Dado
56+ var produto = criarProduto ()
57+ .comTodosOsCamposExcetoDB ();
58+ produtoRepository .save (produto );
59+ // Quando
60+ var response = given ()
61+ .log ().all ()
62+ .get ("/v1/produtos/{id}" , produto .getProdutoId ())
63+ .then ()
64+ .log ().all ()
65+ .extract ()
66+ .response ();
67+ // Então
68+ assertThat (response .statusCode ())
69+ .isEqualTo (200 );
70+ }
71+
72+ @ DisplayName ("Dado uma segunda consulta do mesmo produto deve usar o cache" )
73+ @ Test
74+ void teste2 () {
75+ // Dado
76+ var produto = criarProduto ()
77+ .comTodosOsCamposExcetoDB ();
78+ produtoRepository .save (produto );
79+ for (var i = 0 ; i < 2 ; i ++) {
80+ // Quando
81+ var response = given ()
82+ .log ().all ()
83+ .get ("/v1/produtos/{id}" , produto .getProdutoId ())
84+ .then ()
85+ .log ().all ()
86+ .extract ()
87+ .response ();
88+ // Então
89+ assertThat (response .statusCode ())
90+ .isEqualTo (200 );
91+ produtoRepository .deleteAll ();
92+ }
93+ }
94+ }
95+ }
96+ }
0 commit comments