-
Notifications
You must be signed in to change notification settings - Fork 0
feat(putUser): add the api route to update a user #24
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
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 12 additions & 0 deletions
12
src/main/java/com/xpeho/spring_boot_java_random_user/domain/entities/UserRequest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| package com.xpeho.spring_boot_java_random_user.domain.entities; | ||
|
|
||
| public record UserRequest( | ||
| String gender, | ||
| String firstname, | ||
| String lastname, | ||
| String civility, | ||
| String email, | ||
| String phone, | ||
| String picture, | ||
| String nat | ||
| ) {} |
7 changes: 7 additions & 0 deletions
7
.../java/com/xpeho/spring_boot_java_random_user/domain/exceptions/UserNotFoundException.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| package com.xpeho.spring_boot_java_random_user.domain.exceptions; | ||
|
|
||
| public class UserNotFoundException extends RuntimeException { | ||
| public UserNotFoundException(long id) { | ||
| super("User not found with id: " + id); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
.../java/com/xpeho/spring_boot_java_random_user/domain/usecases/UpdateRandomUserUseCase.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| package com.xpeho.spring_boot_java_random_user.domain.usecases; | ||
|
|
||
| import com.xpeho.spring_boot_java_random_user.domain.entities.UserEntity; | ||
| import com.xpeho.spring_boot_java_random_user.domain.entities.UserRequest; | ||
| import com.xpeho.spring_boot_java_random_user.domain.exceptions.UserNotFoundException; | ||
| import com.xpeho.spring_boot_java_random_user.domain.services.UserService; | ||
| import org.springframework.stereotype.Service; | ||
|
|
||
| @Service | ||
| public class UpdateRandomUserUseCase { | ||
| private final UserService userService; | ||
|
|
||
| public UpdateRandomUserUseCase(UserService userService) { | ||
| this.userService = userService; | ||
| } | ||
|
|
||
| public UserEntity execute(int id, UserRequest user) { | ||
| UserEntity existingUser = userService.getById(id) | ||
| .orElseThrow(() -> new UserNotFoundException(id)); | ||
|
|
||
| UserEntity updatedUser = new UserEntity( | ||
| existingUser.id(), | ||
| user.gender(), | ||
| user.firstname(), | ||
| user.lastname(), | ||
| user.civility(), | ||
| user.email(), | ||
| user.phone(), | ||
| user.picture(), | ||
| user.nat() | ||
| ); | ||
|
|
||
| return userService.save(updatedUser); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
88 changes: 88 additions & 0 deletions
88
src/test/java/com/xpeho/spring_boot_java_random_user/data/services/UserServiceImplTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,88 @@ | ||
| package com.xpeho.spring_boot_java_random_user.data.services; | ||
|
|
||
| import com.xpeho.spring_boot_java_random_user.data.converters.UserConverter; | ||
| import com.xpeho.spring_boot_java_random_user.data.models.db.User; | ||
| import com.xpeho.spring_boot_java_random_user.data.sources.database.UserRepository; | ||
| import com.xpeho.spring_boot_java_random_user.domain.entities.UserEntity; | ||
| import org.junit.jupiter.api.BeforeEach; | ||
| import org.junit.jupiter.api.DisplayName; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| import java.util.Optional; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||
| import static org.junit.jupiter.api.Assertions.assertTrue; | ||
| import static org.mockito.Mockito.mock; | ||
| import static org.mockito.Mockito.verify; | ||
| import static org.mockito.Mockito.when; | ||
|
|
||
| class UserServiceImplTest { | ||
| private UserRepository userRepository; | ||
| private UserConverter userConverter; | ||
| private UserServiceImpl userService; | ||
|
|
||
| @BeforeEach | ||
| void setUp() { | ||
| userRepository = mock(UserRepository.class); | ||
| userConverter = mock(UserConverter.class); | ||
| userService = new UserServiceImpl(userRepository, userConverter); | ||
| } | ||
|
|
||
| @Test | ||
| @DisplayName("Should return mapped user when id exists") | ||
| void shouldReturnMappedUserWhenIdExists() { | ||
| User dao = new User(); | ||
| dao.setId(1L); | ||
| dao.setFirstname("John"); | ||
|
|
||
| UserEntity expected = new UserEntity(1L, "male", "John", "Doe", "Mr", "john@doe.com", "1234", "pic.jpg", "FR"); | ||
|
|
||
| when(userRepository.findById(1L)).thenReturn(Optional.of(dao)); | ||
| when(userConverter.toDomain(dao)).thenReturn(expected); | ||
|
|
||
| Optional<UserEntity> result = userService.getById(1L); | ||
|
|
||
| assertTrue(result.isPresent()); | ||
| assertEquals(expected, result.get()); | ||
| verify(userRepository).findById(1L); | ||
| verify(userConverter).toDomain(dao); | ||
| } | ||
|
|
||
| @Test | ||
| @DisplayName("Should return empty optional when id does not exist") | ||
| void shouldReturnEmptyOptionalWhenIdDoesNotExist() { | ||
| when(userRepository.findById(2L)).thenReturn(Optional.empty()); | ||
|
|
||
| Optional<UserEntity> result = userService.getById(2L); | ||
|
|
||
| assertTrue(result.isEmpty()); | ||
| verify(userRepository).findById(2L); | ||
| } | ||
|
|
||
| @Test | ||
| @DisplayName("Should save mapped user and return mapped domain entity") | ||
| void shouldSaveMappedUserAndReturnMappedDomainEntity() { | ||
| UserEntity input = new UserEntity(3L, "female", "Alice", "Smith", "Mrs", "alice@smith.com", "5678", "new-pic.jpg", "US"); | ||
|
|
||
| User daoToSave = new User(); | ||
| daoToSave.setId(3L); | ||
| daoToSave.setFirstname("Alice"); | ||
|
|
||
| User savedDao = new User(); | ||
| savedDao.setId(3L); | ||
| savedDao.setFirstname("Alice"); | ||
|
|
||
| UserEntity expected = new UserEntity(3L, "female", "Alice", "Smith", "Mrs", "alice@smith.com", "5678", "new-pic.jpg", "US"); | ||
|
|
||
| when(userConverter.toDao(input)).thenReturn(daoToSave); | ||
| when(userRepository.save(daoToSave)).thenReturn(savedDao); | ||
| when(userConverter.toDomain(savedDao)).thenReturn(expected); | ||
|
|
||
| UserEntity result = userService.save(input); | ||
|
|
||
| assertEquals(expected, result); | ||
| verify(userConverter).toDao(input); | ||
| verify(userRepository).save(daoToSave); | ||
| verify(userConverter).toDomain(savedDao); | ||
| } | ||
| } |
76 changes: 76 additions & 0 deletions
76
...a/com/xpeho/spring_boot_java_random_user/domain/usecases/UpdateRandomUserUseCaseTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| package com.xpeho.spring_boot_java_random_user.domain.usecases; | ||
|
|
||
| import com.xpeho.spring_boot_java_random_user.domain.entities.UserEntity; | ||
| import com.xpeho.spring_boot_java_random_user.domain.entities.UserRequest; | ||
| import com.xpeho.spring_boot_java_random_user.domain.exceptions.UserNotFoundException; | ||
| import com.xpeho.spring_boot_java_random_user.domain.services.UserService; | ||
| import org.junit.jupiter.api.BeforeEach; | ||
| import org.junit.jupiter.api.DisplayName; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| import java.util.Optional; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||
| import static org.junit.jupiter.api.Assertions.assertThrows; | ||
| import static org.mockito.ArgumentMatchers.any; | ||
| import static org.mockito.Mockito.mock; | ||
| import static org.mockito.Mockito.never; | ||
| import static org.mockito.Mockito.verify; | ||
| import static org.mockito.Mockito.when; | ||
|
|
||
| class UpdateRandomUserUseCaseTest { | ||
| private UserService userService; | ||
| private UpdateRandomUserUseCase useCase; | ||
|
|
||
| @BeforeEach | ||
| void setUp() { | ||
| userService = mock(UserService.class); | ||
| useCase = new UpdateRandomUserUseCase(userService); | ||
| } | ||
|
|
||
| @Test | ||
| @DisplayName("Should update an existing user and preserve its id") | ||
| void shouldUpdateExistingUserAndPreserveItsId() { | ||
| UserEntity existingUser = new UserEntity( | ||
| 42L, "male", "John", "Doe", "Mr", "john@doe.com", "1234", "pic.jpg", "FR" | ||
| ); | ||
| UserRequest request = new UserRequest( | ||
| "female", "Alice", "Smith", "Mrs", "alice@smith.com", "5678", "new-pic.jpg", "US" | ||
| ); | ||
| UserEntity savedUser = new UserEntity( | ||
| 42L, "female", "Alice", "Smith", "Mrs", "alice@smith.com", "5678", "new-pic.jpg", "US" | ||
| ); | ||
|
|
||
| when(userService.getById(42)).thenReturn(Optional.of(existingUser)); | ||
| when(userService.save(new UserEntity( | ||
| 42L, "female", "Alice", "Smith", "Mrs", "alice@smith.com", "5678", "new-pic.jpg", "US" | ||
| ))).thenReturn(savedUser); | ||
|
|
||
| UserEntity result = useCase.execute(42, request); | ||
|
|
||
| assertEquals(savedUser, result); | ||
| verify(userService).getById(42); | ||
| verify(userService).save(new UserEntity( | ||
| 42L, "female", "Alice", "Smith", "Mrs", "alice@smith.com", "5678", "new-pic.jpg", "US" | ||
| )); | ||
| } | ||
|
|
||
| @Test | ||
| @DisplayName("Should throw when updating a user that does not exist") | ||
| void shouldThrowWhenUserDoesNotExist() { | ||
| UserRequest request = new UserRequest( | ||
| "female", "Alice", "Smith", "Mrs", "alice@smith.com", "5678", "new-pic.jpg", "US" | ||
| ); | ||
|
|
||
| when(userService.getById(99)).thenReturn(Optional.empty()); | ||
|
|
||
| UserNotFoundException exception = assertThrows( | ||
| UserNotFoundException.class, | ||
| () -> useCase.execute(99, request) | ||
| ); | ||
|
|
||
| assertEquals("User not found with id: 99", exception.getMessage()); | ||
| verify(userService).getById(99); | ||
| verify(userService, never()).save(any(UserEntity.class)); | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.