.humanReadableText(humanText.toString()) .report( MDNReport.builder() .reportingUserAgentField(ReportingUserAgent.builder().userAgentName(reportingUAName).userAgentProduct(reportingUAProduct).build()) .build()) .build()) .build() .asMultipart();
@Test public void mdnShouldBeConvertibleToMime4JMessage() throws Exception { Message message = MDN.builder() .humanReadableText("Explanation:\n" + " - We should always write detailed unit tests\n" + " - We should think of all edge cases\n") .report(MINIMAL_REPORT) .build() .asMime4JMessageBuilder() .build(); assertThat(asString(message)) .contains("MIME-Version: 1.0\r\n" + "Content-Type: multipart/report;") .contains("Content-Type: text/plain; charset=UTF-8\r\n" + "\r\n" + "Explanation:\n" + " - We should always write detailed unit tests\n" + " - We should think of all edge cases") .contains("Content-Type: message/disposition-notification; charset=UTF-8\r\n" + "\r\n" + "Final-Recipient: rfc822; final@domain.com\r\n" + "Disposition: automatic-action/MDN-sent-automatically;deleted"); }
@Test public void asMimeMessageShouldComportExplanationPartAndReportPart() throws Exception { MimeMessage mimeMessage = MDN.builder() .humanReadableText("Explanation") .report(MINIMAL_REPORT) .build() .asMimeMessage(); ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); mimeMessage.writeTo(byteArrayOutputStream); assertThat(new String(byteArrayOutputStream.toByteArray(), StandardCharsets.UTF_8)) .contains( "Content-Type: text/plain; charset=UTF-8\r\n" + "Content-Transfer-Encoding: 7bit\r\n" + "Content-Disposition: inline\r\n" + "\r\n" + "Explanation") .contains( "Content-Type: message/disposition-notification\r\n" + "Content-Transfer-Encoding: 7bit\r\n" + "\r\n" + "Final-Recipient: rfc822; final@domain.com\r\n" + "Disposition: automatic-action/MDN-sent-automatically;deleted"); }
@Test public void humanReadableTextShouldNotBeTrimmed() throws Exception { MimeMessage mimeMessage = MDN.builder() .humanReadableText("Explanation:\n" + " - We should always write detailed unit tests\n" + " - We should think of all edge cases\n") .report(MINIMAL_REPORT) .build() .asMimeMessage(); ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); mimeMessage.writeTo(byteArrayOutputStream); assertThat(new String(byteArrayOutputStream.toByteArray(), StandardCharsets.UTF_8)) .contains( "Content-Type: text/plain; charset=UTF-8\r\n" + "Content-Transfer-Encoding: 7bit\r\n" + "Content-Disposition: inline\r\n" + "\r\n" + "Explanation:\n" + " - We should always write detailed unit tests\n" + " - We should think of all edge cases\n"); }
@Test public void asMimeMessageShouldDisplayEmptyExplanation() throws Exception { MimeMessage mimeMessage = MDN.builder() .humanReadableText("Explanation") .report(MINIMAL_REPORT) .build() .asMimeMessage(); ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); mimeMessage.writeTo(byteArrayOutputStream); assertThat(new String(byteArrayOutputStream.toByteArray(), StandardCharsets.UTF_8)) .contains( "Content-Type: text/plain; charset=UTF-8\r\n" + "Content-Transfer-Encoding: 7bit\r\n" + "Content-Disposition: inline\r\n" + "\r\n" + "Explanation") .contains( "Content-Type: message/disposition-notification\r\n" + "Content-Transfer-Encoding: 7bit\r\n" + "\r\n" + "Final-Recipient: rfc822; final@domain.com\r\n" + "Disposition: automatic-action/MDN-sent-automatically;deleted"); }
public static Builder builder() { return new Builder(); }
public Message generateMDNMessage(Message originalMessage, MailboxSession mailboxSession) throws ParseException, IOException, InvalidOriginMessageForMDNException { User user = mailboxSession.getUser().getCoreUser(); return MDN.builder() .report(generateReport(originalMessage, mailboxSession)) .humanReadableText(textBody) .build() .asMime4JMessageBuilder() .setTo(getSenderAddress(originalMessage)) .setFrom(user.asString()) .setSubject(subject) .setMessageId(MimeUtil.createUniqueMessageId(user.getDomainPart().map(Domain::name).orElse(null))) .build(); }
@Test public void asMimeMessageShouldGenerateExpectedContentType() throws Exception { MimeMessage mimeMessage = MDN.builder() .humanReadableText("Explanation") .report(MINIMAL_REPORT) .build() .asMimeMessage(); ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); mimeMessage.writeTo(byteArrayOutputStream); assertThat(new String(byteArrayOutputStream.toByteArray(), StandardCharsets.UTF_8)) .containsPattern( Pattern.compile("Content-Type: multipart/report;.*(\r\n.+)*report-type=disposition-notification.*\r\n\r\n")); }
@Test public void humanReadableTextShouldThrowOnNullValue() { expectedException.expect(NullPointerException.class); MDN.builder() .humanReadableText(null); }
@Test public void mime4JMessageExportShouldGenerateExpectedContentType() throws Exception { Message message = MDN.builder() .humanReadableText("RFCs are not funny") .report(MINIMAL_REPORT) .build() .asMime4JMessageBuilder() .build(); assertThat(asString(message)) .containsPattern(Pattern.compile("Content-Type: multipart/report;.*(\r\n.+)*report-type=disposition-notification.*(\r\n.+)*\r\n\r\n")); }
@Test public void buildShouldThrowOnEmptyHumanReadableText() { expectedException.expect(IllegalStateException.class); MDN.builder() .humanReadableText("") .report(MINIMAL_REPORT) .build(); }
@Test public void reportShouldThrowOnNullValue() { expectedException.expect(NullPointerException.class); MDN.builder() .report(null); }
@Test public void buildShouldThrowOnFoldingWhiteHumanReadableText() { expectedException.expect(IllegalStateException.class); MDN.builder() .humanReadableText(" ") .report(MINIMAL_REPORT) .build(); }