Skip to content

Commit 8967924

Browse files
committed
Agrega todos los tests de combination
1 parent 0b098ab commit 8967924

File tree

3 files changed

+300
-0
lines changed

3 files changed

+300
-0
lines changed
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
package com.outfitlab.project.domain.useCases.combination;
2+
3+
import com.outfitlab.project.domain.interfaces.repositories.CombinationRepository;
4+
import com.outfitlab.project.domain.model.CombinationModel;
5+
import com.outfitlab.project.domain.model.PrendaModel;
6+
import org.junit.jupiter.api.BeforeEach;
7+
import org.junit.jupiter.api.Test;
8+
import org.mockito.ArgumentCaptor;
9+
import static org.junit.jupiter.api.Assertions.*;
10+
import static org.mockito.Mockito.*;
11+
12+
public class CreateCombinationTest {
13+
14+
private CombinationRepository combinationRepository = mock(CombinationRepository.class);
15+
private CreateCombination createCombination;
16+
17+
private PrendaModel mockPrendaSuperior;
18+
private PrendaModel mockPrendaInferior;
19+
private CombinationModel mockSavedModel;
20+
21+
@BeforeEach
22+
void setUp() {
23+
mockPrendaSuperior = mock(PrendaModel.class);
24+
mockPrendaInferior = mock(PrendaModel.class);
25+
mockSavedModel = mock(CombinationModel.class);
26+
createCombination = new CreateCombination(combinationRepository);
27+
}
28+
29+
30+
@Test
31+
public void shouldSuccessfullyCreateAndSaveCombinationModel() {
32+
givenRepositorySavesModelAndReturns(mockSavedModel);
33+
34+
CombinationModel result = whenExecuteCreateCombination(mockPrendaSuperior, mockPrendaInferior);
35+
36+
thenResultMatchesSavedModel(result, mockSavedModel);
37+
thenRepositoryWasCalledWithCorrectPrendas(mockPrendaSuperior, mockPrendaInferior);
38+
}
39+
40+
@Test
41+
public void shouldCreateCombinationModelWhenPrendaInferiorIsNull() {
42+
givenRepositorySavesModelAndReturns(mockSavedModel);
43+
PrendaModel nullPrendaInferior = null;
44+
45+
CombinationModel result = whenExecuteCreateCombination(mockPrendaSuperior, nullPrendaInferior);
46+
47+
thenResultMatchesSavedModel(result, mockSavedModel);
48+
thenRepositoryWasCalledWithCorrectPrendas(mockPrendaSuperior, nullPrendaInferior);
49+
}
50+
51+
@Test
52+
public void shouldCreateCombinationModelWhenPrendaSuperiorIsNull() {
53+
givenRepositorySavesModelAndReturns(mockSavedModel);
54+
PrendaModel nullPrendaSuperior = null;
55+
56+
CombinationModel result = whenExecuteCreateCombination(nullPrendaSuperior, mockPrendaInferior);
57+
58+
thenResultMatchesSavedModel(result, mockSavedModel);
59+
thenRepositoryWasCalledWithCorrectPrendas(nullPrendaSuperior, mockPrendaInferior);
60+
}
61+
62+
@Test
63+
public void shouldCreateCombinationModelWhenBothPrendasAreNull() {
64+
givenRepositorySavesModelAndReturns(mockSavedModel);
65+
66+
CombinationModel result = whenExecuteCreateCombination(null, null);
67+
68+
thenResultMatchesSavedModel(result, mockSavedModel);
69+
thenRepositoryWasCalledWithCorrectPrendas(null, null);
70+
}
71+
72+
@Test
73+
public void shouldPropagateRuntimeExceptionWhenRepositoryFailsToSave() {
74+
givenRepositoryThrowsRuntimeException();
75+
76+
assertThrows(RuntimeException.class,
77+
() -> whenExecuteCreateCombination(mockPrendaSuperior, mockPrendaInferior),
78+
"Se esperaba que la excepción de persistencia se propagara.");
79+
80+
verify(combinationRepository, times(1)).save(any(CombinationModel.class));
81+
}
82+
83+
84+
//privadosss
85+
private void givenRepositorySavesModelAndReturns(CombinationModel savedModel) {
86+
when(combinationRepository.save(any(CombinationModel.class))).thenReturn(savedModel);
87+
}
88+
89+
private void givenRepositoryThrowsRuntimeException() {
90+
doThrow(new RuntimeException("Simulated DB error")).when(combinationRepository).save(any(CombinationModel.class));
91+
}
92+
93+
private CombinationModel whenExecuteCreateCombination(PrendaModel superior, PrendaModel inferior) {
94+
return createCombination.execute(superior, inferior);
95+
}
96+
97+
private void thenResultMatchesSavedModel(CombinationModel actualResult, CombinationModel expectedSavedModel) {
98+
assertNotNull(actualResult, "El resultado no debe ser nulo.");
99+
assertEquals(expectedSavedModel, actualResult, "El resultado debe ser el modelo devuelto por el repositorio.");
100+
}
101+
102+
private void thenRepositoryWasCalledWithCorrectPrendas(PrendaModel expectedSuperior, PrendaModel expectedInferior) {
103+
ArgumentCaptor<CombinationModel> captor = ArgumentCaptor.forClass(CombinationModel.class);
104+
verify(combinationRepository, times(1)).save(captor.capture());
105+
106+
CombinationModel capturedModel = captor.getValue();
107+
108+
assertEquals(expectedSuperior, capturedModel.getPrendaSuperior(), "El modelo debe contener la prenda superior correcta.");
109+
assertEquals(expectedInferior, capturedModel.getPrendaInferior(), "El modelo debe contener la prenda inferior correcta.");
110+
}
111+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package com.outfitlab.project.domain.useCases.combination;
2+
3+
import com.outfitlab.project.domain.interfaces.repositories.CombinationRepository;
4+
import org.junit.jupiter.api.BeforeEach;
5+
import org.junit.jupiter.api.Test;
6+
import static org.junit.jupiter.api.Assertions.assertThrows;
7+
import static org.mockito.Mockito.*;
8+
9+
public class DeleteAllCombinationRelatedToGarmentTest {
10+
11+
private CombinationRepository combinationRepository = mock(CombinationRepository.class);
12+
private DeleteAllCombinationRelatedToGarment deleteAllCombinationRelatedToGarment;
13+
14+
private final String VALID_GARMENT_CODE = "PANTALON-001";
15+
private final String EMPTY_GARMENT_CODE = "";
16+
private final String NULL_GARMENT_CODE = null;
17+
18+
@BeforeEach
19+
void setUp() {
20+
deleteAllCombinationRelatedToGarment = new DeleteAllCombinationRelatedToGarment(combinationRepository);
21+
}
22+
23+
24+
@Test
25+
public void shouldCallRepositoryToDeleteAllCombinationsByValidGarmentCode() {
26+
whenExecuteDeleteAll(VALID_GARMENT_CODE);
27+
28+
thenRepositoryDeleteAllWasCalled(VALID_GARMENT_CODE, 1);
29+
}
30+
31+
@Test
32+
public void shouldCallRepositoryWithEmptyStringWhenGarmentCodeIsEmpty() {
33+
whenExecuteDeleteAll(EMPTY_GARMENT_CODE);
34+
35+
thenRepositoryDeleteAllWasCalled(EMPTY_GARMENT_CODE, 1);
36+
}
37+
38+
@Test
39+
public void shouldCallRepositoryWithNullWhenGarmentCodeIsNull() {
40+
whenExecuteDeleteAll(NULL_GARMENT_CODE);
41+
42+
thenRepositoryDeleteAllWasCalled(NULL_GARMENT_CODE, 1);
43+
}
44+
45+
@Test
46+
public void shouldNotThrowExceptionIfRepositoryThrowsRuntimeException() {
47+
givenRepositoryThrowsRuntimeException();
48+
49+
assertThrows(RuntimeException.class, () -> whenExecuteDeleteAll(VALID_GARMENT_CODE));
50+
51+
thenRepositoryDeleteAllWasCalled(VALID_GARMENT_CODE, 1);
52+
}
53+
54+
55+
//privadoss
56+
private void givenRepositoryThrowsRuntimeException() {
57+
doThrow(new RuntimeException("Simulated DB error")).when(combinationRepository).deleteAllByGarmentcode(anyString());
58+
}
59+
60+
private void whenExecuteDeleteAll(String garmentCode) {
61+
deleteAllCombinationRelatedToGarment.execute(garmentCode);
62+
}
63+
64+
private void thenRepositoryDeleteAllWasCalled(String expectedGarmentCode, int times) {
65+
verify(combinationRepository, times(times)).deleteAllByGarmentcode(expectedGarmentCode);
66+
}
67+
}
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
package com.outfitlab.project.domain.useCases.combination;
2+
3+
import com.outfitlab.project.domain.exceptions.CombinationNotFoundException;
4+
import com.outfitlab.project.domain.interfaces.repositories.CombinationRepository;
5+
import com.outfitlab.project.domain.model.CombinationModel;
6+
import org.junit.jupiter.api.BeforeEach;
7+
import org.junit.jupiter.api.Test;
8+
import java.util.Optional;
9+
import static org.junit.jupiter.api.Assertions.*;
10+
import static org.mockito.Mockito.*;
11+
12+
public class GetCombinationByPrendasTest {
13+
14+
private CombinationRepository combinationRepository = mock(CombinationRepository.class);
15+
private GetCombinationByPrendas getCombinationByPrendas;
16+
17+
private final Long SUPERIOR_ID = 101L;
18+
private final Long INFERIOR_ID = 202L;
19+
private final Long NULL_ID = null;
20+
21+
private CombinationModel mockCombination;
22+
23+
@BeforeEach
24+
void setUp() {
25+
mockCombination = mock(CombinationModel.class);
26+
getCombinationByPrendas = new GetCombinationByPrendas(combinationRepository);
27+
}
28+
29+
30+
@Test
31+
public void shouldReturnCombinationWhenBothPrendaIdsAreFound() throws CombinationNotFoundException {
32+
givenRepositoryFindsCombination(SUPERIOR_ID, INFERIOR_ID, mockCombination);
33+
34+
CombinationModel result = whenExecuteGetCombination(SUPERIOR_ID, INFERIOR_ID);
35+
36+
thenResultMatchesExpectedCombination(result, mockCombination);
37+
thenRepositoryWasCalledOnce(SUPERIOR_ID, INFERIOR_ID);
38+
}
39+
40+
@Test
41+
public void shouldThrowCombinationNotFoundExceptionWhenCombinationDoesNotExist() {
42+
givenRepositoryFindsNoCombination(SUPERIOR_ID, INFERIOR_ID);
43+
44+
assertThrows(CombinationNotFoundException.class,
45+
() -> whenExecuteGetCombination(SUPERIOR_ID, INFERIOR_ID),
46+
"Debe lanzar CombinationNotFoundException cuando Optional está vacío.");
47+
48+
thenRepositoryWasCalledOnce(SUPERIOR_ID, INFERIOR_ID);
49+
}
50+
51+
@Test
52+
public void shouldThrowCombinationNotFoundExceptionWhenSuperiorIdIsNullAndCombinationDoesNotExist() {
53+
givenRepositoryFindsNoCombination(NULL_ID, INFERIOR_ID);
54+
55+
assertThrows(CombinationNotFoundException.class,
56+
() -> whenExecuteGetCombination(NULL_ID, INFERIOR_ID),
57+
"Debe fallar si no se encuentra combinación con ID Superior nulo.");
58+
59+
thenRepositoryWasCalledOnce(NULL_ID, INFERIOR_ID);
60+
}
61+
62+
@Test
63+
public void shouldThrowCombinationNotFoundExceptionWhenInferiorIdIsNullAndCombinationDoesNotExist() {
64+
givenRepositoryFindsNoCombination(SUPERIOR_ID, NULL_ID);
65+
66+
assertThrows(CombinationNotFoundException.class,
67+
() -> whenExecuteGetCombination(SUPERIOR_ID, NULL_ID),
68+
"Debe fallar si no se encuentra combinación con ID Inferior nulo.");
69+
70+
thenRepositoryWasCalledOnce(SUPERIOR_ID, NULL_ID);
71+
}
72+
73+
@Test
74+
public void shouldThrowCombinationNotFoundExceptionWhenBothIdsAreNullAndCombinationDoesNotExist() {
75+
givenRepositoryFindsNoCombination(NULL_ID, NULL_ID);
76+
77+
assertThrows(CombinationNotFoundException.class,
78+
() -> whenExecuteGetCombination(NULL_ID, NULL_ID),
79+
"Debe fallar si no se encuentra combinación con ambos IDs nulos.");
80+
81+
thenRepositoryWasCalledOnce(NULL_ID, NULL_ID);
82+
}
83+
84+
@Test
85+
public void shouldPropagateRuntimeExceptionWhenRepositoryFails() {
86+
givenRepositoryThrowsRuntimeException(SUPERIOR_ID, INFERIOR_ID);
87+
88+
assertThrows(RuntimeException.class,
89+
() -> whenExecuteGetCombination(SUPERIOR_ID, INFERIOR_ID),
90+
"La excepción de tiempo de ejecución debe ser propagada.");
91+
92+
thenRepositoryWasCalledOnce(SUPERIOR_ID, INFERIOR_ID);
93+
}
94+
95+
96+
//privadosss
97+
private void givenRepositoryFindsCombination(Long superiorId, Long inferiorId, CombinationModel model) {
98+
when(combinationRepository.findByPrendas(superiorId, inferiorId)).thenReturn(Optional.of(model));
99+
}
100+
101+
private void givenRepositoryFindsNoCombination(Long superiorId, Long inferiorId) {
102+
when(combinationRepository.findByPrendas(superiorId, inferiorId)).thenReturn(Optional.empty());
103+
}
104+
105+
private void givenRepositoryThrowsRuntimeException(Long superiorId, Long inferiorId) {
106+
doThrow(new RuntimeException("Simulated DB error")).when(combinationRepository).findByPrendas(superiorId, inferiorId);
107+
}
108+
109+
110+
private CombinationModel whenExecuteGetCombination(Long superiorId, Long inferiorId) throws CombinationNotFoundException {
111+
return getCombinationByPrendas.execute(superiorId, inferiorId);
112+
}
113+
114+
private void thenResultMatchesExpectedCombination(CombinationModel actual, CombinationModel expected) {
115+
assertNotNull(actual, "El resultado no debe ser nulo.");
116+
assertEquals(expected, actual, "La combinación devuelta debe coincidir con la simulada.");
117+
}
118+
119+
private void thenRepositoryWasCalledOnce(Long superiorId, Long inferiorId) {
120+
verify(combinationRepository, times(1)).findByPrendas(superiorId, inferiorId);
121+
}
122+
}

0 commit comments

Comments
 (0)