AccountServiceImpl implements AccountService { @Override public String createEmailToken(String username, String emailAddress) throws UsernameNotExistException, EmailAddressNotExistException, MessagingException { String emailLinkPlaceHolder = "?token=%s"; if (!StringUtils.hasText(username)) { throw new IllegalArgumentException("Username is required."); } if (!StringUtils.hasText(emailAddress)) { throw new IllegalArgumentException("Email Address is required."); } Patient patient = patientRepository.findByUsername(username); String patientEmailAddress = patient.getEmail(); if (!patientEmailAddress.equalsIgnoreCase(emailAddress)) { String message = String.format( "Email address %s doesn't exist for username %s.", emailAddress, username); logger.info("message"); throw new EmailAddressNotExistException(message); } EmailToken accountVerificationToken = new EmailToken(); accountVerificationToken .setExpireInHours(accountVerificationTokenExpireInHours); accountVerificationToken.setRequestDateTime(new Date()); String token = tokenGenerator.generateToken(); accountVerificationToken.setToken(token); accountVerificationToken.setUsername(username); accountVerificationToken.setIsTokenUsed(false); accountVerificationToken.setTokenType(TokenType.ACCOUNT_VERIFICATION); emailTokenRepository.save(accountVerificationToken); String link = String.format(emailLinkPlaceHolder, token); return link; } AccountServiceImpl(
			Integer accountVerificationTokenExpireInHours,
			PatientRepository patientRepository,
			AdministrativeGenderCodeRepository administrativeGenderCodeRepository,
			PasswordEncoder passwordEncoder, UserContext userContext,
			EmailSender emailSender, TokenGenerator tokenGenerator,
			EmailTokenRepository emailTokenRepository,
			UsersRepository usersRepository, MrnService mrnService,
			PixService pixService); @Override @Transactional void signup(SignupDto signupDto, String linkUrl); @Override String createEmailToken(String username, String emailAddress); @Override Users findUserByUsername(String username); @Override String recoverUsername(String firstName, String lastName,
			String birthDay, String email);  }
@Test(expected = IllegalArgumentException.class) public void testCreateEmailToken_Throws_Exception_When_Username_Has_Whitespaces_Only() throws UsernameNotExistException, EmailAddressNotExistException, MessagingException { sut.createEmailToken(" ", "emailAddress"); }
@Test(expected = IllegalArgumentException.class) public void testCreateEmailToken_Throws_Exception_When_Username_Is_Null() throws UsernameNotExistException, EmailAddressNotExistException, MessagingException { sut.createEmailToken(null, "emailAddress"); }
@Test(expected = IllegalArgumentException.class) public void testCreateEmailToken_Throws_Exception_When_Email_Has_Whitespaces_Only() throws UsernameNotExistException, EmailAddressNotExistException, MessagingException { sut.createEmailToken("username", " "); }
@Test(expected = IllegalArgumentException.class) public void testCreateEmailToken_Throws_Exception_When_Email_Is_Null() throws UsernameNotExistException, EmailAddressNotExistException, MessagingException { sut.createEmailToken("username", null); }
@Test public void testCreateEmailToken() throws UsernameNotExistException, EmailAddressNotExistException, MessagingException { Patient patient = mock(Patient.class); when(patientRepository.findByUsername(anyString())).thenReturn(patient); when(patient.getEmail()).thenReturn("emailAddress"); sut.createEmailToken("username", "emailAddress"); }