@Test(expected = BusinessException.class) @Transactional public void testCreate_email_is_used() { User user = new User("testuser2", null, "admin@example.com", true, false, "Test", "User", null); user.setRoles(Collections.singleton(new Role(2))); userService.create(user, "password"); }
@Transactional @Override public User create(User user, String rawPassword) { Assert.notNull(user, "user must not be null"); String username = user.getUsername(); if (userRepository.exists(username)) { ResultMessages messages = ResultMessages.error() .add(MessageKeys.E_CT_US_8103, username); throw new BusinessException(messages); } String email = user.getEmail(); if (userRepository.countByEmail(email) > 0) { ResultMessages messages = ResultMessages.error() .add(MessageKeys.E_CT_US_8104, email); throw new BusinessException(messages); } checkRoles(user); String encodedPassword = passwordEncoder.encode(rawPassword); user.setPassword(encodedPassword); DateTime now = dateFactory.newDateTime(); user.setCreatedDate(now); user.setLastModifiedDate(now); userRepository.save(user); return user; }
UserServiceImpl implements UserService { @Transactional @Override public User create(User user, String rawPassword) { Assert.notNull(user, "user must not be null"); String username = user.getUsername(); if (userRepository.exists(username)) { ResultMessages messages = ResultMessages.error() .add(MessageKeys.E_CT_US_8103, username); throw new BusinessException(messages); } String email = user.getEmail(); if (userRepository.countByEmail(email) > 0) { ResultMessages messages = ResultMessages.error() .add(MessageKeys.E_CT_US_8104, email); throw new BusinessException(messages); } checkRoles(user); String encodedPassword = passwordEncoder.encode(rawPassword); user.setPassword(encodedPassword); DateTime now = dateFactory.newDateTime(); user.setCreatedDate(now); user.setLastModifiedDate(now); userRepository.save(user); return user; } }
UserServiceImpl implements UserService { @Transactional @Override public User create(User user, String rawPassword) { Assert.notNull(user, "user must not be null"); String username = user.getUsername(); if (userRepository.exists(username)) { ResultMessages messages = ResultMessages.error() .add(MessageKeys.E_CT_US_8103, username); throw new BusinessException(messages); } String email = user.getEmail(); if (userRepository.countByEmail(email) > 0) { ResultMessages messages = ResultMessages.error() .add(MessageKeys.E_CT_US_8104, email); throw new BusinessException(messages); } checkRoles(user); String encodedPassword = passwordEncoder.encode(rawPassword); user.setPassword(encodedPassword); DateTime now = dateFactory.newDateTime(); user.setCreatedDate(now); user.setLastModifiedDate(now); userRepository.save(user); return user; }  }
UserServiceImpl implements UserService { @Transactional @Override public User create(User user, String rawPassword) { Assert.notNull(user, "user must not be null"); String username = user.getUsername(); if (userRepository.exists(username)) { ResultMessages messages = ResultMessages.error() .add(MessageKeys.E_CT_US_8103, username); throw new BusinessException(messages); } String email = user.getEmail(); if (userRepository.countByEmail(email) > 0) { ResultMessages messages = ResultMessages.error() .add(MessageKeys.E_CT_US_8104, email); throw new BusinessException(messages); } checkRoles(user); String encodedPassword = passwordEncoder.encode(rawPassword); user.setPassword(encodedPassword); DateTime now = dateFactory.newDateTime(); user.setCreatedDate(now); user.setLastModifiedDate(now); userRepository.save(user); return user; }  @Override User findOne(String username); @Override User findOneByUsernameOrEmail(String usernameOrEmail); @Override Page<User> findPage(Pageable pageable); @Transactional @Override User create(User user, String rawPassword); @Transactional @Override User update(String username, User updatedUser,
                       String updatedRawPassword); @Transactional @Override User updateWithoutPassword(String username, User updatedUser); @Transactional @Override void delete(String username); }
UserServiceImpl implements UserService { @Transactional @Override public User create(User user, String rawPassword) { Assert.notNull(user, "user must not be null"); String username = user.getUsername(); if (userRepository.exists(username)) { ResultMessages messages = ResultMessages.error() .add(MessageKeys.E_CT_US_8103, username); throw new BusinessException(messages); } String email = user.getEmail(); if (userRepository.countByEmail(email) > 0) { ResultMessages messages = ResultMessages.error() .add(MessageKeys.E_CT_US_8104, email); throw new BusinessException(messages); } checkRoles(user); String encodedPassword = passwordEncoder.encode(rawPassword); user.setPassword(encodedPassword); DateTime now = dateFactory.newDateTime(); user.setCreatedDate(now); user.setLastModifiedDate(now); userRepository.save(user); return user; }  @Override User findOne(String username); @Override User findOneByUsernameOrEmail(String usernameOrEmail); @Override Page<User> findPage(Pageable pageable); @Transactional @Override User create(User user, String rawPassword); @Transactional @Override User update(String username, User updatedUser,
                       String updatedRawPassword); @Transactional @Override User updateWithoutPassword(String username, User updatedUser); @Transactional @Override void delete(String username);  }