diff --git a/backend/device-management-main/src/test/java/com/amigoscode/devicemanagement/domain/user/UserServiceTest.java b/backend/device-management-main/src/test/java/com/amigoscode/devicemanagement/domain/user/UserServiceTest.java index 79a6800..ff048dc 100644 --- a/backend/device-management-main/src/test/java/com/amigoscode/devicemanagement/domain/user/UserServiceTest.java +++ b/backend/device-management-main/src/test/java/com/amigoscode/devicemanagement/domain/user/UserServiceTest.java @@ -70,41 +70,35 @@ void delete_method_should_not_throw_exception() { Assertions.assertDoesNotThrow(() -> userService.removeById(fakeUser.getId())); } -// @Test -// void save_method_should_return_saved_user_when_user_does_not_exist() { -// Mockito.when((clock.getZone())).thenReturn(NOW.getZone()); -// Mockito.when((clock.instant())).thenReturn(NOW.toInstant()); -// Mockito.when(userRepository.save( -// fakeUser.withPassword( -// encoder.encode(fakeUser.getPassword()) -// ) -// )).thenReturn(fakeUser); -// -// //when -// User savedUser = userService.save(fakeUser, "creatorId"); -// -// //then -// Assertions.assertNotNull(savedUser); -// Assertions.assertEquals(fakeUser.getId(), savedUser.getId()); -// Assertions.assertEquals(fakeUser.getEmail(), savedUser.getEmail()); -// Assertions.assertEquals(fakeUser.getName(), savedUser.getName()); -// Assertions.assertEquals(fakeUser.getPassword(), savedUser.getPassword()); -// } -// -// @Test -// void save_method_should_throw_user_already_exist_exception_when_user_exist() { -// Mockito.when((clock.getZone())).thenReturn(NOW.getZone()); -// Mockito.when((clock.instant())).thenReturn(NOW.toInstant()); -// Mockito.when(userRepository.save( -// fakeUser.withPassword( -// encoder.encode(fakeUser.getPassword()) -// ) -// )).thenThrow(new UserAlreadyExistsException()); -// //when -// //then -// Assertions.assertThrows(UserAlreadyExistsException.class, -// ()-> userService.save(fakeUser, "creatorId")); -// } + @Test + void save_method_should_return_saved_user_when_user_does_not_exist() { + Mockito.when((clock.getZone())).thenReturn(NOW.getZone()); + Mockito.when((clock.instant())).thenReturn(NOW.toInstant()); + Mockito.when(userRepository.save(Mockito.any(User.class))).thenReturn(fakeUser); + + //when + User savedUser = userService.save(fakeUser, "creatorId"); + + //then + Assertions.assertNotNull(savedUser); + Assertions.assertEquals(fakeUser.getId(), savedUser.getId()); + Assertions.assertEquals(fakeUser.getEmail(), savedUser.getEmail()); + Assertions.assertEquals(fakeUser.getName(), savedUser.getName()); + Assertions.assertEquals(fakeUser.getPassword(), savedUser.getPassword()); + } + + @Test + void save_method_should_throw_user_already_exist_exception_when_user_exist() { + Mockito.when((clock.getZone())).thenReturn(NOW.getZone()); + Mockito.when((clock.instant())).thenReturn(NOW.toInstant()); + Mockito.when(userRepository.save(Mockito.any(User.class))) + .thenThrow(new UserAlreadyExistsException()); + + //when + //then + Assertions.assertThrows(UserAlreadyExistsException.class, + ()-> userService.save(fakeUser, "creatorId")); + } @Test void find_by_email_method_should_return_founded_user_when_user_exist() {