1+ package com .outfitlab .project .infrastructure .repositories ;
2+
3+ import com .outfitlab .project .domain .exceptions .GarmentNotFoundException ;
4+ import com .outfitlab .project .domain .model .PrendaModel ;
5+ import com .outfitlab .project .domain .model .dto .PageDTO ;
6+ import com .outfitlab .project .infrastructure .model .*;
7+ import com .outfitlab .project .infrastructure .repositories .interfaces .*;
8+ import org .junit .jupiter .api .Test ;
9+ import org .mockito .InjectMocks ;
10+ import org .mockito .Mock ;
11+ import org .springframework .boot .test .context .SpringBootTest ;
12+ import org .springframework .data .domain .Page ;
13+ import org .springframework .data .domain .PageImpl ;
14+ import org .springframework .data .domain .PageRequest ;
15+ import org .springframework .test .context .ActiveProfiles ;
16+
17+ import java .util .List ;
18+ import java .util .Optional ;
19+
20+ import static org .mockito .Mockito .verify ;
21+
22+ import static org .assertj .core .api .Assertions .*;
23+ import static org .mockito .Mockito .*;
24+
25+ @ SpringBootTest
26+ @ ActiveProfiles ("test" )
27+ class GarmentRepositoryImplTest {
28+ @ Mock
29+ private GarmentJpaRepository garmentJpaRepository ;
30+ @ Mock
31+ private BrandJpaRepository brandJpaRepository ;
32+ @ Mock
33+ private ColorJpaRepository colorJpaRepository ;
34+ @ Mock
35+ private ClimaJpaRepository climaJpaRepository ;
36+ @ Mock
37+ private OcasionJpaRepository ocasionJpaRepository ;
38+
39+ @ InjectMocks
40+ private GarmentRepositoryImpl repository ;
41+
42+ @ Test
43+ void shouldFindByBrandCodeAndTipo () {
44+ givenGarmentsPage ("brand1" , "superior" , 0 );
45+
46+ PageDTO result = whenFindByBrandCodeAndTipo ("brand1" , "superior" , 0 );
47+
48+ thenPageDTOShouldBeCorrect (result , 1 );
49+ }
50+
51+ @ Test
52+ void shouldThrowWhenGarmentCodeNotFound () {
53+ givenGarmentNotFound ("G123" );
54+
55+ assertThatThrownBy (() -> whenFindByGarmentCode ("G123" ))
56+ .isInstanceOf (GarmentNotFoundException .class )
57+ .hasMessageContaining ("No encontramos la prenda" );
58+ }
59+
60+ @ Test
61+ void shouldFindGarmentByGarmentCode () {
62+ givenExistingGarment ("G123" );
63+
64+ PrendaModel model = whenFindByGarmentCode ("G123" );
65+
66+ thenGarmentCodeShouldBe (model , "G123" );
67+ }
68+
69+ @ Test
70+ void shouldCreateGarmentSuccessfully () {
71+ givenBrandExists ("B1" );
72+ givenColorExists ("Red" );
73+ givenClimaExists ("Frio" );
74+ givenOcasionExists ("Casual" );
75+
76+ whenCreateGarment ("Prenda1" , "G123" , "superior" , "Red" , "B1" , "img.png" , "Frio" ,
77+ List .of ("Casual" ), "Hombre" );
78+
79+ thenGarmentShouldBeSaved ();
80+ }
81+
82+ @ Test
83+ void shouldUpdateGarmentSuccessfully () {
84+ givenExistingGarmentEntity ("G123" );
85+ givenColorExists ("Blue" );
86+ givenClimaExists ("Calor" );
87+ givenOcasionExists ("Formal" );
88+
89+ whenUpdateGarment ("Prenda2" , "inferior" , "Blue" , "Formal" , "G123" , "newImg.png" , "G124" , "Calor" ,
90+ List .of ("Formal" ), "Mujer" );
91+
92+ thenGarmentShouldBeSaved ();
93+ }
94+
95+ @ Test
96+ void shouldDeleteGarmentSuccessfully () {
97+ String code = "G123" ;
98+
99+ whenDeleteGarment (code );
100+
101+ thenDeleteByGarmentCodeShouldBeCalled (code );
102+ }
103+
104+ private void givenGarmentsPage (String brandCode , String tipo , int page ) {
105+ // Marca
106+ MarcaEntity brand = new MarcaEntity ();
107+ brand .setCodigoMarca (brandCode );
108+ brand .setNombre ("Brand Name" );
109+
110+ // Color
111+ ColorEntity color = new ColorEntity ();
112+ color .setNombre ("Rojo" );
113+ color .setValor (1 );
114+
115+ // Clima
116+ ClimaEntity clima = new ClimaEntity ();
117+ clima .setNombre ("Frio" );
118+
119+ // Prenda
120+ PrendaEntity entity = new PrendaEntity ();
121+ entity .setGarmentCode ("G1" );
122+ entity .setNombre ("Prenda Test" );
123+ entity .setMarca (brand );
124+ entity .setColor (color );
125+ entity .setClimaAdecuado (clima );
126+ entity .setTipo (tipo );
127+
128+ Page <PrendaEntity > pageResult = new PageImpl <>(List .of (entity ), PageRequest .of (page , 10 ), 1 );
129+
130+ when (garmentJpaRepository .findByMarca_CodigoMarcaAndTipo (
131+ eq (brandCode ),
132+ eq (tipo .toLowerCase ()),
133+ any (PageRequest .class )
134+ )).thenReturn (pageResult );
135+ }
136+
137+ private void givenGarmentNotFound (String garmentCode ) {
138+ when (garmentJpaRepository .findByGarmentCode (garmentCode )).thenReturn (null );
139+ }
140+
141+ private void givenExistingGarment (String garmentCode ) {
142+ PrendaEntity entity = new PrendaEntity ();
143+ entity .setGarmentCode (garmentCode );
144+ when (garmentJpaRepository .findByGarmentCode (garmentCode )).thenReturn (entity );
145+ }
146+
147+ private void givenExistingGarmentEntity (String garmentCode ) {
148+ PrendaEntity entity = new PrendaEntity ();
149+ entity .setGarmentCode (garmentCode );
150+ when (garmentJpaRepository .findByGarmentCode (garmentCode )).thenReturn (entity );
151+ }
152+
153+ private void givenBrandExists (String brandCode ) {
154+ MarcaEntity brand = new MarcaEntity (brandCode , "BrandName" );
155+ when (brandJpaRepository .findByCodigoMarca (brandCode )).thenReturn (brand );
156+ }
157+
158+ private void givenColorExists (String colorNombre ) {
159+ ColorEntity color = new ColorEntity ();
160+ color .setNombre (colorNombre );
161+ when (colorJpaRepository .findColorEntityByNombre (colorNombre )).thenReturn (Optional .of (color ));
162+ }
163+
164+ private void givenClimaExists (String climaNombre ) {
165+ ClimaEntity clima = new ClimaEntity ();
166+ clima .setNombre (climaNombre );
167+ when (climaJpaRepository .findClimaEntityByNombre (climaNombre )).thenReturn (Optional .of (clima ));
168+ }
169+
170+ private void givenOcasionExists (String ocasionNombre ) {
171+ OcasionEntity ocasion = new OcasionEntity ();
172+ ocasion .setNombre (ocasionNombre );
173+ when (ocasionJpaRepository .findOcasionEntityByNombre (ocasionNombre )).thenReturn (Optional .of (ocasion ));
174+ }
175+
176+ private PageDTO whenFindByBrandCodeAndTipo (String brandCode , String tipo , int page ) {
177+ return repository .findByBrandCodeAndTipo (brandCode , tipo , page );
178+ }
179+
180+ private PrendaModel whenFindByGarmentCode (String code ) {
181+ return repository .findByGarmentCode (code );
182+ }
183+
184+ private void whenCreateGarment (String name , String garmentCode , String type , String colorNombre ,
185+ String brandCode , String imageUrl , String climaNombre , List <String > ocasiones , String genero ) {
186+ repository .createGarment (name , garmentCode , type , colorNombre , brandCode , imageUrl , climaNombre , ocasiones , genero );
187+ }
188+
189+ private void whenUpdateGarment (String name , String type , String colorNombre , String event , String garmentCode ,
190+ String imageUrl , String newGarmentCode , String climaNombre , List <String > ocasiones , String genero ) {
191+ repository .updateGarment (name , type , colorNombre , event , garmentCode , imageUrl , newGarmentCode , climaNombre , ocasiones , genero );
192+ }
193+
194+ private void whenDeleteGarment (String garmentCode ) {
195+ repository .deleteGarment (garmentCode );
196+ }
197+
198+ private void thenPageDTOShouldBeCorrect (PageDTO dto , int expectedSize ) {
199+ assertThat (dto .getContent ()).hasSize (expectedSize );
200+ assertThat (dto .getPage ()).isZero ();
201+ assertThat (dto .getSize ()).isEqualTo (10 );
202+ assertThat (dto .getTotalElements ()).isEqualTo (1 );
203+ }
204+
205+ private void thenGarmentCodeShouldBe (PrendaModel model , String code ) {
206+ assertThat (model .getGarmentCode ()).isEqualTo (code );
207+ }
208+
209+ private void thenGarmentShouldBeSaved () {
210+ verify (garmentJpaRepository ).save (any (PrendaEntity .class ));
211+ }
212+
213+ private void thenDeleteByGarmentCodeShouldBeCalled (String code ) {
214+ verify (garmentJpaRepository ).deleteByGarmentCode (code );
215+ }
216+
217+
218+ }
0 commit comments