Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Jterrero/unit testing for user use cases #3

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

public class IsShort {

private final Integer SHORT_NAME_LENGTH = 5;
public static final Integer SHORT_NAME_LENGTH = 5;

public Boolean execute(final IsShortQuery query) {
final var rol = query.getRol();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package local.mydherin.users.application.user.usecases;

import local.mydherin.users.application.user.repository.UserRepository;
import local.mydherin.users.domain.user.User;
import local.mydherin.users.shared.motherobject.UserMother;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;

import java.util.Arrays;
import java.util.List;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

@ExtendWith(MockitoExtension.class)
public final class GetUserCollectionTest {
@Mock
private UserRepository userRepository;
private GetUserCollection getUserCollection;
@BeforeEach
public void setUp() {
getUserCollection = new GetUserCollection(
userRepository
);
}
@Test
void user_repository_works_properly()
{
// Given
final List<User> standardUserList = givenAListOfStandardUsers();
when(userRepository.findBy(null)).thenReturn(standardUserList);

// When
final List<User> result = getUserCollection.execute();

// Then
thenUserRepositoryWorksProperly(standardUserList, result);
}

private List<User> givenAListOfStandardUsers()
{
return Arrays.asList(
UserMother.getStandardUser(),
UserMother.getStandardUser()
);
}

private void thenUserRepositoryWorksProperly(final List<User> expected, final List<User> result)
{
assertEquals(expected, result);
verify(userRepository).findBy(null);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package local.mydherin.users.application.user.usecases;

import local.mydherin.users.application.user.repository.UserRepository;
import local.mydherin.users.domain.rol.services.IsShort;
import local.mydherin.users.domain.user.User;
import local.mydherin.users.domain.user.services.FilterByShortRolName;
import local.mydherin.users.shared.motherobject.UserMother;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.junit.jupiter.MockitoExtension;

import java.util.Arrays;
import java.util.List;

@ExtendWith(MockitoExtension.class)
public final class GetUserCollectionWithShortRolNameTest {
@Mock
private UserRepository userRepository;
private GetUserCollectionWithShortRolName getUserCollectionWithShortRolName;
@BeforeEach
public void setUp() {
getUserCollectionWithShortRolName = new GetUserCollectionWithShortRolName(
userRepository,
new FilterByShortRolName(new IsShort())
);
}
@Test
void return_a_list_of_users_with_short_rol_name_given_users_with_short_and_long_rol_names()
{
// Given
final List<User> userList = givenAListOfUsersWithLongAndShortRolNames();
Mockito.when(userRepository.findBy(null)).thenReturn(userList);

// When
final List<User> result = getUserCollectionWithShortRolName.execute();

// Then
thenAllUsersHaveShortRolName(result);
}

@Test
void return_an_empty_list_of_users_given_users_with_only_long_rol_names()
{
// Given
final List<User> userList = givenAListOfUsersWithOnlyLongRolNames();
Mockito.when(userRepository.findBy(null)).thenReturn(userList);

// When
final List<User> result = getUserCollectionWithShortRolName.execute();

// Then
thenTheListOfUsersIsEmpty(result);
}

private List<User> givenAListOfUsersWithLongAndShortRolNames()
{
return Arrays.asList(
UserMother.getUserWithAdminRol(),
UserMother.getUserWithTontorronRol()
);
}

private List<User> givenAListOfUsersWithOnlyLongRolNames()
{
return Arrays.asList(
UserMother.getUserWithTontorronRol(),
UserMother.getUserWithTontorronRol()
);
}

private void thenAllUsersHaveShortRolName(List<User> userList)
{
userList.forEach(user -> {
Assertions.assertTrue(user.getRol().getName().isShorterThan(IsShort.SHORT_NAME_LENGTH));
});
}

private void thenTheListOfUsersIsEmpty(List<User> userList)
{
Assertions.assertTrue(userList.isEmpty());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package local.mydherin.users.shared.motherobject;

import local.mydherin.users.domain.rol.Rol;
import local.mydherin.users.domain.rol.vos.Name;
import local.mydherin.users.domain.rol.vos.RolId;

public final class RolMother {
private RolMother()
{}

public static Rol getAdminRol()
{
return Rol.of(
RolId.of("1"),
Name.of("ADMIN")
);
}

public static Rol getTontorronRol()
{
return Rol.of(
RolId.of("2"),
Name.of("TONTORRON")
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package local.mydherin.users.shared.motherobject;

import local.mydherin.users.domain.user.User;
import local.mydherin.users.domain.user.vos.Age;
import local.mydherin.users.domain.user.vos.DNI;
import local.mydherin.users.domain.user.vos.Name;
import local.mydherin.users.domain.user.vos.UserId;

public final class UserMother {
private UserMother()
{}
public static User getStandardUser()
{
return User.of(
UserId.of("1"),
Name.of("Any user 1"),
Age.of(25),
DNI.of("10000000Z"),
RolMother.getTontorronRol()
);
}
public static User getUserWithAdminRol()
{
return User.of(
UserId.of("1"),
Name.of("User with rol admin 1"),
Age.of(25),
DNI.of("10000000Z"),
RolMother.getAdminRol()
);
}
public static User getUserWithTontorronRol()
{
return User.of(
UserId.of("1"),
Name.of("User with rol tontorron 1"),
Age.of(25),
DNI.of("10000000Z"),
RolMother.getTontorronRol()
);
}
}