MessageToElasticSearchJson { public String convertToJson(MailboxMessage message, List<Username> usernames) throws JsonProcessingException { Preconditions.checkNotNull(message); return mapper.writeValueAsString(IndexableMessage.builder() .message(message) .extractor(textExtractor) .zoneId(zoneId) .indexAttachments(indexAttachments) .build()); } MessageToElasticSearchJson(TextExtractor textExtractor, ZoneId zoneId, IndexAttachments indexAttachments); @Inject  MessageToElasticSearchJson(TextExtractor textExtractor, IndexAttachments indexAttachments); String convertToJson(MailboxMessage message, List<Username> usernames); String convertToJsonWithoutAttachment(MailboxMessage message, List<Username> usernames); String getUpdatedJsonMessagePart(Flags flags, ModSeq modSeq);  }
@Test void spamEmailShouldBeWellConvertedToJson() throws IOException { MessageToElasticSearchJson messageToElasticSearchJson = new MessageToElasticSearchJson( new DefaultTextExtractor(), ZoneId.of("Europe/Paris"), IndexAttachments.YES); MailboxMessage spamMail = new SimpleMailboxMessage(MESSAGE_ID, date, SIZE, BODY_START_OCTET, ClassLoaderUtils.getSystemResourceAsSharedStream("eml/spamMail.eml"), new Flags(), propertyBuilder, MAILBOX_ID); spamMail.setUid(UID); spamMail.setModSeq(MOD_SEQ); assertThatJson(messageToElasticSearchJson.convertToJson(spamMail, ImmutableList.of(USERNAME))) .when(IGNORING_ARRAY_ORDER) .isEqualTo(ClassLoaderUtils.getSystemResourceAsString("eml/spamMail.json")); }
@Test void invalidCharsetShouldBeWellConvertedToJson() throws IOException { MessageToElasticSearchJson messageToElasticSearchJson = new MessageToElasticSearchJson( new DefaultTextExtractor(), ZoneId.of("Europe/Paris"), IndexAttachments.YES); MailboxMessage spamMail = new SimpleMailboxMessage(MESSAGE_ID, date, SIZE, BODY_START_OCTET, ClassLoaderUtils.getSystemResourceAsSharedStream("eml/invalidCharset.eml"), new Flags(), propertyBuilder, MAILBOX_ID); spamMail.setUid(UID); spamMail.setModSeq(MOD_SEQ); String actual = messageToElasticSearchJson.convertToJson(spamMail, ImmutableList.of(USERNAME)); assertThatJson(actual) .when(IGNORING_ARRAY_ORDER) .isEqualTo(ClassLoaderUtils.getSystemResourceAsString("eml/invalidCharset.json")); }
@Test void htmlEmailShouldBeWellConvertedToJson() throws IOException { MessageToElasticSearchJson messageToElasticSearchJson = new MessageToElasticSearchJson( new DefaultTextExtractor(), ZoneId.of("Europe/Paris"), IndexAttachments.YES); MailboxMessage htmlMail = new SimpleMailboxMessage(MESSAGE_ID, date, SIZE, BODY_START_OCTET, ClassLoaderUtils.getSystemResourceAsSharedStream("eml/htmlMail.eml"), new FlagsBuilder().add(Flags.Flag.DELETED, Flags.Flag.SEEN).add("social", "pocket-money").build(), propertyBuilder, MAILBOX_ID); htmlMail.setModSeq(MOD_SEQ); htmlMail.setUid(UID); assertThatJson(messageToElasticSearchJson.convertToJson(htmlMail, ImmutableList.of(USERNAME))) .when(IGNORING_ARRAY_ORDER) .isEqualTo(ClassLoaderUtils.getSystemResourceAsString("eml/htmlMail.json")); }
@Test void pgpSignedEmailShouldBeWellConvertedToJson() throws IOException { MessageToElasticSearchJson messageToElasticSearchJson = new MessageToElasticSearchJson( new DefaultTextExtractor(), ZoneId.of("Europe/Paris"), IndexAttachments.YES); MailboxMessage pgpSignedMail = new SimpleMailboxMessage(MESSAGE_ID, date, SIZE, BODY_START_OCTET, ClassLoaderUtils.getSystemResourceAsSharedStream("eml/pgpSignedMail.eml"), new FlagsBuilder().add(Flags.Flag.DELETED, Flags.Flag.SEEN).add("debian", "security").build(), propertyBuilder, MAILBOX_ID); pgpSignedMail.setModSeq(MOD_SEQ); pgpSignedMail.setUid(UID); assertThatJson(messageToElasticSearchJson.convertToJson(pgpSignedMail, ImmutableList.of(USERNAME))) .when(IGNORING_ARRAY_ORDER) .isEqualTo(ClassLoaderUtils.getSystemResourceAsString("eml/pgpSignedMail.json")); }
@Test void simpleEmailShouldBeWellConvertedToJson() throws IOException { MessageToElasticSearchJson messageToElasticSearchJson = new MessageToElasticSearchJson( new DefaultTextExtractor(), ZoneId.of("Europe/Paris"), IndexAttachments.YES); MailboxMessage mail = new SimpleMailboxMessage(MESSAGE_ID, date, SIZE, BODY_START_OCTET, ClassLoaderUtils.getSystemResourceAsSharedStream("eml/mail.eml"), new FlagsBuilder().add(Flags.Flag.DELETED, Flags.Flag.SEEN).add("debian", "security").build(), propertyBuilder, MAILBOX_ID); mail.setModSeq(MOD_SEQ); mail.setUid(UID); assertThatJson(messageToElasticSearchJson.convertToJson(mail, ImmutableList.of(Username.of("user1"), Username.of("user2")))) .when(IGNORING_ARRAY_ORDER).when(IGNORING_VALUES) .isEqualTo(ClassLoaderUtils.getSystemResourceAsString("eml/mail.json")); }
@Test void recursiveEmailShouldBeWellConvertedToJson() throws IOException { MessageToElasticSearchJson messageToElasticSearchJson = new MessageToElasticSearchJson( new DefaultTextExtractor(), ZoneId.of("Europe/Paris"), IndexAttachments.YES); MailboxMessage recursiveMail = new SimpleMailboxMessage(MESSAGE_ID, date, SIZE, BODY_START_OCTET, ClassLoaderUtils.getSystemResourceAsSharedStream("eml/recursiveMail.eml"), new FlagsBuilder().add(Flags.Flag.DELETED, Flags.Flag.SEEN).add("debian", "security").build(), propertyBuilder, MAILBOX_ID); recursiveMail.setModSeq(MOD_SEQ); recursiveMail.setUid(UID); assertThatJson(messageToElasticSearchJson.convertToJson(recursiveMail, ImmutableList.of(USERNAME))) .when(IGNORING_ARRAY_ORDER).when(IGNORING_VALUES) .isEqualTo(ClassLoaderUtils.getSystemResourceAsString("eml/recursiveMail.json")); }
@Test void emailWithNoInternalDateShouldUseNowDate() throws IOException { MessageToElasticSearchJson messageToElasticSearchJson = new MessageToElasticSearchJson( new DefaultTextExtractor(), ZoneId.of("Europe/Paris"), IndexAttachments.YES); MailboxMessage mailWithNoInternalDate = new SimpleMailboxMessage(MESSAGE_ID, null, SIZE, BODY_START_OCTET, ClassLoaderUtils.getSystemResourceAsSharedStream("eml/recursiveMail.eml"), new FlagsBuilder().add(Flags.Flag.DELETED, Flags.Flag.SEEN).add("debian", "security").build(), propertyBuilder, MAILBOX_ID); mailWithNoInternalDate.setModSeq(MOD_SEQ); mailWithNoInternalDate.setUid(UID); assertThatJson(messageToElasticSearchJson.convertToJson(mailWithNoInternalDate, ImmutableList.of(USERNAME))) .when(IGNORING_ARRAY_ORDER) .when(IGNORING_VALUES) .isEqualTo(ClassLoaderUtils.getSystemResourceAsString("eml/recursiveMail.json")); }
@Test void emailWithAttachmentsShouldConvertAttachmentsWhenIndexAttachmentsIsTrue() throws IOException { MailboxMessage mailWithNoInternalDate = new SimpleMailboxMessage(MESSAGE_ID, null, SIZE, BODY_START_OCTET, ClassLoaderUtils.getSystemResourceAsSharedStream("eml/recursiveMail.eml"), new FlagsBuilder().add(Flags.Flag.DELETED, Flags.Flag.SEEN).add("debian", "security").build(), propertyBuilder, MAILBOX_ID); mailWithNoInternalDate.setModSeq(MOD_SEQ); mailWithNoInternalDate.setUid(UID); MessageToElasticSearchJson messageToElasticSearchJson = new MessageToElasticSearchJson( new DefaultTextExtractor(), ZoneId.of("Europe/Paris"), IndexAttachments.YES); String convertToJson = messageToElasticSearchJson.convertToJson(mailWithNoInternalDate, ImmutableList.of(USERNAME)); assertThatJson(convertToJson) .when(IGNORING_ARRAY_ORDER) .when(IGNORING_VALUES) .isEqualTo(ClassLoaderUtils.getSystemResourceAsString("eml/recursiveMail.json")); }
@Test void emailWithAttachmentsShouldNotConvertAttachmentsWhenIndexAttachmentsIsFalse() throws IOException { MailboxMessage mailWithNoInternalDate = new SimpleMailboxMessage(MESSAGE_ID, null, SIZE, BODY_START_OCTET, ClassLoaderUtils.getSystemResourceAsSharedStream("eml/recursiveMail.eml"), new FlagsBuilder().add(Flags.Flag.DELETED, Flags.Flag.SEEN).add("debian", "security").build(), propertyBuilder, MAILBOX_ID); mailWithNoInternalDate.setModSeq(MOD_SEQ); mailWithNoInternalDate.setUid(UID); MessageToElasticSearchJson messageToElasticSearchJson = new MessageToElasticSearchJson( new DefaultTextExtractor(), ZoneId.of("Europe/Paris"), IndexAttachments.NO); String convertToJson = messageToElasticSearchJson.convertToJson(mailWithNoInternalDate, ImmutableList.of(USERNAME)); assertThatJson(convertToJson) .when(IGNORING_ARRAY_ORDER) .when(IGNORING_VALUES) .isEqualTo(ClassLoaderUtils.getSystemResourceAsString("eml/recursiveMailWithoutAttachments.json")); }
@Test void emailWithNoMailboxIdShouldThrow() { MessageToElasticSearchJson messageToElasticSearchJson = new MessageToElasticSearchJson( new DefaultTextExtractor(), ZoneId.of("Europe/Paris"), IndexAttachments.YES); MailboxMessage mailWithNoMailboxId = new SimpleMailboxMessage(MESSAGE_ID, date, SIZE, BODY_START_OCTET, ClassLoaderUtils.getSystemResourceAsSharedStream("eml/recursiveMail.eml"), new FlagsBuilder().add(Flags.Flag.DELETED, Flags.Flag.SEEN).add("debian", "security").build(), propertyBuilder, null); mailWithNoMailboxId.setModSeq(MOD_SEQ); mailWithNoMailboxId.setUid(UID); assertThatThrownBy(() -> messageToElasticSearchJson.convertToJson(mailWithNoMailboxId, ImmutableList.of(USERNAME))) .isInstanceOf(NullPointerException.class); }
@Test void spamEmailShouldBeWellConvertedToJsonWithApacheTika() throws IOException { MessageToElasticSearchJson messageToElasticSearchJson = new MessageToElasticSearchJson( textExtractor, ZoneId.of("Europe/Paris"), IndexAttachments.YES); MailboxMessage spamMail = new SimpleMailboxMessage(MESSAGE_ID, date, SIZE, BODY_START_OCTET, ClassLoaderUtils.getSystemResourceAsSharedStream("eml/nonTextual.eml"), new Flags(), propertyBuilder, MAILBOX_ID); spamMail.setUid(UID); spamMail.setModSeq(MOD_SEQ); assertThatJson(messageToElasticSearchJson.convertToJson(spamMail, ImmutableList.of(USERNAME))) .when(IGNORING_ARRAY_ORDER) .isEqualTo( ClassLoaderUtils.getSystemResourceAsString("eml/nonTextual.json", StandardCharsets.UTF_8)); }