@Override public MessageId fromString(String serialized) { return of(Long.valueOf(serialized)); }
public static InMemoryMessageId of(long value) { return new InMemoryMessageId(value); }
@Override public MessageId generate() { return of(counter.incrementAndGet()); } }
@Override public MessageId createNotUsedMessageId() { return InMemoryMessageId.of(Long.valueOf(lastMessageIdUsed.orElse(FIRST_MESSAGE_ID).serialize()) + ONE_HUNDRED); }
@Test public void textShouldContainsFromWhenFrom() throws Exception { MailboxMessage mailboxMessage = mock(MailboxMessage.class); TestId mailboxId = TestId.of(1); when(mailboxMessage.getMailboxId()) .thenReturn(mailboxId); when(mailboxMessage.getMessageId()) .thenReturn(InMemoryMessageId.of(42)); when(mailboxMessage.getFullContent()) .thenReturn(new ByteArrayInputStream("From: First user <user@james.org>\nFrom: Second user <user2@james.org>".getBytes())); when(mailboxMessage.createFlags()) .thenReturn(new Flags()); when(mailboxMessage.getUid()) .thenReturn(MESSAGE_UID); IndexableMessage indexableMessage = IndexableMessage.builder() .message(mailboxMessage) .users(ImmutableList.of(new MockMailboxSession("username").getUser())) .extractor(new DefaultTextExtractor()) .zoneId(ZoneId.of("Europe/Paris")) .indexAttachments(IndexAttachments.NO) .build(); assertThat(indexableMessage.getText()).isEqualTo("Second user user2@james.org First user user@james.org"); }
@Test public void textShouldContainsCcWhenCc() throws Exception { MailboxMessage mailboxMessage = mock(MailboxMessage.class); TestId mailboxId = TestId.of(1); when(mailboxMessage.getMailboxId()) .thenReturn(mailboxId); when(mailboxMessage.getMessageId()) .thenReturn(InMemoryMessageId.of(42)); when(mailboxMessage.getFullContent()) .thenReturn(new ByteArrayInputStream("Cc: First cc <user@james.org>\nCc: Second cc <user2@james.org>".getBytes())); when(mailboxMessage.createFlags()) .thenReturn(new Flags()); when(mailboxMessage.getUid()) .thenReturn(MESSAGE_UID); IndexableMessage indexableMessage = IndexableMessage.builder() .message(mailboxMessage) .users(ImmutableList.of(new MockMailboxSession("username").getUser())) .extractor(new DefaultTextExtractor()) .zoneId(ZoneId.of("Europe/Paris")) .indexAttachments(IndexAttachments.NO) .build(); assertThat(indexableMessage.getText()).isEqualTo("First cc user@james.org Second cc user2@james.org"); }
@Test public void textShouldContainsToWhenTo() throws Exception { MailboxMessage mailboxMessage = mock(MailboxMessage.class); TestId mailboxId = TestId.of(1); when(mailboxMessage.getMailboxId()) .thenReturn(mailboxId); when(mailboxMessage.getMessageId()) .thenReturn(InMemoryMessageId.of(42)); when(mailboxMessage.getFullContent()) .thenReturn(new ByteArrayInputStream("To: First to <user@james.org>\nTo: Second to <user2@james.org>".getBytes())); when(mailboxMessage.createFlags()) .thenReturn(new Flags()); when(mailboxMessage.getUid()) .thenReturn(MESSAGE_UID); IndexableMessage indexableMessage = IndexableMessage.builder() .message(mailboxMessage) .users(ImmutableList.of(new MockMailboxSession("username").getUser())) .extractor(new DefaultTextExtractor()) .zoneId(ZoneId.of("Europe/Paris")) .indexAttachments(IndexAttachments.NO) .build(); assertThat(indexableMessage.getText()).isEqualTo("First to user@james.org Second to user2@james.org"); }
@Test public void textShouldContainsBccWhenBcc() throws Exception { MailboxMessage mailboxMessage = mock(MailboxMessage.class); TestId mailboxId = TestId.of(1); when(mailboxMessage.getMailboxId()) .thenReturn(mailboxId); when(mailboxMessage.getMessageId()) .thenReturn(InMemoryMessageId.of(42)); when(mailboxMessage.getUid()) .thenReturn(MESSAGE_UID); when(mailboxMessage.getFullContent()) .thenReturn(new ByteArrayInputStream("Bcc: First bcc <user@james.org>\nBcc: Second bcc <user2@james.org>".getBytes())); when(mailboxMessage.createFlags()) .thenReturn(new Flags()); IndexableMessage indexableMessage = IndexableMessage.builder() .message(mailboxMessage) .users(ImmutableList.of(new MockMailboxSession("username").getUser())) .extractor(new DefaultTextExtractor()) .zoneId(ZoneId.of("Europe/Paris")) .indexAttachments(IndexAttachments.NO) .build(); assertThat(indexableMessage.getText()).isEqualTo("Second bcc user2@james.org First bcc user@james.org"); }
@Test public void textShouldContainsBodyWhenBody() throws Exception { MailboxMessage mailboxMessage = mock(MailboxMessage.class); TestId mailboxId = TestId.of(1); when(mailboxMessage.getMailboxId()) .thenReturn(mailboxId); when(mailboxMessage.getMessageId()) .thenReturn(InMemoryMessageId.of(42)); when(mailboxMessage.getFullContent()) .thenReturn(new ByteArrayInputStream("\nMy body".getBytes())); when(mailboxMessage.createFlags()) .thenReturn(new Flags()); when(mailboxMessage.getUid()) .thenReturn(MESSAGE_UID); IndexableMessage indexableMessage = IndexableMessage.builder() .message(mailboxMessage) .users(ImmutableList.of(new MockMailboxSession("username").getUser())) .extractor(new DefaultTextExtractor()) .zoneId(ZoneId.of("Europe/Paris")) .indexAttachments(IndexAttachments.NO) .build(); assertThat(indexableMessage.getText()).isEqualTo("My body"); }
@Test public void hasAttachmentsShouldReturnFalseWhenPropertyIsPresentButFalse() throws IOException { //Given MailboxMessage mailboxMessage = mock(MailboxMessage.class); TestId mailboxId = TestId.of(1); when(mailboxMessage.getMailboxId()) .thenReturn(mailboxId); when(mailboxMessage.getMessageId()) .thenReturn(InMemoryMessageId.of(42)); when(mailboxMessage.getFullContent()) .thenReturn(ClassLoader.getSystemResourceAsStream("eml/mailWithHeaders.eml")); when(mailboxMessage.createFlags()) .thenReturn(new Flags()); when(mailboxMessage.getUid()) .thenReturn(MESSAGE_UID); when(mailboxMessage.getProperties()) .thenReturn(ImmutableList.of(new SimpleProperty(PropertyBuilder.JAMES_INTERNALS, PropertyBuilder.HAS_ATTACHMENT, "false"))); // When IndexableMessage indexableMessage = IndexableMessage.builder() .message(mailboxMessage) .users(ImmutableList.of(new MockMailboxSession("username").getUser())) .extractor(new DefaultTextExtractor()) .zoneId(ZoneId.of("Europe/Paris")) .indexAttachments(IndexAttachments.NO) .build(); // Then assertThat(indexableMessage.getHasAttachment()).isFalse(); }
@Test public void messageShouldBeIndexedEvenIfTikaParserThrowsAnError() throws Exception { //Given MailboxMessage mailboxMessage = mock(MailboxMessage.class); TestId mailboxId = TestId.of(1); when(mailboxMessage.getMailboxId()) .thenReturn(mailboxId); when(mailboxMessage.getMessageId()) .thenReturn(InMemoryMessageId.of(42)); when(mailboxMessage.getFullContent()) .thenReturn(ClassLoader.getSystemResourceAsStream("eml/bodyMakeTikaToFail.eml")); when(mailboxMessage.createFlags()) .thenReturn(new Flags()); when(mailboxMessage.getUid()) .thenReturn(MESSAGE_UID); // When IndexableMessage indexableMessage = IndexableMessage.builder() .message(mailboxMessage) .users(ImmutableList.of(new MockMailboxSession("username").getUser())) .extractor(textExtractor) .zoneId(ZoneId.of("Europe/Paris")) .indexAttachments(IndexAttachments.YES) .build(); // Then assertThat(indexableMessage.getText()).contains("subject should be parsed"); }
@Test public void textShouldContainsSubjectsWhenSubjects() throws Exception { MailboxMessage mailboxMessage = mock(MailboxMessage.class); TestId mailboxId = TestId.of(1); when(mailboxMessage.getMailboxId()) .thenReturn(mailboxId); when(mailboxMessage.getMessageId()) .thenReturn(InMemoryMessageId.of(42)); when(mailboxMessage.getFullContent()) .thenReturn(new ByteArrayInputStream("Subject: subject1\nSubject: subject2".getBytes())); when(mailboxMessage.createFlags()) .thenReturn(new Flags()); when(mailboxMessage.getUid()) .thenReturn(MESSAGE_UID); IndexableMessage indexableMessage = IndexableMessage.builder() .message(mailboxMessage) .users(ImmutableList.of(new MockMailboxSession("username").getUser())) .extractor(new DefaultTextExtractor()) .zoneId(ZoneId.of("Europe/Paris")) .indexAttachments(IndexAttachments.NO) .build(); assertThat(indexableMessage.getText()).isEqualTo("subject1 subject2"); }
@Test public void hasAttachmentsShouldReturnTrueWhenPropertyIsPresentAndTrue() throws IOException { //Given MailboxMessage mailboxMessage = mock(MailboxMessage.class); TestId mailboxId = TestId.of(1); when(mailboxMessage.getMailboxId()) .thenReturn(mailboxId); when(mailboxMessage.getMessageId()) .thenReturn(InMemoryMessageId.of(42)); when(mailboxMessage.getFullContent()) .thenReturn(ClassLoader.getSystemResourceAsStream("eml/mailWithHeaders.eml")); when(mailboxMessage.createFlags()) .thenReturn(new Flags()); when(mailboxMessage.getUid()) .thenReturn(MESSAGE_UID); when(mailboxMessage.getProperties()).thenReturn(ImmutableList.of(IndexableMessage.HAS_ATTACHMENT_PROPERTY)); // When IndexableMessage indexableMessage = IndexableMessage.builder() .message(mailboxMessage) .users(ImmutableList.of(new MockMailboxSession("username").getUser())) .extractor(new DefaultTextExtractor()) .zoneId(ZoneId.of("Europe/Paris")) .indexAttachments(IndexAttachments.YES) .build(); // Then assertThat(indexableMessage.getHasAttachment()).isTrue(); }
.thenReturn(mailboxId); when(mailboxMessage.getMessageId()) .thenReturn(InMemoryMessageId.of(42)); when(mailboxMessage.getFullContent()) .thenReturn(ClassLoader.getSystemResourceAsStream("eml/emailWith3Attachments.eml"));
.thenReturn(mailboxId); when(mailboxMessage.getMessageId()) .thenReturn(InMemoryMessageId.of(42)); when(mailboxMessage.getFullContent()) .thenReturn(ClassLoader.getSystemResourceAsStream("eml/mailWithHeaders.eml"));
@Test public void hasAttachmentsShouldReturnFalseWhenPropertyIsAbsent() throws IOException { //Given MailboxMessage mailboxMessage = mock(MailboxMessage.class); TestId mailboxId = TestId.of(1); when(mailboxMessage.getMailboxId()) .thenReturn(mailboxId); when(mailboxMessage.getMessageId()) .thenReturn(InMemoryMessageId.of(42)); when(mailboxMessage.getFullContent()) .thenReturn(ClassLoader.getSystemResourceAsStream("eml/mailWithHeaders.eml")); when(mailboxMessage.createFlags()) .thenReturn(new Flags()); when(mailboxMessage.getUid()) .thenReturn(MESSAGE_UID); when(mailboxMessage.getProperties()) .thenReturn(ImmutableList.of()); // When IndexableMessage indexableMessage = IndexableMessage.builder() .message(mailboxMessage) .users(ImmutableList.of(new MockMailboxSession("username").getUser())) .extractor(new DefaultTextExtractor()) .zoneId(ZoneId.of("Europe/Paris")) .indexAttachments(IndexAttachments.NO) .build(); // Then assertThat(indexableMessage.getHasAttachment()).isFalse(); }
@Test public void textShouldBeEmptyWhenNoMatchingHeaders() throws Exception { MailboxMessage mailboxMessage = mock(MailboxMessage.class); TestId mailboxId = TestId.of(1); when(mailboxMessage.getMailboxId()) .thenReturn(mailboxId); when(mailboxMessage.getMessageId()) .thenReturn(InMemoryMessageId.of(42)); when(mailboxMessage.getFullContent()) .thenReturn(new ByteArrayInputStream("".getBytes())); when(mailboxMessage.createFlags()) .thenReturn(new Flags()); when(mailboxMessage.getUid()) .thenReturn(MESSAGE_UID); IndexableMessage indexableMessage = IndexableMessage.builder() .message(mailboxMessage) .users(ImmutableList.of(new MockMailboxSession("username").getUser())) .extractor(new DefaultTextExtractor()) .zoneId(ZoneId.of("Europe/Paris")) .indexAttachments(IndexAttachments.NO) .build(); assertThat(indexableMessage.getText()).isEmpty(); }
@Test public void attachmentsShouldNotBeenIndexedWhenAsked() throws Exception { //Given MailboxMessage mailboxMessage = mock(MailboxMessage.class); TestId mailboxId = TestId.of(1); when(mailboxMessage.getMailboxId()) .thenReturn(mailboxId); when(mailboxMessage.getMessageId()) .thenReturn(InMemoryMessageId.of(42)); when(mailboxMessage.getFullContent()) .thenReturn(ClassLoader.getSystemResourceAsStream("eml/mailWithHeaders.eml")); when(mailboxMessage.createFlags()) .thenReturn(new Flags()); when(mailboxMessage.getUid()) .thenReturn(MESSAGE_UID); // When IndexableMessage indexableMessage = IndexableMessage.builder() .message(mailboxMessage) .users(ImmutableList.of(new MockMailboxSession("username").getUser())) .extractor(new DefaultTextExtractor()) .zoneId(ZoneId.of("Europe/Paris")) .indexAttachments(IndexAttachments.NO) .build(); // Then assertThat(indexableMessage.getAttachments()).isEmpty(); }
@Test public void attachmentsShouldBeenIndexedWhenAsked() throws Exception { //Given MailboxMessage mailboxMessage = mock(MailboxMessage.class); TestId mailboxId = TestId.of(1); when(mailboxMessage.getMailboxId()) .thenReturn(mailboxId); when(mailboxMessage.getMessageId()) .thenReturn(InMemoryMessageId.of(42)); when(mailboxMessage.getFullContent()) .thenReturn(ClassLoader.getSystemResourceAsStream("eml/emailWith3Attachments.eml")); when(mailboxMessage.createFlags()) .thenReturn(new Flags()); when(mailboxMessage.getUid()) .thenReturn(MESSAGE_UID); // When IndexableMessage indexableMessage = IndexableMessage.builder() .message(mailboxMessage) .users(ImmutableList.of(new MockMailboxSession("username").getUser())) .extractor(new DefaultTextExtractor()) .zoneId(ZoneId.of("Europe/Paris")) .indexAttachments(IndexAttachments.YES) .build(); // Then assertThat(indexableMessage.getAttachments()).isNotEmpty(); }