1+ package com .outfitlab .project .domain .useCases .combinationFavorite ;
2+
3+ import com .outfitlab .project .domain .exceptions .FavoritesException ;
4+ import com .outfitlab .project .domain .exceptions .PlanLimitExceededException ;
5+ import com .outfitlab .project .domain .exceptions .SubscriptionNotFoundException ;
6+ import com .outfitlab .project .domain .exceptions .UserCombinationFavoriteAlreadyExistsException ;
7+ import com .outfitlab .project .domain .exceptions .UserCombinationFavoriteNotFoundException ;
8+ import com .outfitlab .project .domain .exceptions .UserNotFoundException ;
9+ import com .outfitlab .project .domain .interfaces .repositories .UserCombinationFavoriteRepository ;
10+ import com .outfitlab .project .domain .model .UserCombinationFavoriteModel ;
11+ import com .outfitlab .project .domain .useCases .subscription .CheckUserPlanLimit ;
12+ import com .outfitlab .project .domain .useCases .subscription .IncrementUsageCounter ;
13+ import com .outfitlab .project .infrastructure .model .UserCombinationFavoriteEntity ;
14+ import org .junit .jupiter .api .BeforeEach ;
15+ import org .junit .jupiter .api .Test ;
16+ import static org .junit .jupiter .api .Assertions .*;
17+ import static org .mockito .Mockito .*;
18+
19+ public class AddCombinationToFavouriteTest {
20+
21+ private UserCombinationFavoriteRepository userCombinationFavoriteRepository = mock (UserCombinationFavoriteRepository .class );
22+ private CheckUserPlanLimit checkUserPlanLimit = mock (CheckUserPlanLimit .class );
23+ private IncrementUsageCounter incrementUsageCounter = mock (IncrementUsageCounter .class );
24+ private AddCombinationToFavourite addCombinationToFavourite ;
25+
26+ private final String USER_EMAIL = "user@test.com" ;
27+ private final String COMBINATION_URL = "http://url/comb/abc" ;
28+ private final String LIMIT_TYPE = "favorites" ;
29+ private final String SUCCESS_MESSAGE = "Combinación añadida a favoritos." ;
30+
31+ @ BeforeEach
32+ void setUp () throws UserCombinationFavoriteNotFoundException , FavoritesException , UserNotFoundException {
33+ addCombinationToFavourite = new AddCombinationToFavourite (
34+ userCombinationFavoriteRepository ,
35+ checkUserPlanLimit ,
36+ incrementUsageCounter
37+ );
38+ givenFavoriteDoesNotExist ();
39+ givenAddToFavoritesIsSuccessful ();
40+ }
41+
42+
43+ @ Test
44+ public void shouldAddCombinationAndIncrementCounterWhenChecksPass () throws Exception {
45+ String result = whenExecuteAddCombination (COMBINATION_URL , USER_EMAIL );
46+
47+ thenResultIsSuccessMessage (result );
48+ thenPlanLimitWasChecked (1 );
49+ thenFavoriteExistenceWasChecked (1 );
50+ thenAddToFavoritesWasCalled (COMBINATION_URL , USER_EMAIL , 1 );
51+ thenUsageCounterWasIncremented (1 );
52+ }
53+
54+ @ Test
55+ public void shouldThrowPlanLimitExceededExceptionWhenLimitIsReached () throws Exception {
56+ doThrow (PlanLimitExceededException .class ).when (checkUserPlanLimit ).execute (USER_EMAIL , LIMIT_TYPE );
57+
58+ assertThrows (PlanLimitExceededException .class ,
59+ () -> whenExecuteAddCombination (COMBINATION_URL , USER_EMAIL ));
60+
61+ thenPlanLimitWasChecked (1 );
62+ thenFavoriteExistenceWasChecked (0 );
63+ thenAddToFavoritesWasCalled (COMBINATION_URL , USER_EMAIL , 0 );
64+ thenUsageCounterWasIncremented (0 );
65+ }
66+
67+ @ Test
68+ public void shouldThrowUserCombinationFavoriteAlreadyExistsExceptionWhenAlreadyExists () throws Exception {
69+ givenFavoriteAlreadyExists ();
70+
71+ assertThrows (UserCombinationFavoriteAlreadyExistsException .class ,
72+ () -> whenExecuteAddCombination (COMBINATION_URL , USER_EMAIL ));
73+
74+ thenPlanLimitWasChecked (1 );
75+ thenFavoriteExistenceWasChecked (1 );
76+ thenAddToFavoritesWasCalled (COMBINATION_URL , USER_EMAIL , 0 );
77+ thenUsageCounterWasIncremented (0 );
78+ }
79+
80+ @ Test
81+ public void shouldThrowFavoritesExceptionWhenAddToFavoritesFails () throws Exception {
82+ givenAddToFavoritesFailsWithNull ();
83+
84+ assertThrows (FavoritesException .class ,
85+ () -> whenExecuteAddCombination (COMBINATION_URL , USER_EMAIL ));
86+
87+ thenPlanLimitWasChecked (1 );
88+ thenFavoriteExistenceWasChecked (1 );
89+ thenAddToFavoritesWasCalled (COMBINATION_URL , USER_EMAIL , 1 );
90+ thenUsageCounterWasIncremented (0 );
91+ }
92+
93+ @ Test
94+ public void shouldPropagateSubscriptionNotFoundException () throws Exception {
95+ doThrow (SubscriptionNotFoundException .class ).when (checkUserPlanLimit ).execute (USER_EMAIL , LIMIT_TYPE );
96+
97+ assertThrows (SubscriptionNotFoundException .class ,
98+ () -> whenExecuteAddCombination (COMBINATION_URL , USER_EMAIL ));
99+
100+ thenPlanLimitWasChecked (1 );
101+ thenFavoriteExistenceWasChecked (0 );
102+ thenUsageCounterWasIncremented (0 );
103+ }
104+
105+ @ Test
106+ public void shouldPropagateUserNotFoundExceptionWhenAddingToFavorites () throws Exception {
107+ givenAddToFavoritesThrowsUserNotFound ();
108+
109+ assertThrows (UserNotFoundException .class ,
110+ () -> whenExecuteAddCombination (COMBINATION_URL , USER_EMAIL ));
111+
112+ thenPlanLimitWasChecked (1 );
113+ thenFavoriteExistenceWasChecked (1 );
114+ thenAddToFavoritesWasCalled (COMBINATION_URL , USER_EMAIL , 1 );
115+ thenUsageCounterWasIncremented (0 );
116+ }
117+
118+ @ Test
119+ public void shouldPropagateRuntimeExceptionWhenCheckingIfFavoriteExistsFails () throws Exception {
120+ givenFavoriteExistenceThrowsRuntimeException ();
121+
122+ assertThrows (RuntimeException .class ,
123+ () -> whenExecuteAddCombination (COMBINATION_URL , USER_EMAIL ));
124+
125+ thenPlanLimitWasChecked (1 );
126+ thenFavoriteExistenceWasChecked (1 );
127+ thenAddToFavoritesWasCalled (COMBINATION_URL , USER_EMAIL , 0 );
128+ thenUsageCounterWasIncremented (0 );
129+ }
130+
131+
132+ //privadoss
133+ private void givenFavoriteDoesNotExist () throws UserCombinationFavoriteNotFoundException {
134+ doThrow (UserCombinationFavoriteNotFoundException .class )
135+ .when (userCombinationFavoriteRepository ).findByCombinationUrlAndUserEmail (COMBINATION_URL , USER_EMAIL );
136+ }
137+
138+ private void givenFavoriteAlreadyExists () throws UserCombinationFavoriteNotFoundException {
139+ doReturn (mock (UserCombinationFavoriteModel .class ))
140+ .when (userCombinationFavoriteRepository ).findByCombinationUrlAndUserEmail (COMBINATION_URL , USER_EMAIL );
141+ }
142+
143+ private void givenFavoriteExistenceThrowsRuntimeException () throws UserCombinationFavoriteNotFoundException {
144+ doThrow (new RuntimeException ("DB Connection Error during check" ))
145+ .when (userCombinationFavoriteRepository ).findByCombinationUrlAndUserEmail (COMBINATION_URL , USER_EMAIL );
146+ }
147+
148+ private void givenAddToFavoritesIsSuccessful () throws UserNotFoundException {
149+ UserCombinationFavoriteEntity successEntity = mock (UserCombinationFavoriteEntity .class );
150+ when (userCombinationFavoriteRepository .addToFavorite (anyString (), anyString ())).thenReturn (successEntity );
151+ }
152+
153+ private void givenAddToFavoritesFailsWithNull () {
154+ when (userCombinationFavoriteRepository .addToFavorite (anyString (), anyString ())).thenReturn (null );
155+ }
156+
157+ private void givenAddToFavoritesThrowsUserNotFound () throws UserNotFoundException {
158+ doThrow (UserNotFoundException .class )
159+ .when (userCombinationFavoriteRepository ).addToFavorite (COMBINATION_URL , USER_EMAIL );
160+ }
161+
162+ private String whenExecuteAddCombination (String combinationUrl , String userEmail ) throws Exception {
163+ return addCombinationToFavourite .execute (combinationUrl , userEmail );
164+ }
165+
166+ private void thenResultIsSuccessMessage (String result ) {
167+ assertEquals (SUCCESS_MESSAGE , result , "El mensaje de retorno debe ser el de éxito." );
168+ }
169+
170+ private void thenPlanLimitWasChecked (int times ) throws Exception {
171+ verify (checkUserPlanLimit , times (times )).execute (USER_EMAIL , LIMIT_TYPE );
172+ }
173+
174+ private void thenFavoriteExistenceWasChecked (int times ) throws UserCombinationFavoriteNotFoundException {
175+ verify (userCombinationFavoriteRepository , times (times )).findByCombinationUrlAndUserEmail (COMBINATION_URL , USER_EMAIL );
176+ }
177+
178+ private void thenAddToFavoritesWasCalled (String url , String email , int times ) throws FavoritesException , UserNotFoundException {
179+ verify (userCombinationFavoriteRepository , times (times )).addToFavorite (url , email );
180+ }
181+
182+ private void thenUsageCounterWasIncremented (int times ) {
183+ verify (incrementUsageCounter , times (times )).execute (USER_EMAIL , LIMIT_TYPE );
184+ }
185+ }
0 commit comments