@Test public void requestsPasswordReset() throws Exception { PasswordResetToken token = token(); given(passwordResetTokenRepository.findOneByUserUsername(token.getUser().getUsername())).willReturn(Optional.empty()); given(passwordResetTokenRepository.save((PasswordResetToken) notNull())).willReturn(token); given(passwordResetTokenGenerator.generateToken(token.getUser())).willReturn(token); given(passwordResetNotifier.sendPasswordResetNotification(token.getUser(), token.getToken())).willReturn(Try.success(ImmutableEmailResponse.builder().withMessage("message").build())); given(passwordResetService.resetPassword(token.getUser())).willReturn(ImmutableGeneratePasswordResetResponse.builder().withPassword(token.getToken()).build()); given(userService.getByUsername(token.getUser().getUsername())).willReturn(Try.success(token.getUser())); PasswordResetRequest request = ImmutablePasswordResetRequest.builder().withUsername(token.getUser().getUsername()).build(); Try<RequestPasswordResetResponse> uname = underTest.requestPasswordReset(request); assertThat(uname.get().username(), equalTo(token.getUser().getUsername())); verify(passwordResetNotifier, times(1)).sendPasswordResetNotification(token.getUser(), token.getToken()); }
@Transactional public Try<RequestPasswordResetResponse> requestPasswordReset(PasswordResetRequest request) { return userService .getByUsername(request.username()) .flatMap(user -> { PasswordResetToken resetToken = passwordResetTokenGenerator.generateToken(user); return passwordResetNotifier .sendPasswordResetNotification(user, resetToken.getToken()) .map(emailResponse -> { persistToken(user, resetToken); return ImmutableRequestPasswordResetResponse.builder().withUsername(user.getUsername()).build(); }); }); }
RequestPasswordResetService { @Transactional public Try<RequestPasswordResetResponse> requestPasswordReset(PasswordResetRequest request) { return userService .getByUsername(request.username()) .flatMap(user -> { PasswordResetToken resetToken = passwordResetTokenGenerator.generateToken(user); return passwordResetNotifier .sendPasswordResetNotification(user, resetToken.getToken()) .map(emailResponse -> { persistToken(user, resetToken); return ImmutableRequestPasswordResetResponse.builder().withUsername(user.getUsername()).build(); }); }); } }
RequestPasswordResetService { @Transactional public Try<RequestPasswordResetResponse> requestPasswordReset(PasswordResetRequest request) { return userService .getByUsername(request.username()) .flatMap(user -> { PasswordResetToken resetToken = passwordResetTokenGenerator.generateToken(user); return passwordResetNotifier .sendPasswordResetNotification(user, resetToken.getToken()) .map(emailResponse -> { persistToken(user, resetToken); return ImmutableRequestPasswordResetResponse.builder().withUsername(user.getUsername()).build(); }); }); } @Autowired  RequestPasswordResetService(UserService userService,
                                       PasswordResetNotifier passwordResetNotifier,
                                       PasswordResetTokenGenerator passwordResetToken,
                                       PasswordResetTokenRepository passwordResetTokenRepository,
                                       PasswordResetService passwordResetService); }
RequestPasswordResetService { @Transactional public Try<RequestPasswordResetResponse> requestPasswordReset(PasswordResetRequest request) { return userService .getByUsername(request.username()) .flatMap(user -> { PasswordResetToken resetToken = passwordResetTokenGenerator.generateToken(user); return passwordResetNotifier .sendPasswordResetNotification(user, resetToken.getToken()) .map(emailResponse -> { persistToken(user, resetToken); return ImmutableRequestPasswordResetResponse.builder().withUsername(user.getUsername()).build(); }); }); } @Autowired  RequestPasswordResetService(UserService userService,
                                       PasswordResetNotifier passwordResetNotifier,
                                       PasswordResetTokenGenerator passwordResetToken,
                                       PasswordResetTokenRepository passwordResetTokenRepository,
                                       PasswordResetService passwordResetService); @Transactional Try<RequestPasswordResetResponse> requestPasswordReset(PasswordResetRequest request); @Transactional Try<GeneratePasswordResetResponse> generatePasswordFromToken(GeneratePasswordFromTokenRequest request); }
RequestPasswordResetService { @Transactional public Try<RequestPasswordResetResponse> requestPasswordReset(PasswordResetRequest request) { return userService .getByUsername(request.username()) .flatMap(user -> { PasswordResetToken resetToken = passwordResetTokenGenerator.generateToken(user); return passwordResetNotifier .sendPasswordResetNotification(user, resetToken.getToken()) .map(emailResponse -> { persistToken(user, resetToken); return ImmutableRequestPasswordResetResponse.builder().withUsername(user.getUsername()).build(); }); }); } @Autowired  RequestPasswordResetService(UserService userService,
                                       PasswordResetNotifier passwordResetNotifier,
                                       PasswordResetTokenGenerator passwordResetToken,
                                       PasswordResetTokenRepository passwordResetTokenRepository,
                                       PasswordResetService passwordResetService); @Transactional Try<RequestPasswordResetResponse> requestPasswordReset(PasswordResetRequest request); @Transactional Try<GeneratePasswordResetResponse> generatePasswordFromToken(GeneratePasswordFromTokenRequest request);  }