EmailSenderService implements JobStartListener { public void send(EmailTO emailTO) throws EmailSenderException { try { checkIfEmailCredentialsAreConfigured(); if (emailSendCounter < MAX_EMAILS_TO_BE_SEND) { Email email = new EmailMapper().mapFromEmailTO(emailTO); email.setSSLOnConnect(smtpUseSsl); email.setSmtpPort(Integer.valueOf(smtp_port)); email.setHostName(smtp_server); if (isNoneBlank(smtp_username, smtp_password)) { email.setAuthenticator(new DefaultAuthenticator(smtp_username, smtp_password)); } LOG.info("Sending email: " + emailTO); email.send(); emailSendCounter++; } else { LOG.info("Request of sending email to {} ignored. Limit reached!", emailTO); } } catch (IllegalArgumentException e) { LOG.error("IllegalArgumentException occurred while sending the email ", e); throw e; } catch (EmailException | IOException e) { LOG.error("Errors occurred while sending the email ", e); throw new EmailSenderException(e); } }  void send(EmailTO emailTO); @Override void jobHasBeenStarted(String jobName);  }
@Test public void whenSMTP_port_CredentialsIsNotConfigured_anExceptionIsThrown() throws Exception { exception.expect(EmailSenderException.class); exception.expectMessage("SMTP port is not configured"); emailSenderService.send(emailTO()); }
@Test public void whenSMTP_server_IsNotConfigured_anExceptionIsThrown() throws Exception { exception.expect(EmailSenderException.class); exception.expectMessage("SMTP server is not configured"); setInternalState(emailSenderService, "smtp_port", "2500"); emailSenderService.send(emailTO()); }
@Test public void whenSMTP_username_IsNotConfigured_anExceptionIsThrown() throws Exception { exception.expect(EmailSenderException.class); exception.expectMessage("SMTP username is not configured"); setInternalState(emailSenderService, "smtp_port", "2500"); setInternalState(emailSenderService, "smtp_server", "smtp.server.com"); emailSenderService.send(emailTO()); }
@Test public void whenSMTP_password_IsNotConfigured_anExceptionIsThrown() throws Exception { exception.expect(EmailSenderException.class); exception.expectMessage("SMTP password is not configured"); setInternalState(emailSenderService, "smtp_port", "2500"); setInternalState(emailSenderService, "smtp_server", "smtp.server.com"); setInternalState(emailSenderService, "smtp_username", "username"); emailSenderService.send(emailTO()); }