From fda3dd44e1ff4c28ec7a7791a90824ba8e5baf96 Mon Sep 17 00:00:00 2001 From: SteveGT96 Date: Tue, 5 Nov 2024 11:43:13 +0100 Subject: [PATCH] chore: Align with core and fix tests --- .../usergroups/rest/UserGroupController.java | 4 +- .../org/isf/users/rest/UserController.java | 21 ++++---- .../rest/UserGroupControllerTest.java | 4 +- .../isf/users/rest/UserControllerTest.java | 54 ++++++++++--------- 4 files changed, 42 insertions(+), 41 deletions(-) diff --git a/src/main/java/org/isf/usergroups/rest/UserGroupController.java b/src/main/java/org/isf/usergroups/rest/UserGroupController.java index f318ea57..386dfa4c 100644 --- a/src/main/java/org/isf/usergroups/rest/UserGroupController.java +++ b/src/main/java/org/isf/usergroups/rest/UserGroupController.java @@ -156,8 +156,8 @@ public UserGroupDTO updateUserGroup(@PathVariable("group_code") String code, @Va .toList(); } - boolean isUpdated = userManager.updateUserGroup(group, permissions); - if (isUpdated) { + UserGroup updatedUserGroup = userManager.updateUserGroup(group, permissions); + if (updatedUserGroup != null) { return getUserGroup(group.getCode()); } else { throw new OHAPIException(new OHExceptionMessage("User group not updated.")); diff --git a/src/main/java/org/isf/users/rest/UserController.java b/src/main/java/org/isf/users/rest/UserController.java index 61a3d121..37e54fe7 100644 --- a/src/main/java/org/isf/users/rest/UserController.java +++ b/src/main/java/org/isf/users/rest/UserController.java @@ -131,17 +131,16 @@ public UserDTO updateUser( throw new OHAPIException(new OHExceptionMessage("The specified user does not exist."), HttpStatus.NOT_FOUND); } User user = userMapper.map2Model(userDTO); - boolean isUpdated; + User updatedUser; if (!user.getPasswd().isEmpty()) { - isUpdated = userManager.updatePassword(user); + updatedUser = userManager.updatePassword(user); } else { - isUpdated = userManager.updateUser(user); + updatedUser = userManager.updateUser(user); } - if (isUpdated) { + if (updatedUser != null) { LOGGER.info("User {} has been updated successfully.", userName); - user = userManager.getUserByName(userName); - user.setPasswd(null); - return userMapper.map2DTO(user); + updatedUser.setPasswd(null); + return userMapper.map2DTO(updatedUser); } else { throw new OHAPIException(new OHExceptionMessage("User not updated.")); } @@ -226,13 +225,13 @@ public UserProfileDTO updateProfile( } User user = userMapper.map2Model(userDTO); user.setUserGroupName(entity.getUserGroupName()); - boolean isUpdated; + User updatedUser; if (!user.getPasswd().isEmpty()) { - isUpdated = userManager.updatePassword(user); + updatedUser = userManager.updatePassword(user); } else { - isUpdated = userManager.updateUser(user); + updatedUser = userManager.updateUser(user); } - if (isUpdated) { + if (updatedUser != null) { LOGGER.info("User {} has been successfully updated.", currentUser); return retrieveProfile(currentUser); } else { diff --git a/src/test/java/org/isf/usergroups/rest/UserGroupControllerTest.java b/src/test/java/org/isf/usergroups/rest/UserGroupControllerTest.java index 6941cf0f..d635e5c6 100644 --- a/src/test/java/org/isf/usergroups/rest/UserGroupControllerTest.java +++ b/src/test/java/org/isf/usergroups/rest/UserGroupControllerTest.java @@ -179,7 +179,7 @@ void deleteUserGroup() throws Exception { UserGroupDTO userGroupDTO = userGroupMapper.map2DTO(userGroup); when(userManager.getUserGroup()).thenReturn(List.of(userGroup)); - doNothing().when(userManager).deleteGroup(any()); + when(userManager.deleteGroup(any())).thenReturn(userGroup); var result = mvc.perform( delete("/usergroups/{group_code}", userGroup.getCode()).contentType(MediaType.APPLICATION_JSON)) @@ -206,7 +206,7 @@ void shouldUpdateUserGroup() throws Exception { userGroupDTO.setPermissions(permissionDTOS); when(userManager.findUserGroupByCode(any())).thenReturn(userGroup); - when(userManager.updateUserGroup(any(), any())).thenReturn(true); + when(userManager.updateUserGroup(any(), any())).thenReturn(userGroup); var result = mvc.perform( put("/usergroups/{group_code}", userGroupDTO.getCode()) diff --git a/src/test/java/org/isf/users/rest/UserControllerTest.java b/src/test/java/org/isf/users/rest/UserControllerTest.java index ee524c43..84677285 100644 --- a/src/test/java/org/isf/users/rest/UserControllerTest.java +++ b/src/test/java/org/isf/users/rest/UserControllerTest.java @@ -22,6 +22,7 @@ package org.isf.users.rest; import com.fasterxml.jackson.databind.ObjectMapper; + import org.isf.OpenHospitalApiApplication; import org.isf.menu.manager.UserBrowsingManager; import org.isf.menu.model.User; @@ -80,7 +81,7 @@ public class UserControllerTest { private PermissionManager permissionManager; @Test - @WithMockUser(username = "admin", authorities = {"users.create"}) + @WithMockUser(username = "admin", authorities = { "users.create" }) @DisplayName("Create user") void createUser() throws Exception { User user = UserHelper.generateUser(); @@ -101,13 +102,13 @@ void createUser() throws Exception { } @Test - @WithMockUser(username = "admin", authorities = {"users.delete"}) + @WithMockUser(username = "admin", authorities = { "users.delete" }) @DisplayName("Delete user") void deleteUser() throws Exception { User user = UserHelper.generateUser(); when(userManager.getUserByName(any())).thenReturn(user); - doNothing().when(userManager).deleteUser(any()); + when(userManager.deleteUser(any())).thenReturn(user); var result = mvc.perform( delete("/users/{username}", user.getUserName()).contentType(MediaType.APPLICATION_JSON)) @@ -121,8 +122,9 @@ void deleteUser() throws Exception { @Nested @DisplayName("Get user") class GetUser { + @Test - @WithMockUser(username = "admin", authorities = {"users.read"}) + @WithMockUser(username = "admin", authorities = { "users.read" }) @DisplayName("Should get user by name") void shouldGetUserByName() throws Exception { User user = UserHelper.generateUsers(1).get(0); @@ -152,7 +154,7 @@ void shouldThrowForbiddenErrorWhenInsufficientPermissions() throws Exception { } @Test - @WithMockUser(username = "user", authorities = {"users.read"}) + @WithMockUser(username = "user", authorities = { "users.read" }) @DisplayName("Should throw not found error when user not found") void shouldThrowNotFoundErrorWhenUserNotFound() throws Exception { when(userManager.getUserByName(any())).thenReturn(null); @@ -191,7 +193,7 @@ void shouldGetUserProfile() throws Exception { class GetUsers { @Test - @WithMockUser(username = "admin", authorities = {"users.read"}) + @WithMockUser(username = "admin", authorities = { "users.read" }) @DisplayName("Get all users") void getAllUsers() throws Exception { List users = UserHelper.generateUsers(3); @@ -210,7 +212,7 @@ void getAllUsers() throws Exception { } @Test - @WithMockUser(username = "admin", authorities = {"users.read"}) + @WithMockUser(username = "admin", authorities = { "users.read" }) @DisplayName("Get users by group code") void getAllUsersByGroupCode() throws Exception { List users = UserHelper.generateUsers(3); @@ -230,7 +232,7 @@ void getAllUsersByGroupCode() throws Exception { } @Test - @WithMockUser(username = "admin", authorities = {"users.read"}) + @WithMockUser(username = "admin", authorities = { "users.read" }) @DisplayName("Get user by name") void getUserByName() throws Exception { User user = UserHelper.generateUser(); @@ -258,12 +260,12 @@ class UpdateUserTests { @Test @DisplayName("Should update the user") - @WithMockUser(username = "admin", authorities = {"users.read", "users.update"}) + @WithMockUser(username = "admin", authorities = { "users.read", "users.update" }) void shouldUpdateTheUser() throws Exception { var user = new User("doctor", new UserGroup("doctor", "Doctor group"), "", "Simple user"); - when(userManager.updateUser(any())).thenReturn(true); - when(userManager.updatePassword(any())).thenReturn(true); + when(userManager.updateUser(any())).thenReturn(user); + when(userManager.updatePassword(any())).thenReturn(user); when(userManager.getUserByName(any())).thenReturn(user); var result = mvc.perform( @@ -277,7 +279,7 @@ void shouldUpdateTheUser() throws Exception { @Test @DisplayName("Should fail to update when username in the patch doesn't match") - @WithMockUser(username = "admin", authorities = {"users.read", "users.update"}) + @WithMockUser(username = "admin", authorities = { "users.read", "users.update" }) void shouldFailToUpdateWhenUsernameInThePathDoesNtMatch() throws Exception { var user = new User("laboratorist", new UserGroup("doctor", "Doctor group"), "", "Simple user"); var result = mvc.perform( @@ -291,7 +293,7 @@ void shouldFailToUpdateWhenUsernameInThePathDoesNtMatch() throws Exception { @Test @DisplayName("Should fail to update user when insufficient permissions") - @WithMockUser(username = "admin", authorities = {"roles.read"}) + @WithMockUser(username = "admin", authorities = { "roles.read" }) void shouldFailToUpdateUserWhenInsufficientPermissions() throws Exception { var user = new User("doctor", new UserGroup("doctor", "Doctor group"), "", "Simple user"); @@ -306,7 +308,7 @@ void shouldFailToUpdateUserWhenInsufficientPermissions() throws Exception { @Test @DisplayName("Should fail when user to update doesn't exist") - @WithMockUser(username = "admin", authorities = {"users.read", "users.update"}) + @WithMockUser(username = "admin", authorities = { "users.read", "users.update" }) void shouldFailWhenUserToUpdateIsNotFound() throws Exception { var user = new User("doctor", new UserGroup("doctor", "Doctor group"), "", "Simple user"); @@ -323,12 +325,12 @@ void shouldFailWhenUserToUpdateIsNotFound() throws Exception { @Test @DisplayName("Should update only password") - @WithMockUser(username = "admin", authorities = {"users.read", "users.update"}) + @WithMockUser(username = "admin", authorities = { "users.read", "users.update" }) void shouldUpdateOnlyPassword() throws Exception { var user = new User("doctor", new UserGroup("doctor", "Doctor group"), "?..passwordAA", "Simple user"); - when(userManager.updateUser(any())).thenReturn(true); - when(userManager.updatePassword(any())).thenReturn(true); + when(userManager.updateUser(any())).thenReturn(user); + when(userManager.updatePassword(any())).thenReturn(user); when(userManager.getUserByName(any())).thenReturn(user); var result = mvc.perform( @@ -345,12 +347,12 @@ void shouldUpdateOnlyPassword() throws Exception { @Test @DisplayName("Should update user without updating password") - @WithMockUser(username = "admin", authorities = {"users.read", "users.update"}) + @WithMockUser(username = "admin", authorities = { "users.read", "users.update" }) void shouldUpdateUserWithoutUpdatingPassword() throws Exception { var user = new User("doctor", new UserGroup("doctor", "Doctor group"), "", "Simple user"); - when(userManager.updateUser(any())).thenReturn(true); - when(userManager.updatePassword(any())).thenReturn(true); + when(userManager.updateUser(any())).thenReturn(user); + when(userManager.updatePassword(any())).thenReturn(user); when(userManager.getUserByName(any())).thenReturn(user); var result = mvc.perform( @@ -384,8 +386,8 @@ public void setup() throws OHServiceException { void shouldUpdateUserProfile() throws Exception { var user = new User("doctor", new UserGroup("doctor", "Doctor group"), "", "Simple user"); - when(userManager.updateUser(any())).thenReturn(true); - when(userManager.updatePassword(any())).thenReturn(true); + when(userManager.updateUser(any())).thenReturn(user); + when(userManager.updatePassword(any())).thenReturn(user); when(userManager.getUserByName(any())).thenReturn(user); var result = mvc.perform( @@ -433,8 +435,8 @@ void shouldFailToUpdateUserWhenNotAuthenticated() throws Exception { void shouldUpdateOnlyPassword() throws Exception { var user = new User("doctor", new UserGroup("doctor", "Doctor group"), "?..passwordAA", "Simple user"); - when(userManager.updateUser(any())).thenReturn(true); - when(userManager.updatePassword(any())).thenReturn(true); + when(userManager.updateUser(any())).thenReturn(user); + when(userManager.updatePassword(any())).thenReturn(user); when(userManager.getUserByName(any())).thenReturn(user); var result = mvc.perform( @@ -455,8 +457,8 @@ void shouldUpdateOnlyPassword() throws Exception { void shouldUpdateUserWithoutUpdatingPassword() throws Exception { var user = new User("doctor", new UserGroup("doctor", "Doctor group"), "", "Simple user"); - when(userManager.updateUser(any())).thenReturn(true); - when(userManager.updatePassword(any())).thenReturn(true); + when(userManager.updateUser(any())).thenReturn(user); + when(userManager.updatePassword(any())).thenReturn(user); when(userManager.getUserByName(any())).thenReturn(user); var result = mvc.perform(