UserManagementServiceImpl implements UserManagementService { @PUT @Path("/{username}") @Override public Response updateUser(@PathParam("username") String username, @QueryParam("domain") String domain, UserInfo userInfo) { if (domain != null && !domain.isEmpty()) { username = domain + '/' + username; } try { UserStoreManager userStoreManager = DeviceMgtAPIUtils.getUserStoreManager(); if (!userStoreManager.isExistingUser(username)) { if (log.isDebugEnabled()) { log.debug("User by username: " + username + " doesn't exists. Therefore, request made to update user was refused."); } return Response.status(Response.Status.NOT_FOUND).entity( new ErrorResponse.ErrorResponseBuilder().setMessage("User by username: " + username + " doesn't exist.").build()).build(); } Map<String, String> defaultUserClaims = this.buildDefaultUserClaims(userInfo.getFirstname(), userInfo.getLastname(), userInfo.getEmailAddress()); if (StringUtils.isNotEmpty(userInfo.getPassword())) { userStoreManager.updateCredentialByAdmin(username, userInfo.getPassword()); log.debug("User credential of username: " + username + " has been changed"); } List<String> currentRoles = this.getFilteredRoles(userStoreManager, username); List<String> newRoles = new ArrayList<>(); if (userInfo.getRoles() != null) { newRoles = Arrays.asList(userInfo.getRoles()); } List<String> rolesToAdd = new ArrayList<>(newRoles); List<String> rolesToDelete = new ArrayList<>(); for (String role : currentRoles) { if (newRoles.contains(role)) { rolesToAdd.remove(role); } else { rolesToDelete.add(role); } } rolesToDelete.remove(ROLE_EVERYONE); rolesToAdd.remove(ROLE_EVERYONE); userStoreManager.updateRoleListOfUser(username, rolesToDelete.toArray(new String[rolesToDelete.size()]), rolesToAdd.toArray(new String[rolesToAdd.size()])); userStoreManager.setUserClaimValues(username, defaultUserClaims, null); if (log.isDebugEnabled()) { log.debug("User by username: " + username + " was successfully updated."); } BasicUserInfo updatedUserInfo = this.getBasicUserInfo(username); return Response.ok().entity(updatedUserInfo).build(); } catch (UserStoreException e) { String msg = "Error occurred while trying to update user '" + username + "'"; log.error(msg, e); return Response.serverError().entity( new ErrorResponse.ErrorResponseBuilder().setMessage(msg).build()).build(); } }  @POST @Override Response addUser(UserInfo userInfo); @GET @Path("/{username}") @Override Response getUser(@PathParam("username") String username, @QueryParam("domain") String domain,
                            @HeaderParam("If-Modified-Since") String ifModifiedSince); @PUT @Path("/{username}") @Override Response updateUser(@PathParam("username") String username, @QueryParam("domain") String domain, UserInfo userInfo); @DELETE @Path("/{username}") @Override Response removeUser(@PathParam("username") String username, @QueryParam("domain") String domain); @GET @Path("/{username}/roles") @Override Response getRolesOfUser(@PathParam("username") String username, @QueryParam("domain") String domain); @GET @Override Response getUsers(@QueryParam("filter") String filter, @HeaderParam("If-Modified-Since") String timestamp,
                             @QueryParam("offset") int offset,
                             @QueryParam("limit") int limit); @GET @Path("/count") @Override Response getUserCount(); @GET @Path("/checkUser") @Override Response isUserExists(@QueryParam("username") String userName); @GET @Path("/search/usernames") @Override Response getUserNames(@QueryParam("filter") String filter, @QueryParam("domain") String domain,
                                 @HeaderParam("If-Modified-Since") String timestamp,
                                 @QueryParam("offset") int offset, @QueryParam("limit") int limit); @PUT @Path("/credentials") @Override Response resetPassword(OldPasswordResetWrapper credentials); @POST @Path("/send-invitation") @Produces({MediaType.APPLICATION_JSON}) Response inviteExistingUsersToEnrollDevice(List<String> usernames); @POST @Path("/enrollment-invite") @Override Response inviteToEnrollDevice(EnrollmentInvitation enrollmentInvitation);  }
@Test(description = "This method tests the updateUser method of UserManagementService", dependsOnMethods = {"testGetUser"}) public void testUpdateUser() throws UserStoreException { PowerMockito.stub(PowerMockito.method(DeviceMgtAPIUtils.class, "getUserStoreManager")) .toReturn(this.userStoreManager); Response response = userManagementService.updateUser(TEST2_USERNAME, null, null); Assert.assertEquals(response.getStatus(), Response.Status.NOT_FOUND.getStatusCode(), "Non-existing user was successfully updated"); String[] roles = { "Internal/everyone", DEFAULT_DEVICE_USER }; Mockito.doReturn(roles).when(userStoreManager).getRoleListOfUser(TEST_USERNAME); Mockito.doNothing().when(userStoreManager).updateRoleListOfUser(Mockito.any(), Mockito.any(), Mockito.any()); Mockito.doNothing().when(userStoreManager).setUserClaimValues(Mockito.any(), Mockito.any(), Mockito.any()); response = userManagementService.updateUser(TEST_USERNAME, null, getUserInfo()); Assert.assertEquals(response.getStatus(), Response.Status.OK.getStatusCode(), "Updating the user info failed"); }