UserManagementServiceImpl implements UserManagementService { @Override @Transactional public boolean createUser(String userName, String password, boolean admin) { UserIndex userIndex = userService.getOrCreateUserForUsernameAndPassword(userName, password); if(userIndex == null) return false; if(admin) { User user = userIndex.getUser(); userService.enableAdminRoleForUser(user, false); disableOperatorRole(user); } log.info("User '{}' created successfully", userName); 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 testCreateUser() { UserIndex userIndex = mock(UserIndex.class); when(userService.getOrCreateUserForUsernameAndPassword("operator", "password")).thenReturn(userIndex); boolean success = service.createUser("operator", "password", false); assertTrue("Expecting user to be created successfully", success); verify(userService).getOrCreateUserForUsernameAndPassword("operator", "password"); verify(userService, times(0)).enableAdminRoleForUser(isA(User.class), isA(Boolean.class)); }
@Test public void testCreateAdmin() { UserIndex userIndex = mock(UserIndex.class); UserRole role = mock(UserRole.class); Set<UserRole> roles = new HashSet<UserRole>(); roles.add(role); when(userService.getOrCreateUserForUsernameAndPassword("admin", "password")).thenReturn(userIndex); when(userIndex.getUser()).thenReturn(user); when(authoritiesService.getUserRoleForName(StandardAuthoritiesService.USER)).thenReturn(role); when(user.getRoles()).thenReturn(roles); boolean success = service.createUser("admin", "password", true); assertTrue("Expecting user to be created successfully", success); verify(userService).getOrCreateUserForUsernameAndPassword("admin", "password"); verify(userService).enableAdminRoleForUser(user, false); verify(userDao).saveOrUpdateUser(user); }