SlaCheckerEmailService { void sendEmail(long incidentId, String message) { MimeMessage mailMessage = emailSender.createMimeMessage(); try { mailMessage.setSubject(getSubject(incidentId), StandardCharsets.UTF_8.toString()); MimeMessageHelper helper = new MimeMessageHelper(mailMessage, true, StandardCharsets.UTF_8.toString()); helper.setFrom(slaCheckerEmailConfig.getFrom()); helper.setTo(slaCheckerEmailConfig.getTo()); helper.setText(message, true); emailSender.send(mailMessage); } catch (Exception ex) { logger.error("Can't send OOSLA email", ex); } }  void sendCloseIncidentEmail(long incidentId); void sendOpenIncidentEmail(long incidentId, List<Repository> ooslaRepositories); boolean shouldResendEmail(DateTime previousEmailDateTime);  }
@Test public void testSendEmail() throws MessagingException, IOException { long incidentId = 0L; String message = "some message"; slaCheckerEmailService.sendEmail(incidentId, message); ArgumentCaptor<MimeMessage> argument = ArgumentCaptor.forClass(MimeMessage.class); verify(emailSender).send(argument.capture()); assertEquals("Out of SLA strings, incident #0", argument.getValue().getSubject()); assertEquals(slaCheckerEmailConfig.from, argument.getValue().getFrom()[0].toString()); assertEquals(slaCheckerEmailConfig.to[0], argument.getValue().getAllRecipients()[0].toString()); MimeMultipart mimeMultipart = ((MimeMultipart) argument.getValue().getContent()); MimeMultipart mimeMultipart2 = (MimeMultipart) mimeMultipart.getBodyPart(0).getContent(); String emailMessage = (String) mimeMultipart2.getBodyPart(0).getContent(); assertEquals(message, emailMessage); }
@Test public void testSendEmailError() throws MessagingException, IOException { long incidentId = 0L; String message = "some message"; MailException mailException = mock(MailException.class); doThrow(mailException).when(emailSender).send(any(MimeMessage.class)); SlaCheckerEmailService.logger = mock(Logger.class); slaCheckerEmailService.sendEmail(incidentId, message); verify(SlaCheckerEmailService.logger).error("Can't send OOSLA email", mailException); }