Skip to content

Commit 67ee3a0

Browse files
committed
test controllers (algunos)
1 parent f377405 commit 67ee3a0

File tree

4 files changed

+350
-86
lines changed

4 files changed

+350
-86
lines changed

src/test/java/com/outfitlab/project/presentation/BrandControllerTest.java

Lines changed: 80 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,61 @@
1616

1717
class BrandControllerTest {
1818

19-
private GetAllBrands getAllBrands = mock(GetAllBrands.class);;
19+
private GetAllBrands getAllBrands = mock(GetAllBrands.class);
2020
private GetBrandAndGarmentsByBrandCode getBrandAndGarmentsByBrandCode = mock(GetBrandAndGarmentsByBrandCode.class);
2121
private ActivateBrand activateBrand = mock(ActivateBrand.class);
2222
private DesactivateBrand desactivateBrand = mock(DesactivateBrand.class);
2323
private GetAllBrandsWithRelatedUsers getAllBrandsWithRelatedUsers = mock(GetAllBrandsWithRelatedUsers.class);
2424
private GetNotificationsNewBrands getNotificationsNewBrands = mock(GetNotificationsNewBrands.class);
25-
private BrandController brandController = new BrandController(getAllBrands, getBrandAndGarmentsByBrandCode, activateBrand, desactivateBrand, getAllBrandsWithRelatedUsers, getNotificationsNewBrands);
25+
26+
private BrandController controller = new BrandController(
27+
getAllBrands,
28+
getBrandAndGarmentsByBrandCode,
29+
activateBrand,
30+
desactivateBrand,
31+
getAllBrandsWithRelatedUsers,
32+
getNotificationsNewBrands);
33+
34+
@Test
35+
void givenValidPageWhenGetMarcasThenReturnOk() throws Exception {
36+
givenBrandsExist();
37+
ResponseEntity<?> response = whenCallGetMarcas(0);
38+
thenResponseOkWithBrands(response);
39+
thenGetAllBrandsCalledOnce();
40+
}
41+
42+
@Test
43+
void givenNoBrandsWhenGetMarcasThenReturn404() throws Exception {
44+
givenBrandsNotFound();
45+
ResponseEntity<?> response = whenCallGetMarcas(0);
46+
thenResponseNotFound(response, "No se encontraron marcas");
47+
}
48+
49+
@Test
50+
void givenNegativePageWhenGetMarcasThenReturn404() throws Exception {
51+
givenNegativePageError();
52+
ResponseEntity<?> response = whenCallGetMarcas(-1);
53+
thenResponseNotFound(response, "Página menor que cero");
54+
}
55+
56+
@Test
57+
void givenNonExistingBrandWhenGetBrandGarmentsThenReturn404() throws Exception {
58+
givenBrandNotFoundForGetBrandGarments();
59+
ResponseEntity<?> response = whenCallGetBrandAndGarments("puma", 0);
60+
thenResponseNotFound(response, "Marca no encontrada");
61+
}
2662

2763
@Test
28-
public void givenValidPageWhenGetMarcasThenReturnsOkWithContent() throws Exception, BrandsNotFoundException {
29-
BrandDTO brand1 = new BrandDTO("nike", "Nike", "https://logos.com/logaso.png");
30-
BrandDTO brand2 = new BrandDTO("adidas", "Adidas", "https://logos.com/logaso.png");
64+
void givenNegativePageWhenGetBrandGarmentsThenReturn404() throws Exception {
65+
givenNegativePageErrorForBrandGarments();
66+
ResponseEntity<?> response = whenCallGetBrandAndGarments("nike", -1);
67+
thenResponseNotFound(response, "Página menor que cero");
68+
}
69+
70+
// privadosss ----
71+
private void givenBrandsExist() throws Exception {
72+
BrandDTO brand1 = new BrandDTO("nike", "Nike", "https://logos.com/logaso.png");
73+
BrandDTO brand2 = new BrandDTO("adidas", "Adidas", "https://logos.com/logaso.png");
3174

3275
Page<BrandDTO> pageResult = new PageImpl<>(
3376
List.of(brand1, brand2),
@@ -36,57 +79,51 @@ public void givenValidPageWhenGetMarcasThenReturnsOkWithContent() throws Excepti
3679
);
3780

3881
when(getAllBrands.execute(0)).thenReturn(pageResult);
39-
ResponseEntity<?> response = brandController.getMarcas(0);
40-
41-
42-
assertEquals(200, response.getStatusCode().value());
43-
Map<?, ?> body = (Map<?, ?>) response.getBody();
44-
assertNotNull(body);
45-
assertEquals(2L, body.get("totalElements"));
46-
assertEquals(0, body.get("page"));
47-
assertNotNull(body.get("content"));
48-
49-
verify(getAllBrands, times(1)).execute(0);
5082
}
5183

52-
@Test
53-
public void givenNoBrandsFoundWhenGetMarcasThenReturns404() throws Exception, BrandsNotFoundException {
54-
when(getAllBrands.execute(0)).thenThrow(new BrandsNotFoundException("No se encontraron marcas"));
55-
56-
ResponseEntity<?> response = brandController.getMarcas(0);
57-
58-
assertEquals(404, response.getStatusCode().value());
59-
assertEquals("No se encontraron marcas", response.getBody());
84+
private void givenBrandsNotFound() throws Exception {
85+
when(getAllBrands.execute(0))
86+
.thenThrow(new BrandsNotFoundException("No se encontraron marcas"));
6087
}
6188

62-
@Test
63-
public void givenNegativePageWhenGetMarcasThenReturns404() throws Exception, BrandsNotFoundException {
64-
when(getAllBrands.execute(-1)).thenThrow(new PageLessThanZeroException("Página menor que cero"));
65-
66-
ResponseEntity<?> response = brandController.getMarcas(-1);
89+
private void givenNegativePageError() throws Exception {
90+
when(getAllBrands.execute(-1))
91+
.thenThrow(new PageLessThanZeroException("Página menor que cero"));
92+
}
6793

68-
assertEquals(404, response.getStatusCode().value());
69-
assertEquals("Página menor que cero", response.getBody());
94+
private void givenBrandNotFoundForGetBrandGarments() throws Exception {
95+
when(getBrandAndGarmentsByBrandCode.execute("puma", 0))
96+
.thenThrow(new BrandsNotFoundException("Marca no encontrada"));
7097
}
7198

72-
@Test
73-
public void givenNoExistingBrandWhenGetBrandAndGarmentsByBrandCodeThenReturns404() throws Exception, BrandsNotFoundException {
74-
when(getBrandAndGarmentsByBrandCode.execute("puma", 0)).thenThrow(new BrandsNotFoundException("Marca no encontrada"));
99+
private void givenNegativePageErrorForBrandGarments() throws Exception {
100+
when(getBrandAndGarmentsByBrandCode.execute("nike", -1))
101+
.thenThrow(new PageLessThanZeroException("Página menor que cero"));
102+
}
75103

76-
ResponseEntity<?> response = brandController.getBrandAndGarmentsByBrandCode("puma", 0);
104+
private ResponseEntity<?> whenCallGetMarcas(int page) {
105+
return controller.getMarcas(page);
106+
}
77107

78-
assertEquals(404, response.getStatusCode().value());
79-
assertEquals("Marca no encontrada", response.getBody());
108+
private ResponseEntity<?> whenCallGetBrandAndGarments(String brand, int page) {
109+
return controller.getBrandAndGarmentsByBrandCode(brand, page);
80110
}
81111

82-
@Test
83-
public void givenNegativePageWhenGetBrandAndGarmentsByBrandCodeThenReturns404() throws Exception, BrandsNotFoundException {
84-
when(getBrandAndGarmentsByBrandCode.execute("nike", -1)).thenThrow(new PageLessThanZeroException("Página menor que cero"));
112+
private void thenResponseOkWithBrands(ResponseEntity<?> response) {
113+
assertEquals(200, response.getStatusCode().value());
114+
Map<?, ?> body = (Map<?, ?>) response.getBody();
115+
assertNotNull(body);
116+
assertEquals(2L, body.get("totalElements"));
117+
assertEquals(0, body.get("page"));
118+
assertNotNull(body.get("content"));
119+
}
85120

86-
ResponseEntity<?> response = brandController.getBrandAndGarmentsByBrandCode("nike", -1);
121+
private void thenGetAllBrandsCalledOnce() {
122+
verify(getAllBrands, times(1)).execute(0);
123+
}
87124

125+
private void thenResponseNotFound(ResponseEntity<?> response, String expectedMessage) {
88126
assertEquals(404, response.getStatusCode().value());
89-
assertEquals("Página menor que cero", response.getBody());
127+
assertEquals(expectedMessage, response.getBody());
90128
}
91129
}
92-
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
package com.outfitlab.project.presentation;
2+
3+
import com.outfitlab.project.domain.model.ClimaModel;
4+
import com.outfitlab.project.domain.model.ColorModel;
5+
import com.outfitlab.project.domain.model.OcasionModel;
6+
import com.outfitlab.project.domain.model.dto.RecommendationCategoriesDTO;
7+
import com.outfitlab.project.domain.useCases.recomendations.GetAllClima;
8+
import com.outfitlab.project.domain.useCases.recomendations.GetAllColors;
9+
import com.outfitlab.project.domain.useCases.recomendations.GetAllOcacion;
10+
import org.junit.jupiter.api.BeforeEach;
11+
import org.junit.jupiter.api.Test;
12+
import org.mockito.Mockito;
13+
import org.springframework.http.ResponseEntity;
14+
15+
import java.util.Arrays;
16+
import java.util.List;
17+
18+
import static org.mockito.Mockito.when;
19+
import static org.junit.jupiter.api.Assertions.assertEquals;
20+
21+
public class CategoryControllerTest {
22+
23+
private GetAllClima getAllClima;
24+
private GetAllOcacion getAllOcacion;
25+
private GetAllColors getAllColors;
26+
27+
private CategoryController controller;
28+
29+
@BeforeEach
30+
void setUp() {
31+
getAllClima = Mockito.mock(GetAllClima.class);
32+
getAllOcacion = Mockito.mock(GetAllOcacion.class);
33+
getAllColors = Mockito.mock(GetAllColors.class);
34+
35+
controller = new CategoryController(getAllClima, getAllOcacion, getAllColors);
36+
}
37+
38+
@Test
39+
void shouldReturnRecommendationCategories() {
40+
41+
List<ClimaModel> climas = givenClimas();
42+
List<OcasionModel> ocasiones = givenOcasiones();
43+
List<ColorModel> colores = givenColores();
44+
45+
mockUseCases(climas, ocasiones, colores);
46+
47+
ResponseEntity<RecommendationCategoriesDTO> response = whenCallingEndpoint();
48+
49+
thenResponseIsCorrect(response, climas, ocasiones, colores);
50+
}
51+
52+
private List<ClimaModel> givenClimas() {
53+
return Arrays.asList(new ClimaModel(1L, "calido"), new ClimaModel(2L,"frio"));
54+
}
55+
56+
private List<OcasionModel> givenOcasiones() {
57+
return Arrays.asList(new OcasionModel(1L,"fiesta"), new OcasionModel(2L,"trabajo"));
58+
}
59+
60+
private List<ColorModel> givenColores() {
61+
return Arrays.asList(new ColorModel(1L, "rojo", 1), new ColorModel(2L, "azul", 1));
62+
}
63+
64+
private void mockUseCases(List<ClimaModel> climas, List<OcasionModel> ocasiones, List<ColorModel> colores) {
65+
when(getAllClima.execute()).thenReturn(climas);
66+
when(getAllOcacion.execute()).thenReturn(ocasiones);
67+
when(getAllColors.execute()).thenReturn(colores);
68+
}
69+
70+
private ResponseEntity<RecommendationCategoriesDTO> whenCallingEndpoint() {
71+
return controller.getRecommendationCategories();
72+
}
73+
74+
private void thenResponseIsCorrect(ResponseEntity<RecommendationCategoriesDTO> response, List<ClimaModel> climas, List<OcasionModel> ocasiones, List<ColorModel> colores) {
75+
assertEquals(200, response.getStatusCodeValue());
76+
RecommendationCategoriesDTO body = response.getBody();
77+
assertEquals(climas, body.getClimas());
78+
assertEquals(ocasiones, body.getOcasiones());
79+
assertEquals(colores, body.getColores());
80+
}
81+
}

0 commit comments

Comments
 (0)