TextCalendarBodyToAttachment extends GenericMailet { @Override public void service(Mail mail) throws MessagingException { MimeMessage mimeMessage = mail.getMessage(); if (mimeMessage.isMimeType(TEXT_CALENDAR_TYPE)) { processTextBodyAsAttachment(mimeMessage); } }  @Override String getMailetInfo(); @Override void service(Mail mail);  }
@Test public void serviceShouldThrowWhenCanNotGetMessageFromMail() throws Exception { expectedException.expect(MessagingException.class); Mail mail = mock(Mail.class); when(mail.getMessage()) .thenThrow(new MessagingException()); mailet.service(mail); }
@Test public void serviceShouldThrowWhenMessageCanNotGetContentType() throws Exception { expectedException.expect(MessagingException.class); MimeMessage message = mock(MimeMessage.class); Mail mail = FakeMail.from(message); when(message.isMimeType(anyString())).thenThrow(new MessagingException()); mailet.service(mail); }
@Test public void serviceShouldKeepMessageAsItIsWhenMessageIsNotTextCalendar() throws Exception { String messageContent = "Content-type: text/html; method=REPLY; charset=UTF-8\n" + "Content-transfer-encoding: 8BIT\n" + "\n" + "BEGIN:VCALENDAR"; MimeMessage message = MimeMessageUtil.mimeMessageFromString(messageContent); Mail mail = FakeMail.builder() .name("name") .mimeMessage(message) .build(); mailet.service(mail); assertThat(mail.getMessage().isMimeType("text/html")).isTrue(); assertThat(mail.getMessage().getDisposition()).isNull(); }
@Test public void serviceShouldChangeMessageContentTypeToMultipartWhenTextCalendarMessage() throws Exception { String messageContent = "Content-type: text/calendar; method=REPLY; charset=UTF-8\n" + "Content-transfer-encoding: 8BIT\n" + "\n" + "BEGIN:VCALENDAR"; MimeMessage message = MimeMessageUtil.mimeMessageFromString(messageContent); Mail mail = FakeMail.builder() .name("name") .mimeMessage(message) .build(); mailet.service(mail); assertThat(mail.getMessage().isMimeType("multipart/*")).isTrue(); }
@Test public void serviceShouldConvertTextBodyOfMessageToAttachmentWhenTextCalendar() throws Exception { String messageContent = "Content-type: text/calendar; method=REPLY; charset=UTF-8\n" + "Content-transfer-encoding: 8BIT\n" + "\n" + "BEGIN:VCALENDAR\n" + "END:VEVENT\n" + "END:VCALENDAR"; MimeMessage message = MimeMessageUtil.mimeMessageFromString(messageContent); Mail mail = FakeMail.builder() .name("name") .mimeMessage(message) .build(); mailet.service(mail); MimeMessage actual = mail.getMessage(); Multipart multipart = (Multipart)actual.getContent(); assertThat(multipart.getCount()).isEqualTo(1); assertThat(multipart.getBodyPart(0).getDisposition()).isEqualTo("attachment"); }
@Test public void contentTypeOfAttachmentShouldBeTakenFromOriginalMessage() throws Exception { Mail mail = FakeMail.builder() .name("name") .mimeMessage(calendarMessage) .build(); mailet.service(mail); Multipart multipart = (Multipart)mail.getMessage().getContent(); int firstBodyPartIndex = 0; BodyPart firstBodyPart = multipart.getBodyPart(firstBodyPartIndex); assertThat(firstBodyPart.getContentType()).isEqualTo("text/calendar; method=REPLY; charset=UTF-8"); }
@Test public void contentTransferEncodingOfAttachmentShouldBeTakenFromOriginalMessage() throws Exception { Mail mail = FakeMail.builder() .name("name") .mimeMessage(calendarMessage) .build(); mailet.service(mail); Multipart multipart = (Multipart)mail.getMessage().getContent(); int firstBodyPartIndex = 0; BodyPart firstBodyPart = multipart.getBodyPart(firstBodyPartIndex); assertThat(firstBodyPart.getHeader("Content-transfer-encoding")).containsExactly("8BIT"); }
@Test public void contentClassOfAttachmentShouldBeTakenFromOriginalMessage() throws Exception { Mail mail = FakeMail.builder() .name("name") .mimeMessage(calendarMessage) .build(); mailet.service(mail); Multipart multipart = (Multipart)mail.getMessage().getContent(); int firstBodyPartIndex = 0; BodyPart firstBodyPart = multipart.getBodyPart(firstBodyPartIndex); assertThat(firstBodyPart.getHeader("Content-class")).containsExactly("urn:content-classes:calendarmessage"); }
@Test public void contentClassHeaderShouldBeRemoveFromOriginalMessage() throws Exception { Mail mail = FakeMail.builder() .name("name") .mimeMessage(calendarMessage) .build(); mailet.service(mail); assertThat(mail.getMessage().getHeader("Content-class")).isNullOrEmpty(); }
@Test public void contentDispositionHeaderShouldBeRemoveFromOriginalMessage() throws Exception { String messageContent = "Content-type: text/calendar; method=REPLY; charset=UTF-8\n" + "Content-Disposition: inline\n" + "\n" + "BEGIN:VCALENDAR\n" + "END:VEVENT\n" + "END:VCALENDAR"; MimeMessage message = MimeMessageUtil.mimeMessageFromString(messageContent); Mail mail = FakeMail.builder() .name("name") .mimeMessage(message) .build(); mailet.service(mail); assertThat(mail.getMessage().getHeader("Content-Disposition")).isNullOrEmpty(); }
@Test public void contentDispositionOfAttachmentShouldBeOverwrittenWhenOriginalMessageHasContentDisposition() throws Exception { String messageContent = "Content-type: text/calendar; method=REPLY; charset=UTF-8\n" + "Content-Disposition: inline\n" + "\n" + "BEGIN:VCALENDAR\n" + "END:VEVENT\n" + "END:VCALENDAR"; MimeMessage message = MimeMessageUtil.mimeMessageFromString(messageContent); Mail mail = FakeMail.builder() .name("name") .mimeMessage(message) .build(); mailet.service(mail); Multipart multipart = (Multipart)mail.getMessage().getContent(); int firstBodyPartIndex = 0; BodyPart firstBodyPart = multipart.getBodyPart(firstBodyPartIndex); assertThat(firstBodyPart.getHeader("Content-Disposition")).containsExactly("attachment"); }