UserManagementServiceImpl implements UserManagementService { @Override @Transactional public boolean deactivateUser(UserDetail userDetail) { User user = userService.getUserForId(userDetail.getId()); if(user == null) { log.info("User '{}' does not exist in the system", userDetail.getUsername()); return false; } for(Iterator<UserIndex> it = user.getUserIndices().iterator(); it.hasNext();) { UserIndex userIndex = it.next(); userDao.deleteUserIndex(userIndex); it.remove(); } userDao.saveOrUpdateUser(user); log.info("User '{}' deleted 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 testDeactivateNonExistingUser() { UserDetail userDetail = mock(UserDetail.class); when(userDetail.getId()).thenReturn(1); when(userDetail.getUsername()).thenReturn("admin2"); when(userDao.getUserForId(1)).thenReturn(null); boolean success = service.deactivateUser(userDetail); assertFalse("Unable to delete non existing user", success); verify(userDao, times(0)).deleteUserIndex(isA(UserIndex.class)); verify(userDao, times(0)).saveOrUpdateUser(user); }
@Test public void testDeactivateUser() { Integer userId = 1; UserDetail userDetail = mock(UserDetail.class); buildUserDetail(userId, userDetail, "password"); UserIndex userIndex = mock(UserIndex.class); Set<UserIndex> userIndices = new HashSet<UserIndex>(); userIndices.add(userIndex); when(userService.getUserForId(userId)).thenReturn(user); when(user.getUserIndices()).thenReturn(userIndices); boolean success = service.deactivateUser(userDetail); assertTrue("User deactivated successfully", success); verify(userDao).deleteUserIndex(isA(UserIndex.class)); verify(userDao).saveOrUpdateUser(user); }