Skip to content

Commit 01b64ab

Browse files
committed
Agrega todos los tests de dashboard
1 parent 52802b1 commit 01b64ab

File tree

4 files changed

+672
-0
lines changed

4 files changed

+672
-0
lines changed
Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
package com.outfitlab.project.domain.useCases.dashboard;
2+
3+
import com.outfitlab.project.domain.interfaces.repositories.CombinationAttemptRepository;
4+
import com.outfitlab.project.domain.model.CombinationAttemptModel;
5+
import com.outfitlab.project.domain.model.dto.PrendaDashboardDTO.DiaPrueba;
6+
import org.junit.jupiter.api.BeforeEach;
7+
import org.junit.jupiter.api.Test;
8+
import java.time.LocalDateTime;
9+
import java.util.ArrayList;
10+
import java.util.Collections;
11+
import java.util.List;
12+
import java.util.stream.IntStream;
13+
import static org.junit.jupiter.api.Assertions.*;
14+
import static org.mockito.Mockito.*;
15+
16+
public class GetActividadPorDiasTest {
17+
18+
private CombinationAttemptRepository combinationAttemptRepository = mock(CombinationAttemptRepository.class);
19+
private GetActividadPorDias getActividadPorDias;
20+
21+
private final int DAYS_TO_CHECK = 30;
22+
23+
@BeforeEach
24+
void setUp() {
25+
getActividadPorDias = new GetActividadPorDias(combinationAttemptRepository);
26+
}
27+
28+
29+
@Test
30+
public void shouldReturnCorrectCountsWhenAttemptsAreSpreadAcrossDays() {
31+
List<CombinationAttemptModel> mockAttempts = givenAttemptsExistOnSpecificDays(1, 1, 3, 15, 2, 30);
32+
givenRepositoryReturnsAttempts(mockAttempts);
33+
34+
List<DiaPrueba> result = whenExecuteGetActividad();
35+
36+
thenRepositoryFindLastNDaysWasCalled(DAYS_TO_CHECK);
37+
38+
List<DiaPrueba> expected = List.of(
39+
createDiaPrueba(1, 1),
40+
createDiaPrueba(15, 3),
41+
createDiaPrueba(30, 2)
42+
);
43+
thenDailyCountsAreCorrect(result, expected);
44+
}
45+
46+
@Test
47+
public void shouldReturn30DaysWithZeroCountsWhenNoAttemptsAreFound() {
48+
givenRepositoryReturnsEmptyAttempts();
49+
50+
List<DiaPrueba> result = whenExecuteGetActividad();
51+
52+
thenRepositoryFindLastNDaysWasCalled(DAYS_TO_CHECK);
53+
thenAllDaysHaveZeroCount(result);
54+
}
55+
56+
@Test
57+
public void shouldCountAllAttemptsCorrectlyInSingleDay() {
58+
List<CombinationAttemptModel> mockAttempts = givenAttemptsExistOnSpecificDays(5, 1);
59+
givenRepositoryReturnsAttempts(mockAttempts);
60+
61+
List<DiaPrueba> result = whenExecuteGetActividad();
62+
63+
thenDailyCountsAreCorrect(result, List.of(createDiaPrueba(1, 5)));
64+
}
65+
66+
@Test
67+
public void shouldCountAttemptsOnBoundaryDaysCorrectly() {
68+
List<CombinationAttemptModel> mockAttempts = givenAttemptsExistOnSpecificDays(2, 1, 4, 30);
69+
givenRepositoryReturnsAttempts(mockAttempts);
70+
71+
List<DiaPrueba> result = whenExecuteGetActividad();
72+
73+
List<DiaPrueba> expected = List.of(
74+
createDiaPrueba(1, 2),
75+
createDiaPrueba(30, 4)
76+
);
77+
thenDailyCountsAreCorrect(result, expected);
78+
}
79+
80+
@Test
81+
public void shouldIgnoreAttemptsWithInvalidDayOfMonth() {
82+
List<CombinationAttemptModel> mockAttempts = new ArrayList<>();
83+
mockAttempts.add(createAttempt(10));
84+
mockAttempts.add(createAttempt(31));
85+
86+
givenRepositoryReturnsAttempts(mockAttempts);
87+
88+
List<DiaPrueba> result = whenExecuteGetActividad();
89+
90+
thenDailyCountsAreCorrect(result, List.of(createDiaPrueba(10, 1)));
91+
}
92+
93+
94+
//privadoss
95+
private void givenRepositoryReturnsAttempts(List<CombinationAttemptModel> attempts) {
96+
when(combinationAttemptRepository.findLastNDays(DAYS_TO_CHECK)).thenReturn(attempts);
97+
}
98+
99+
private void givenRepositoryReturnsEmptyAttempts() {
100+
when(combinationAttemptRepository.findLastNDays(DAYS_TO_CHECK)).thenReturn(Collections.emptyList());
101+
}
102+
103+
private CombinationAttemptModel createAttempt(int dayOfMonth) {
104+
CombinationAttemptModel attempt = mock(CombinationAttemptModel.class);
105+
106+
LocalDateTime date = LocalDateTime.of(2025, 1, dayOfMonth, 10, 0);
107+
when(attempt.getCreatedAt()).thenReturn(date);
108+
return attempt;
109+
}
110+
111+
private List<CombinationAttemptModel> givenAttemptsExistOnSpecificDays(int... countDayPairs) {
112+
List<CombinationAttemptModel> attempts = new ArrayList<>();
113+
IntStream.range(0, countDayPairs.length / 2)
114+
.forEach(i -> {
115+
int count = countDayPairs[i * 2];
116+
int day = countDayPairs[i * 2 + 1];
117+
118+
for (int j = 0; j < count; j++) {
119+
attempts.add(createAttempt(day));
120+
}
121+
});
122+
return attempts;
123+
}
124+
125+
private List<DiaPrueba> whenExecuteGetActividad() {
126+
return getActividadPorDias.execute();
127+
}
128+
129+
private DiaPrueba createDiaPrueba(int dia, int cantidad) {
130+
return new DiaPrueba(dia, cantidad);
131+
}
132+
133+
private void thenRepositoryFindLastNDaysWasCalled(int days) {
134+
verify(combinationAttemptRepository, times(1)).findLastNDays(days);
135+
}
136+
137+
private void thenDailyCountsAreCorrect(List<DiaPrueba> actualResult, List<DiaPrueba> expectedNonZeroDays) {
138+
assertEquals(DAYS_TO_CHECK, actualResult.size(), "La lista debe tener exactamente 30 días.");
139+
140+
java.util.Map<Integer, Integer> actualCounts = new java.util.HashMap<>();
141+
actualResult.forEach(dp -> actualCounts.put(dp.dia(), dp.pruebas()));
142+
143+
expectedNonZeroDays.forEach(expectedDP -> {
144+
assertTrue(actualCounts.containsKey(expectedDP.dia()), "El día " + expectedDP.dia() + " debe existir en el resultado.");
145+
146+
assertEquals(expectedDP.pruebas(), actualCounts.get(expectedDP.dia()), "El día " + expectedDP.dia() + " debe tener el conteo correcto.");
147+
});
148+
}
149+
150+
private void thenAllDaysHaveZeroCount(List<DiaPrueba> actualResult) {
151+
assertEquals(DAYS_TO_CHECK, actualResult.size(), "La lista debe tener 30 días.");
152+
actualResult.forEach(dp -> assertEquals(0, dp.pruebas(), "Todos los días deben tener conteo cero."));
153+
}
154+
}
Lines changed: 175 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,175 @@
1+
package com.outfitlab.project.domain.useCases.dashboard;
2+
3+
import com.outfitlab.project.domain.interfaces.repositories.CombinationAttemptRepository;
4+
import com.outfitlab.project.domain.model.CombinationAttemptModel;
5+
import com.outfitlab.project.domain.model.CombinationModel;
6+
import com.outfitlab.project.domain.model.PrendaModel;
7+
import com.outfitlab.project.domain.model.BrandModel;
8+
import com.outfitlab.project.domain.model.ColorModel;
9+
import com.outfitlab.project.domain.model.dto.PrendaDashboardDTO.ColorConversion;
10+
import org.junit.jupiter.api.BeforeEach;
11+
import org.junit.jupiter.api.Test;
12+
import java.util.*;
13+
14+
import static org.junit.jupiter.api.Assertions.*;
15+
import static org.mockito.Mockito.*;
16+
17+
public class GetColorConversionTest {
18+
19+
private CombinationAttemptRepository combinationAttemptRepository = mock(CombinationAttemptRepository.class);
20+
private GetColorConversion getColorConversion;
21+
22+
private final String TARGET_BRAND = "ADIDAS";
23+
private final String OTHER_BRAND = "NIKE";
24+
private final String COLOR_RED = "Rojo";
25+
private final String COLOR_BLUE = "Azul";
26+
27+
@BeforeEach
28+
void setUp() {
29+
getColorConversion = new GetColorConversion(combinationAttemptRepository);
30+
}
31+
32+
33+
@Test
34+
public void shouldReturnCorrectCountsAndSortByPruebasForTargetBrand() {
35+
List<CombinationAttemptModel> mockAttempts = new ArrayList<>();
36+
mockAttempts.add(createAttempt(TARGET_BRAND, COLOR_RED, OTHER_BRAND, COLOR_BLUE));
37+
mockAttempts.add(createAttempt(TARGET_BRAND, COLOR_RED, TARGET_BRAND, COLOR_BLUE));
38+
mockAttempts.add(createAttempt(OTHER_BRAND, COLOR_BLUE, TARGET_BRAND, COLOR_RED));
39+
mockAttempts.add(createAttempt(TARGET_BRAND, COLOR_BLUE, OTHER_BRAND, COLOR_RED));
40+
41+
givenRepositoryReturnsAttempts(mockAttempts);
42+
43+
List<ColorConversion> result = whenExecuteGetColorConversion(TARGET_BRAND);
44+
45+
thenResultSizeIsCorrect(result, 2);
46+
thenResultIsSortedAndCountedCorrectly(result, COLOR_RED, 3, COLOR_BLUE, 2);
47+
}
48+
49+
@Test
50+
public void shouldReturnEmptyListWhenNoAttemptsAreFound() {
51+
givenRepositoryReturnsAttempts(Collections.emptyList());
52+
53+
List<ColorConversion> result = whenExecuteGetColorConversion(TARGET_BRAND);
54+
55+
thenResultSizeIsCorrect(result, 0);
56+
}
57+
58+
@Test
59+
public void shouldCountColorOncePerAttemptEvenIfUsedTwice() {
60+
List<CombinationAttemptModel> mockAttempts = List.of(
61+
createAttempt(TARGET_BRAND, COLOR_RED, TARGET_BRAND, COLOR_RED)
62+
);
63+
givenRepositoryReturnsAttempts(mockAttempts);
64+
65+
List<ColorConversion> result = whenExecuteGetColorConversion(TARGET_BRAND);
66+
67+
thenResultSizeIsCorrect(result, 1);
68+
thenResultIsSortedAndCountedCorrectly(result, COLOR_RED, 1);
69+
}
70+
71+
@Test
72+
public void shouldHandleNullPrendasOrNullColorNamesWithoutNPE() {
73+
List<CombinationAttemptModel> mockAttempts = new ArrayList<>();
74+
75+
mockAttempts.add(createAttempt(TARGET_BRAND, COLOR_RED, null, COLOR_BLUE));
76+
mockAttempts.add(createAttempt(null, COLOR_RED, TARGET_BRAND, COLOR_BLUE));
77+
mockAttempts.add(createAttempt(null, null, null, null));
78+
79+
givenRepositoryReturnsAttempts(mockAttempts);
80+
81+
List<ColorConversion> result = whenExecuteGetColorConversion(TARGET_BRAND);
82+
83+
thenResultSizeIsCorrect(result, 2);
84+
thenResultIsSortedAndCountedCorrectly(result, COLOR_RED, 1, COLOR_BLUE, 1);
85+
}
86+
87+
@Test
88+
public void shouldReturnEmptyListWhenTargetBrandCodeIsNull() {
89+
givenRepositoryReturnsAttempts(List.of(createAttempt(TARGET_BRAND, COLOR_RED, TARGET_BRAND, COLOR_RED)));
90+
91+
List<ColorConversion> result = whenExecuteGetColorConversion(null);
92+
93+
thenResultSizeIsCorrect(result, 0);
94+
}
95+
96+
97+
//privadoss
98+
private void givenRepositoryReturnsAttempts(List<CombinationAttemptModel> attempts) {
99+
when(combinationAttemptRepository.findAll()).thenReturn(attempts);
100+
}
101+
102+
private CombinationAttemptModel createAttempt(String supBrandCode, String supColorName,
103+
String infBrandCode, String infColorName) {
104+
105+
final String safeSupBrandCode = supBrandCode != null ? supBrandCode : "";
106+
final String safeInfBrandCode = infBrandCode != null ? infBrandCode : "";
107+
final String safeSupColorName = supColorName != null ? supColorName : "";
108+
final String safeInfColorName = infColorName != null ? infColorName : "";
109+
110+
CombinationAttemptModel attempt = mock(CombinationAttemptModel.class);
111+
CombinationModel combination = mock(CombinationModel.class);
112+
113+
PrendaModel prendaSup = mock(PrendaModel.class);
114+
115+
BrandModel brandSup = mock(BrandModel.class);
116+
when(brandSup.getCodigoMarca()).thenReturn(safeSupBrandCode);
117+
when(prendaSup.getMarca()).thenReturn(brandSup);
118+
119+
ColorModel colorSup = mock(ColorModel.class);
120+
when(colorSup.getNombre()).thenReturn(safeSupColorName);
121+
when(prendaSup.getColor()).thenReturn(colorSup);
122+
123+
PrendaModel prendaInf = mock(PrendaModel.class);
124+
125+
BrandModel brandInf = mock(BrandModel.class);
126+
when(brandInf.getCodigoMarca()).thenReturn(safeInfBrandCode);
127+
when(prendaInf.getMarca()).thenReturn(brandInf);
128+
129+
ColorModel colorInf = mock(ColorModel.class);
130+
when(colorInf.getNombre()).thenReturn(safeInfColorName);
131+
when(prendaInf.getColor()).thenReturn(colorInf);
132+
133+
when(combination.getPrendaSuperior()).thenReturn(prendaSup);
134+
when(combination.getPrendaInferior()).thenReturn(prendaInf);
135+
136+
when(attempt.getCombination()).thenReturn(combination);
137+
138+
return attempt;
139+
}
140+
141+
private List<ColorConversion> whenExecuteGetColorConversion(String brandCode) {
142+
return getColorConversion.execute(brandCode);
143+
}
144+
145+
private void thenResultSizeIsCorrect(List<ColorConversion> result, int size) {
146+
assertNotNull(result);
147+
assertEquals(size, result.size(), "La lista resultante debe tener el tamaño esperado.");
148+
}
149+
150+
private void thenResultIsSortedAndCountedCorrectly(List<ColorConversion> result, Object... expectedPairs) {
151+
152+
if (result.size() > 1) {
153+
assertTrue(result.get(0).pruebas() >= result.get(1).pruebas(), "El resultado debe estar ordenado por 'pruebas' descendente.");
154+
}
155+
156+
Map<String, Integer> actualCounts = new HashMap<>();
157+
result.forEach(cc -> actualCounts.put(cc.color(), cc.pruebas()));
158+
159+
for (int i = 0; i < expectedPairs.length; i += 2) {
160+
String colorName = (String) expectedPairs[i];
161+
int expectedCount = (Integer) expectedPairs[i + 1];
162+
163+
assertTrue(actualCounts.containsKey(colorName), "El color '" + colorName + "' debe estar en la lista.");
164+
assertEquals(expectedCount, actualCounts.get(colorName), "El conteo de pruebas para " + colorName + " es incorrecto.");
165+
166+
result.stream()
167+
.filter(cc -> cc.color().equals(colorName))
168+
.findFirst()
169+
.ifPresent(cc -> {
170+
assertEquals(0, cc.favoritos(), "El campo 'favoritos' debe ser 0.");
171+
assertEquals(0.0, cc.conversion(), 0.001, "El campo 'conversion' debe ser 0.0.");
172+
});
173+
}
174+
}
175+
}

0 commit comments

Comments
 (0)