Repository: james-project Updated Branches: refs/heads/master 7abdb538b -> df6ad23db
MAILBOX-278 Renaming class tests to match tested class and migrate it to Mockito Project: http://git-wip-us.apache.org/repos/asf/james-project/repo Commit: http://git-wip-us.apache.org/repos/asf/james-project/commit/fed30079 Tree: http://git-wip-us.apache.org/repos/asf/james-project/tree/fed30079 Diff: http://git-wip-us.apache.org/repos/asf/james-project/diff/fed30079 Branch: refs/heads/master Commit: fed30079cbec38048075c66b28edfdc99ace9f02 Parents: 41c860c Author: Laura Royet <[email protected]> Authored: Wed Dec 7 12:18:14 2016 +0100 Committer: Quynh Nguyen <[email protected]> Committed: Tue Feb 7 15:14:47 2017 +0700 ---------------------------------------------------------------------- ...hListeningMailboxMessageSearchIndexTest.java | 281 --------------- ...icSearchListeningMessageSearchIndexTest.java | 281 +++++++++++++++ .../MailboxMessageToElasticSearchJsonTest.java | 357 ------------------- .../json/MessageToElasticSearchJsonTest.java | 357 +++++++++++++++++++ 4 files changed, 638 insertions(+), 638 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/james-project/blob/fed30079/mailbox/elasticsearch/src/test/java/org/apache/james/mailbox/elasticsearch/events/ElasticSearchListeningMailboxMessageSearchIndexTest.java ---------------------------------------------------------------------- diff --git a/mailbox/elasticsearch/src/test/java/org/apache/james/mailbox/elasticsearch/events/ElasticSearchListeningMailboxMessageSearchIndexTest.java b/mailbox/elasticsearch/src/test/java/org/apache/james/mailbox/elasticsearch/events/ElasticSearchListeningMailboxMessageSearchIndexTest.java deleted file mode 100644 index 3efe045..0000000 --- a/mailbox/elasticsearch/src/test/java/org/apache/james/mailbox/elasticsearch/events/ElasticSearchListeningMailboxMessageSearchIndexTest.java +++ /dev/null @@ -1,281 +0,0 @@ -/**************************************************************** - * Licensed to the Apache Software Foundation (ASF) under one * - * or more contributor license agreements. See the NOTICE file * - * distributed with this work for additional information * - * regarding copyright ownership. The ASF licenses this file * - * to you under the Apache License, Version 2.0 (the * - * "License"); you may not use this file except in compliance * - * with the License. You may obtain a copy of the License at * - * * - * http://www.apache.org/licenses/LICENSE-2.0 * - * * - * Unless required by applicable law or agreed to in writing, * - * software distributed under the License is distributed on an * - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * - * KIND, either express or implied. See the License for the * - * specific language governing permissions and limitations * - * under the License. * - ****************************************************************/ -package org.apache.james.mailbox.elasticsearch.events; - -import static org.easymock.EasyMock.anyLong; -import static org.easymock.EasyMock.anyObject; -import static org.easymock.EasyMock.anyString; -import static org.easymock.EasyMock.createControl; -import static org.easymock.EasyMock.eq; -import static org.easymock.EasyMock.expect; -import static org.easymock.EasyMock.expectLastCall; - -import java.io.IOException; -import java.util.List; - -import javax.mail.Flags; - -import org.apache.james.backends.es.ElasticSearchIndexer; -import org.apache.james.mailbox.MailboxSession; -import org.apache.james.mailbox.MessageUid; -import org.apache.james.mailbox.MailboxSession.User; -import org.apache.james.mailbox.elasticsearch.json.MessageToElasticSearchJson; -import org.apache.james.mailbox.elasticsearch.search.ElasticSearchSearcher; -import org.apache.james.mailbox.model.TestId; -import org.apache.james.mailbox.model.UpdatedFlags; -import org.apache.james.mailbox.store.mail.MessageMapperFactory; -import org.apache.james.mailbox.store.mail.model.Mailbox; -import org.apache.james.mailbox.store.mail.model.MailboxMessage; -import org.easymock.EasyMock; -import org.easymock.IMocksControl; -import org.elasticsearch.ElasticsearchException; -import org.elasticsearch.action.bulk.BulkResponse; -import org.elasticsearch.action.index.IndexResponse; -import org.elasticsearch.index.query.QueryBuilder; -import org.junit.Before; -import org.junit.Test; - -import com.fasterxml.jackson.core.JsonProcessingException; -import com.google.common.collect.Lists; - -public class ElasticSearchListeningMailboxMessageSearchIndexTest { - - public static final long MODSEQ = 18L; - private IMocksControl control; - - private ElasticSearchIndexer indexer; - private ElasticSearchListeningMessageSearchIndex testee; - - @Before - public void setup() throws JsonProcessingException { - control = createControl(); - - MessageMapperFactory mapperFactory = control.createMock(MessageMapperFactory.class); - MessageToElasticSearchJson messageToElasticSearchJson = control.createMock(MessageToElasticSearchJson.class); - ElasticSearchSearcher elasticSearchSearcher = control.createMock(ElasticSearchSearcher.class); - - indexer = control.createMock(ElasticSearchIndexer.class); - - List<User> users = anyObject(); - expect(messageToElasticSearchJson.convertToJson(anyObject(MailboxMessage.class), users)).andReturn("json content").anyTimes(); - expect(messageToElasticSearchJson.getUpdatedJsonMessagePart(anyObject(Flags.class), anyLong())).andReturn("json updated content").anyTimes(); - - testee = new ElasticSearchListeningMessageSearchIndex(mapperFactory, indexer, elasticSearchSearcher, messageToElasticSearchJson); - } - - @Test - public void addShouldIndex() throws Exception { - MailboxSession.User user = control.createMock(MailboxSession.User.class); - MailboxSession session = control.createMock(MailboxSession.class); - expect(session.getUser()) - .andReturn(user); - - Mailbox mailbox = control.createMock(Mailbox.class); - MessageUid messageUid = MessageUid.of(1); - TestId mailboxId = TestId.of(12); - expect(mailbox.getMailboxId()).andReturn(mailboxId); - MailboxMessage message = mockedMessage(messageUid); - - IndexResponse expectedIndexResponse = control.createMock(IndexResponse.class); - expect(indexer.indexMessage(eq(mailboxId.serialize() + ":" + messageUid.asLong()), anyString())) - .andReturn(expectedIndexResponse); - - control.replay(); - testee.add(session, mailbox, message); - control.verify(); - } - - private MailboxMessage mockedMessage(MessageUid messageId) throws IOException { - MailboxMessage message = control.createMock(MailboxMessage.class); - expect(message.getUid()).andReturn(messageId).anyTimes(); - return message; - } - - @Test - public void addShouldNotPropagateExceptionWhenExceptionOccurs() throws Exception { - MailboxSession.User user = control.createMock(MailboxSession.User.class); - MailboxSession session = control.createMock(MailboxSession.class); - expect(session.getUser()) - .andReturn(user); - - Mailbox mailbox = control.createMock(Mailbox.class); - - MessageUid messageUid = MessageUid.of(1); - TestId mailboxId = TestId.of(12); - MailboxMessage message = mockedMessage(messageUid); - expect(mailbox.getMailboxId()).andReturn(mailboxId); - - expect(indexer.indexMessage(eq(mailboxId.serialize() + ":" + messageUid.asLong()), anyString())) - .andThrow(new ElasticsearchException("")); - - control.replay(); - testee.add(session, mailbox, message); - control.verify(); - } - - @Test - @SuppressWarnings("unchecked") - public void deleteShouldWork() throws Exception { - MailboxSession session = control.createMock(MailboxSession.class); - Mailbox mailbox = control.createMock(Mailbox.class); - MessageUid messageUid = MessageUid.of(1); - TestId mailboxId = TestId.of(12); - expect(mailbox.getMailboxId()).andReturn(mailboxId); - - BulkResponse expectedBulkResponse = control.createMock(BulkResponse.class); - expect(indexer.deleteMessages(anyObject(List.class))) - .andReturn(expectedBulkResponse); - - control.replay(); - testee.delete(session, mailbox, Lists.newArrayList(messageUid)); - control.verify(); - } - - @Test - @SuppressWarnings("unchecked") - public void deleteShouldWorkWhenMultipleMessageIds() throws Exception { - MailboxSession session = control.createMock(MailboxSession.class); - Mailbox mailbox = control.createMock(Mailbox.class); - MessageUid messageUid1 = MessageUid.of(1); - MessageUid messageUid2 = MessageUid.of(2); - MessageUid messageUid3 = MessageUid.of(3); - MessageUid messageUid4 = MessageUid.of(4); - MessageUid messageUid5 = MessageUid.of(5); - TestId mailboxId = TestId.of(12); - expect(mailbox.getMailboxId()).andReturn(mailboxId).times(5); - - BulkResponse expectedBulkResponse = control.createMock(BulkResponse.class); - expect(indexer.deleteMessages(anyObject(List.class))) - .andReturn(expectedBulkResponse); - - control.replay(); - testee.delete(session, mailbox, Lists.newArrayList(messageUid1, messageUid2, messageUid3, messageUid4, messageUid5)); - control.verify(); - } - - @Test - @SuppressWarnings("unchecked") - public void deleteShouldNotPropagateExceptionWhenExceptionOccurs() throws Exception { - MailboxSession session = control.createMock(MailboxSession.class); - Mailbox mailbox = control.createMock(Mailbox.class); - MessageUid messageUid = MessageUid.of(1); - TestId mailboxId = TestId.of(12); - expect(mailbox.getMailboxId()).andReturn(mailboxId).times(2); - - expect(indexer.deleteMessages(anyObject(List.class))) - .andThrow(new ElasticsearchException("")); - - control.replay(); - testee.delete(session, mailbox, Lists.newArrayList(messageUid)); - control.verify(); - } - - @Test - @SuppressWarnings("unchecked") - public void updateShouldWork() throws Exception { - MailboxSession session = control.createMock(MailboxSession.class); - - Mailbox mailbox = control.createMock(Mailbox.class); - - Flags flags = new Flags(); - MessageUid messageUid = MessageUid.of(1); - UpdatedFlags updatedFlags = UpdatedFlags.builder() - .uid(messageUid) - .modSeq(MODSEQ) - .oldFlags(flags) - .newFlags(flags) - .build(); - TestId mailboxId = TestId.of(12); - - expectLastCall(); - expect(mailbox.getMailboxId()).andReturn(mailboxId); - - BulkResponse expectedBulkResponse = control.createMock(BulkResponse.class); - expect(indexer.updateMessages(anyObject(List.class))) - .andReturn(expectedBulkResponse); - - control.replay(); - testee.update(session, mailbox, Lists.newArrayList(updatedFlags)); - control.verify(); - } - - @Test - @SuppressWarnings("unchecked") - public void updateShouldNotPropagateExceptionWhenExceptionOccurs() throws Exception { - MailboxSession session = control.createMock(MailboxSession.class); - - Mailbox mailbox = control.createMock(Mailbox.class); - Flags flags = new Flags(); - MessageUid messageUid = MessageUid.of(1); - UpdatedFlags updatedFlags = UpdatedFlags.builder() - .uid(messageUid) - .modSeq(MODSEQ) - .oldFlags(flags) - .newFlags(flags) - .build(); - TestId mailboxId = TestId.of(12); - - expectLastCall(); - expect(mailbox.getMailboxId()).andReturn(mailboxId).times(2); - - expect(indexer.updateMessages(anyObject(List.class))) - .andThrow(new ElasticsearchException("")); - - control.replay(); - testee.update(session, mailbox, Lists.newArrayList(updatedFlags)); - control.verify(); - } - - @Test - public void deleteAllShouldWork() throws Exception { - MailboxSession session = control.createMock(MailboxSession.class); - - Mailbox mailbox = control.createMock(Mailbox.class); - - TestId mailboxId = TestId.of(12); - - expectLastCall(); - expect(mailbox.getMailboxId()).andReturn(mailboxId); - - indexer.deleteAllMatchingQuery(anyObject(QueryBuilder.class)); - EasyMock.expectLastCall(); - - control.replay(); - testee.deleteAll(session, mailbox); - control.verify(); - } - - @Test - public void deleteAllShouldNotPropagateExceptionWhenExceptionOccurs() throws Exception { - MailboxSession session = control.createMock(MailboxSession.class); - - Mailbox mailbox = control.createMock(Mailbox.class); - TestId mailboxId = TestId.of(12); - - expectLastCall(); - expect(mailbox.getMailboxId()).andReturn(mailboxId).times(2); - - indexer.deleteAllMatchingQuery(anyObject(QueryBuilder.class)); - EasyMock.expectLastCall().andThrow(new ElasticsearchException("")); - - control.replay(); - testee.deleteAll(session, mailbox); - control.verify(); - } -} http://git-wip-us.apache.org/repos/asf/james-project/blob/fed30079/mailbox/elasticsearch/src/test/java/org/apache/james/mailbox/elasticsearch/events/ElasticSearchListeningMessageSearchIndexTest.java ---------------------------------------------------------------------- diff --git a/mailbox/elasticsearch/src/test/java/org/apache/james/mailbox/elasticsearch/events/ElasticSearchListeningMessageSearchIndexTest.java b/mailbox/elasticsearch/src/test/java/org/apache/james/mailbox/elasticsearch/events/ElasticSearchListeningMessageSearchIndexTest.java new file mode 100644 index 0000000..3efe045 --- /dev/null +++ b/mailbox/elasticsearch/src/test/java/org/apache/james/mailbox/elasticsearch/events/ElasticSearchListeningMessageSearchIndexTest.java @@ -0,0 +1,281 @@ +/**************************************************************** + * Licensed to the Apache Software Foundation (ASF) under one * + * or more contributor license agreements. See the NOTICE file * + * distributed with this work for additional information * + * regarding copyright ownership. The ASF licenses this file * + * to you under the Apache License, Version 2.0 (the * + * "License"); you may not use this file except in compliance * + * with the License. You may obtain a copy of the License at * + * * + * http://www.apache.org/licenses/LICENSE-2.0 * + * * + * Unless required by applicable law or agreed to in writing, * + * software distributed under the License is distributed on an * + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * + * KIND, either express or implied. See the License for the * + * specific language governing permissions and limitations * + * under the License. * + ****************************************************************/ +package org.apache.james.mailbox.elasticsearch.events; + +import static org.easymock.EasyMock.anyLong; +import static org.easymock.EasyMock.anyObject; +import static org.easymock.EasyMock.anyString; +import static org.easymock.EasyMock.createControl; +import static org.easymock.EasyMock.eq; +import static org.easymock.EasyMock.expect; +import static org.easymock.EasyMock.expectLastCall; + +import java.io.IOException; +import java.util.List; + +import javax.mail.Flags; + +import org.apache.james.backends.es.ElasticSearchIndexer; +import org.apache.james.mailbox.MailboxSession; +import org.apache.james.mailbox.MessageUid; +import org.apache.james.mailbox.MailboxSession.User; +import org.apache.james.mailbox.elasticsearch.json.MessageToElasticSearchJson; +import org.apache.james.mailbox.elasticsearch.search.ElasticSearchSearcher; +import org.apache.james.mailbox.model.TestId; +import org.apache.james.mailbox.model.UpdatedFlags; +import org.apache.james.mailbox.store.mail.MessageMapperFactory; +import org.apache.james.mailbox.store.mail.model.Mailbox; +import org.apache.james.mailbox.store.mail.model.MailboxMessage; +import org.easymock.EasyMock; +import org.easymock.IMocksControl; +import org.elasticsearch.ElasticsearchException; +import org.elasticsearch.action.bulk.BulkResponse; +import org.elasticsearch.action.index.IndexResponse; +import org.elasticsearch.index.query.QueryBuilder; +import org.junit.Before; +import org.junit.Test; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.google.common.collect.Lists; + +public class ElasticSearchListeningMailboxMessageSearchIndexTest { + + public static final long MODSEQ = 18L; + private IMocksControl control; + + private ElasticSearchIndexer indexer; + private ElasticSearchListeningMessageSearchIndex testee; + + @Before + public void setup() throws JsonProcessingException { + control = createControl(); + + MessageMapperFactory mapperFactory = control.createMock(MessageMapperFactory.class); + MessageToElasticSearchJson messageToElasticSearchJson = control.createMock(MessageToElasticSearchJson.class); + ElasticSearchSearcher elasticSearchSearcher = control.createMock(ElasticSearchSearcher.class); + + indexer = control.createMock(ElasticSearchIndexer.class); + + List<User> users = anyObject(); + expect(messageToElasticSearchJson.convertToJson(anyObject(MailboxMessage.class), users)).andReturn("json content").anyTimes(); + expect(messageToElasticSearchJson.getUpdatedJsonMessagePart(anyObject(Flags.class), anyLong())).andReturn("json updated content").anyTimes(); + + testee = new ElasticSearchListeningMessageSearchIndex(mapperFactory, indexer, elasticSearchSearcher, messageToElasticSearchJson); + } + + @Test + public void addShouldIndex() throws Exception { + MailboxSession.User user = control.createMock(MailboxSession.User.class); + MailboxSession session = control.createMock(MailboxSession.class); + expect(session.getUser()) + .andReturn(user); + + Mailbox mailbox = control.createMock(Mailbox.class); + MessageUid messageUid = MessageUid.of(1); + TestId mailboxId = TestId.of(12); + expect(mailbox.getMailboxId()).andReturn(mailboxId); + MailboxMessage message = mockedMessage(messageUid); + + IndexResponse expectedIndexResponse = control.createMock(IndexResponse.class); + expect(indexer.indexMessage(eq(mailboxId.serialize() + ":" + messageUid.asLong()), anyString())) + .andReturn(expectedIndexResponse); + + control.replay(); + testee.add(session, mailbox, message); + control.verify(); + } + + private MailboxMessage mockedMessage(MessageUid messageId) throws IOException { + MailboxMessage message = control.createMock(MailboxMessage.class); + expect(message.getUid()).andReturn(messageId).anyTimes(); + return message; + } + + @Test + public void addShouldNotPropagateExceptionWhenExceptionOccurs() throws Exception { + MailboxSession.User user = control.createMock(MailboxSession.User.class); + MailboxSession session = control.createMock(MailboxSession.class); + expect(session.getUser()) + .andReturn(user); + + Mailbox mailbox = control.createMock(Mailbox.class); + + MessageUid messageUid = MessageUid.of(1); + TestId mailboxId = TestId.of(12); + MailboxMessage message = mockedMessage(messageUid); + expect(mailbox.getMailboxId()).andReturn(mailboxId); + + expect(indexer.indexMessage(eq(mailboxId.serialize() + ":" + messageUid.asLong()), anyString())) + .andThrow(new ElasticsearchException("")); + + control.replay(); + testee.add(session, mailbox, message); + control.verify(); + } + + @Test + @SuppressWarnings("unchecked") + public void deleteShouldWork() throws Exception { + MailboxSession session = control.createMock(MailboxSession.class); + Mailbox mailbox = control.createMock(Mailbox.class); + MessageUid messageUid = MessageUid.of(1); + TestId mailboxId = TestId.of(12); + expect(mailbox.getMailboxId()).andReturn(mailboxId); + + BulkResponse expectedBulkResponse = control.createMock(BulkResponse.class); + expect(indexer.deleteMessages(anyObject(List.class))) + .andReturn(expectedBulkResponse); + + control.replay(); + testee.delete(session, mailbox, Lists.newArrayList(messageUid)); + control.verify(); + } + + @Test + @SuppressWarnings("unchecked") + public void deleteShouldWorkWhenMultipleMessageIds() throws Exception { + MailboxSession session = control.createMock(MailboxSession.class); + Mailbox mailbox = control.createMock(Mailbox.class); + MessageUid messageUid1 = MessageUid.of(1); + MessageUid messageUid2 = MessageUid.of(2); + MessageUid messageUid3 = MessageUid.of(3); + MessageUid messageUid4 = MessageUid.of(4); + MessageUid messageUid5 = MessageUid.of(5); + TestId mailboxId = TestId.of(12); + expect(mailbox.getMailboxId()).andReturn(mailboxId).times(5); + + BulkResponse expectedBulkResponse = control.createMock(BulkResponse.class); + expect(indexer.deleteMessages(anyObject(List.class))) + .andReturn(expectedBulkResponse); + + control.replay(); + testee.delete(session, mailbox, Lists.newArrayList(messageUid1, messageUid2, messageUid3, messageUid4, messageUid5)); + control.verify(); + } + + @Test + @SuppressWarnings("unchecked") + public void deleteShouldNotPropagateExceptionWhenExceptionOccurs() throws Exception { + MailboxSession session = control.createMock(MailboxSession.class); + Mailbox mailbox = control.createMock(Mailbox.class); + MessageUid messageUid = MessageUid.of(1); + TestId mailboxId = TestId.of(12); + expect(mailbox.getMailboxId()).andReturn(mailboxId).times(2); + + expect(indexer.deleteMessages(anyObject(List.class))) + .andThrow(new ElasticsearchException("")); + + control.replay(); + testee.delete(session, mailbox, Lists.newArrayList(messageUid)); + control.verify(); + } + + @Test + @SuppressWarnings("unchecked") + public void updateShouldWork() throws Exception { + MailboxSession session = control.createMock(MailboxSession.class); + + Mailbox mailbox = control.createMock(Mailbox.class); + + Flags flags = new Flags(); + MessageUid messageUid = MessageUid.of(1); + UpdatedFlags updatedFlags = UpdatedFlags.builder() + .uid(messageUid) + .modSeq(MODSEQ) + .oldFlags(flags) + .newFlags(flags) + .build(); + TestId mailboxId = TestId.of(12); + + expectLastCall(); + expect(mailbox.getMailboxId()).andReturn(mailboxId); + + BulkResponse expectedBulkResponse = control.createMock(BulkResponse.class); + expect(indexer.updateMessages(anyObject(List.class))) + .andReturn(expectedBulkResponse); + + control.replay(); + testee.update(session, mailbox, Lists.newArrayList(updatedFlags)); + control.verify(); + } + + @Test + @SuppressWarnings("unchecked") + public void updateShouldNotPropagateExceptionWhenExceptionOccurs() throws Exception { + MailboxSession session = control.createMock(MailboxSession.class); + + Mailbox mailbox = control.createMock(Mailbox.class); + Flags flags = new Flags(); + MessageUid messageUid = MessageUid.of(1); + UpdatedFlags updatedFlags = UpdatedFlags.builder() + .uid(messageUid) + .modSeq(MODSEQ) + .oldFlags(flags) + .newFlags(flags) + .build(); + TestId mailboxId = TestId.of(12); + + expectLastCall(); + expect(mailbox.getMailboxId()).andReturn(mailboxId).times(2); + + expect(indexer.updateMessages(anyObject(List.class))) + .andThrow(new ElasticsearchException("")); + + control.replay(); + testee.update(session, mailbox, Lists.newArrayList(updatedFlags)); + control.verify(); + } + + @Test + public void deleteAllShouldWork() throws Exception { + MailboxSession session = control.createMock(MailboxSession.class); + + Mailbox mailbox = control.createMock(Mailbox.class); + + TestId mailboxId = TestId.of(12); + + expectLastCall(); + expect(mailbox.getMailboxId()).andReturn(mailboxId); + + indexer.deleteAllMatchingQuery(anyObject(QueryBuilder.class)); + EasyMock.expectLastCall(); + + control.replay(); + testee.deleteAll(session, mailbox); + control.verify(); + } + + @Test + public void deleteAllShouldNotPropagateExceptionWhenExceptionOccurs() throws Exception { + MailboxSession session = control.createMock(MailboxSession.class); + + Mailbox mailbox = control.createMock(Mailbox.class); + TestId mailboxId = TestId.of(12); + + expectLastCall(); + expect(mailbox.getMailboxId()).andReturn(mailboxId).times(2); + + indexer.deleteAllMatchingQuery(anyObject(QueryBuilder.class)); + EasyMock.expectLastCall().andThrow(new ElasticsearchException("")); + + control.replay(); + testee.deleteAll(session, mailbox); + control.verify(); + } +} http://git-wip-us.apache.org/repos/asf/james-project/blob/fed30079/mailbox/elasticsearch/src/test/java/org/apache/james/mailbox/elasticsearch/json/MailboxMessageToElasticSearchJsonTest.java ---------------------------------------------------------------------- diff --git a/mailbox/elasticsearch/src/test/java/org/apache/james/mailbox/elasticsearch/json/MailboxMessageToElasticSearchJsonTest.java b/mailbox/elasticsearch/src/test/java/org/apache/james/mailbox/elasticsearch/json/MailboxMessageToElasticSearchJsonTest.java deleted file mode 100644 index 6fa9d1f..0000000 --- a/mailbox/elasticsearch/src/test/java/org/apache/james/mailbox/elasticsearch/json/MailboxMessageToElasticSearchJsonTest.java +++ /dev/null @@ -1,357 +0,0 @@ -/**************************************************************** - * Licensed to the Apache Software Foundation (ASF) under one * - * or more contributor license agreements. See the NOTICE file * - * distributed with this work for additional information * - * regarding copyright ownership. The ASF licenses this file * - * to you under the Apache License, Version 2.0 (the * - * "License"); you may not use this file except in compliance * - * with the License. You may obtain a copy of the License at * - * * - * http://www.apache.org/licenses/LICENSE-2.0 * - * * - * Unless required by applicable law or agreed to in writing, * - * software distributed under the License is distributed on an * - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * - * KIND, either express or implied. See the License for the * - * specific language governing permissions and limitations * - * under the License. * - ****************************************************************/ - -package org.apache.james.mailbox.elasticsearch.json; - -import static net.javacrumbs.jsonunit.core.Option.IGNORING_ARRAY_ORDER; -import static net.javacrumbs.jsonunit.core.Option.IGNORING_VALUES; -import static net.javacrumbs.jsonunit.fluent.JsonFluentAssert.assertThatJson; -import static org.assertj.core.api.Assertions.assertThatThrownBy; - -import java.io.IOException; -import java.nio.charset.Charset; -import java.time.ZoneId; -import java.util.Date; - -import javax.mail.Flags; -import javax.mail.util.SharedByteArrayInputStream; - -import org.apache.commons.io.IOUtils; -import org.apache.james.mailbox.FlagsBuilder; -import org.apache.james.mailbox.MailboxSession.User; -import org.apache.james.mailbox.MessageUid; -import org.apache.james.mailbox.elasticsearch.IndexAttachments; -import org.apache.james.mailbox.mock.MockMailboxSession; -import org.apache.james.mailbox.model.MessageId; -import org.apache.james.mailbox.model.TestId; -import org.apache.james.mailbox.model.TestMessageId; -import org.apache.james.mailbox.store.extractor.DefaultTextExtractor; -import org.apache.james.mailbox.store.mail.model.MailboxMessage; -import org.apache.james.mailbox.store.mail.model.impl.PropertyBuilder; -import org.apache.james.mailbox.store.mail.model.impl.SimpleMailboxMessage; -import org.apache.james.mailbox.store.search.MessageSearchIndex; -import org.apache.james.mailbox.tika.extractor.TikaTextExtractor; -import org.junit.Before; -import org.junit.Test; - -import com.google.common.base.Charsets; -import com.google.common.base.Throwables; -import com.google.common.collect.ImmutableList; - -public class MailboxMessageToElasticSearchJsonTest { - - public static final int SIZE = 25; - public static final int BODY_START_OCTET = 100; - public static final TestId MAILBOX_ID = TestId.of(18L); - public static final MessageId MESSAGE_ID = TestMessageId.of(184L); - public static final long MOD_SEQ = 42L; - public static final MessageUid UID = MessageUid.of(25); - public static final Charset CHARSET = Charsets.UTF_8; - - private Date date; - private PropertyBuilder propertyBuilder; - - @Before - public void setUp() throws Exception { - // 2015/06/07 00:00:00 0200 (Paris time zone) - date = new Date(1433628000000L); - propertyBuilder = new PropertyBuilder(); - propertyBuilder.setMediaType("plain"); - propertyBuilder.setSubType("text"); - propertyBuilder.setTextualLineCount(18L); - propertyBuilder.setContentDescription("An e-mail"); - } - - @Test - public void convertToJsonShouldThrowWhenNoUser() throws Exception { - MessageToElasticSearchJson messageToElasticSearchJson = new MessageToElasticSearchJson( - new DefaultTextExtractor(), - ZoneId.of("Europe/Paris"), IndexAttachments.YES, - MessageSearchIndex.IndexMessageId.Required); - MailboxMessage spamMail = new SimpleMailboxMessage(MESSAGE_ID, - date, - SIZE, - BODY_START_OCTET, - new SharedByteArrayInputStream("message".getBytes(Charsets.UTF_8)), - new Flags(), - propertyBuilder, - MAILBOX_ID); - ImmutableList<User> users = ImmutableList.of(); - - assertThatThrownBy(() -> messageToElasticSearchJson.convertToJson(spamMail, users)) - .isInstanceOf(IllegalArgumentException.class); - } - - @Test - public void spamEmailShouldBeWellConvertedToJson() throws IOException { - MessageToElasticSearchJson messageToElasticSearchJson = new MessageToElasticSearchJson( - new DefaultTextExtractor(), - ZoneId.of("Europe/Paris"), IndexAttachments.YES, - MessageSearchIndex.IndexMessageId.Required); - MailboxMessage spamMail = new SimpleMailboxMessage(MESSAGE_ID, - date, - SIZE, - BODY_START_OCTET, - new SharedByteArrayInputStream(IOUtils.toByteArray(ClassLoader.getSystemResourceAsStream("eml/spamMail.eml"))), - new Flags(), - propertyBuilder, - MAILBOX_ID); - spamMail.setUid(UID); - spamMail.setModSeq(MOD_SEQ); - assertThatJson(messageToElasticSearchJson.convertToJson(spamMail, ImmutableList.of(new MockMailboxSession("username").getUser()))) - .when(IGNORING_ARRAY_ORDER) - .isEqualTo(IOUtils.toString(ClassLoader.getSystemResource("eml/spamMail.json"), CHARSET)); - } - - @Test - public void htmlEmailShouldBeWellConvertedToJson() throws IOException { - MessageToElasticSearchJson messageToElasticSearchJson = new MessageToElasticSearchJson( - new DefaultTextExtractor(), - ZoneId.of("Europe/Paris"), IndexAttachments.YES, - MessageSearchIndex.IndexMessageId.Required); - MailboxMessage htmlMail = new SimpleMailboxMessage(MESSAGE_ID, - date, - SIZE, - BODY_START_OCTET, - new SharedByteArrayInputStream(IOUtils.toByteArray(ClassLoader.getSystemResourceAsStream("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(new MockMailboxSession("username").getUser()))) - .when(IGNORING_ARRAY_ORDER) - .isEqualTo(IOUtils.toString(ClassLoader.getSystemResource("eml/htmlMail.json"))); - } - - @Test - public void pgpSignedEmailShouldBeWellConvertedToJson() throws IOException { - MessageToElasticSearchJson messageToElasticSearchJson = new MessageToElasticSearchJson( - new DefaultTextExtractor(), - ZoneId.of("Europe/Paris"), IndexAttachments.YES, - MessageSearchIndex.IndexMessageId.Required); - MailboxMessage pgpSignedMail = new SimpleMailboxMessage(MESSAGE_ID, - date, - SIZE, - BODY_START_OCTET, - new SharedByteArrayInputStream(IOUtils.toByteArray(ClassLoader.getSystemResourceAsStream("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(new MockMailboxSession("username").getUser()))) - .when(IGNORING_ARRAY_ORDER) - .isEqualTo(IOUtils.toString(ClassLoader.getSystemResource("eml/pgpSignedMail.json"))); - } - - @Test - public void simpleEmailShouldBeWellConvertedToJson() throws IOException { - MessageToElasticSearchJson messageToElasticSearchJson = new MessageToElasticSearchJson( - new DefaultTextExtractor(), - ZoneId.of("Europe/Paris"), IndexAttachments.YES, - MessageSearchIndex.IndexMessageId.Required); - MailboxMessage mail = new SimpleMailboxMessage(MESSAGE_ID, - date, - SIZE, - BODY_START_OCTET, - new SharedByteArrayInputStream(IOUtils.toByteArray(ClassLoader.getSystemResourceAsStream("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(new MockMailboxSession("user1").getUser(), new MockMailboxSession("user2").getUser()))) - .when(IGNORING_ARRAY_ORDER).when(IGNORING_VALUES) - .isEqualTo(IOUtils.toString(ClassLoader.getSystemResource("eml/mail.json"))); - } - - @Test - public void recursiveEmailShouldBeWellConvertedToJson() throws IOException { - MessageToElasticSearchJson messageToElasticSearchJson = new MessageToElasticSearchJson( - new DefaultTextExtractor(), - ZoneId.of("Europe/Paris"), IndexAttachments.YES, - MessageSearchIndex.IndexMessageId.Required); - MailboxMessage recursiveMail = new SimpleMailboxMessage(MESSAGE_ID, - date, - SIZE, - BODY_START_OCTET, - new SharedByteArrayInputStream(IOUtils.toByteArray(ClassLoader.getSystemResourceAsStream("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(new MockMailboxSession("username").getUser()))) - .when(IGNORING_ARRAY_ORDER).when(IGNORING_VALUES) - .isEqualTo(IOUtils.toString(ClassLoader.getSystemResource("eml/recursiveMail.json"))); - } - - @Test - public void emailWithNoInternalDateShouldUseNowDate() throws IOException { - MessageToElasticSearchJson messageToElasticSearchJson = new MessageToElasticSearchJson( - new DefaultTextExtractor(), - ZoneId.of("Europe/Paris"), IndexAttachments.YES, - MessageSearchIndex.IndexMessageId.Required); - MailboxMessage mailWithNoInternalDate = new SimpleMailboxMessage(MESSAGE_ID, - null, - SIZE, - BODY_START_OCTET, - new SharedByteArrayInputStream(IOUtils.toByteArray(ClassLoader.getSystemResourceAsStream("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(new MockMailboxSession("username").getUser()))) - .when(IGNORING_ARRAY_ORDER) - .when(IGNORING_VALUES) - .isEqualTo(IOUtils.toString(ClassLoader.getSystemResource("eml/recursiveMail.json"))); - } - - @Test - public void emailWithAttachmentsShouldConvertAttachmentsWhenIndexAttachmentsIsTrue() throws IOException { - // Given - MailboxMessage mailWithNoInternalDate = new SimpleMailboxMessage(MESSAGE_ID, - null, - SIZE, - BODY_START_OCTET, - new SharedByteArrayInputStream(IOUtils.toByteArray(ClassLoader.getSystemResourceAsStream("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); - - // When - MessageToElasticSearchJson messageToElasticSearchJson = new MessageToElasticSearchJson( - new DefaultTextExtractor(), - ZoneId.of("Europe/Paris"), IndexAttachments.YES, - MessageSearchIndex.IndexMessageId.Required); - String convertToJson = messageToElasticSearchJson.convertToJson(mailWithNoInternalDate, ImmutableList.of(new MockMailboxSession("username").getUser())); - - // Then - assertThatJson(convertToJson) - .when(IGNORING_ARRAY_ORDER) - .when(IGNORING_VALUES) - .isEqualTo(IOUtils.toString(ClassLoader.getSystemResource("eml/recursiveMail.json"))); - } - - @Test - public void emailWithAttachmentsShouldNotConvertAttachmentsWhenIndexAttachmentsIsFalse() throws IOException { - // Given - MailboxMessage mailWithNoInternalDate = new SimpleMailboxMessage(MESSAGE_ID, - null, - SIZE, - BODY_START_OCTET, - new SharedByteArrayInputStream(IOUtils.toByteArray(ClassLoader.getSystemResourceAsStream("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); - - // When - MessageToElasticSearchJson messageToElasticSearchJson = new MessageToElasticSearchJson( - new DefaultTextExtractor(), - ZoneId.of("Europe/Paris"), IndexAttachments.NO, - MessageSearchIndex.IndexMessageId.Required); - String convertToJson = messageToElasticSearchJson.convertToJson(mailWithNoInternalDate, ImmutableList.of(new MockMailboxSession("username").getUser())); - - // Then - assertThatJson(convertToJson) - .when(IGNORING_ARRAY_ORDER) - .when(IGNORING_VALUES) - .isEqualTo(IOUtils.toString(ClassLoader.getSystemResource("eml/recursiveMailWithoutAttachments.json"))); - } - - @Test(expected = NullPointerException.class) - public void emailWithNoMailboxIdShouldThrow() throws IOException { - MessageToElasticSearchJson messageToElasticSearchJson = new MessageToElasticSearchJson( - new DefaultTextExtractor(), - ZoneId.of("Europe/Paris"), IndexAttachments.YES, - MessageSearchIndex.IndexMessageId.Required); - MailboxMessage mailWithNoMailboxId; - try { - mailWithNoMailboxId = new SimpleMailboxMessage(MESSAGE_ID, date, - SIZE, - BODY_START_OCTET, - new SharedByteArrayInputStream(IOUtils.toByteArray(ClassLoader.getSystemResourceAsStream("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); - } catch(Exception exception) { - throw Throwables.propagate(exception); - } - messageToElasticSearchJson.convertToJson(mailWithNoMailboxId, ImmutableList.of(new MockMailboxSession("username").getUser())); - } - - @Test - public void getUpdatedJsonMessagePartShouldBehaveWellOnEmptyFlags() throws Exception { - MessageToElasticSearchJson messageToElasticSearchJson = new MessageToElasticSearchJson( - new DefaultTextExtractor(), - ZoneId.of("Europe/Paris"), IndexAttachments.YES, - MessageSearchIndex.IndexMessageId.Required); - assertThatJson(messageToElasticSearchJson.getUpdatedJsonMessagePart(new Flags(), MOD_SEQ)) - .isEqualTo("{\"modSeq\":42,\"isAnswered\":false,\"isDeleted\":false,\"isDraft\":false,\"isFlagged\":false,\"isRecent\":false,\"userFlags\":[],\"isUnread\":true}"); - } - - @Test - public void getUpdatedJsonMessagePartShouldBehaveWellOnNonEmptyFlags() throws Exception { - MessageToElasticSearchJson messageToElasticSearchJson = new MessageToElasticSearchJson( - new DefaultTextExtractor(), - ZoneId.of("Europe/Paris"), IndexAttachments.YES, - MessageSearchIndex.IndexMessageId.Required); - assertThatJson(messageToElasticSearchJson.getUpdatedJsonMessagePart(new FlagsBuilder().add(Flags.Flag.DELETED, Flags.Flag.FLAGGED).add("user").build(), MOD_SEQ)) - .isEqualTo("{\"modSeq\":42,\"isAnswered\":false,\"isDeleted\":true,\"isDraft\":false,\"isFlagged\":true,\"isRecent\":false,\"userFlags\":[\"user\"],\"isUnread\":true}"); - } - - @Test(expected = NullPointerException.class) - public void getUpdatedJsonMessagePartShouldThrowIfFlagsIsNull() throws Exception { - MessageToElasticSearchJson messageToElasticSearchJson = new MessageToElasticSearchJson( - new DefaultTextExtractor(), - ZoneId.of("Europe/Paris"), IndexAttachments.YES, - MessageSearchIndex.IndexMessageId.Required); - messageToElasticSearchJson.getUpdatedJsonMessagePart(null, MOD_SEQ); - } - - @Test - public void spamEmailShouldBeWellConvertedToJsonWithApacheTika() throws IOException { - MessageToElasticSearchJson messageToElasticSearchJson = new MessageToElasticSearchJson( - new TikaTextExtractor(), - ZoneId.of("Europe/Paris"), IndexAttachments.YES, - MessageSearchIndex.IndexMessageId.Required); - MailboxMessage spamMail = new SimpleMailboxMessage(MESSAGE_ID, date, - SIZE, - BODY_START_OCTET, - new SharedByteArrayInputStream(IOUtils.toByteArray(ClassLoader.getSystemResourceAsStream("eml/nonTextual.eml"))), - new Flags(), - propertyBuilder, - MAILBOX_ID); - spamMail.setUid(UID); - spamMail.setModSeq(MOD_SEQ); - assertThatJson(messageToElasticSearchJson.convertToJson(spamMail, ImmutableList.of(new MockMailboxSession("username").getUser()))) - .when(IGNORING_ARRAY_ORDER) - .isEqualTo(IOUtils.toString(ClassLoader.getSystemResource("eml/nonTextual.json"), CHARSET)); - } - -} http://git-wip-us.apache.org/repos/asf/james-project/blob/fed30079/mailbox/elasticsearch/src/test/java/org/apache/james/mailbox/elasticsearch/json/MessageToElasticSearchJsonTest.java ---------------------------------------------------------------------- diff --git a/mailbox/elasticsearch/src/test/java/org/apache/james/mailbox/elasticsearch/json/MessageToElasticSearchJsonTest.java b/mailbox/elasticsearch/src/test/java/org/apache/james/mailbox/elasticsearch/json/MessageToElasticSearchJsonTest.java new file mode 100644 index 0000000..2b0a4de --- /dev/null +++ b/mailbox/elasticsearch/src/test/java/org/apache/james/mailbox/elasticsearch/json/MessageToElasticSearchJsonTest.java @@ -0,0 +1,357 @@ +/**************************************************************** + * Licensed to the Apache Software Foundation (ASF) under one * + * or more contributor license agreements. See the NOTICE file * + * distributed with this work for additional information * + * regarding copyright ownership. The ASF licenses this file * + * to you under the Apache License, Version 2.0 (the * + * "License"); you may not use this file except in compliance * + * with the License. You may obtain a copy of the License at * + * * + * http://www.apache.org/licenses/LICENSE-2.0 * + * * + * Unless required by applicable law or agreed to in writing, * + * software distributed under the License is distributed on an * + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * + * KIND, either express or implied. See the License for the * + * specific language governing permissions and limitations * + * under the License. * + ****************************************************************/ + +package org.apache.james.mailbox.elasticsearch.json; + +import static net.javacrumbs.jsonunit.core.Option.IGNORING_ARRAY_ORDER; +import static net.javacrumbs.jsonunit.core.Option.IGNORING_VALUES; +import static net.javacrumbs.jsonunit.fluent.JsonFluentAssert.assertThatJson; +import static org.assertj.core.api.Assertions.assertThatThrownBy; + +import java.io.IOException; +import java.nio.charset.Charset; +import java.time.ZoneId; +import java.util.Date; + +import javax.mail.Flags; +import javax.mail.util.SharedByteArrayInputStream; + +import org.apache.commons.io.IOUtils; +import org.apache.james.mailbox.FlagsBuilder; +import org.apache.james.mailbox.MailboxSession.User; +import org.apache.james.mailbox.MessageUid; +import org.apache.james.mailbox.elasticsearch.IndexAttachments; +import org.apache.james.mailbox.mock.MockMailboxSession; +import org.apache.james.mailbox.model.MessageId; +import org.apache.james.mailbox.model.TestId; +import org.apache.james.mailbox.model.TestMessageId; +import org.apache.james.mailbox.store.extractor.DefaultTextExtractor; +import org.apache.james.mailbox.store.mail.model.MailboxMessage; +import org.apache.james.mailbox.store.mail.model.impl.PropertyBuilder; +import org.apache.james.mailbox.store.mail.model.impl.SimpleMailboxMessage; +import org.apache.james.mailbox.store.search.MessageSearchIndex; +import org.apache.james.mailbox.tika.extractor.TikaTextExtractor; +import org.junit.Before; +import org.junit.Test; + +import com.google.common.base.Charsets; +import com.google.common.base.Throwables; +import com.google.common.collect.ImmutableList; + +public class MessageToElasticSearchJsonTest { + + public static final int SIZE = 25; + public static final int BODY_START_OCTET = 100; + public static final TestId MAILBOX_ID = TestId.of(18L); + public static final MessageId MESSAGE_ID = TestMessageId.of(184L); + public static final long MOD_SEQ = 42L; + public static final MessageUid UID = MessageUid.of(25); + public static final Charset CHARSET = Charsets.UTF_8; + + private Date date; + private PropertyBuilder propertyBuilder; + + @Before + public void setUp() throws Exception { + // 2015/06/07 00:00:00 0200 (Paris time zone) + date = new Date(1433628000000L); + propertyBuilder = new PropertyBuilder(); + propertyBuilder.setMediaType("plain"); + propertyBuilder.setSubType("text"); + propertyBuilder.setTextualLineCount(18L); + propertyBuilder.setContentDescription("An e-mail"); + } + + @Test + public void convertToJsonShouldThrowWhenNoUser() throws Exception { + MessageToElasticSearchJson messageToElasticSearchJson = new MessageToElasticSearchJson( + new DefaultTextExtractor(), + ZoneId.of("Europe/Paris"), IndexAttachments.YES, + MessageSearchIndex.IndexMessageId.Required); + MailboxMessage spamMail = new SimpleMailboxMessage(MESSAGE_ID, + date, + SIZE, + BODY_START_OCTET, + new SharedByteArrayInputStream("message".getBytes(Charsets.UTF_8)), + new Flags(), + propertyBuilder, + MAILBOX_ID); + ImmutableList<User> users = ImmutableList.of(); + + assertThatThrownBy(() -> messageToElasticSearchJson.convertToJson(spamMail, users)) + .isInstanceOf(IllegalArgumentException.class); + } + + @Test + public void spamEmailShouldBeWellConvertedToJson() throws IOException { + MessageToElasticSearchJson messageToElasticSearchJson = new MessageToElasticSearchJson( + new DefaultTextExtractor(), + ZoneId.of("Europe/Paris"), IndexAttachments.YES, + MessageSearchIndex.IndexMessageId.Required); + MailboxMessage spamMail = new SimpleMailboxMessage(MESSAGE_ID, + date, + SIZE, + BODY_START_OCTET, + new SharedByteArrayInputStream(IOUtils.toByteArray(ClassLoader.getSystemResourceAsStream("eml/spamMail.eml"))), + new Flags(), + propertyBuilder, + MAILBOX_ID); + spamMail.setUid(UID); + spamMail.setModSeq(MOD_SEQ); + assertThatJson(messageToElasticSearchJson.convertToJson(spamMail, ImmutableList.of(new MockMailboxSession("username").getUser()))) + .when(IGNORING_ARRAY_ORDER) + .isEqualTo(IOUtils.toString(ClassLoader.getSystemResource("eml/spamMail.json"), CHARSET)); + } + + @Test + public void htmlEmailShouldBeWellConvertedToJson() throws IOException { + MessageToElasticSearchJson messageToElasticSearchJson = new MessageToElasticSearchJson( + new DefaultTextExtractor(), + ZoneId.of("Europe/Paris"), IndexAttachments.YES, + MessageSearchIndex.IndexMessageId.Required); + MailboxMessage htmlMail = new SimpleMailboxMessage(MESSAGE_ID, + date, + SIZE, + BODY_START_OCTET, + new SharedByteArrayInputStream(IOUtils.toByteArray(ClassLoader.getSystemResourceAsStream("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(new MockMailboxSession("username").getUser()))) + .when(IGNORING_ARRAY_ORDER) + .isEqualTo(IOUtils.toString(ClassLoader.getSystemResource("eml/htmlMail.json"))); + } + + @Test + public void pgpSignedEmailShouldBeWellConvertedToJson() throws IOException { + MessageToElasticSearchJson messageToElasticSearchJson = new MessageToElasticSearchJson( + new DefaultTextExtractor(), + ZoneId.of("Europe/Paris"), IndexAttachments.YES, + MessageSearchIndex.IndexMessageId.Required); + MailboxMessage pgpSignedMail = new SimpleMailboxMessage(MESSAGE_ID, + date, + SIZE, + BODY_START_OCTET, + new SharedByteArrayInputStream(IOUtils.toByteArray(ClassLoader.getSystemResourceAsStream("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(new MockMailboxSession("username").getUser()))) + .when(IGNORING_ARRAY_ORDER) + .isEqualTo(IOUtils.toString(ClassLoader.getSystemResource("eml/pgpSignedMail.json"))); + } + + @Test + public void simpleEmailShouldBeWellConvertedToJson() throws IOException { + MessageToElasticSearchJson messageToElasticSearchJson = new MessageToElasticSearchJson( + new DefaultTextExtractor(), + ZoneId.of("Europe/Paris"), IndexAttachments.YES, + MessageSearchIndex.IndexMessageId.Required); + MailboxMessage mail = new SimpleMailboxMessage(MESSAGE_ID, + date, + SIZE, + BODY_START_OCTET, + new SharedByteArrayInputStream(IOUtils.toByteArray(ClassLoader.getSystemResourceAsStream("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(new MockMailboxSession("user1").getUser(), new MockMailboxSession("user2").getUser()))) + .when(IGNORING_ARRAY_ORDER).when(IGNORING_VALUES) + .isEqualTo(IOUtils.toString(ClassLoader.getSystemResource("eml/mail.json"))); + } + + @Test + public void recursiveEmailShouldBeWellConvertedToJson() throws IOException { + MessageToElasticSearchJson messageToElasticSearchJson = new MessageToElasticSearchJson( + new DefaultTextExtractor(), + ZoneId.of("Europe/Paris"), IndexAttachments.YES, + MessageSearchIndex.IndexMessageId.Required); + MailboxMessage recursiveMail = new SimpleMailboxMessage(MESSAGE_ID, + date, + SIZE, + BODY_START_OCTET, + new SharedByteArrayInputStream(IOUtils.toByteArray(ClassLoader.getSystemResourceAsStream("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(new MockMailboxSession("username").getUser()))) + .when(IGNORING_ARRAY_ORDER).when(IGNORING_VALUES) + .isEqualTo(IOUtils.toString(ClassLoader.getSystemResource("eml/recursiveMail.json"))); + } + + @Test + public void emailWithNoInternalDateShouldUseNowDate() throws IOException { + MessageToElasticSearchJson messageToElasticSearchJson = new MessageToElasticSearchJson( + new DefaultTextExtractor(), + ZoneId.of("Europe/Paris"), IndexAttachments.YES, + MessageSearchIndex.IndexMessageId.Required); + MailboxMessage mailWithNoInternalDate = new SimpleMailboxMessage(MESSAGE_ID, + null, + SIZE, + BODY_START_OCTET, + new SharedByteArrayInputStream(IOUtils.toByteArray(ClassLoader.getSystemResourceAsStream("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(new MockMailboxSession("username").getUser()))) + .when(IGNORING_ARRAY_ORDER) + .when(IGNORING_VALUES) + .isEqualTo(IOUtils.toString(ClassLoader.getSystemResource("eml/recursiveMail.json"))); + } + + @Test + public void emailWithAttachmentsShouldConvertAttachmentsWhenIndexAttachmentsIsTrue() throws IOException { + // Given + MailboxMessage mailWithNoInternalDate = new SimpleMailboxMessage(MESSAGE_ID, + null, + SIZE, + BODY_START_OCTET, + new SharedByteArrayInputStream(IOUtils.toByteArray(ClassLoader.getSystemResourceAsStream("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); + + // When + MessageToElasticSearchJson messageToElasticSearchJson = new MessageToElasticSearchJson( + new DefaultTextExtractor(), + ZoneId.of("Europe/Paris"), IndexAttachments.YES, + MessageSearchIndex.IndexMessageId.Required); + String convertToJson = messageToElasticSearchJson.convertToJson(mailWithNoInternalDate, ImmutableList.of(new MockMailboxSession("username").getUser())); + + // Then + assertThatJson(convertToJson) + .when(IGNORING_ARRAY_ORDER) + .when(IGNORING_VALUES) + .isEqualTo(IOUtils.toString(ClassLoader.getSystemResource("eml/recursiveMail.json"))); + } + + @Test + public void emailWithAttachmentsShouldNotConvertAttachmentsWhenIndexAttachmentsIsFalse() throws IOException { + // Given + MailboxMessage mailWithNoInternalDate = new SimpleMailboxMessage(MESSAGE_ID, + null, + SIZE, + BODY_START_OCTET, + new SharedByteArrayInputStream(IOUtils.toByteArray(ClassLoader.getSystemResourceAsStream("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); + + // When + MessageToElasticSearchJson messageToElasticSearchJson = new MessageToElasticSearchJson( + new DefaultTextExtractor(), + ZoneId.of("Europe/Paris"), IndexAttachments.NO, + MessageSearchIndex.IndexMessageId.Required); + String convertToJson = messageToElasticSearchJson.convertToJson(mailWithNoInternalDate, ImmutableList.of(new MockMailboxSession("username").getUser())); + + // Then + assertThatJson(convertToJson) + .when(IGNORING_ARRAY_ORDER) + .when(IGNORING_VALUES) + .isEqualTo(IOUtils.toString(ClassLoader.getSystemResource("eml/recursiveMailWithoutAttachments.json"))); + } + + @Test(expected = NullPointerException.class) + public void emailWithNoMailboxIdShouldThrow() throws IOException { + MessageToElasticSearchJson messageToElasticSearchJson = new MessageToElasticSearchJson( + new DefaultTextExtractor(), + ZoneId.of("Europe/Paris"), IndexAttachments.YES, + MessageSearchIndex.IndexMessageId.Required); + MailboxMessage mailWithNoMailboxId; + try { + mailWithNoMailboxId = new SimpleMailboxMessage(MESSAGE_ID, date, + SIZE, + BODY_START_OCTET, + new SharedByteArrayInputStream(IOUtils.toByteArray(ClassLoader.getSystemResourceAsStream("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); + } catch (Exception exception) { + throw Throwables.propagate(exception); + } + messageToElasticSearchJson.convertToJson(mailWithNoMailboxId, ImmutableList.of(new MockMailboxSession("username").getUser())); + } + + @Test + public void getUpdatedJsonMessagePartShouldBehaveWellOnEmptyFlags() throws Exception { + MessageToElasticSearchJson messageToElasticSearchJson = new MessageToElasticSearchJson( + new DefaultTextExtractor(), + ZoneId.of("Europe/Paris"), IndexAttachments.YES, + MessageSearchIndex.IndexMessageId.Required); + assertThatJson(messageToElasticSearchJson.getUpdatedJsonMessagePart(new Flags(), MOD_SEQ)) + .isEqualTo("{\"modSeq\":42,\"isAnswered\":false,\"isDeleted\":false,\"isDraft\":false,\"isFlagged\":false,\"isRecent\":false,\"userFlags\":[],\"isUnread\":true}"); + } + + @Test + public void getUpdatedJsonMessagePartShouldBehaveWellOnNonEmptyFlags() throws Exception { + MessageToElasticSearchJson messageToElasticSearchJson = new MessageToElasticSearchJson( + new DefaultTextExtractor(), + ZoneId.of("Europe/Paris"), IndexAttachments.YES, + MessageSearchIndex.IndexMessageId.Required); + assertThatJson(messageToElasticSearchJson.getUpdatedJsonMessagePart(new FlagsBuilder().add(Flags.Flag.DELETED, Flags.Flag.FLAGGED).add("user").build(), MOD_SEQ)) + .isEqualTo("{\"modSeq\":42,\"isAnswered\":false,\"isDeleted\":true,\"isDraft\":false,\"isFlagged\":true,\"isRecent\":false,\"userFlags\":[\"user\"],\"isUnread\":true}"); + } + + @Test(expected = NullPointerException.class) + public void getUpdatedJsonMessagePartShouldThrowIfFlagsIsNull() throws Exception { + MessageToElasticSearchJson messageToElasticSearchJson = new MessageToElasticSearchJson( + new DefaultTextExtractor(), + ZoneId.of("Europe/Paris"), IndexAttachments.YES, + MessageSearchIndex.IndexMessageId.Required); + messageToElasticSearchJson.getUpdatedJsonMessagePart(null, MOD_SEQ); + } + + @Test + public void spamEmailShouldBeWellConvertedToJsonWithApacheTika() throws IOException { + MessageToElasticSearchJson messageToElasticSearchJson = new MessageToElasticSearchJson( + new TikaTextExtractor(), + ZoneId.of("Europe/Paris"), IndexAttachments.YES, + MessageSearchIndex.IndexMessageId.Required); + MailboxMessage spamMail = new SimpleMailboxMessage(MESSAGE_ID, date, + SIZE, + BODY_START_OCTET, + new SharedByteArrayInputStream(IOUtils.toByteArray(ClassLoader.getSystemResourceAsStream("eml/nonTextual.eml"))), + new Flags(), + propertyBuilder, + MAILBOX_ID); + spamMail.setUid(UID); + spamMail.setModSeq(MOD_SEQ); + assertThatJson(messageToElasticSearchJson.convertToJson(spamMail, ImmutableList.of(new MockMailboxSession("username").getUser()))) + .when(IGNORING_ARRAY_ORDER) + .isEqualTo(IOUtils.toString(ClassLoader.getSystemResource("eml/nonTextual.json"), CHARSET)); + } + +} --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
