MimeMessageBodyGenerator { public MimeMessage from(MimeMessage messageHoldingHeaders, Optional<String> plainText, Optional<String> htmlText) throws MessagingException { Preconditions.checkNotNull(messageHoldingHeaders); Preconditions.checkNotNull(plainText); Preconditions.checkNotNull(htmlText); if (htmlText.isPresent()) { messageHoldingHeaders.setContent(generateMultipart(htmlText.get(), plainText)); } else { messageHoldingHeaders.setText(plainText.orElse(EMPTY_TEXT)); } return messageHoldingHeaders; } @Inject @VisibleForTesting  MimeMessageBodyGenerator(HtmlTextExtractor htmlTextExtractor); MimeMessage from(MimeMessage messageHoldingHeaders, Optional<String> plainText, Optional<String> htmlText); static final String MIXED; static final String EMPTY_TEXT; }
@Test public void fromShouldNotWriteAMultipartWhenOnlyPlainText() throws Exception { assertThat(IOUtils.toString( mimeMessageBodyGenerator.from(original, Optional.of("Plain text"), Optional.empty()) .getInputStream(), StandardCharsets.UTF_8)) .isEqualTo("Plain text"); verifyZeroInteractions(htmlTextExtractor); }
@Test public void fromShouldPreservePreviouslySetHeaders() throws Exception { String subject = "Important, I should be kept"; original.setHeader("Subject", subject); mimeMessageBodyGenerator.from(original, Optional.of("Plain text"), Optional.empty()) .getInputStream(); assertThat(original.getSubject()).isEqualTo(subject); verifyZeroInteractions(htmlTextExtractor); }
@Test public void fromShouldProvideAPlainTextVersionWhenOnlyHtml() throws Exception { String htmlText = "<p>HTML text</p>"; String plainText = "Plain text"; when(htmlTextExtractor.toPlainText(htmlText)).thenReturn(plainText); String rowContent = IOUtils.toString( mimeMessageBodyGenerator.from(original, Optional.empty(), Optional.of(htmlText)) .getInputStream(), StandardCharsets.UTF_8); assertThat(rowContent).containsSequence(htmlText); assertThat(rowContent).containsSequence(plainText); }
@Test public void fromShouldCombinePlainTextAndHtml() throws Exception { String htmlText = "<p>HTML text</p>"; String plainText = "Plain text"; String rowContent = IOUtils.toString( mimeMessageBodyGenerator.from(original, Optional.of(plainText), Optional.of(htmlText)) .getInputStream(), StandardCharsets.UTF_8); assertThat(rowContent).containsSequence(htmlText); assertThat(rowContent).containsSequence(plainText); verifyZeroInteractions(htmlTextExtractor); }
@Test public void fromShouldUseEmptyTextWhenNoPlainTextNorHtmlBody() throws Exception { String rowContent = IOUtils.toString(mimeMessageBodyGenerator.from(original, Optional.empty(), Optional.empty()) .getInputStream(), StandardCharsets.UTF_8); assertThat(rowContent).isEmpty(); }
@Test public void fromShouldThrowOnNullPlainText() throws Exception { assertThatThrownBy(() -> mimeMessageBodyGenerator.from(original, null, Optional.empty())) .isInstanceOf(NullPointerException.class); }
@Test public void fromShouldThrowOnNullHtml() throws Exception { assertThatThrownBy(() -> mimeMessageBodyGenerator.from(original, Optional.empty(), null)) .isInstanceOf(NullPointerException.class); }
@Test public void fromShouldThrowOnNullMimeMessageToDecorate() throws Exception { assertThatThrownBy(() -> mimeMessageBodyGenerator.from(null, Optional.empty(), Optional.empty())) .isInstanceOf(NullPointerException.class); }