@Test public void testSendEmail() { String externalApex = "staffjoy-v2.local"; when(appProps.getSigningSecret()).thenReturn("test_secret"); when(envConfig.getExternalApex()).thenReturn(externalApex); when(mailClient.send(any(EmailRequest.class))).thenReturn(BaseResponse.builder().message("email sent").build()); String userId = UUID.randomUUID().toString(); String email = "test@jskillcloud.com"; String name = "test_name"; String subject = "Activate your Staffjoy account"; String template = AccountConstant.ACTIVATE_ACCOUNT_TMPL; boolean activateOrConfirm = true; accountService.sendEmail(userId, email, name, subject, template, true); ArgumentCaptor<EmailRequest> argument = ArgumentCaptor.forClass(EmailRequest.class); verify(mailClient, times(1)).send(argument.capture()); EmailRequest emailRequest = argument.getValue(); log.info(emailRequest.toString()); assertThat(emailRequest.getTo()).isEqualTo(email); assertThat(emailRequest.getSubject()).isEqualTo(subject); assertThat(emailRequest.getName()).isEqualTo(name); assertThat(StringUtils.countMatches(emailRequest.getHtmlBody(), "http: assertThat(StringUtils.countMatches(emailRequest.getHtmlBody(), name)).isEqualTo(1); assertThat(emailRequest.getHtmlBody()).startsWith("<div><p>Hi"); template = AccountConstant.CONFIRM_EMAIL_TMPL; accountService.sendEmail(userId, email, name, subject, template, true); verify(mailClient, times(2)).send(argument.capture()); emailRequest = argument.getValue(); log.info(emailRequest.toString()); assertThat(emailRequest.getTo()).isEqualTo(email); assertThat(emailRequest.getSubject()).isEqualTo(subject); assertThat(emailRequest.getName()).isEqualTo(name); assertThat(StringUtils.countMatches(emailRequest.getHtmlBody(), "http: assertThat(StringUtils.countMatches(emailRequest.getHtmlBody(), name)).isEqualTo(1); assertThat(emailRequest.getHtmlBody()).startsWith("<div>Hi"); subject = "Reset your Staffjoy password"; template = AccountConstant.RESET_PASSWORD_TMPL; accountService.sendEmail(userId, email, name, subject, template, false); verify(mailClient, times(3)).send(argument.capture()); emailRequest = argument.getValue(); log.info(emailRequest.toString()); assertThat(emailRequest.getTo()).isEqualTo(email); assertThat(emailRequest.getSubject()).isEqualTo(subject); assertThat(emailRequest.getName()).isEqualTo(name); assertThat(StringUtils.countMatches(emailRequest.getHtmlBody(), "http: assertThat(emailRequest.getHtmlBody()).startsWith("<div>We received a request to reset the password on your account."); }
void sendEmail(String userId, String email, String name, String subject, String template, boolean activateOrConfirm) { String token = null; try { token = Sign.generateEmailConfirmationToken(userId, email, appProps.getSigningSecret()); } catch(Exception ex) { String errMsg = "Could not create token"; serviceHelper.handleException(logger, ex, errMsg); throw new ServiceException(errMsg, ex); } String pathFormat = "/activate/%s"; if (!activateOrConfirm) { pathFormat = "/reset/%s"; } String path = String.format(pathFormat, token); URI link = null; try { link = new URI("http", "www." + envConfig.getExternalApex(), path, null); } catch (URISyntaxException ex) { String errMsg = "Could not create activation url"; if (!activateOrConfirm) { errMsg = "Could not create reset url"; } serviceHelper.handleException(logger, ex, errMsg); throw new ServiceException(errMsg, ex); } String htmlBody = null; if (activateOrConfirm) { htmlBody = String.format(template, name, link.toString(), link.toString(), link.toString()); } else { htmlBody = String.format(template, link.toString(), link.toString()); } EmailRequest emailRequest = EmailRequest.builder() .to(email) .name(name) .subject(subject) .htmlBody(htmlBody) .build(); BaseResponse baseResponse = null; try { baseResponse = mailClient.send(emailRequest); } catch (Exception ex) { String errMsg = "Unable to send email"; serviceHelper.handleException(logger, ex, errMsg); throw new ServiceException(errMsg, ex); } if (!baseResponse.isSuccess()) { serviceHelper.handleError(logger, baseResponse.getMessage()); throw new ServiceException(baseResponse.getMessage()); } }
AccountService { void sendEmail(String userId, String email, String name, String subject, String template, boolean activateOrConfirm) { String token = null; try { token = Sign.generateEmailConfirmationToken(userId, email, appProps.getSigningSecret()); } catch(Exception ex) { String errMsg = "Could not create token"; serviceHelper.handleException(logger, ex, errMsg); throw new ServiceException(errMsg, ex); } String pathFormat = "/activate/%s"; if (!activateOrConfirm) { pathFormat = "/reset/%s"; } String path = String.format(pathFormat, token); URI link = null; try { link = new URI("http", "www." + envConfig.getExternalApex(), path, null); } catch (URISyntaxException ex) { String errMsg = "Could not create activation url"; if (!activateOrConfirm) { errMsg = "Could not create reset url"; } serviceHelper.handleException(logger, ex, errMsg); throw new ServiceException(errMsg, ex); } String htmlBody = null; if (activateOrConfirm) { htmlBody = String.format(template, name, link.toString(), link.toString(), link.toString()); } else { htmlBody = String.format(template, link.toString(), link.toString()); } EmailRequest emailRequest = EmailRequest.builder() .to(email) .name(name) .subject(subject) .htmlBody(htmlBody) .build(); BaseResponse baseResponse = null; try { baseResponse = mailClient.send(emailRequest); } catch (Exception ex) { String errMsg = "Unable to send email"; serviceHelper.handleException(logger, ex, errMsg); throw new ServiceException(errMsg, ex); } if (!baseResponse.isSuccess()) { serviceHelper.handleError(logger, baseResponse.getMessage()); throw new ServiceException(baseResponse.getMessage()); } } }
AccountService { void sendEmail(String userId, String email, String name, String subject, String template, boolean activateOrConfirm) { String token = null; try { token = Sign.generateEmailConfirmationToken(userId, email, appProps.getSigningSecret()); } catch(Exception ex) { String errMsg = "Could not create token"; serviceHelper.handleException(logger, ex, errMsg); throw new ServiceException(errMsg, ex); } String pathFormat = "/activate/%s"; if (!activateOrConfirm) { pathFormat = "/reset/%s"; } String path = String.format(pathFormat, token); URI link = null; try { link = new URI("http", "www." + envConfig.getExternalApex(), path, null); } catch (URISyntaxException ex) { String errMsg = "Could not create activation url"; if (!activateOrConfirm) { errMsg = "Could not create reset url"; } serviceHelper.handleException(logger, ex, errMsg); throw new ServiceException(errMsg, ex); } String htmlBody = null; if (activateOrConfirm) { htmlBody = String.format(template, name, link.toString(), link.toString(), link.toString()); } else { htmlBody = String.format(template, link.toString(), link.toString()); } EmailRequest emailRequest = EmailRequest.builder() .to(email) .name(name) .subject(subject) .htmlBody(htmlBody) .build(); BaseResponse baseResponse = null; try { baseResponse = mailClient.send(emailRequest); } catch (Exception ex) { String errMsg = "Unable to send email"; serviceHelper.handleException(logger, ex, errMsg); throw new ServiceException(errMsg, ex); } if (!baseResponse.isSuccess()) { serviceHelper.handleError(logger, baseResponse.getMessage()); throw new ServiceException(baseResponse.getMessage()); } }  }
AccountService { void sendEmail(String userId, String email, String name, String subject, String template, boolean activateOrConfirm) { String token = null; try { token = Sign.generateEmailConfirmationToken(userId, email, appProps.getSigningSecret()); } catch(Exception ex) { String errMsg = "Could not create token"; serviceHelper.handleException(logger, ex, errMsg); throw new ServiceException(errMsg, ex); } String pathFormat = "/activate/%s"; if (!activateOrConfirm) { pathFormat = "/reset/%s"; } String path = String.format(pathFormat, token); URI link = null; try { link = new URI("http", "www." + envConfig.getExternalApex(), path, null); } catch (URISyntaxException ex) { String errMsg = "Could not create activation url"; if (!activateOrConfirm) { errMsg = "Could not create reset url"; } serviceHelper.handleException(logger, ex, errMsg); throw new ServiceException(errMsg, ex); } String htmlBody = null; if (activateOrConfirm) { htmlBody = String.format(template, name, link.toString(), link.toString(), link.toString()); } else { htmlBody = String.format(template, link.toString(), link.toString()); } EmailRequest emailRequest = EmailRequest.builder() .to(email) .name(name) .subject(subject) .htmlBody(htmlBody) .build(); BaseResponse baseResponse = null; try { baseResponse = mailClient.send(emailRequest); } catch (Exception ex) { String errMsg = "Unable to send email"; serviceHelper.handleException(logger, ex, errMsg); throw new ServiceException(errMsg, ex); } if (!baseResponse.isSuccess()) { serviceHelper.handleError(logger, baseResponse.getMessage()); throw new ServiceException(baseResponse.getMessage()); } }  AccountDto getOrCreate(String name, String email, String phoneNumber); AccountDto getAccountByPhoneNumber(String phoneNumber); AccountDto create(String name, String email, String phoneNumber); AccountList list(int offset, int limit); AccountDto get(String userId); AccountDto update(AccountDto newAccountDto); void updatePassword(String userId, String password); AccountDto verifyPassword(String email, String password); void requestPasswordReset(String email); void requestEmailChange(String userId, String email); void changeEmailAndActivateAccount(String userId, String email); void trackEvent(String userId, String eventName); void syncUser(String userId); }
AccountService { void sendEmail(String userId, String email, String name, String subject, String template, boolean activateOrConfirm) { String token = null; try { token = Sign.generateEmailConfirmationToken(userId, email, appProps.getSigningSecret()); } catch(Exception ex) { String errMsg = "Could not create token"; serviceHelper.handleException(logger, ex, errMsg); throw new ServiceException(errMsg, ex); } String pathFormat = "/activate/%s"; if (!activateOrConfirm) { pathFormat = "/reset/%s"; } String path = String.format(pathFormat, token); URI link = null; try { link = new URI("http", "www." + envConfig.getExternalApex(), path, null); } catch (URISyntaxException ex) { String errMsg = "Could not create activation url"; if (!activateOrConfirm) { errMsg = "Could not create reset url"; } serviceHelper.handleException(logger, ex, errMsg); throw new ServiceException(errMsg, ex); } String htmlBody = null; if (activateOrConfirm) { htmlBody = String.format(template, name, link.toString(), link.toString(), link.toString()); } else { htmlBody = String.format(template, link.toString(), link.toString()); } EmailRequest emailRequest = EmailRequest.builder() .to(email) .name(name) .subject(subject) .htmlBody(htmlBody) .build(); BaseResponse baseResponse = null; try { baseResponse = mailClient.send(emailRequest); } catch (Exception ex) { String errMsg = "Unable to send email"; serviceHelper.handleException(logger, ex, errMsg); throw new ServiceException(errMsg, ex); } if (!baseResponse.isSuccess()) { serviceHelper.handleError(logger, baseResponse.getMessage()); throw new ServiceException(baseResponse.getMessage()); } }  AccountDto getOrCreate(String name, String email, String phoneNumber); AccountDto getAccountByPhoneNumber(String phoneNumber); AccountDto create(String name, String email, String phoneNumber); AccountList list(int offset, int limit); AccountDto get(String userId); AccountDto update(AccountDto newAccountDto); void updatePassword(String userId, String password); AccountDto verifyPassword(String email, String password); void requestPasswordReset(String email); void requestEmailChange(String userId, String email); void changeEmailAndActivateAccount(String userId, String email); void trackEvent(String userId, String eventName); void syncUser(String userId);  }