NotifyMailetsMessage { public String generateMessage(String parameterMessage, Mail originalMail) throws MessagingException { MimeMessage message = originalMail.getMessage(); StringBuilder builder = new StringBuilder(); builder.append(parameterMessage).append(LINE_BREAK); if (originalMail.getErrorMessage() != null) { builder.append(LINE_BREAK) .append("Error message below:") .append(LINE_BREAK) .append(originalMail.getErrorMessage()) .append(LINE_BREAK); } builder.append(LINE_BREAK) .append("Message details:") .append(LINE_BREAK); if (message.getSubject() != null) { builder.append(" Subject: ") .append(safelyDecode(message.getSubject())) .append(LINE_BREAK); } if (message.getSentDate() != null) { builder.append(" Sent date: " + message.getSentDate()) .append(LINE_BREAK); } builder.append(" MAIL FROM: " + originalMail.getMaybeSender().asString()) .append(LINE_BREAK); boolean firstRecipient = true; for (MailAddress recipient : originalMail.getRecipients()) { if (firstRecipient) { builder.append(" RCPT TO: " + recipient) .append(LINE_BREAK); firstRecipient = false; } else { builder.append(" " + recipient) .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: ") .append(SizeUtils.humanReadableSize(size)) .append(LINE_BREAK)); if (message.getLineCount() >= 0) { builder.append(" Number of lines: " + message.getLineCount()) .append(LINE_BREAK); } return builder.toString(); }  String generateMessage(String parameterMessage, Mail originalMail);  }
@Test public void generateMessageShouldReturnTheMessageWhenSimpleMimeMessage() throws Exception { FakeMail mail = FakeMail.builder() .name("name") .mimeMessage(MimeMessageBuilder.mimeMessageBuilder()) .sender(new MailAddress("user", "james.org")) .build(); String generateMessage = new NotifyMailetsMessage().generateMessage("my message", mail); assertThat(generateMessage).contains("my message\n") .contains("Message details:") .contains(" MAIL FROM: user@james.org\n"); }
@Test public void generateMessageShouldAddErrorMessageWhenMimeMessageAsSome() throws Exception { FakeMail mail = FakeMail.from(MimeMessageBuilder.mimeMessageBuilder()); String myErrorMessage = "my error message"; mail.setErrorMessage(myErrorMessage); String generateMessage = new NotifyMailetsMessage().generateMessage("my message", mail); assertThat(generateMessage).contains( "Error message below:\n" + "my error message\n"); }
@Test public void generateMessageShouldAddSubjectWhenMimeMessageAsSome() throws Exception { FakeMail mail = FakeMail.from(MimeMessageBuilder.mimeMessageBuilder() .setSubject("my subject")); String generateMessage = new NotifyMailetsMessage().generateMessage("my message", mail); assertThat(generateMessage).contains("Subject: my subject"); }
@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 generateMessageShouldAddRecipientsWhenMimeMessageAsSome() throws Exception { FakeMail mail = FakeMail.builder() .name("name") .mimeMessage(MimeMessageBuilder.mimeMessageBuilder()) .recipients("user@james.org", "user2@james.org") .build(); String generateMessage = new NotifyMailetsMessage().generateMessage("my message", mail); assertThat(generateMessage).contains("RCPT TO: user@james.org\n" + " user2@james.org"); }
@Test public void generateMessageShouldAddFromWhenMimeMessageAsSome() throws Exception { FakeMail mail = FakeMail.from(MimeMessageBuilder.mimeMessageBuilder() .addFrom("user@james.org")); String generateMessage = new NotifyMailetsMessage().generateMessage("my message", mail); assertThat(generateMessage).contains("From: \n" + "user@james.org"); }
@Test public void generateMessageShouldAddToWhenMimeMessageAsSome() throws Exception { FakeMail mail = FakeMail.from(MimeMessageBuilder.mimeMessageBuilder() .addToRecipient("user@james.org", "user2@james.org")); String generateMessage = new NotifyMailetsMessage().generateMessage("my message", mail); assertThat(generateMessage).contains("To: \n" + "user@james.org \n" + "user2@james.org"); }
@Test public void generateMessageShouldAddCCWhenMimeMessageAsSome() throws Exception { FakeMail mail = FakeMail.from(MimeMessageBuilder.mimeMessageBuilder() .addCcRecipient("user@james.org", "user2@james.org")); String generateMessage = new NotifyMailetsMessage().generateMessage("my message", mail); assertThat(generateMessage).contains("CC: \n" + "user@james.org \n" + "user2@james.org"); }
@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"); }
@Test public void generateMessageShouldSpecifySizeInAReadableWay() throws Exception { String content = "MIME-Version: 1.0\r\n" + "Content-Type: text/plain; charset=utf-8\r\n" + "\r\n" + "test\r\n"; FakeMail mail = FakeMail.from(MimeMessageUtil.mimeMessageFromString(content)); mail.setMessageSize((long)(5.9 * 1024)); String generateMessage = new NotifyMailetsMessage().generateMessage("my message", mail); assertThat(generateMessage).contains("Size: 5.9 KiB"); }
@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 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 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 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"); }