@Test public void testUpdateUser() throws Exception { alice.setUsername("bob"); alice.setEmail("new@email.com"); alice.setAccountNonExpired(false); alice.setAccountNonLocked(false); alice.setCredentialsNonExpired(false); alice.setEnabled(false); alice.setRememberMeToken("rememberme"); alice.setPassword("newpassword"); alice.getAuthorities().add(new AuthorityApiData("admin", "")); alice.setRegistrationConfirmationToken("ignored"); alice.setRegistrationDate(LocalDate.MIN); alice.setLastLogin(LocalDateTime.MIN); alice.getResource().getUpdateResource().update(); alice = service() .users() .search() .findByUsername("bob") .getData().stream().findFirst().get() .getResource() .useProjection(UserProjections.FULL_DATA) .read(); assertThat(alice.getUsername(), is("bob")); assertThat(alice.getEmail(), is("new@email.com")); assertThat(alice.isAccountNonExpired(), is(false)); assertThat(alice.isAccountNonLocked(), is(false)); assertThat(alice.isCredentialsNonExpired(), is(false)); assertThat(alice.isEnabled(), is(false)); assertThat(alice.getRememberMeToken(), is("rememberme")); assertThat(alice.getAuthorities(), hasSize(1)); assertThat(alice.getPassword(), is(not(alicePasswordHash))); assertThat(alice.getPassword(), is(notNullValue())); assertThat(alice.getRegistrationConfirmationToken(), is(nullValue())); assertThat(alice.getRegistrationDate().getYear(), is(LocalDate.now().getYear())); assertThat(alice.getLastLogin(), is(nullValue())); }
@RequestMapping(value = "{id}", method = RequestMethod.PUT) public ResponseEntity<User> updateUser(@PathVariable Long id, @RequestBody final UserApiData user) { Preconditions.checkNotNull(user); final User userFromDB = loadUpdatedUserFromDatabase(id); if (usernameUpdateRequested(user, userFromDB)) { if (user.getUsername() != null && usernameIsAvailable(user)) { userFromDB.setUsername(user.getUsername()); } else { throw new UsernameAlreadyInUseException(); } } userFromDB.setAccountNonExpired(user.isAccountNonExpired()); userFromDB.setAccountNonLocked(user.isAccountNonLocked()); userFromDB.setCredentialsNonExpired(user.isCredentialsNonExpired()); userFromDB.setEmail(user.getEmail()); userFromDB.setEnabled(user.isEnabled()); userFromDB.setRememberMeToken(user.getRememberMeToken()); userFromDB.getAuthorities().clear(); userFromDB.getAuthorities().addAll( user.getAuthorities() .stream() .map(authority -> authorityService.findByAuthority(authority.getAuthority())) .filter(authority -> authority != null) .distinct() .collect(Collectors.toList())); if (!isNullOrEmpty(firstNonNull(user.getPassword(), ""))) { userFromDB.setUnencryptedPassword(user.getPassword()); } userFromDB.setLastModified(LocalDateTime.now()); return new ResponseEntity<>(userService.save(userFromDB), OK); }
UpdateUserRestController { @RequestMapping(value = "{id}", method = RequestMethod.PUT) public ResponseEntity<User> updateUser(@PathVariable Long id, @RequestBody final UserApiData user) { Preconditions.checkNotNull(user); final User userFromDB = loadUpdatedUserFromDatabase(id); if (usernameUpdateRequested(user, userFromDB)) { if (user.getUsername() != null && usernameIsAvailable(user)) { userFromDB.setUsername(user.getUsername()); } else { throw new UsernameAlreadyInUseException(); } } userFromDB.setAccountNonExpired(user.isAccountNonExpired()); userFromDB.setAccountNonLocked(user.isAccountNonLocked()); userFromDB.setCredentialsNonExpired(user.isCredentialsNonExpired()); userFromDB.setEmail(user.getEmail()); userFromDB.setEnabled(user.isEnabled()); userFromDB.setRememberMeToken(user.getRememberMeToken()); userFromDB.getAuthorities().clear(); userFromDB.getAuthorities().addAll( user.getAuthorities() .stream() .map(authority -> authorityService.findByAuthority(authority.getAuthority())) .filter(authority -> authority != null) .distinct() .collect(Collectors.toList())); if (!isNullOrEmpty(firstNonNull(user.getPassword(), ""))) { userFromDB.setUnencryptedPassword(user.getPassword()); } userFromDB.setLastModified(LocalDateTime.now()); return new ResponseEntity<>(userService.save(userFromDB), OK); } }
UpdateUserRestController { @RequestMapping(value = "{id}", method = RequestMethod.PUT) public ResponseEntity<User> updateUser(@PathVariable Long id, @RequestBody final UserApiData user) { Preconditions.checkNotNull(user); final User userFromDB = loadUpdatedUserFromDatabase(id); if (usernameUpdateRequested(user, userFromDB)) { if (user.getUsername() != null && usernameIsAvailable(user)) { userFromDB.setUsername(user.getUsername()); } else { throw new UsernameAlreadyInUseException(); } } userFromDB.setAccountNonExpired(user.isAccountNonExpired()); userFromDB.setAccountNonLocked(user.isAccountNonLocked()); userFromDB.setCredentialsNonExpired(user.isCredentialsNonExpired()); userFromDB.setEmail(user.getEmail()); userFromDB.setEnabled(user.isEnabled()); userFromDB.setRememberMeToken(user.getRememberMeToken()); userFromDB.getAuthorities().clear(); userFromDB.getAuthorities().addAll( user.getAuthorities() .stream() .map(authority -> authorityService.findByAuthority(authority.getAuthority())) .filter(authority -> authority != null) .distinct() .collect(Collectors.toList())); if (!isNullOrEmpty(firstNonNull(user.getPassword(), ""))) { userFromDB.setUnencryptedPassword(user.getPassword()); } userFromDB.setLastModified(LocalDateTime.now()); return new ResponseEntity<>(userService.save(userFromDB), OK); }  }
UpdateUserRestController { @RequestMapping(value = "{id}", method = RequestMethod.PUT) public ResponseEntity<User> updateUser(@PathVariable Long id, @RequestBody final UserApiData user) { Preconditions.checkNotNull(user); final User userFromDB = loadUpdatedUserFromDatabase(id); if (usernameUpdateRequested(user, userFromDB)) { if (user.getUsername() != null && usernameIsAvailable(user)) { userFromDB.setUsername(user.getUsername()); } else { throw new UsernameAlreadyInUseException(); } } userFromDB.setAccountNonExpired(user.isAccountNonExpired()); userFromDB.setAccountNonLocked(user.isAccountNonLocked()); userFromDB.setCredentialsNonExpired(user.isCredentialsNonExpired()); userFromDB.setEmail(user.getEmail()); userFromDB.setEnabled(user.isEnabled()); userFromDB.setRememberMeToken(user.getRememberMeToken()); userFromDB.getAuthorities().clear(); userFromDB.getAuthorities().addAll( user.getAuthorities() .stream() .map(authority -> authorityService.findByAuthority(authority.getAuthority())) .filter(authority -> authority != null) .distinct() .collect(Collectors.toList())); if (!isNullOrEmpty(firstNonNull(user.getPassword(), ""))) { userFromDB.setUnencryptedPassword(user.getPassword()); } userFromDB.setLastModified(LocalDateTime.now()); return new ResponseEntity<>(userService.save(userFromDB), OK); }  @RequestMapping(value = "{id}", method = RequestMethod.PUT) ResponseEntity<User> updateUser(@PathVariable Long id, @RequestBody final UserApiData user); }
UpdateUserRestController { @RequestMapping(value = "{id}", method = RequestMethod.PUT) public ResponseEntity<User> updateUser(@PathVariable Long id, @RequestBody final UserApiData user) { Preconditions.checkNotNull(user); final User userFromDB = loadUpdatedUserFromDatabase(id); if (usernameUpdateRequested(user, userFromDB)) { if (user.getUsername() != null && usernameIsAvailable(user)) { userFromDB.setUsername(user.getUsername()); } else { throw new UsernameAlreadyInUseException(); } } userFromDB.setAccountNonExpired(user.isAccountNonExpired()); userFromDB.setAccountNonLocked(user.isAccountNonLocked()); userFromDB.setCredentialsNonExpired(user.isCredentialsNonExpired()); userFromDB.setEmail(user.getEmail()); userFromDB.setEnabled(user.isEnabled()); userFromDB.setRememberMeToken(user.getRememberMeToken()); userFromDB.getAuthorities().clear(); userFromDB.getAuthorities().addAll( user.getAuthorities() .stream() .map(authority -> authorityService.findByAuthority(authority.getAuthority())) .filter(authority -> authority != null) .distinct() .collect(Collectors.toList())); if (!isNullOrEmpty(firstNonNull(user.getPassword(), ""))) { userFromDB.setUnencryptedPassword(user.getPassword()); } userFromDB.setLastModified(LocalDateTime.now()); return new ResponseEntity<>(userService.save(userFromDB), OK); }  @RequestMapping(value = "{id}", method = RequestMethod.PUT) ResponseEntity<User> updateUser(@PathVariable Long id, @RequestBody final UserApiData user);  }