UserManagementServiceImpl implements UserManagementService { @Override @Transactional public boolean updateUser(UserDetail userDetail) { User user = userService.getUserForId(userDetail.getId()); if(user == null) { log.info("User '{}' does not exist in the system", userDetail.getUsername()); return false; } if(StringUtils.isNotBlank(userDetail.getPassword())) { String credentials = passwordEncoder.encodePassword(userDetail.getPassword(), userDetail.getUsername()); for(UserIndex userIndex : user.getUserIndices()) { userIndex.setCredentials(credentials); } } updateRole(userDetail.getRole(), user); userDao.saveOrUpdateUser(user); log.info("User '{}' updated successfully", userDetail.getUsername()); return true; }  @Override @SuppressWarnings("unchecked") @Transactional(readOnly=true) List<String> getUserNames(final String searchString); @Override @Transactional(readOnly=true) Integer getUserDetailsCount(); @Override @Transactional(readOnly=true) List<UserDetail> getUserDetails(final int start, final int maxResults); @Override @Transactional(readOnly=true) List<UserDetail> getActiveUsersDetails(); @Override @Transactional(readOnly=true) List<UserDetail> getInactiveUsersDetails(); @Override @Transactional(readOnly=true) UserDetail getUserDetail(final String userName); @Override @Transactional void disableOperatorRole(User user); @Override @Transactional boolean createUser(String userName, String password, boolean admin); boolean createUser(String userName, String password, String role); @Override @Transactional boolean updateUser(UserDetail userDetail); @Override @Transactional boolean inactivateUser(UserDetail userDetail); @Override @Transactional boolean activateUser(UserDetail userDetail); @Override @Transactional boolean deactivateUser(UserDetail userDetail); @Autowired void setSesssionFactory(SessionFactory sessionFactory); @Autowired void setAuthoritiesService(StandardAuthoritiesService authoritiesService); @Autowired void setUserDao(UserDao userDao); @Autowired void setUserService(UserService userService); @Autowired void setPasswordEncoder(PasswordEncoder passwordEncoder); @Override List<String> getAllRoleNames(); @Override List<String> getManagedRoleNames(); @Autowired void setUserPropertiesService(UserPropertiesService userPropertiesService);  }
@Test public void testUpdateNonExistingUser() { UserDetail userDetail = mock(UserDetail.class); when(userDetail.getId()).thenReturn(1); when(userDetail.getUsername()).thenReturn("admin2"); when(userDao.getUserForId(1)).thenReturn(null); boolean success = service.updateUser(userDetail); assertFalse("Unable to update non existing user", success); verify(passwordEncoder, times(0)).encodePassword("password", "admin2"); verify(userDao, times(0)).saveOrUpdateUser(user); }
@Test public void testUpdateUser() { String credentials = "encryptedPassword"; Integer userId = 1; UserDetail userDetail = mock(UserDetail.class); UserIndex userIndex = mock(UserIndex.class); UserRole userRole = mock(UserRole.class); UserRole adminRole = mock(UserRole.class); Set<UserIndex> userIndices = new HashSet<UserIndex>(); userIndices.add(userIndex); Set<UserRole> userRoles = new HashSet<UserRole>(); userRoles.add(userRole); when(userRole.getName()).thenReturn("ROLE_USER"); buildUserDetail(userId, userDetail, "password"); when(userService.getUserForId(userId)).thenReturn(user); when(passwordEncoder.encodePassword("password", "admin")).thenReturn(credentials); when(user.getUserIndices()).thenReturn(userIndices); when(user.getRoles()).thenReturn(userRoles); when(authoritiesService.getUserRoleForName("ROLE_ADMINISTRATOR")).thenReturn(adminRole); boolean success = service.updateUser(userDetail); assertTrue("User updated successfully", success); verify(passwordEncoder).encodePassword("password", "admin"); verify(authoritiesService).getUserRoleForName("ROLE_ADMINISTRATOR"); verify(userDao).saveOrUpdateUser(user); }