@Test public void returnsNotFoundErrorIfUserDoesNotExist() throws Exception { String username = "idontexist@nothing.me"; given(userService.getByUsername(username)).willReturn(Try.failure(new NotFoundFailure("No user with name"))); PasswordResetRequest request = ImmutablePasswordResetRequest.builder().withUsername(username).build(); Try<RequestPasswordResetResponse> uname = underTest.requestPasswordReset(request); assertThat(uname, isFailure(instanceOf(NotFoundFailure.class))); verifyZeroInteractions(passwordResetNotifier); }
@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);  }