Skip to content

Commit 241b433

Browse files
committed
Agrega todos los tests de user
1 parent 8ff380e commit 241b433

13 files changed

+1495
-0
lines changed
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
package com.outfitlab.project.domain.useCases.user;
2+
3+
import com.outfitlab.project.domain.interfaces.repositories.UserRepository;
4+
import org.junit.jupiter.api.BeforeEach;
5+
import org.junit.jupiter.api.Test;
6+
7+
import static org.junit.jupiter.api.Assertions.assertEquals;
8+
import static org.junit.jupiter.api.Assertions.assertThrows;
9+
import static org.mockito.Mockito.*;
10+
11+
public class ActivateUserTest {
12+
13+
private UserRepository userRepository = mock(UserRepository.class);
14+
private ActivateUser activateUser;
15+
16+
private final String VALID_EMAIL = "[email protected]";
17+
private final String EMPTY_EMAIL = "";
18+
private final String NULL_EMAIL = null;
19+
private final String SUCCESS_MESSAGE = "Usuario activado con éxito.";
20+
21+
@BeforeEach
22+
void setUp() {
23+
activateUser = new ActivateUser(userRepository);
24+
}
25+
26+
27+
@Test
28+
public void shouldCallRepositoryToActivateUserAndReturnSuccessMessage() {
29+
String result = whenExecuteActivateUser(VALID_EMAIL);
30+
31+
assertEquals(SUCCESS_MESSAGE, result, "Debe retornar el mensaje de éxito.");
32+
thenRepositoryActivateWasCalled(VALID_EMAIL, 1);
33+
}
34+
35+
@Test
36+
public void shouldCallRepositoryWithEmptyStringWhenEmailIsEmpty() {
37+
String result = whenExecuteActivateUser(EMPTY_EMAIL);
38+
39+
assertEquals(SUCCESS_MESSAGE, result, "Debe retornar el mensaje de éxito.");
40+
thenRepositoryActivateWasCalled(EMPTY_EMAIL, 1);
41+
}
42+
43+
@Test
44+
public void shouldPropagateRuntimeExceptionWhenEmailIsNull() {
45+
givenRepositoryThrowsRuntimeException(NULL_EMAIL);
46+
47+
assertThrows(RuntimeException.class,
48+
() -> whenExecuteActivateUser(NULL_EMAIL),
49+
"Se espera que el RuntimeException se propague al delegar el email nulo.");
50+
51+
thenRepositoryActivateWasCalled(NULL_EMAIL, 1);
52+
}
53+
54+
@Test
55+
public void shouldPropagateRuntimeExceptionWhenRepositoryFails() {
56+
givenRepositoryThrowsRuntimeException(VALID_EMAIL);
57+
58+
assertThrows(RuntimeException.class,
59+
() -> whenExecuteActivateUser(VALID_EMAIL),
60+
"Se espera que el RuntimeException se propague si el repositorio falla.");
61+
62+
thenRepositoryActivateWasCalled(VALID_EMAIL, 1);
63+
}
64+
65+
66+
//privadoss
67+
private void givenRepositoryThrowsRuntimeException(String email) {
68+
doThrow(new RuntimeException("Simulated DB error"))
69+
.when(userRepository).activateUser(email);
70+
}
71+
72+
private String whenExecuteActivateUser(String email) {
73+
return activateUser.execute(email);
74+
}
75+
76+
private void thenRepositoryActivateWasCalled(String expectedEmail, int times) {
77+
verify(userRepository, times(times)).activateUser(expectedEmail);
78+
}
79+
}
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
package com.outfitlab.project.domain.useCases.user;
2+
3+
import com.outfitlab.project.domain.interfaces.repositories.UserRepository;
4+
import org.junit.jupiter.api.BeforeEach;
5+
import org.junit.jupiter.api.Test;
6+
7+
import static org.junit.jupiter.api.Assertions.assertEquals;
8+
import static org.junit.jupiter.api.Assertions.assertThrows;
9+
import static org.mockito.Mockito.*;
10+
11+
public class ConvertToAdminTest {
12+
13+
private UserRepository userRepository = mock(UserRepository.class);
14+
private ConvertToAdmin convertToAdmin;
15+
16+
private final String VALID_EMAIL = "[email protected]";
17+
private final String EMPTY_EMAIL = "";
18+
private final String NULL_EMAIL = null;
19+
private final String SUCCESS_MESSAGE = "El usuario se ha convertido a Administrador con éxito.";
20+
21+
@BeforeEach
22+
void setUp() {
23+
convertToAdmin = new ConvertToAdmin(userRepository);
24+
}
25+
26+
27+
@Test
28+
public void shouldCallRepositoryToConvertUserToAdminAndReturnSuccessMessage() {
29+
String result = whenExecuteConvertToAdmin(VALID_EMAIL);
30+
31+
assertEquals(SUCCESS_MESSAGE, result, "Debe retornar el mensaje de éxito.");
32+
thenRepositoryConvertWasCalled(VALID_EMAIL, 1);
33+
}
34+
35+
@Test
36+
public void shouldCallRepositoryWithEmptyStringWhenEmailIsEmpty() {
37+
String result = whenExecuteConvertToAdmin(EMPTY_EMAIL);
38+
39+
assertEquals(SUCCESS_MESSAGE, result, "Debe retornar el mensaje de éxito.");
40+
thenRepositoryConvertWasCalled(EMPTY_EMAIL, 1);
41+
}
42+
43+
@Test
44+
public void shouldPropagateRuntimeExceptionWhenEmailIsNull() {
45+
givenRepositoryThrowsRuntimeException(NULL_EMAIL);
46+
47+
assertThrows(RuntimeException.class,
48+
() -> whenExecuteConvertToAdmin(NULL_EMAIL),
49+
"Se espera que el RuntimeException se propague al delegar el email nulo.");
50+
51+
thenRepositoryConvertWasCalled(NULL_EMAIL, 1);
52+
}
53+
54+
@Test
55+
public void shouldPropagateRuntimeExceptionWhenRepositoryFails() {
56+
givenRepositoryThrowsRuntimeException(VALID_EMAIL);
57+
58+
assertThrows(RuntimeException.class,
59+
() -> whenExecuteConvertToAdmin(VALID_EMAIL),
60+
"Se espera que el RuntimeException se propague si el repositorio falla.");
61+
62+
thenRepositoryConvertWasCalled(VALID_EMAIL, 1);
63+
}
64+
65+
66+
//privados
67+
private void givenRepositoryThrowsRuntimeException(String email) {
68+
doThrow(new RuntimeException("Simulated DB error"))
69+
.when(userRepository).convertToAdmin(email);
70+
}
71+
72+
private String whenExecuteConvertToAdmin(String email) {
73+
return convertToAdmin.execute(email);
74+
}
75+
76+
private void thenRepositoryConvertWasCalled(String expectedEmail, int times) {
77+
verify(userRepository, times(times)).convertToAdmin(expectedEmail);
78+
}
79+
}
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
package com.outfitlab.project.domain.useCases.user;
2+
3+
import com.outfitlab.project.domain.interfaces.repositories.UserRepository;
4+
import org.junit.jupiter.api.BeforeEach;
5+
import org.junit.jupiter.api.Test;
6+
7+
import static org.junit.jupiter.api.Assertions.assertEquals;
8+
import static org.junit.jupiter.api.Assertions.assertThrows;
9+
import static org.mockito.Mockito.*;
10+
11+
public class ConvertToUserTest {
12+
13+
private UserRepository userRepository = mock(UserRepository.class);
14+
private ConvertToUser convertToUser;
15+
16+
private final String VALID_EMAIL = "[email protected]";
17+
private final String EMPTY_EMAIL = "";
18+
private final String NULL_EMAIL = null;
19+
private final String SUCCESS_MESSAGE = "El administrador se ha convertido a Usuario con éxito.";
20+
21+
@BeforeEach
22+
void setUp() {
23+
convertToUser = new ConvertToUser(userRepository);
24+
}
25+
26+
27+
@Test
28+
public void shouldCallRepositoryToConvertAdminToUserAndReturnSuccessMessage() {
29+
String result = whenExecuteConvertToUser(VALID_EMAIL);
30+
31+
assertEquals(SUCCESS_MESSAGE, result, "Debe retornar el mensaje de éxito.");
32+
thenRepositoryConvertWasCalled(VALID_EMAIL, 1);
33+
}
34+
35+
@Test
36+
public void shouldCallRepositoryWithEmptyStringWhenEmailIsEmpty() {
37+
String result = whenExecuteConvertToUser(EMPTY_EMAIL);
38+
39+
assertEquals(SUCCESS_MESSAGE, result, "Debe retornar el mensaje de éxito.");
40+
thenRepositoryConvertWasCalled(EMPTY_EMAIL, 1);
41+
}
42+
43+
@Test
44+
public void shouldPropagateRuntimeExceptionWhenEmailIsNull() {
45+
givenRepositoryThrowsRuntimeException(NULL_EMAIL);
46+
47+
assertThrows(RuntimeException.class,
48+
() -> whenExecuteConvertToUser(NULL_EMAIL),
49+
"Se espera que el RuntimeException se propague al delegar el email nulo.");
50+
51+
thenRepositoryConvertWasCalled(NULL_EMAIL, 1);
52+
}
53+
54+
@Test
55+
public void shouldPropagateRuntimeExceptionWhenRepositoryFails() {
56+
givenRepositoryThrowsRuntimeException(VALID_EMAIL);
57+
58+
assertThrows(RuntimeException.class,
59+
() -> whenExecuteConvertToUser(VALID_EMAIL),
60+
"Se espera que el RuntimeException se propague si el repositorio falla.");
61+
62+
thenRepositoryConvertWasCalled(VALID_EMAIL, 1);
63+
}
64+
65+
66+
//privadoss
67+
private void givenRepositoryThrowsRuntimeException(String email) {
68+
doThrow(new RuntimeException("Simulated DB error"))
69+
.when(userRepository).convertToUser(email);
70+
}
71+
72+
private String whenExecuteConvertToUser(String email) {
73+
return convertToUser.execute(email);
74+
}
75+
76+
private void thenRepositoryConvertWasCalled(String expectedEmail, int times) {
77+
verify(userRepository, times(times)).convertToUser(expectedEmail);
78+
}
79+
}
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
package com.outfitlab.project.domain.useCases.user;
2+
3+
import com.outfitlab.project.domain.interfaces.repositories.UserRepository;
4+
import org.junit.jupiter.api.BeforeEach;
5+
import org.junit.jupiter.api.Test;
6+
7+
import static org.junit.jupiter.api.Assertions.assertEquals;
8+
import static org.junit.jupiter.api.Assertions.assertThrows;
9+
import static org.mockito.Mockito.*;
10+
11+
public class DesactivateUserTest {
12+
13+
private UserRepository userRepository = mock(UserRepository.class);
14+
private DesactivateUser desactivateUser;
15+
16+
private final String VALID_EMAIL = "[email protected]";
17+
private final String EMPTY_EMAIL = "";
18+
private final String NULL_EMAIL = null;
19+
private final String SUCCESS_MESSAGE = "Usuario desactivado con éxito.";
20+
21+
@BeforeEach
22+
void setUp() {
23+
desactivateUser = new DesactivateUser(userRepository);
24+
}
25+
26+
27+
@Test
28+
public void shouldCallRepositoryToDesactivateUserAndReturnSuccessMessage() {
29+
String result = whenExecuteDesactivateUser(VALID_EMAIL);
30+
31+
assertEquals(SUCCESS_MESSAGE, result, "Debe retornar el mensaje de éxito.");
32+
thenRepositoryDesactivateWasCalled(VALID_EMAIL, 1);
33+
}
34+
35+
@Test
36+
public void shouldCallRepositoryWithEmptyStringWhenEmailIsEmpty() {
37+
String result = whenExecuteDesactivateUser(EMPTY_EMAIL);
38+
39+
assertEquals(SUCCESS_MESSAGE, result, "Debe retornar el mensaje de éxito.");
40+
thenRepositoryDesactivateWasCalled(EMPTY_EMAIL, 1);
41+
}
42+
43+
@Test
44+
public void shouldPropagateRuntimeExceptionWhenEmailIsNull() {
45+
givenRepositoryThrowsRuntimeException(NULL_EMAIL);
46+
47+
assertThrows(RuntimeException.class,
48+
() -> whenExecuteDesactivateUser(NULL_EMAIL),
49+
"Se espera que el RuntimeException se propague al delegar el email nulo.");
50+
51+
thenRepositoryDesactivateWasCalled(NULL_EMAIL, 1);
52+
}
53+
54+
@Test
55+
public void shouldPropagateRuntimeExceptionWhenRepositoryFails() {
56+
givenRepositoryThrowsRuntimeException(VALID_EMAIL);
57+
58+
assertThrows(RuntimeException.class,
59+
() -> whenExecuteDesactivateUser(VALID_EMAIL),
60+
"Se espera que el RuntimeException se propague si el repositorio falla.");
61+
62+
thenRepositoryDesactivateWasCalled(VALID_EMAIL, 1);
63+
}
64+
65+
66+
//privadoss
67+
private void givenRepositoryThrowsRuntimeException(String email) {
68+
doThrow(new RuntimeException("Simulated DB error"))
69+
.when(userRepository).desactivateUser(email);
70+
}
71+
72+
private String whenExecuteDesactivateUser(String email) {
73+
return desactivateUser.execute(email);
74+
}
75+
76+
private void thenRepositoryDesactivateWasCalled(String expectedEmail, int times) {
77+
verify(userRepository, times(times)).desactivateUser(expectedEmail);
78+
}
79+
}

0 commit comments

Comments
 (0)