@Test(expected = UsernameNotExistException.class) public void testCreatePasswordResetToken_Throws_Exception_When_Username_Not_Exist() throws UsernameNotExistException, EmailAddressNotExistException, MessagingException { when(usersRepository.loadUserByUsername(anyString())).thenThrow( new UsernameNotFoundException("The Message")); final String resetPasswordLinkPlaceHolder = "http: final String emailAddress = "emailAddress"; sut.createPasswordResetToken("username", emailAddress, resetPasswordLinkPlaceHolder); }
@Override public void createPasswordResetToken(String username, String emailAddress, String linkUrl) throws UsernameNotExistException, EmailAddressNotExistException, MessagingException { Staff staff = null; if (!StringUtils.hasText(username)) { throw new IllegalArgumentException("Username is required."); } if (!StringUtils.hasText(emailAddress)) { throw new IllegalArgumentException("Email Address is required."); } if (!StringUtils.hasText(linkUrl)) { throw new IllegalArgumentException("Email link is required."); } Patient patient = patientRepository.findByUsername(username); if (patient == null) { staff = staffRepository.findByUsername(username); if (staff == null) throw new UsernameNotExistException( "The username is not found."); } String patientEmailAddress = (patient != null) ? patient.getEmail() : staff.getEmail(); String firstName = (patient != null) ? patient.getFirstName() : staff .getFirstName(); String lastName = (patient != null) ? patient.getLastName() : staff .getLastName(); if (!patientEmailAddress.equalsIgnoreCase(emailAddress)) { String message = String.format( "Email address %s doesn't exist for username %s.", emailAddress, username); logger.warn(message); throw new EmailAddressNotExistException(message); } EmailToken passwordResetToken = new EmailToken(); passwordResetToken.setExpireInHours(passwordResetTokenExpireInHours); passwordResetToken.setRequestDateTime(new Date()); String token = tokenGenerator.generateToken(); passwordResetToken.setUsername(username); passwordResetToken.setToken(token); passwordResetToken.setIsTokenUsed(false); passwordResetToken.setTokenType(TokenType.PASSWORD_RESET); passwordResetTokenRepository.save(passwordResetToken); emailSender.sendMessage(firstName + " " + lastName, emailAddress, EmailType.PASSWORD_RESET_REQUEST, linkUrl, token); }
PasswordResetServiceImpl implements PasswordResetService { @Override public void createPasswordResetToken(String username, String emailAddress, String linkUrl) throws UsernameNotExistException, EmailAddressNotExistException, MessagingException { Staff staff = null; if (!StringUtils.hasText(username)) { throw new IllegalArgumentException("Username is required."); } if (!StringUtils.hasText(emailAddress)) { throw new IllegalArgumentException("Email Address is required."); } if (!StringUtils.hasText(linkUrl)) { throw new IllegalArgumentException("Email link is required."); } Patient patient = patientRepository.findByUsername(username); if (patient == null) { staff = staffRepository.findByUsername(username); if (staff == null) throw new UsernameNotExistException( "The username is not found."); } String patientEmailAddress = (patient != null) ? patient.getEmail() : staff.getEmail(); String firstName = (patient != null) ? patient.getFirstName() : staff .getFirstName(); String lastName = (patient != null) ? patient.getLastName() : staff .getLastName(); if (!patientEmailAddress.equalsIgnoreCase(emailAddress)) { String message = String.format( "Email address %s doesn't exist for username %s.", emailAddress, username); logger.warn(message); throw new EmailAddressNotExistException(message); } EmailToken passwordResetToken = new EmailToken(); passwordResetToken.setExpireInHours(passwordResetTokenExpireInHours); passwordResetToken.setRequestDateTime(new Date()); String token = tokenGenerator.generateToken(); passwordResetToken.setUsername(username); passwordResetToken.setToken(token); passwordResetToken.setIsTokenUsed(false); passwordResetToken.setTokenType(TokenType.PASSWORD_RESET); passwordResetTokenRepository.save(passwordResetToken); emailSender.sendMessage(firstName + " " + lastName, emailAddress, EmailType.PASSWORD_RESET_REQUEST, linkUrl, token); } }
PasswordResetServiceImpl implements PasswordResetService { @Override public void createPasswordResetToken(String username, String emailAddress, String linkUrl) throws UsernameNotExistException, EmailAddressNotExistException, MessagingException { Staff staff = null; if (!StringUtils.hasText(username)) { throw new IllegalArgumentException("Username is required."); } if (!StringUtils.hasText(emailAddress)) { throw new IllegalArgumentException("Email Address is required."); } if (!StringUtils.hasText(linkUrl)) { throw new IllegalArgumentException("Email link is required."); } Patient patient = patientRepository.findByUsername(username); if (patient == null) { staff = staffRepository.findByUsername(username); if (staff == null) throw new UsernameNotExistException( "The username is not found."); } String patientEmailAddress = (patient != null) ? patient.getEmail() : staff.getEmail(); String firstName = (patient != null) ? patient.getFirstName() : staff .getFirstName(); String lastName = (patient != null) ? patient.getLastName() : staff .getLastName(); if (!patientEmailAddress.equalsIgnoreCase(emailAddress)) { String message = String.format( "Email address %s doesn't exist for username %s.", emailAddress, username); logger.warn(message); throw new EmailAddressNotExistException(message); } EmailToken passwordResetToken = new EmailToken(); passwordResetToken.setExpireInHours(passwordResetTokenExpireInHours); passwordResetToken.setRequestDateTime(new Date()); String token = tokenGenerator.generateToken(); passwordResetToken.setUsername(username); passwordResetToken.setToken(token); passwordResetToken.setIsTokenUsed(false); passwordResetToken.setTokenType(TokenType.PASSWORD_RESET); passwordResetTokenRepository.save(passwordResetToken); emailSender.sendMessage(firstName + " " + lastName, emailAddress, EmailType.PASSWORD_RESET_REQUEST, linkUrl, token); } PasswordResetServiceImpl(Integer passwordResetTokenExpireInHours,
			UsersRepository usersRepository,
			PatientRepository patientRepository,
			StaffRepository staffRepository, TokenGenerator tokenGenerator,
			EmailTokenRepository passwordResetTokenRepository,
			EmailSender emailSender, PasswordEncoder passwordEncoder); }
PasswordResetServiceImpl implements PasswordResetService { @Override public void createPasswordResetToken(String username, String emailAddress, String linkUrl) throws UsernameNotExistException, EmailAddressNotExistException, MessagingException { Staff staff = null; if (!StringUtils.hasText(username)) { throw new IllegalArgumentException("Username is required."); } if (!StringUtils.hasText(emailAddress)) { throw new IllegalArgumentException("Email Address is required."); } if (!StringUtils.hasText(linkUrl)) { throw new IllegalArgumentException("Email link is required."); } Patient patient = patientRepository.findByUsername(username); if (patient == null) { staff = staffRepository.findByUsername(username); if (staff == null) throw new UsernameNotExistException( "The username is not found."); } String patientEmailAddress = (patient != null) ? patient.getEmail() : staff.getEmail(); String firstName = (patient != null) ? patient.getFirstName() : staff .getFirstName(); String lastName = (patient != null) ? patient.getLastName() : staff .getLastName(); if (!patientEmailAddress.equalsIgnoreCase(emailAddress)) { String message = String.format( "Email address %s doesn't exist for username %s.", emailAddress, username); logger.warn(message); throw new EmailAddressNotExistException(message); } EmailToken passwordResetToken = new EmailToken(); passwordResetToken.setExpireInHours(passwordResetTokenExpireInHours); passwordResetToken.setRequestDateTime(new Date()); String token = tokenGenerator.generateToken(); passwordResetToken.setUsername(username); passwordResetToken.setToken(token); passwordResetToken.setIsTokenUsed(false); passwordResetToken.setTokenType(TokenType.PASSWORD_RESET); passwordResetTokenRepository.save(passwordResetToken); emailSender.sendMessage(firstName + " " + lastName, emailAddress, EmailType.PASSWORD_RESET_REQUEST, linkUrl, token); } PasswordResetServiceImpl(Integer passwordResetTokenExpireInHours,
			UsersRepository usersRepository,
			PatientRepository patientRepository,
			StaffRepository staffRepository, TokenGenerator tokenGenerator,
			EmailTokenRepository passwordResetTokenRepository,
			EmailSender emailSender, PasswordEncoder passwordEncoder); @Override void createPasswordResetToken(String username, String emailAddress,
			String linkUrl); @Override Boolean isPasswordResetTokenExpired(String token); @Override void resetPassword(PasswordResetDto passwordResetDto, String linkUrl); @Override boolean changePassword(PasswordChangeDto passwordChangeDto); }
PasswordResetServiceImpl implements PasswordResetService { @Override public void createPasswordResetToken(String username, String emailAddress, String linkUrl) throws UsernameNotExistException, EmailAddressNotExistException, MessagingException { Staff staff = null; if (!StringUtils.hasText(username)) { throw new IllegalArgumentException("Username is required."); } if (!StringUtils.hasText(emailAddress)) { throw new IllegalArgumentException("Email Address is required."); } if (!StringUtils.hasText(linkUrl)) { throw new IllegalArgumentException("Email link is required."); } Patient patient = patientRepository.findByUsername(username); if (patient == null) { staff = staffRepository.findByUsername(username); if (staff == null) throw new UsernameNotExistException( "The username is not found."); } String patientEmailAddress = (patient != null) ? patient.getEmail() : staff.getEmail(); String firstName = (patient != null) ? patient.getFirstName() : staff .getFirstName(); String lastName = (patient != null) ? patient.getLastName() : staff .getLastName(); if (!patientEmailAddress.equalsIgnoreCase(emailAddress)) { String message = String.format( "Email address %s doesn't exist for username %s.", emailAddress, username); logger.warn(message); throw new EmailAddressNotExistException(message); } EmailToken passwordResetToken = new EmailToken(); passwordResetToken.setExpireInHours(passwordResetTokenExpireInHours); passwordResetToken.setRequestDateTime(new Date()); String token = tokenGenerator.generateToken(); passwordResetToken.setUsername(username); passwordResetToken.setToken(token); passwordResetToken.setIsTokenUsed(false); passwordResetToken.setTokenType(TokenType.PASSWORD_RESET); passwordResetTokenRepository.save(passwordResetToken); emailSender.sendMessage(firstName + " " + lastName, emailAddress, EmailType.PASSWORD_RESET_REQUEST, linkUrl, token); } PasswordResetServiceImpl(Integer passwordResetTokenExpireInHours,
			UsersRepository usersRepository,
			PatientRepository patientRepository,
			StaffRepository staffRepository, TokenGenerator tokenGenerator,
			EmailTokenRepository passwordResetTokenRepository,
			EmailSender emailSender, PasswordEncoder passwordEncoder); @Override void createPasswordResetToken(String username, String emailAddress,
			String linkUrl); @Override Boolean isPasswordResetTokenExpired(String token); @Override void resetPassword(PasswordResetDto passwordResetDto, String linkUrl); @Override boolean changePassword(PasswordChangeDto passwordChangeDto);  }