@Override public String getMessage(Mail originalMail) throws MessagingException { return new NotifyMailetsMessage().generateMessage(getInitParameters().getMessage(), originalMail); }
private void appendAddresses(StringBuilder builder, String title, String[] addresses) { if (addresses != null) { builder.append(" " + title + ": ") .append(LINE_BREAK); for (String address : flatten(addresses)) { builder.append(safelyDecode(address)) .append(" ") .append(LINE_BREAK); } builder.append(LINE_BREAK); } }
.append(safelyDecode(message.getSubject())) .append(LINE_BREAK); appendAddresses(builder, "From", message.getHeader(RFC2822Headers.FROM)); appendAddresses(builder, "To", message.getHeader(RFC2822Headers.TO)); appendAddresses(builder, "CC", message.getHeader(RFC2822Headers.CC)); getMessageSizeEstimation(originalMail).ifPresent(size -> builder .append(" Size: ")
@Test public void safelyDecodeShouldReturnEncodedTextUnmodifiedWhenUnknownCharset() throws Exception { String encodedTextWithUnknownCharset = "=?UTF-9?Q?=E2=99=A5=F0=9F=9A=B2?="; assertThat(NotifyMailetsMessage.safelyDecode(encodedTextWithUnknownCharset)) .isEqualTo(encodedTextWithUnknownCharset); } }
@Test public void getMessageInternalSizeShouldTransformZeroSizeIntoEmpty() throws MessagingException { Mail mail = mock(Mail.class); when(mail.getMessageSize()).thenReturn(0L); assertThat(NotifyMailetsMessage.getMessageSizeEstimation(mail)) .isEqualTo(Optional.empty()); }
@Test public void safelyDecodeShouldReturnTextNotEncodedUnmodified() throws Exception { String text = "Why not unicode for Llama"; assertThat(NotifyMailetsMessage.safelyDecode(text)) .isEqualTo(text); }
@Test public void getMessageInternalSizeShouldReturnSizeWhenAvailable() throws MessagingException { long size = 42L; Mail mail = mock(Mail.class); when(mail.getMessageSize()).thenReturn(size); assertThat(NotifyMailetsMessage.getMessageSizeEstimation(mail)) .isEqualTo(Optional.of(size)); }
@Override public String getMessage(Mail originalMail) throws MessagingException { return new NotifyMailetsMessage().generateMessage(getInitParameters().getMessage(), originalMail); }
@Test public void safelyDecodeShouldCorrectlyDecodeQuotedPrintable() throws Exception { assertThat(NotifyMailetsMessage.safelyDecode("=?UTF-8?Q?=E2=99=A5=F0=9F=9A=B2?=")) .isEqualTo("♥🚲"); }
@Test public void getMessageInternalSizeShouldTransformNegativeIntoEmpty() throws MessagingException { Mail mail = mock(Mail.class); when(mail.getMessageSize()).thenReturn(-1L); assertThat(NotifyMailetsMessage.getMessageSizeEstimation(mail)) .isEqualTo(Optional.empty()); }
@Override public String getMessage(Mail originalMail) throws MessagingException { return new NotifyMailetsMessage().generateMessage(getInitParameters().getMessage(), originalMail); }
@Test public void safelyDecodeShouldReturnInvalidEncodedTextUnmodified() throws Exception { String invalidEncodedText = "=?UTF-8?Q?=E2=99=A5=FX=9F=9A=B2?="; assertThat(NotifyMailetsMessage.safelyDecode(invalidEncodedText)) .isEqualTo(invalidEncodedText); }
@Test public void getMessageInternalSizeShouldTransformMessagingErrorIntoEmpty() throws MessagingException { Mail mail = mock(Mail.class); when(mail.getMessageSize()).thenThrow(new MessagingException()); assertThat(NotifyMailetsMessage.getMessageSizeEstimation(mail)) .isEqualTo(Optional.empty()); }
@Override public String getMessage(Mail originalMail) throws MessagingException { return new NotifyMailetsMessage().generateMessage(getInitParameters().getMessage(), originalMail); }
@Test public void generateMessageShouldAddSentDateWhenMimeMessageAsSome() throws Exception { MimeMessage message = MimeMessageUtil.defaultMimeMessage(); message.setSentDate(Date.from(Instant.parse("2016-09-08T14:25:52.000Z"))); FakeMail mail = FakeMail.from(message); String generateMessage = new NotifyMailetsMessage().generateMessage("my message", mail); assertThat(generateMessage).contains("Sent date: Thu Sep 08 14:25:52 UTC 2016"); }
@Test public void generateMessageShouldDecodeEncodedCc() throws Exception { String content = "MIME-Version: 1.0\r\n" + "Cc: =?UTF-8?Q?=F0=9F=9A=B2@linagora.com?=\r\n" + "Content-Type: text/plain; charset=utf-8\r\n" + "\r\n" + "test\r\n"; FakeMail mail = FakeMail.from(MimeMessageUtil.mimeMessageFromString(content)); String generateMessage = new NotifyMailetsMessage().generateMessage("my message", mail); assertThat(generateMessage).contains(" CC: \n" + "🚲@linagora.com"); }
@Test public void generateMessageShouldDecodeEncodedFrom() throws Exception { String content = "MIME-Version: 1.0\r\n" + "From: =?UTF-8?Q?=F0=9F=90=83@linagora.com?=\r\n" + "Content-Type: text/plain; charset=utf-8\r\n" + "\r\n" + "test\r\n"; FakeMail mail = FakeMail.from(MimeMessageUtil.mimeMessageFromString(content)); String generateMessage = new NotifyMailetsMessage().generateMessage("my message", mail); assertThat(generateMessage).contains(" From: \n" + "🐃@linagora.com"); }
@Test public void generateMessageShouldDecodeEncodedSubject() throws Exception { String content = "MIME-Version: 1.0\r\n" + "Subject: =?UTF-8?Q?Cl=c3=b4ture_&_Paie_du_mois?=\r\n" + "Content-Type: text/plain; charset=utf-8\r\n" + "\r\n" + "test\r\n"; FakeMail mail = FakeMail.from(MimeMessageUtil.mimeMessageFromString(content)); String generateMessage = new NotifyMailetsMessage().generateMessage("my message", mail); assertThat(generateMessage).contains("Subject: Clôture & Paie du mois"); }
@Test public void generateMessageShouldDecodeEncodedTo() throws Exception { String content = "MIME-Version: 1.0\r\n" + "To: =?UTF-8?Q?=F0=9F=9A=BE@linagora.com?=\r\n" + "Content-Type: text/plain; charset=utf-8\r\n" + "\r\n" + "test\r\n"; FakeMail mail = FakeMail.from(MimeMessageUtil.mimeMessageFromString(content)); String generateMessage = new NotifyMailetsMessage().generateMessage("my message", mail); assertThat(generateMessage).contains(" To: \n" + "🚾@linagora.com"); }
@Test public void generateMessageShouldAddSizeWhenPossible() throws Exception { FakeMail mail = FakeMail.from(MimeMessageBuilder.mimeMessageBuilder()); mail.setMessageSize(6); String generateMessage = new NotifyMailetsMessage().generateMessage("my message", mail); assertThat(generateMessage).contains("Size: 6 B"); }