From 589bbc584f6225d3d13fa09e7fb2a2420e36bdf2 Mon Sep 17 00:00:00 2001 From: Lauro Correia Silveira Date: Wed, 3 Jan 2024 18:13:48 +0100 Subject: [PATCH] Moved AuthenticationController to another folder - Moved AuthenticationController to another folder - Create RolesDto to avoid to use Role entity - Adjusted unit tests --- .../AuthenticationController.java | 2 +- .../AuthenticationControllerAdvice.java | 2 +- .../aluraflixapi/domain/category/Rating.java | 2 +- .../domain/user/dto/RolesDto.java | 7 ++++ .../aluraflixapi/domain/user/dto/UserDto.java | 3 +- .../service/user/UserService.java | 1 + .../service/user/UserServiceImpl.java | 7 ++-- .../AuthenticationControllerTest.java | 2 +- .../controller/CategoryControllerTest.java | 2 +- .../controller/ControllerAdviceTest.java | 2 +- .../controller/UserControllerTest.java | 14 +++---- .../controller/VideoControllerTest.java | 2 +- .../service/CategoryServiceImplTest.java | 8 ++-- .../service/UserServiceImplTest.java | 38 +++++++------------ 14 files changed, 43 insertions(+), 49 deletions(-) rename src/main/java/com/alura/aluraflixapi/controller/{user => authentication}/AuthenticationController.java (96%) rename src/main/java/com/alura/aluraflixapi/controller/{user => authentication}/AuthenticationControllerAdvice.java (93%) create mode 100644 src/main/java/com/alura/aluraflixapi/domain/user/dto/RolesDto.java diff --git a/src/main/java/com/alura/aluraflixapi/controller/user/AuthenticationController.java b/src/main/java/com/alura/aluraflixapi/controller/authentication/AuthenticationController.java similarity index 96% rename from src/main/java/com/alura/aluraflixapi/controller/user/AuthenticationController.java rename to src/main/java/com/alura/aluraflixapi/controller/authentication/AuthenticationController.java index 4bb1100..805e89e 100644 --- a/src/main/java/com/alura/aluraflixapi/controller/user/AuthenticationController.java +++ b/src/main/java/com/alura/aluraflixapi/controller/authentication/AuthenticationController.java @@ -1,4 +1,4 @@ -package com.alura.aluraflixapi.controller.user; +package com.alura.aluraflixapi.controller.authentication; import com.alura.aluraflixapi.domain.user.User; import com.alura.aluraflixapi.domain.user.dto.AuthenticationDto; diff --git a/src/main/java/com/alura/aluraflixapi/controller/user/AuthenticationControllerAdvice.java b/src/main/java/com/alura/aluraflixapi/controller/authentication/AuthenticationControllerAdvice.java similarity index 93% rename from src/main/java/com/alura/aluraflixapi/controller/user/AuthenticationControllerAdvice.java rename to src/main/java/com/alura/aluraflixapi/controller/authentication/AuthenticationControllerAdvice.java index a069157..743d198 100644 --- a/src/main/java/com/alura/aluraflixapi/controller/user/AuthenticationControllerAdvice.java +++ b/src/main/java/com/alura/aluraflixapi/controller/authentication/AuthenticationControllerAdvice.java @@ -1,4 +1,4 @@ -package com.alura.aluraflixapi.controller.user; +package com.alura.aluraflixapi.controller.authentication; import com.alura.aluraflixapi.infraestructure.exception.ErrorMessageVO; import org.springframework.http.HttpStatus; diff --git a/src/main/java/com/alura/aluraflixapi/domain/category/Rating.java b/src/main/java/com/alura/aluraflixapi/domain/category/Rating.java index ecc3b64..d6dc7f6 100644 --- a/src/main/java/com/alura/aluraflixapi/domain/category/Rating.java +++ b/src/main/java/com/alura/aluraflixapi/domain/category/Rating.java @@ -6,7 +6,7 @@ public enum Rating { FANTASY(2, "Fantasy", "#FFD700"), TERROR(3, "Terror", "#FF0000"), ACTION(4, "Action", "#008000"), - TRILLER(5, "Thiller", "#808080"), + THRILLER(5, "Thriller", "#808080"), ROMANTIC_COMEDY(6, "Romantic Comedy", "#E41B17"); public int getCategoryId() { diff --git a/src/main/java/com/alura/aluraflixapi/domain/user/dto/RolesDto.java b/src/main/java/com/alura/aluraflixapi/domain/user/dto/RolesDto.java new file mode 100644 index 0000000..ba10ee4 --- /dev/null +++ b/src/main/java/com/alura/aluraflixapi/domain/user/dto/RolesDto.java @@ -0,0 +1,7 @@ +package com.alura.aluraflixapi.domain.user.dto; + +import com.alura.aluraflixapi.domain.user.roles.RolesEnum; + +public record RolesDto(String id, + RolesEnum role) { +} diff --git a/src/main/java/com/alura/aluraflixapi/domain/user/dto/UserDto.java b/src/main/java/com/alura/aluraflixapi/domain/user/dto/UserDto.java index f011e8c..c1e2f8a 100644 --- a/src/main/java/com/alura/aluraflixapi/domain/user/dto/UserDto.java +++ b/src/main/java/com/alura/aluraflixapi/domain/user/dto/UserDto.java @@ -1,6 +1,5 @@ package com.alura.aluraflixapi.domain.user.dto; -import com.alura.aluraflixapi.domain.user.roles.Roles; import jakarta.validation.constraints.NotBlank; import java.util.Set; @@ -11,6 +10,6 @@ public record UserDto( String username, @NotBlank String password, - Set roles) { + Set roles) { } diff --git a/src/main/java/com/alura/aluraflixapi/infraestructure/service/user/UserService.java b/src/main/java/com/alura/aluraflixapi/infraestructure/service/user/UserService.java index c461c93..53d2d2e 100644 --- a/src/main/java/com/alura/aluraflixapi/infraestructure/service/user/UserService.java +++ b/src/main/java/com/alura/aluraflixapi/infraestructure/service/user/UserService.java @@ -6,6 +6,7 @@ public interface UserService { + @Transactional UserDto saveUser(UserDto dto); List getUsers(); diff --git a/src/main/java/com/alura/aluraflixapi/infraestructure/service/user/UserServiceImpl.java b/src/main/java/com/alura/aluraflixapi/infraestructure/service/user/UserServiceImpl.java index f5a5eb3..fe9807d 100644 --- a/src/main/java/com/alura/aluraflixapi/infraestructure/service/user/UserServiceImpl.java +++ b/src/main/java/com/alura/aluraflixapi/infraestructure/service/user/UserServiceImpl.java @@ -1,13 +1,14 @@ package com.alura.aluraflixapi.infraestructure.service.user; +import com.alura.aluraflixapi.domain.user.User; import com.alura.aluraflixapi.domain.user.dto.UserDto; import com.alura.aluraflixapi.infraestructure.mapper.UserMapper; -import com.alura.aluraflixapi.domain.user.User; import com.alura.aluraflixapi.infraestructure.repository.RoleRepository; import com.alura.aluraflixapi.infraestructure.repository.UserRepository; -import java.util.List; import org.springframework.stereotype.Service; +import java.util.List; + @Service public class UserServiceImpl implements UserService { @@ -28,7 +29,7 @@ public UserServiceImpl(final UserRepository repository, final RoleRepository rol public UserDto saveUser(UserDto dto) { final User user = mapper.mapToEntity(dto); //first save Document Roles - this.roleRepository.saveAll(dto.roles()); + this.roleRepository.saveAll(user.getRoles()); //After save Document User final User newUser = repository.save(user); return mapper.mapToDto(newUser); diff --git a/src/test/java/com/alura/aluraflixapi/controller/AuthenticationControllerTest.java b/src/test/java/com/alura/aluraflixapi/controller/AuthenticationControllerTest.java index 011ada1..c5db0a3 100644 --- a/src/test/java/com/alura/aluraflixapi/controller/AuthenticationControllerTest.java +++ b/src/test/java/com/alura/aluraflixapi/controller/AuthenticationControllerTest.java @@ -1,6 +1,6 @@ package com.alura.aluraflixapi.controller; -import com.alura.aluraflixapi.controller.user.AuthenticationController; +import com.alura.aluraflixapi.controller.authentication.AuthenticationController; import com.alura.aluraflixapi.domain.user.User; import com.alura.aluraflixapi.domain.user.dto.AuthenticationDto; import com.alura.aluraflixapi.domain.user.roles.Roles; diff --git a/src/test/java/com/alura/aluraflixapi/controller/CategoryControllerTest.java b/src/test/java/com/alura/aluraflixapi/controller/CategoryControllerTest.java index e4c088c..3290f02 100644 --- a/src/test/java/com/alura/aluraflixapi/controller/CategoryControllerTest.java +++ b/src/test/java/com/alura/aluraflixapi/controller/CategoryControllerTest.java @@ -1,6 +1,6 @@ package com.alura.aluraflixapi.controller; -import com.alura.aluraflixapi.controller.user.AuthenticationController; +import com.alura.aluraflixapi.controller.authentication.AuthenticationController; import com.alura.aluraflixapi.domain.category.dto.CategoryDto; import com.alura.aluraflixapi.domain.video.dto.VideoDto; import com.alura.aluraflixapi.infraestructure.repository.UserRepository; diff --git a/src/test/java/com/alura/aluraflixapi/controller/ControllerAdviceTest.java b/src/test/java/com/alura/aluraflixapi/controller/ControllerAdviceTest.java index cf6a8ea..ff0c14f 100644 --- a/src/test/java/com/alura/aluraflixapi/controller/ControllerAdviceTest.java +++ b/src/test/java/com/alura/aluraflixapi/controller/ControllerAdviceTest.java @@ -1,7 +1,7 @@ package com.alura.aluraflixapi.controller; import com.alura.aluraflixapi.controller.dto.ErrorVO; -import com.alura.aluraflixapi.controller.user.AuthenticationController; +import com.alura.aluraflixapi.controller.authentication.AuthenticationController; import com.alura.aluraflixapi.domain.video.dto.VideoDto; import com.alura.aluraflixapi.infraestructure.exception.ErrorMessageVO; import com.alura.aluraflixapi.infraestructure.exception.ResourceNotFoundException; diff --git a/src/test/java/com/alura/aluraflixapi/controller/UserControllerTest.java b/src/test/java/com/alura/aluraflixapi/controller/UserControllerTest.java index d1b0a96..c418531 100644 --- a/src/test/java/com/alura/aluraflixapi/controller/UserControllerTest.java +++ b/src/test/java/com/alura/aluraflixapi/controller/UserControllerTest.java @@ -1,6 +1,7 @@ package com.alura.aluraflixapi.controller; import com.alura.aluraflixapi.controller.user.UserController; +import com.alura.aluraflixapi.domain.user.dto.RolesDto; import com.alura.aluraflixapi.domain.user.dto.UserDto; import com.alura.aluraflixapi.domain.user.roles.Roles; import com.alura.aluraflixapi.domain.user.roles.RolesEnum; @@ -36,9 +37,7 @@ void saveUser_test() { final var userId = UUID.randomUUID().toString(); final var roleId = UUID.randomUUID().toString(); final var user = new UserDto(userId, "guest@aluraflix.com.br", "123456", - Set.of(Roles.builder() - .id(roleId) - .role(RolesEnum.ROLE_GUEST).build())); + Set.of(new RolesDto(roleId, RolesEnum.ROLE_GUEST))); when(this.userService.saveUser(any())).thenReturn(user); //When @@ -53,12 +52,11 @@ void saveUser_test() { @DisplayName("Should return a list of users and response 200 OK") void getUsers_test() { //Given - final var usersList = List.of(new UserDto(UUID.randomUUID().toString(), "guest@aluraflix.com.br", "123456", - Set.of(Roles.builder().id(UUID.randomUUID().toString()).role(RolesEnum.ROLE_GUEST).build())), + final var usersList = List.of(new UserDto(UUID.randomUUID().toString(), + "guest@aluraflix.com.br", "123456", + Set.of(new RolesDto(UUID.randomUUID().toString(), RolesEnum.ROLE_GUEST))), new UserDto(UUID.randomUUID().toString(), "admin@aluraflix.com.br", "admin", - Set.of(Roles.builder() - .id(UUID.randomUUID().toString()) - .role(RolesEnum.ROLE_ADMIN).build()))); + Set.of(new RolesDto(UUID.randomUUID().toString(), RolesEnum.ROLE_ADMIN)))); when(this.userService.getUsers()).thenReturn(usersList); //When diff --git a/src/test/java/com/alura/aluraflixapi/controller/VideoControllerTest.java b/src/test/java/com/alura/aluraflixapi/controller/VideoControllerTest.java index f343764..ad0c6c4 100644 --- a/src/test/java/com/alura/aluraflixapi/controller/VideoControllerTest.java +++ b/src/test/java/com/alura/aluraflixapi/controller/VideoControllerTest.java @@ -1,7 +1,7 @@ package com.alura.aluraflixapi.controller; import com.alura.aluraflixapi.controller.category.CategoryController; -import com.alura.aluraflixapi.controller.user.AuthenticationController; +import com.alura.aluraflixapi.controller.authentication.AuthenticationController; import com.alura.aluraflixapi.controller.user.UserController; import com.alura.aluraflixapi.controller.video.VideoController; import com.alura.aluraflixapi.domain.category.Rating; diff --git a/src/test/java/com/alura/aluraflixapi/infraestructure/service/CategoryServiceImplTest.java b/src/test/java/com/alura/aluraflixapi/infraestructure/service/CategoryServiceImplTest.java index 4e9912c..3963be1 100644 --- a/src/test/java/com/alura/aluraflixapi/infraestructure/service/CategoryServiceImplTest.java +++ b/src/test/java/com/alura/aluraflixapi/infraestructure/service/CategoryServiceImplTest.java @@ -59,9 +59,9 @@ void should_return_all_categories_in_database_test() { this.buildCategory(UUID.randomUUID().toString(), Rating.TERROR.getTitle(), Rating.TERROR.getHexDecimalColor(), Rating.TERROR), - this.buildCategory(UUID.randomUUID().toString(), Rating.TRILLER.getTitle(), - Rating.TRILLER.getHexDecimalColor(), - Rating.TRILLER), + this.buildCategory(UUID.randomUUID().toString(), Rating.THRILLER.getTitle(), + Rating.THRILLER.getHexDecimalColor(), + Rating.THRILLER), this.buildCategory(UUID.randomUUID().toString(), Rating.ROMANTIC_COMEDY.getTitle(), Rating.ROMANTIC_COMEDY.getHexDecimalColor(), Rating.ROMANTIC_COMEDY) ); @@ -166,7 +166,7 @@ void should_return_a_category_searching_by_video_test() { //When List videosByCategory = this.categoryService.getVideosByCategory( - Rating.TRILLER.getTitle()); + Rating.THRILLER.getTitle()); //When assertThat(videosByCategory).isNotNull().isNotEmpty(); diff --git a/src/test/java/com/alura/aluraflixapi/infraestructure/service/UserServiceImplTest.java b/src/test/java/com/alura/aluraflixapi/infraestructure/service/UserServiceImplTest.java index a4c91a7..52797b1 100644 --- a/src/test/java/com/alura/aluraflixapi/infraestructure/service/UserServiceImplTest.java +++ b/src/test/java/com/alura/aluraflixapi/infraestructure/service/UserServiceImplTest.java @@ -1,6 +1,7 @@ package com.alura.aluraflixapi.infraestructure.service; import com.alura.aluraflixapi.domain.user.User; +import com.alura.aluraflixapi.domain.user.dto.RolesDto; import com.alura.aluraflixapi.domain.user.dto.UserDto; import com.alura.aluraflixapi.domain.user.roles.Roles; import com.alura.aluraflixapi.domain.user.roles.RolesEnum; @@ -46,13 +47,11 @@ void saveUser_test() { //Given final var userDto = new UserDto(UUID.randomUUID().toString(), "user@aluraflix.com", new BCryptPasswordEncoder().encode("user@123456"), - Set.of(Roles.builder() - .id(UUID.randomUUID().toString()) - .role(RolesEnum.ROLE_USER) - .build())); - final var user = new User(userDto.id(), userDto.username(), userDto.password(), userDto.roles()); + Set.of(new RolesDto(UUID.randomUUID().toString(), RolesEnum.ROLE_USER))); + final var user = new User(userDto.id(), userDto.username(), userDto.password(), + Set.of(Roles.builder().id(userDto.roles().stream().toList().get(0).id()).role(userDto.roles().stream().toList().get(0).role()).build())); when(this.mapper.mapToEntity(any())).thenReturn(user); - when(this.roleRepository.saveAll(Mockito.anyList())).thenReturn(userDto.roles().stream().toList()); + when(this.roleRepository.saveAll(Mockito.anyList())).thenReturn(user.getRoles().stream().toList()); when(this.userRepository.save(any())).thenReturn(user); when(this.mapper.mapToDto(any())).thenReturn(userDto); @@ -69,27 +68,16 @@ void saveUser_test() { @DisplayName("Should retrieve all users") void getUsers_test() { //Given - final var usersDto = List.of(new UserDto(UUID.randomUUID().toString(), - "user@aluraflix.com", "user@123456", Set.of(Roles.builder() - .id(UUID.randomUUID().toString()) - .role(RolesEnum.ROLE_USER) - .build())), - new UserDto(UUID.randomUUID().toString(), - "admin@aluraflix.com", "admin@admin", Set.of(Roles.builder() - .id(UUID.randomUUID().toString()) - .role(RolesEnum.ROLE_ADMIN) - .build())), - new UserDto(UUID.randomUUID().toString(), - "guest@aluraflix.com", "quest@123456", Set.of(Roles.builder() - .id(UUID.randomUUID().toString()) - .role(RolesEnum.ROLE_GUEST) - .build())) + final var usersDto = List.of( + new UserDto(UUID.randomUUID().toString(), "user@aluraflix.com", "user@123456", Set.of(new RolesDto(UUID.randomUUID().toString(), RolesEnum.ROLE_USER))), + new UserDto(UUID.randomUUID().toString(), "guest@aluraflix.com", "quest@123456", Set.of(new RolesDto(UUID.randomUUID().toString(), RolesEnum.ROLE_GUEST))), + new UserDto(UUID.randomUUID().toString(), "admin@aluraflix.com", "admin@admin", Set.of(new RolesDto(UUID.randomUUID().toString(), RolesEnum.ROLE_ADMIN))) ); final var users = List.of( - new User(usersDto.get(0).id(), usersDto.get(0).username(), usersDto.get(0).password(), usersDto.get(0).roles()), - new User(usersDto.get(1).id(), usersDto.get(1).username(), usersDto.get(1).password(), usersDto.get(1).roles()), - new User(usersDto.get(2).id(), usersDto.get(2).username(), usersDto.get(2).password(), usersDto.get(2).roles()) - ); + new User(usersDto.get(0).id(), usersDto.get(0).username(), usersDto.get(0).password(), Set.of(Roles.builder().role(usersDto.get(0).roles().stream().toList().get(0).role()).build())), + new User(usersDto.get(1).id(), usersDto.get(1).username(), usersDto.get(1).password(), Set.of(Roles.builder().role(usersDto.get(1).roles().stream().toList().get(1).role()).build())), + new User(usersDto.get(2).id(), usersDto.get(2).username(), usersDto.get(2).password(), Set.of(Roles.builder().role(usersDto.get(2).roles().stream().toList().get(2).role()).build()) + )); when(this.userRepository.findAll()).thenReturn(users);