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