@Test(expected = WasabiEmailException.class) public void testSend() throws EmailException { EmailTextProcessor emailTextProcessor = mock(EmailTextProcessor.class); Email simpleEmail = mock(Email.class); EmailServiceImpl emailService = spy(new EmailServiceImpl (false, "host1", "from1", "prefix","username","", false,false, emailTextProcessor)); emailService.setActive(true); when(emailService.createSimpleMailService()).thenReturn(simpleEmail); emailService.send("mocked subject", "mocked message", "mock@example.com"); verify(simpleEmail, times(1)).send(); doThrow(new EmailException()).when(simpleEmail).send(); emailService.send("mocked subject", "mocked message", "mock@example.com"); }
void send(String subject, String msg, String... to) { String[] clearTo = removeInvalidEmails(to); if (isActive()) { try { Email email = createSimpleMailService(); email.setHostName(host); email.setFrom(from); email.setSubject(subjectPrefix + " " + subject); email.setMsg(msg); email.addTo(clearTo); if (this.authenticationEnabled) { email.setAuthentication(this.userName, this.password); } email.setSSLOnConnect(sslEnabled); email.send(); } catch (EmailException mailExcp) { LOGGER.error("Email could not be send because of " + mailExcp.getMessage(),mailExcp); throw new WasabiEmailException("Email: " + emailToString(subject, msg, to) + " could not be sent.", mailExcp); } } else { LOGGER.info("EmailService would have sent: " + emailToString(subject, msg, to)); throw new WasabiEmailException(ErrorCode.EMAIL_NOT_ACTIVE_ERROR, "The EmailService is not active."); } }
EmailServiceImpl implements EmailService { void send(String subject, String msg, String... to) { String[] clearTo = removeInvalidEmails(to); if (isActive()) { try { Email email = createSimpleMailService(); email.setHostName(host); email.setFrom(from); email.setSubject(subjectPrefix + " " + subject); email.setMsg(msg); email.addTo(clearTo); if (this.authenticationEnabled) { email.setAuthentication(this.userName, this.password); } email.setSSLOnConnect(sslEnabled); email.send(); } catch (EmailException mailExcp) { LOGGER.error("Email could not be send because of " + mailExcp.getMessage(),mailExcp); throw new WasabiEmailException("Email: " + emailToString(subject, msg, to) + " could not be sent.", mailExcp); } } else { LOGGER.info("EmailService would have sent: " + emailToString(subject, msg, to)); throw new WasabiEmailException(ErrorCode.EMAIL_NOT_ACTIVE_ERROR, "The EmailService is not active."); } } }
EmailServiceImpl implements EmailService { void send(String subject, String msg, String... to) { String[] clearTo = removeInvalidEmails(to); if (isActive()) { try { Email email = createSimpleMailService(); email.setHostName(host); email.setFrom(from); email.setSubject(subjectPrefix + " " + subject); email.setMsg(msg); email.addTo(clearTo); if (this.authenticationEnabled) { email.setAuthentication(this.userName, this.password); } email.setSSLOnConnect(sslEnabled); email.send(); } catch (EmailException mailExcp) { LOGGER.error("Email could not be send because of " + mailExcp.getMessage(),mailExcp); throw new WasabiEmailException("Email: " + emailToString(subject, msg, to) + " could not be sent.", mailExcp); } } else { LOGGER.info("EmailService would have sent: " + emailToString(subject, msg, to)); throw new WasabiEmailException(ErrorCode.EMAIL_NOT_ACTIVE_ERROR, "The EmailService is not active."); } } @Inject  EmailServiceImpl(final @Named(EMAIL_SERVICE_ENABLED) boolean enabled,
                            final @Named(EMAIL_SERVICE_HOST) String host,
                            final @Named(EMAIL_SERVICE_FROM) String from,
                            final @Named(EMAIL_SERVICE_SUBJECT_PREFIX) String subjectPrefix,
                            final @Named(EMAIL_SERVICE_USERNAME) String userName,
                            final @Named(EMAIL_SERVICE_PASSWORD) String password,
                            final @Named(EMAIL_SERVICE_AUTHENTICATION_ENABLED) boolean authenticationEnabled,
                            final @Named(EMAIL_SERVICE_SSL_ENABLED) boolean sslEnabled,
                            final EmailTextProcessor emailTextProcessor); }
EmailServiceImpl implements EmailService { void send(String subject, String msg, String... to) { String[] clearTo = removeInvalidEmails(to); if (isActive()) { try { Email email = createSimpleMailService(); email.setHostName(host); email.setFrom(from); email.setSubject(subjectPrefix + " " + subject); email.setMsg(msg); email.addTo(clearTo); if (this.authenticationEnabled) { email.setAuthentication(this.userName, this.password); } email.setSSLOnConnect(sslEnabled); email.send(); } catch (EmailException mailExcp) { LOGGER.error("Email could not be send because of " + mailExcp.getMessage(),mailExcp); throw new WasabiEmailException("Email: " + emailToString(subject, msg, to) + " could not be sent.", mailExcp); } } else { LOGGER.info("EmailService would have sent: " + emailToString(subject, msg, to)); throw new WasabiEmailException(ErrorCode.EMAIL_NOT_ACTIVE_ERROR, "The EmailService is not active."); } } @Inject  EmailServiceImpl(final @Named(EMAIL_SERVICE_ENABLED) boolean enabled,
                            final @Named(EMAIL_SERVICE_HOST) String host,
                            final @Named(EMAIL_SERVICE_FROM) String from,
                            final @Named(EMAIL_SERVICE_SUBJECT_PREFIX) String subjectPrefix,
                            final @Named(EMAIL_SERVICE_USERNAME) String userName,
                            final @Named(EMAIL_SERVICE_PASSWORD) String password,
                            final @Named(EMAIL_SERVICE_AUTHENTICATION_ENABLED) boolean authenticationEnabled,
                            final @Named(EMAIL_SERVICE_SSL_ENABLED) boolean sslEnabled,
                            final EmailTextProcessor emailTextProcessor); @Override boolean isActive(); @Override void setActive(boolean enabled); String getHost(); String getFrom(); String getSubjectPrefix(); @Override void doSend(String subject, String msg, String... to); @Override void sendEmailForUserPermission(Application.Name appName, UserInfo.Username user, EmailLinksList links); void setHost(String host); void setFrom(String from); void setSubjectPrefix(String subjectPrefix); }
EmailServiceImpl implements EmailService { void send(String subject, String msg, String... to) { String[] clearTo = removeInvalidEmails(to); if (isActive()) { try { Email email = createSimpleMailService(); email.setHostName(host); email.setFrom(from); email.setSubject(subjectPrefix + " " + subject); email.setMsg(msg); email.addTo(clearTo); if (this.authenticationEnabled) { email.setAuthentication(this.userName, this.password); } email.setSSLOnConnect(sslEnabled); email.send(); } catch (EmailException mailExcp) { LOGGER.error("Email could not be send because of " + mailExcp.getMessage(),mailExcp); throw new WasabiEmailException("Email: " + emailToString(subject, msg, to) + " could not be sent.", mailExcp); } } else { LOGGER.info("EmailService would have sent: " + emailToString(subject, msg, to)); throw new WasabiEmailException(ErrorCode.EMAIL_NOT_ACTIVE_ERROR, "The EmailService is not active."); } } @Inject  EmailServiceImpl(final @Named(EMAIL_SERVICE_ENABLED) boolean enabled,
                            final @Named(EMAIL_SERVICE_HOST) String host,
                            final @Named(EMAIL_SERVICE_FROM) String from,
                            final @Named(EMAIL_SERVICE_SUBJECT_PREFIX) String subjectPrefix,
                            final @Named(EMAIL_SERVICE_USERNAME) String userName,
                            final @Named(EMAIL_SERVICE_PASSWORD) String password,
                            final @Named(EMAIL_SERVICE_AUTHENTICATION_ENABLED) boolean authenticationEnabled,
                            final @Named(EMAIL_SERVICE_SSL_ENABLED) boolean sslEnabled,
                            final EmailTextProcessor emailTextProcessor); @Override boolean isActive(); @Override void setActive(boolean enabled); String getHost(); String getFrom(); String getSubjectPrefix(); @Override void doSend(String subject, String msg, String... to); @Override void sendEmailForUserPermission(Application.Name appName, UserInfo.Username user, EmailLinksList links); void setHost(String host); void setFrom(String from); void setSubjectPrefix(String subjectPrefix);  }