Skip to content

Commit 8a6c696

Browse files
committed
Testing Garment and Trippo repository
1 parent 130ac77 commit 8a6c696

File tree

5 files changed

+404
-8
lines changed

5 files changed

+404
-8
lines changed

src/main/java/com/outfitlab/project/infrastructure/repositories/TripoRepositoryImpl.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ private void checkIfExtensionIsValid(String extension) {
145145
}
146146
}
147147

148-
private MultipartFile convertImageUrlToMultipartFile(String imageUrl) throws ErrorUploadImageToTripoException {
148+
protected MultipartFile convertImageUrlToMultipartFile(String imageUrl) throws ErrorUploadImageToTripoException {
149149
InputStream inputStream = null;
150150
try {
151151
URL url = new URL(imageUrl);
@@ -233,7 +233,7 @@ private Map<String, Object> getHttpBodyImageToModelTripo(Map<String, Object> upl
233233
return bodyMap;
234234
}
235235

236-
private HttpHeaders getHttpHeaders(MediaType type) {
236+
protected HttpHeaders getHttpHeaders(MediaType type) {
237237
HttpHeaders taskHeaders = new HttpHeaders();
238238
taskHeaders.setContentType(type);
239239
taskHeaders.setBearerAuth(tripoApiKey);
@@ -267,7 +267,7 @@ private static void checkIfResponseIsOk(ResponseEntity<String> response) throws
267267
}
268268
}
269269

270-
private ResponseEntity<String> generateRequestToUploadImageToTripo(MultipartFile imageFile, String originalFilename) throws ErroBytesException {
270+
protected ResponseEntity<String> generateRequestToUploadImageToTripo(MultipartFile imageFile, String originalFilename) throws ErroBytesException {
271271
MultiValueMap<String, Object> body = new LinkedMultiValueMap<>();
272272
body.add("file", tryGetByteArrayResourceFromImage(imageFile, originalFilename));
273273
HttpHeaders headers = getHttpHeaders(MediaType.MULTIPART_FORM_DATA);
@@ -287,7 +287,7 @@ private String tryGetImageToken(ResponseEntity<String> response) throws ErrorRea
287287
return imageToken;
288288
}
289289

290-
private ResponseEntity<String> requestTripoTaskStatus(String taskId, HttpEntity<Void> entityWithTaskHeaders) {
290+
protected ResponseEntity<String> requestTripoTaskStatus(String taskId, HttpEntity<Void> entityWithTaskHeaders) {
291291
return restTemplate.exchange(
292292
taskUrl + "/" + taskId,
293293
HttpMethod.GET,

src/test/java/com/outfitlab/project/infrastructure/repositories/ClimaRepositoryImplTest.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package com.outfitlab.project.infrastructure.repositories;
22

3-
import static org.junit.jupiter.api.Assertions.*;
4-
53
import static org.assertj.core.api.Assertions.*;
64

75
import com.outfitlab.project.domain.exceptions.ClimaNotFoundException;
@@ -15,7 +13,6 @@
1513
import org.springframework.test.context.ActiveProfiles;
1614

1715
import java.util.List;
18-
import java.util.Optional;
1916

2017
import static org.mockito.Mockito.*;
2118
@SpringBootTest
Lines changed: 218 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,218 @@
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+
}

src/test/java/com/outfitlab/project/infrastructure/repositories/RecomendationRepositoryImplTest.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package com.outfitlab.project.infrastructure.repositories;
22

3-
import static org.junit.jupiter.api.Assertions.*;
43

54
import com.outfitlab.project.domain.exceptions.GarmentNotFoundException;
65
import com.outfitlab.project.domain.model.GarmentRecomendationModel;

0 commit comments

Comments
 (0)