@Test public void updatesTheUser() throws Exception { User user = user() .withFirstName("anil") .withLastName("kumar") .withUsername("anil@kumar.com") .withWriteAccess(false) .build(); User admin = user().withRole(Role.ADMIN).build(); UpdateUserRequest request = new UpdateUserRequest("john", "smith", user.getUsername(), "", "", true); given(userRepository.findOne(user.getId())).willReturn(user); given(userRepository.findOneByUsername(user.getUsername())).willReturn(Optional.empty()); ArgumentCaptor<User> savedCaptor = ArgumentCaptor.forClass(User.class); underTest.update(admin, user.getId(), request); verify(userRepository).save(savedCaptor.capture()); User saved = savedCaptor.getValue(); assertThat(saved.getFirstName(), is(request.getFirstName())); assertThat(saved.getLastName(), is(request.getLastName())); assertThat(saved.getUsername(), is(request.getUsername())); assertThat(saved.getWriteAccess(), is(request.getWriteAccess())); }
@Transactional public Try<User> update(User user, UUID userToUpdate, UpdateUserRequest request) { if (!user.isAdmin()) { log.warn("Non admin tried to update a user!. User={}, userToUpdate={}", user, userToUpdate); return Try.failure(new NotAuthorizedFailure("Not authorized")); } User foundUser = userRepository.findOne(userToUpdate); if (foundUser == null) { return Try.failure(new NotFoundFailure("No user with ID=" + userToUpdate)); } Optional<User> userByNewEmail = userRepository.findOneByUsername(request.getUsername()); if (userByNewEmail.isPresent() && userByNewEmail.get().getId() != foundUser.getId()) { return Try.failure(new BadRequestFailure("Username already exists")); } foundUser.setFirstName(request.getFirstName()); foundUser.setLastName(request.getLastName()); foundUser.setUsername(request.getUsername()); foundUser.setWriteAccess(request.getWriteAccess()); if (request.getPassword() != null && Objects.equals(request.getPassword(), request.getRepeatPassword())) { foundUser.setPasswordHash(hashPw(request.getPassword())); } User updatedUser = userRepository.save(foundUser); log.debug("Updated user={}", updatedUser); return Try.success(updatedUser); }
UserService { @Transactional public Try<User> update(User user, UUID userToUpdate, UpdateUserRequest request) { if (!user.isAdmin()) { log.warn("Non admin tried to update a user!. User={}, userToUpdate={}", user, userToUpdate); return Try.failure(new NotAuthorizedFailure("Not authorized")); } User foundUser = userRepository.findOne(userToUpdate); if (foundUser == null) { return Try.failure(new NotFoundFailure("No user with ID=" + userToUpdate)); } Optional<User> userByNewEmail = userRepository.findOneByUsername(request.getUsername()); if (userByNewEmail.isPresent() && userByNewEmail.get().getId() != foundUser.getId()) { return Try.failure(new BadRequestFailure("Username already exists")); } foundUser.setFirstName(request.getFirstName()); foundUser.setLastName(request.getLastName()); foundUser.setUsername(request.getUsername()); foundUser.setWriteAccess(request.getWriteAccess()); if (request.getPassword() != null && Objects.equals(request.getPassword(), request.getRepeatPassword())) { foundUser.setPasswordHash(hashPw(request.getPassword())); } User updatedUser = userRepository.save(foundUser); log.debug("Updated user={}", updatedUser); return Try.success(updatedUser); } }
UserService { @Transactional public Try<User> update(User user, UUID userToUpdate, UpdateUserRequest request) { if (!user.isAdmin()) { log.warn("Non admin tried to update a user!. User={}, userToUpdate={}", user, userToUpdate); return Try.failure(new NotAuthorizedFailure("Not authorized")); } User foundUser = userRepository.findOne(userToUpdate); if (foundUser == null) { return Try.failure(new NotFoundFailure("No user with ID=" + userToUpdate)); } Optional<User> userByNewEmail = userRepository.findOneByUsername(request.getUsername()); if (userByNewEmail.isPresent() && userByNewEmail.get().getId() != foundUser.getId()) { return Try.failure(new BadRequestFailure("Username already exists")); } foundUser.setFirstName(request.getFirstName()); foundUser.setLastName(request.getLastName()); foundUser.setUsername(request.getUsername()); foundUser.setWriteAccess(request.getWriteAccess()); if (request.getPassword() != null && Objects.equals(request.getPassword(), request.getRepeatPassword())) { foundUser.setPasswordHash(hashPw(request.getPassword())); } User updatedUser = userRepository.save(foundUser); log.debug("Updated user={}", updatedUser); return Try.success(updatedUser); } @Autowired  UserService(UserRepository userRepository,
                       AllUsersQueryConverter converter,
                       NewAccountNotifier newAccountNotifier); }
UserService { @Transactional public Try<User> update(User user, UUID userToUpdate, UpdateUserRequest request) { if (!user.isAdmin()) { log.warn("Non admin tried to update a user!. User={}, userToUpdate={}", user, userToUpdate); return Try.failure(new NotAuthorizedFailure("Not authorized")); } User foundUser = userRepository.findOne(userToUpdate); if (foundUser == null) { return Try.failure(new NotFoundFailure("No user with ID=" + userToUpdate)); } Optional<User> userByNewEmail = userRepository.findOneByUsername(request.getUsername()); if (userByNewEmail.isPresent() && userByNewEmail.get().getId() != foundUser.getId()) { return Try.failure(new BadRequestFailure("Username already exists")); } foundUser.setFirstName(request.getFirstName()); foundUser.setLastName(request.getLastName()); foundUser.setUsername(request.getUsername()); foundUser.setWriteAccess(request.getWriteAccess()); if (request.getPassword() != null && Objects.equals(request.getPassword(), request.getRepeatPassword())) { foundUser.setPasswordHash(hashPw(request.getPassword())); } User updatedUser = userRepository.save(foundUser); log.debug("Updated user={}", updatedUser); return Try.success(updatedUser); } @Autowired  UserService(UserRepository userRepository,
                       AllUsersQueryConverter converter,
                       NewAccountNotifier newAccountNotifier); Try<CurrentUser> testRole(User user, String role); @Transactional(readOnly = true) Try<User> getByUsername(String email); @Transactional(readOnly = true) Try<Collection<UserSummary>> getAll(User user); @Transactional(readOnly = true) Try<User> getByID(User user, UUID id); @Transactional Try<Void> delete(User user, UUID userToDelete); @Transactional Try<User> update(User user, UUID userToUpdate, UpdateUserRequest request); @Transactional Try<User> create(User user, CreateUserRequest request); }
UserService { @Transactional public Try<User> update(User user, UUID userToUpdate, UpdateUserRequest request) { if (!user.isAdmin()) { log.warn("Non admin tried to update a user!. User={}, userToUpdate={}", user, userToUpdate); return Try.failure(new NotAuthorizedFailure("Not authorized")); } User foundUser = userRepository.findOne(userToUpdate); if (foundUser == null) { return Try.failure(new NotFoundFailure("No user with ID=" + userToUpdate)); } Optional<User> userByNewEmail = userRepository.findOneByUsername(request.getUsername()); if (userByNewEmail.isPresent() && userByNewEmail.get().getId() != foundUser.getId()) { return Try.failure(new BadRequestFailure("Username already exists")); } foundUser.setFirstName(request.getFirstName()); foundUser.setLastName(request.getLastName()); foundUser.setUsername(request.getUsername()); foundUser.setWriteAccess(request.getWriteAccess()); if (request.getPassword() != null && Objects.equals(request.getPassword(), request.getRepeatPassword())) { foundUser.setPasswordHash(hashPw(request.getPassword())); } User updatedUser = userRepository.save(foundUser); log.debug("Updated user={}", updatedUser); return Try.success(updatedUser); } @Autowired  UserService(UserRepository userRepository,
                       AllUsersQueryConverter converter,
                       NewAccountNotifier newAccountNotifier); Try<CurrentUser> testRole(User user, String role); @Transactional(readOnly = true) Try<User> getByUsername(String email); @Transactional(readOnly = true) Try<Collection<UserSummary>> getAll(User user); @Transactional(readOnly = true) Try<User> getByID(User user, UUID id); @Transactional Try<Void> delete(User user, UUID userToDelete); @Transactional Try<User> update(User user, UUID userToUpdate, UpdateUserRequest request); @Transactional Try<User> create(User user, CreateUserRequest request);  }