diff --git a/1-anemic-ddd/src/main/java/local/mydherin/users/domain/rol/services/IsShort.java b/1-anemic-ddd/src/main/java/local/mydherin/users/domain/rol/services/IsShort.java index 0a6aaa0..0d99e75 100644 --- a/1-anemic-ddd/src/main/java/local/mydherin/users/domain/rol/services/IsShort.java +++ b/1-anemic-ddd/src/main/java/local/mydherin/users/domain/rol/services/IsShort.java @@ -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(); diff --git a/1-anemic-ddd/src/test/java/local/mydherin/users/application/user/usecases/GetUserCollectionTest.java b/1-anemic-ddd/src/test/java/local/mydherin/users/application/user/usecases/GetUserCollectionTest.java new file mode 100644 index 0000000..c9625be --- /dev/null +++ b/1-anemic-ddd/src/test/java/local/mydherin/users/application/user/usecases/GetUserCollectionTest.java @@ -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 standardUserList = givenAListOfStandardUsers(); + when(userRepository.findBy(null)).thenReturn(standardUserList); + + // When + final List result = getUserCollection.execute(); + + // Then + thenUserRepositoryWorksProperly(standardUserList, result); + } + + private List givenAListOfStandardUsers() + { + return Arrays.asList( + UserMother.getStandardUser(), + UserMother.getStandardUser() + ); + } + + private void thenUserRepositoryWorksProperly(final List expected, final List result) + { + assertEquals(expected, result); + verify(userRepository).findBy(null); + } +} \ No newline at end of file diff --git a/1-anemic-ddd/src/test/java/local/mydherin/users/application/user/usecases/GetUserCollectionWithShortRolNameTest.java b/1-anemic-ddd/src/test/java/local/mydherin/users/application/user/usecases/GetUserCollectionWithShortRolNameTest.java new file mode 100644 index 0000000..b1ad747 --- /dev/null +++ b/1-anemic-ddd/src/test/java/local/mydherin/users/application/user/usecases/GetUserCollectionWithShortRolNameTest.java @@ -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 userList = givenAListOfUsersWithLongAndShortRolNames(); + Mockito.when(userRepository.findBy(null)).thenReturn(userList); + + // When + final List result = getUserCollectionWithShortRolName.execute(); + + // Then + thenAllUsersHaveShortRolName(result); + } + + @Test + void return_an_empty_list_of_users_given_users_with_only_long_rol_names() + { + // Given + final List userList = givenAListOfUsersWithOnlyLongRolNames(); + Mockito.when(userRepository.findBy(null)).thenReturn(userList); + + // When + final List result = getUserCollectionWithShortRolName.execute(); + + // Then + thenTheListOfUsersIsEmpty(result); + } + + private List givenAListOfUsersWithLongAndShortRolNames() + { + return Arrays.asList( + UserMother.getUserWithAdminRol(), + UserMother.getUserWithTontorronRol() + ); + } + + private List givenAListOfUsersWithOnlyLongRolNames() + { + return Arrays.asList( + UserMother.getUserWithTontorronRol(), + UserMother.getUserWithTontorronRol() + ); + } + + private void thenAllUsersHaveShortRolName(List userList) + { + userList.forEach(user -> { + Assertions.assertTrue(user.getRol().getName().isShorterThan(IsShort.SHORT_NAME_LENGTH)); + }); + } + + private void thenTheListOfUsersIsEmpty(List userList) + { + Assertions.assertTrue(userList.isEmpty()); + } +} diff --git a/1-anemic-ddd/src/test/java/local/mydherin/users/shared/motherobject/RolMother.java b/1-anemic-ddd/src/test/java/local/mydherin/users/shared/motherobject/RolMother.java new file mode 100644 index 0000000..3a2571b --- /dev/null +++ b/1-anemic-ddd/src/test/java/local/mydherin/users/shared/motherobject/RolMother.java @@ -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") + ); + } +} diff --git a/1-anemic-ddd/src/test/java/local/mydherin/users/shared/motherobject/UserMother.java b/1-anemic-ddd/src/test/java/local/mydherin/users/shared/motherobject/UserMother.java new file mode 100644 index 0000000..4a946c9 --- /dev/null +++ b/1-anemic-ddd/src/test/java/local/mydherin/users/shared/motherobject/UserMother.java @@ -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() + ); + } +}