Skip to content

Commit

Permalink
Moved AuthenticationController to another folder
Browse files Browse the repository at this point in the history
- Moved AuthenticationController to another folder
- Create RolesDto to avoid to use Role entity
- Adjusted unit tests
  • Loading branch information
LauroSilveira committed Jan 3, 2024
1 parent ce7337c commit 589bbc5
Show file tree
Hide file tree
Showing 14 changed files with 43 additions and 49 deletions.
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
@@ -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) {
}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -11,6 +10,6 @@ public record UserDto(
String username,
@NotBlank
String password,
Set<Roles> roles) {
Set<RolesDto> roles) {

}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

public interface UserService {

@Transactional
UserDto saveUser(UserDto dto);

List<UserDto> getUsers();
Expand Down
Original file line number Diff line number Diff line change
@@ -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 {

Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
);
Expand Down Expand Up @@ -166,7 +166,7 @@ void should_return_a_category_searching_by_video_test() {

//When
List<VideoDto> videosByCategory = this.categoryService.getVideosByCategory(
Rating.TRILLER.getTitle());
Rating.THRILLER.getTitle());

//When
assertThat(videosByCategory).isNotNull().isNotEmpty();
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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);

Expand All @@ -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())),

Check failure on line 78 in src/test/java/com/alura/aluraflixapi/infraestructure/service/UserServiceImplTest.java

View workflow job for this annotation

GitHub Actions / Test Reporter Summary

com.alura.aluraflixapi.infraestructure.service.UserServiceImplTest ► getUsers_test

Failed test found in: target/surefire-reports/TEST-com.alura.aluraflixapi.infraestructure.service.UserServiceImplTest.xml Error: java.lang.ArrayIndexOutOfBoundsException: Index 1 out of bounds for length 1
Raw output
java.lang.ArrayIndexOutOfBoundsException: Index 1 out of bounds for length 1
	at java.base/java.util.ImmutableCollections$ListN.get(ImmutableCollections.java:680)
	at com.alura.aluraflixapi.infraestructure.service.UserServiceImplTest.getUsers_test(UserServiceImplTest.java:78)
	at java.base/java.lang.reflect.Method.invoke(Method.java:568)
	at java.base/java.util.ArrayList.forEach(ArrayList.java:1511)
	at java.base/java.util.ArrayList.forEach(ArrayList.java:1511)
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);

Expand Down

0 comments on commit 589bbc5

Please sign in to comment.