Skip to content

Commit 52802b1

Browse files
committed
Agrega todos los tests de combinationFavorite
1 parent 5ebc04d commit 52802b1

File tree

3 files changed

+476
-0
lines changed

3 files changed

+476
-0
lines changed
Lines changed: 185 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,185 @@
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+
}
Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
package com.outfitlab.project.domain.useCases.combinationFavorite;
2+
3+
import com.outfitlab.project.domain.exceptions.UserCombinationFavoriteNotFoundException;
4+
import com.outfitlab.project.domain.exceptions.UserNotFoundException;
5+
import com.outfitlab.project.domain.interfaces.repositories.UserCombinationFavoriteRepository;
6+
import com.outfitlab.project.domain.model.UserCombinationFavoriteModel;
7+
import org.junit.jupiter.api.BeforeEach;
8+
import org.junit.jupiter.api.Test;
9+
import static org.junit.jupiter.api.Assertions.*;
10+
import static org.mockito.Mockito.*;
11+
12+
public class DeleteCombinationFromFavoriteTest {
13+
14+
private UserCombinationFavoriteRepository userCombinationFavoriteRepository = mock(UserCombinationFavoriteRepository.class);
15+
private DeleteCombinationFromFavorite deleteCombinationFromFavorite;
16+
17+
private final String USER_EMAIL = "user@test.com";
18+
private final String COMBINATION_URL = "http://url/comb/abc";
19+
private final String SUCCESS_MESSAGE = "Combinación eliminada de favoritos.";
20+
private final String NULL_URL = null;
21+
private final String EMPTY_EMAIL = "";
22+
23+
24+
@BeforeEach
25+
void setUp() throws UserCombinationFavoriteNotFoundException, UserNotFoundException {
26+
deleteCombinationFromFavorite = new DeleteCombinationFromFavorite(userCombinationFavoriteRepository);
27+
givenFavoriteExists();
28+
givenDeletionIsSuccessful();
29+
}
30+
31+
32+
@Test
33+
public void shouldDeleteCombinationAndReturnSuccessMessageWhenFavoriteExists() throws Exception {
34+
String result = whenExecuteDelete(COMBINATION_URL, USER_EMAIL);
35+
36+
thenResultIsSuccessMessage(result);
37+
thenFavoriteExistenceWasChecked(1);
38+
thenDeletionWasCalled(COMBINATION_URL, USER_EMAIL, 1);
39+
}
40+
41+
@Test
42+
public void shouldThrowUserCombinationFavoriteNotFoundExceptionWhenFavoriteDoesNotExist() throws UserNotFoundException {
43+
givenFavoriteDoesNotExist();
44+
45+
assertThrows(UserCombinationFavoriteNotFoundException.class,
46+
() -> whenExecuteDelete(COMBINATION_URL, USER_EMAIL),
47+
"Debe lanzar NotFoundException si no se encuentra la combinación.");
48+
49+
thenFavoriteExistenceWasChecked(1);
50+
thenDeletionWasCalled(COMBINATION_URL, USER_EMAIL, 0);
51+
}
52+
53+
@Test
54+
public void shouldPropagateUserNotFoundExceptionWhenDeletingFavorite() throws UserCombinationFavoriteNotFoundException {
55+
givenDeletionThrowsUserNotFound();
56+
57+
assertThrows(UserNotFoundException.class,
58+
() -> whenExecuteDelete(COMBINATION_URL, USER_EMAIL),
59+
"Debe propagar UserNotFoundException si el usuario no es válido durante la eliminación.");
60+
61+
thenFavoriteExistenceWasChecked(1);
62+
thenDeletionWasCalled(COMBINATION_URL, USER_EMAIL, 1);
63+
}
64+
65+
@Test
66+
public void shouldPropagateRuntimeExceptionWhenCheckingExistenceFails() {
67+
givenExistenceCheckThrowsRuntimeException();
68+
69+
assertThrows(RuntimeException.class,
70+
() -> whenExecuteDelete(COMBINATION_URL, USER_EMAIL));
71+
72+
thenFavoriteExistenceWasChecked(1);
73+
thenDeletionWasCalled(COMBINATION_URL, USER_EMAIL, 0);
74+
}
75+
76+
@Test
77+
public void shouldPropagateRuntimeExceptionWhenDeletionFails() throws UserNotFoundException {
78+
givenDeletionThrowsRuntimeException();
79+
80+
assertThrows(RuntimeException.class,
81+
() -> whenExecuteDelete(COMBINATION_URL, USER_EMAIL));
82+
83+
thenFavoriteExistenceWasChecked(1);
84+
thenDeletionWasCalled(COMBINATION_URL, USER_EMAIL, 1);
85+
}
86+
87+
@Test
88+
public void shouldAttemptDeletionWhenCombinationUrlIsNull() throws Exception {
89+
whenExecuteDelete(NULL_URL, USER_EMAIL);
90+
91+
thenFavoriteExistenceWasChecked(1, NULL_URL, USER_EMAIL);
92+
thenDeletionWasCalled(NULL_URL, USER_EMAIL, 1);
93+
}
94+
95+
@Test
96+
public void shouldAttemptDeletionWhenUserEmailIsEmpty() throws Exception {
97+
whenExecuteDelete(COMBINATION_URL, EMPTY_EMAIL);
98+
99+
thenFavoriteExistenceWasChecked(1, COMBINATION_URL, EMPTY_EMAIL);
100+
thenDeletionWasCalled(COMBINATION_URL, EMPTY_EMAIL, 1);
101+
}
102+
103+
104+
//privadosss
105+
private void givenFavoriteExists() throws UserCombinationFavoriteNotFoundException {
106+
when(userCombinationFavoriteRepository.findByCombinationUrlAndUserEmail(anyString(), anyString()))
107+
.thenReturn(mock(UserCombinationFavoriteModel.class));
108+
}
109+
110+
private void givenFavoriteDoesNotExist() throws UserCombinationFavoriteNotFoundException {
111+
doThrow(UserCombinationFavoriteNotFoundException.class)
112+
.when(userCombinationFavoriteRepository).findByCombinationUrlAndUserEmail(COMBINATION_URL, USER_EMAIL);
113+
}
114+
115+
private void givenDeletionIsSuccessful() throws UserNotFoundException {
116+
doNothing().when(userCombinationFavoriteRepository).deleteFromFavorites(anyString(), anyString());
117+
}
118+
119+
private void givenDeletionThrowsUserNotFound() throws UserNotFoundException {
120+
doThrow(UserNotFoundException.class)
121+
.when(userCombinationFavoriteRepository).deleteFromFavorites(COMBINATION_URL, USER_EMAIL);
122+
}
123+
124+
private void givenExistenceCheckThrowsRuntimeException() {
125+
doThrow(new RuntimeException("DB error on read")).when(userCombinationFavoriteRepository).findByCombinationUrlAndUserEmail(anyString(), anyString());
126+
}
127+
128+
private void givenDeletionThrowsRuntimeException() throws UserNotFoundException {
129+
doThrow(new RuntimeException("DB error on delete")).when(userCombinationFavoriteRepository).deleteFromFavorites(COMBINATION_URL, USER_EMAIL);
130+
}
131+
132+
private String whenExecuteDelete(String combinationUrl, String userEmail) throws UserCombinationFavoriteNotFoundException, UserNotFoundException {
133+
return deleteCombinationFromFavorite.execute(combinationUrl, userEmail);
134+
}
135+
136+
private void thenResultIsSuccessMessage(String result) {
137+
assertEquals(SUCCESS_MESSAGE, result, "El mensaje de retorno debe ser de eliminación exitosa.");
138+
}
139+
140+
private void thenFavoriteExistenceWasChecked(int times) throws UserCombinationFavoriteNotFoundException {
141+
verify(userCombinationFavoriteRepository, times(times)).findByCombinationUrlAndUserEmail(COMBINATION_URL, USER_EMAIL);
142+
}
143+
144+
private void thenFavoriteExistenceWasChecked(int times, String url, String email) throws UserCombinationFavoriteNotFoundException {
145+
verify(userCombinationFavoriteRepository, times(times)).findByCombinationUrlAndUserEmail(url, email);
146+
}
147+
148+
private void thenDeletionWasCalled(String url, String email, int times) throws UserNotFoundException {
149+
verify(userCombinationFavoriteRepository, times(times)).deleteFromFavorites(url, email);
150+
}
151+
}

0 commit comments

Comments
 (0)