@Test public void testSaveUser() throws Exception { final User testData = new User("1"); testData.getRoles().add(new Role("user")); context.checking(new Expectations() {{ one(userDao).get(with(equal(1))); will(returnValue(testData)); }}); final User user = userManager.getUser("1"); user.setPhoneNumber("303-555-1212"); context.checking(new Expectations() {{ one(userDao).saveUser(with(same(user))); will(returnValue(user)); }}); User returned = userManager.saveUser(user); assertTrue(returned.getPhoneNumber().equals("303-555-1212")); assertTrue(returned.getRoles().size() == 1); }
public User saveUser(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 { String currentPassword = dao.getUserPassword(user.getUsername()); if (currentPassword == null) { passwordChanged = true; } else { if (!currentPassword.equals(user.getPassword())) { passwordChanged = true; } } } if (passwordChanged) { user.setPassword(passwordEncoder.encodePassword(user.getPassword(), null)); } } else { log.warn("PasswordEncoder not set, skipping password encryption..."); } try { return dao.saveUser(user); } catch (DataIntegrityViolationException e) { e.printStackTrace(); log.warn(e.getMessage()); throw new UserExistsException("User '" + user.getUsername() + "' already exists!"); } catch (EntityExistsException e) { e.printStackTrace(); log.warn(e.getMessage()); throw new UserExistsException("User '" + user.getUsername() + "' already exists!"); } }
UserManagerImpl extends UniversalManagerImpl implements UserManager, UserService { public User saveUser(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 { String currentPassword = dao.getUserPassword(user.getUsername()); if (currentPassword == null) { passwordChanged = true; } else { if (!currentPassword.equals(user.getPassword())) { passwordChanged = true; } } } if (passwordChanged) { user.setPassword(passwordEncoder.encodePassword(user.getPassword(), null)); } } else { log.warn("PasswordEncoder not set, skipping password encryption..."); } try { return dao.saveUser(user); } catch (DataIntegrityViolationException e) { e.printStackTrace(); log.warn(e.getMessage()); throw new UserExistsException("User '" + user.getUsername() + "' already exists!"); } catch (EntityExistsException e) { e.printStackTrace(); log.warn(e.getMessage()); throw new UserExistsException("User '" + user.getUsername() + "' already exists!"); } } }
UserManagerImpl extends UniversalManagerImpl implements UserManager, UserService { public User saveUser(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 { String currentPassword = dao.getUserPassword(user.getUsername()); if (currentPassword == null) { passwordChanged = true; } else { if (!currentPassword.equals(user.getPassword())) { passwordChanged = true; } } } if (passwordChanged) { user.setPassword(passwordEncoder.encodePassword(user.getPassword(), null)); } } else { log.warn("PasswordEncoder not set, skipping password encryption..."); } try { return dao.saveUser(user); } catch (DataIntegrityViolationException e) { e.printStackTrace(); log.warn(e.getMessage()); throw new UserExistsException("User '" + user.getUsername() + "' already exists!"); } catch (EntityExistsException e) { e.printStackTrace(); log.warn(e.getMessage()); throw new UserExistsException("User '" + user.getUsername() + "' already exists!"); } }  }
UserManagerImpl extends UniversalManagerImpl implements UserManager, UserService { public User saveUser(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 { String currentPassword = dao.getUserPassword(user.getUsername()); if (currentPassword == null) { passwordChanged = true; } else { if (!currentPassword.equals(user.getPassword())) { passwordChanged = true; } } } if (passwordChanged) { user.setPassword(passwordEncoder.encodePassword(user.getPassword(), null)); } } else { log.warn("PasswordEncoder not set, skipping password encryption..."); } try { return dao.saveUser(user); } catch (DataIntegrityViolationException e) { e.printStackTrace(); log.warn(e.getMessage()); throw new UserExistsException("User '" + user.getUsername() + "' already exists!"); } catch (EntityExistsException e) { e.printStackTrace(); log.warn(e.getMessage()); throw new UserExistsException("User '" + user.getUsername() + "' already exists!"); } }  @Required void setPasswordEncoder(PasswordEncoder passwordEncoder); User getUser(String userId); List<User> getUsers(User user); String createSession(User user); User authenticate(String username, String password); User authenticate(String sessionKey); void logout(UserSession userSession); User saveUser(User user); void removeUser(String userId); User getUserByUsername(String username); UserSessionDao getUserSessionDao(); void setUserSessionDao(UserSessionDao userSessionDao); @Required void setUserDao(UserDao dao); }
UserManagerImpl extends UniversalManagerImpl implements UserManager, UserService { public User saveUser(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 { String currentPassword = dao.getUserPassword(user.getUsername()); if (currentPassword == null) { passwordChanged = true; } else { if (!currentPassword.equals(user.getPassword())) { passwordChanged = true; } } } if (passwordChanged) { user.setPassword(passwordEncoder.encodePassword(user.getPassword(), null)); } } else { log.warn("PasswordEncoder not set, skipping password encryption..."); } try { return dao.saveUser(user); } catch (DataIntegrityViolationException e) { e.printStackTrace(); log.warn(e.getMessage()); throw new UserExistsException("User '" + user.getUsername() + "' already exists!"); } catch (EntityExistsException e) { e.printStackTrace(); log.warn(e.getMessage()); throw new UserExistsException("User '" + user.getUsername() + "' already exists!"); } }  @Required void setPasswordEncoder(PasswordEncoder passwordEncoder); User getUser(String userId); List<User> getUsers(User user); String createSession(User user); User authenticate(String username, String password); User authenticate(String sessionKey); void logout(UserSession userSession); User saveUser(User user); void removeUser(String userId); User getUserByUsername(String username); UserSessionDao getUserSessionDao(); void setUserSessionDao(UserSessionDao userSessionDao); @Required void setUserDao(UserDao dao);  }