UserManagerImpl extends GenericManagerImpl<User, Long> implements UserManager, UserService { @Override public User saveUser(final User user) throws UserExistsException { if (user.getVersion() == null) { user.setUsername(user.getUsername().toLowerCase()); } boolean passwordChanged = false; if (passwordEncoder != null) { if (user.getVersion() == null) { passwordChanged = true; } else { final String currentPassword = userDao.getUserPassword(user.getId()); if (currentPassword == null) { passwordChanged = true; } else { if (!currentPassword.equals(user.getPassword())) { passwordChanged = true; } } } if (passwordChanged) { user.setPassword(passwordEncoder.encode(user.getPassword())); } } else { log.warn("PasswordEncoder not set, skipping password encryption..."); } try { return userDao.saveUser(user); } catch (final Exception e) { e.printStackTrace(); log.warn(e.getMessage()); throw new UserExistsException("User '" + user.getUsername() + "' already exists!"); } }  @Autowired @Qualifier("passwordEncoder") void setPasswordEncoder(final PasswordEncoder passwordEncoder); @Override @Autowired void setUserDao(final UserDao userDao); @Autowired(required = false) void setMailEngine(final MailEngine mailEngine); @Autowired(required = false) void setMailMessage(final SimpleMailMessage message); @Autowired(required = false) void setPasswordTokenManager(final PasswordTokenManager passwordTokenManager); void setPasswordRecoveryTemplate(final String passwordRecoveryTemplate); void setPasswordUpdatedTemplate(final String passwordUpdatedTemplate); @Override User getUser(final String userId); @Override List<User> getUsers(); @Override User saveUser(final User user); @Override void removeUser(final User user); @Override void removeUser(final String userId); @Override User getUserByUsername(final String username); @Override List<User> search(final String searchTerm); @Override String buildRecoveryPasswordUrl(final User user, final String urlTemplate); @Override String generateRecoveryToken(final User user); @Override boolean isRecoveryTokenValid(final String username, final String token); @Override boolean isRecoveryTokenValid(final User user, final String token); @Override void sendPasswordRecoveryEmail(final String username, final String urlTemplate); @Override User updatePassword(final String username, final String currentPassword, final String recoveryToken, final String newPassword, final String applicationUrl);  }
@Test public void testSaveUser() throws Exception { final User testData = new User("1"); testData.getRoles().add(new Role("user")); given(userDao.get(1L)).willReturn(testData); final User user = userManager.getUser("1"); user.setPhoneNumber("303-555-1212"); given(userDao.saveUser(user)).willReturn(user); User returned = userManager.saveUser(user); assertTrue(returned.getPhoneNumber().equals("303-555-1212")); assertTrue(returned.getRoles().size() == 1); }
@Test public void testUserExistsException() { final User user = new User("admin"); user.setEmail("matt@raibledesigns.com"); willThrow(new DataIntegrityViolationException("")).given(userDao).saveUser(user); try { userManager.saveUser(user); fail("Expected UserExistsException not thrown"); } catch (UserExistsException e) { log.debug("expected exception: " + e.getMessage()); assertNotNull(e); } }