MAILBOX-278 Make sure indexing index at least email body when attachment 
conversion fails


Project: http://git-wip-us.apache.org/repos/asf/james-project/repo
Commit: http://git-wip-us.apache.org/repos/asf/james-project/commit/d87642ab
Tree: http://git-wip-us.apache.org/repos/asf/james-project/tree/d87642ab
Diff: http://git-wip-us.apache.org/repos/asf/james-project/diff/d87642ab

Branch: refs/heads/master
Commit: d87642abd996c4535a2234a9dd00182596489d8c
Parents: fed3007
Author: Laura Royet <lro...@linagora.com>
Authored: Fri Dec 9 09:27:45 2016 +0100
Committer: Quynh Nguyen <qngu...@linagora.com>
Committed: Tue Feb 7 16:15:13 2017 +0700

----------------------------------------------------------------------
 .../james/backends/es/ElasticSearchIndexer.java |   25 +
 ...lasticSearchListeningMessageSearchIndex.java |   16 +-
 .../json/MessageToElasticSearchJson.java        |   12 +
 ...icSearchListeningMessageSearchIndexTest.java |  329 ++--
 .../json/MessageToElasticSearchJsonTest.java    |   28 +
 .../eml/emailWithNonIndexableAttachment.eml     | 1655 ++++++++++++++++++
 ...NonIndexableAttachmentWithoutAttachment.json |   96 +
 .../integration/SetMessagesMethodTest.java      |  175 ++
 .../attachment/nonIndexableAttachment.html      |  827 +++++++++
 9 files changed, 3009 insertions(+), 154 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/james-project/blob/d87642ab/backends-common/elasticsearch/src/main/java/org/apache/james/backends/es/ElasticSearchIndexer.java
----------------------------------------------------------------------
diff --git 
a/backends-common/elasticsearch/src/main/java/org/apache/james/backends/es/ElasticSearchIndexer.java
 
b/backends-common/elasticsearch/src/main/java/org/apache/james/backends/es/ElasticSearchIndexer.java
index f1df1d7..55bf479 100644
--- 
a/backends-common/elasticsearch/src/main/java/org/apache/james/backends/es/ElasticSearchIndexer.java
+++ 
b/backends-common/elasticsearch/src/main/java/org/apache/james/backends/es/ElasticSearchIndexer.java
@@ -19,6 +19,7 @@
 package org.apache.james.backends.es;
 
 import java.util.List;
+import java.util.Objects;
 
 import javax.inject.Inject;
 
@@ -31,6 +32,7 @@ import org.elasticsearch.index.query.QueryBuilder;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
+import com.google.common.base.MoreObjects;
 import com.google.common.base.Preconditions;
 
 public class ElasticSearchIndexer {
@@ -53,6 +55,29 @@ public class ElasticSearchIndexer {
         public String getUpdatedDocumentPart() {
             return updatedDocumentPart;
         }
+
+        @Override
+        public final boolean equals(Object o) {
+            if (o instanceof UpdatedRepresentation) {
+                UpdatedRepresentation other = (UpdatedRepresentation) o;
+                return Objects.equals(id, other.id)
+                    && Objects.equals(updatedDocumentPart, 
other.updatedDocumentPart);
+            }
+            return false;
+        }
+
+        @Override
+        public final int hashCode() {
+            return Objects.hash(id, updatedDocumentPart);
+        }
+
+        @Override
+        public String toString() {
+            return MoreObjects.toStringHelper(this)
+                .add("id", id)
+                .add("updatedDocumentPart", updatedDocumentPart)
+                .toString();
+        }
     }
 
     private static final Logger LOGGER = 
LoggerFactory.getLogger(ElasticSearchIndexer.class);

http://git-wip-us.apache.org/repos/asf/james-project/blob/d87642ab/mailbox/elasticsearch/src/main/java/org/apache/james/mailbox/elasticsearch/events/ElasticSearchListeningMessageSearchIndex.java
----------------------------------------------------------------------
diff --git 
a/mailbox/elasticsearch/src/main/java/org/apache/james/mailbox/elasticsearch/events/ElasticSearchListeningMessageSearchIndex.java
 
b/mailbox/elasticsearch/src/main/java/org/apache/james/mailbox/elasticsearch/events/ElasticSearchListeningMessageSearchIndex.java
index 7372e2b..aa6983d 100644
--- 
a/mailbox/elasticsearch/src/main/java/org/apache/james/mailbox/elasticsearch/events/ElasticSearchListeningMessageSearchIndex.java
+++ 
b/mailbox/elasticsearch/src/main/java/org/apache/james/mailbox/elasticsearch/events/ElasticSearchListeningMessageSearchIndex.java
@@ -98,6 +98,7 @@ public class ElasticSearchListeningMessageSearchIndex extends 
ListeningMessageSe
             throws MailboxException {
         Preconditions.checkArgument(session != null, "'session' is mandatory");
         return searcher.search(ImmutableList.of(session.getUser()), 
searchQuery, Optional.of(limit))
+            .peek(this::logIfNoMessageId)
             .map(SearchResult::getMessageId)
             .map(com.google.common.base.Optional::get)
             .collect(Guavate.toImmutableList());
@@ -108,10 +109,15 @@ public class ElasticSearchListeningMessageSearchIndex 
extends ListeningMessageSe
         try {
             indexer.indexMessage(indexIdFor(mailbox, message.getUid()), 
messageToElasticSearchJson.convertToJson(message, 
ImmutableList.of(session.getUser())));
         } catch (Exception e) {
-            LOGGER.error("Error when indexing message " + message.getUid(), e);
+            try {
+                LOGGER.warn("indexing message {} without attachments ", 
message.getUid());
+                indexer.indexMessage(indexIdFor(mailbox, message.getUid()), 
messageToElasticSearchJson.convertToJsonWithoutAttachment(message, 
ImmutableList.of(session.getUser())));
+            } catch (JsonProcessingException e1) {
+                LOGGER.error("Error when indexing message " + message.getUid() 
+ " without its attachment", e1);
+            }
         }
     }
-
+    
     @Override
     public void delete(MailboxSession session, Mailbox mailbox, 
List<MessageUid> expungedUids) throws MailboxException {
         try {
@@ -162,4 +168,10 @@ public class ElasticSearchListeningMessageSearchIndex 
extends ListeningMessageSe
         return String.join(ID_SEPARATOR, mailbox.getMailboxId().serialize(), 
String.valueOf(uid.asLong()));
     }
 
+    private void logIfNoMessageId(SearchResult searchResult) {
+        if (!searchResult.getMessageId().isPresent()) {
+            LOGGER.error("No messageUid for {} in mailbox {}", 
searchResult.getMessageUid(), searchResult.getMailboxId());
+        }
+    }
+
 }

http://git-wip-us.apache.org/repos/asf/james-project/blob/d87642ab/mailbox/elasticsearch/src/main/java/org/apache/james/mailbox/elasticsearch/json/MessageToElasticSearchJson.java
----------------------------------------------------------------------
diff --git 
a/mailbox/elasticsearch/src/main/java/org/apache/james/mailbox/elasticsearch/json/MessageToElasticSearchJson.java
 
b/mailbox/elasticsearch/src/main/java/org/apache/james/mailbox/elasticsearch/json/MessageToElasticSearchJson.java
index be23f98..6b699b9 100644
--- 
a/mailbox/elasticsearch/src/main/java/org/apache/james/mailbox/elasticsearch/json/MessageToElasticSearchJson.java
+++ 
b/mailbox/elasticsearch/src/main/java/org/apache/james/mailbox/elasticsearch/json/MessageToElasticSearchJson.java
@@ -81,6 +81,18 @@ public class MessageToElasticSearchJson {
         }
     }
 
+    public String convertToJsonWithoutAttachment(MailboxMessage message, 
List<User> users) throws JsonProcessingException {
+        Preconditions.checkNotNull(message);
+        switch (indexMessageId) {
+            case Required:
+                return 
mapper.writeValueAsString(IndexableMessageWithMessageId.from(message, users, 
textExtractor, zoneId, IndexAttachments.NO));
+            case Optional:
+                return 
mapper.writeValueAsString(IndexableMessage.from(message, users, textExtractor, 
zoneId, IndexAttachments.NO));
+            default:
+                throw new NotImplementedException();
+        }
+    }
+
     public String getUpdatedJsonMessagePart(Flags flags, long modSeq) throws 
JsonProcessingException {
         Preconditions.checkNotNull(flags);
         return mapper.writeValueAsString(new MessageUpdateJson(flags, modSeq));

http://git-wip-us.apache.org/repos/asf/james-project/blob/d87642ab/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
index 3efe045..28323b2 100644
--- 
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
@@ -18,13 +18,13 @@
  ****************************************************************/
 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 static org.mockito.Matchers.any;
+import static org.mockito.Matchers.eq;
+import static org.mockito.Matchers.refEq;
+import static org.mockito.Mockito.doThrow;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
 
 import java.io.IOException;
 import java.util.List;
@@ -32,250 +32,275 @@ import java.util.List;
 import javax.mail.Flags;
 
 import org.apache.james.backends.es.ElasticSearchIndexer;
+import org.apache.james.backends.es.ElasticSearchIndexer.UpdatedRepresentation;
 import org.apache.james.mailbox.MailboxSession;
-import org.apache.james.mailbox.MessageUid;
 import org.apache.james.mailbox.MailboxSession.User;
+import org.apache.james.mailbox.MessageUid;
 import org.apache.james.mailbox.elasticsearch.json.MessageToElasticSearchJson;
 import org.apache.james.mailbox.elasticsearch.search.ElasticSearchSearcher;
+import org.apache.james.mailbox.mock.MockMailboxSession;
 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.elasticsearch.index.query.QueryBuilders;
 import org.junit.Before;
 import org.junit.Test;
 
+import com.fasterxml.jackson.core.JsonGenerationException;
 import com.fasterxml.jackson.core.JsonProcessingException;
+import com.google.common.collect.ImmutableList;
 import com.google.common.collect.Lists;
 
-public class ElasticSearchListeningMailboxMessageSearchIndexTest {
+public class ElasticSearchListeningMessageSearchIndexTest {
+    
 
     public static final long MODSEQ = 18L;
-    private IMocksControl control;
+    public static final MessageUid MESSAGE_UID = MessageUid.of(1);
+    public static final TestId MAILBOX_ID = TestId.of(12);
+    public static final String ELASTIC_SEARCH_ID = "12:1";
+    public static final String EXPECTED_JSON_CONTENT = "json content";
+    public static final String USERNAME = "username";
 
     private ElasticSearchIndexer indexer;
+    private MessageToElasticSearchJson messageToElasticSearchJson;
     private ElasticSearchListeningMessageSearchIndex testee;
+    private MailboxSession session;
+    private User user;
     
     @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();
+        MessageMapperFactory mapperFactory = mock(MessageMapperFactory.class);
+        messageToElasticSearchJson = mock(MessageToElasticSearchJson.class);
+        ElasticSearchSearcher elasticSearchSearcher = 
mock(ElasticSearchSearcher.class);
 
+        indexer = mock(ElasticSearchIndexer.class);
+        
         testee = new ElasticSearchListeningMessageSearchIndex(mapperFactory, 
indexer, elasticSearchSearcher, messageToElasticSearchJson);
+        session = new MockMailboxSession(USERNAME);
+        user = session.getUser();
     }
     
     @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);
+        //Given
+        Mailbox mailbox = mock(Mailbox.class);
+        when(mailbox.getMailboxId())
+            .thenReturn(MAILBOX_ID);
+        MailboxMessage message = mockedMessage(MESSAGE_UID);
+        List<User> users = ImmutableList.of(user);
+        
+        when(messageToElasticSearchJson.convertToJson(eq(message), eq(users)))
+            .thenReturn(EXPECTED_JSON_CONTENT);
+        
+        //When
+        testee.add(session, mailbox, message);
+        
+        //Then
+        verify(indexer).indexMessage(eq(ELASTIC_SEARCH_ID), 
eq(EXPECTED_JSON_CONTENT));
+    }
 
-        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);
+    @SuppressWarnings("unchecked")
+    @Test
+    public void addShouldIndexEmailBodyWhenNotIndexableAttachment() throws 
Exception {
+        //Given
+        Mailbox mailbox = mock(Mailbox.class);
+        when(mailbox.getMailboxId())
+            .thenReturn(MAILBOX_ID);
+        
+        MailboxMessage message = mockedMessage(MESSAGE_UID);
+        List<User> users = ImmutableList.of(user);
+        
+        when(messageToElasticSearchJson.convertToJson(eq(message), eq(users)))
+            .thenThrow(JsonProcessingException.class);
         
-        IndexResponse expectedIndexResponse = 
control.createMock(IndexResponse.class);
-        expect(indexer.indexMessage(eq(mailboxId.serialize() + ":" + 
messageUid.asLong()), anyString()))
-            .andReturn(expectedIndexResponse);
+        
when(messageToElasticSearchJson.convertToJsonWithoutAttachment(eq(message), 
eq(users)))
+            .thenReturn(EXPECTED_JSON_CONTENT);
         
-        control.replay();
+        //When
         testee.add(session, mailbox, message);
-        control.verify();
+        
+        //Then
+        verify(indexer).indexMessage(eq(ELASTIC_SEARCH_ID), 
eq(EXPECTED_JSON_CONTENT));
     }
 
     private MailboxMessage mockedMessage(MessageUid messageId) throws 
IOException {
-        MailboxMessage message = control.createMock(MailboxMessage.class);
-        expect(message.getUid()).andReturn(messageId).anyTimes();
+        MailboxMessage message = mock(MailboxMessage.class);
+        when(message.getUid())
+            .thenReturn(messageId);
         return message;
     }
-    
+
+    @SuppressWarnings("unchecked")
     @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);
+        //Given
+        Mailbox mailbox = mock(Mailbox.class);
+        when(mailbox.getMailboxId())
+            .thenReturn(MAILBOX_ID);
+        MailboxMessage message = mockedMessage(MESSAGE_UID);
+        List<User> users = ImmutableList.of(user);
         
-        MessageUid messageUid = MessageUid.of(1);
-        TestId mailboxId = TestId.of(12);
-        MailboxMessage message = mockedMessage(messageUid);
-        expect(mailbox.getMailboxId()).andReturn(mailboxId);
+        when(messageToElasticSearchJson.convertToJson(eq(message), eq(users)))
+            .thenThrow(JsonProcessingException.class);
         
-        expect(indexer.indexMessage(eq(mailboxId.serialize() + ":" + 
messageUid.asLong()), anyString()))
-            .andThrow(new ElasticsearchException(""));
+        
when(messageToElasticSearchJson.convertToJsonWithoutAttachment(eq(message), 
eq(users)))
+            .thenThrow(new JsonGenerationException("expected error"));
         
-        control.replay();
+        //When
         testee.add(session, mailbox, message);
-        control.verify();
+        
+        //Then
+        //No exception
     }
-    
+
     @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();
+        //Given
+        Mailbox mailbox = mock(Mailbox.class);
+        when(mailbox.getMailboxId())
+            .thenReturn(MAILBOX_ID);
+
+        BulkResponse expectedBulkResponse = mock(BulkResponse.class);
+        when(indexer.deleteMessages(any(List.class)))
+            .thenReturn(expectedBulkResponse);
+
+        //When
+        testee.delete(session, mailbox, Lists.newArrayList(MESSAGE_UID));
+
+        //Then
+        
verify(indexer).deleteMessages(eq(Lists.newArrayList(ELASTIC_SEARCH_ID)));
     }
-    
+
     @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);
+        //Given
+        Mailbox mailbox = mock(Mailbox.class);
+        MessageUid messageId2 = MessageUid.of(2);
+        MessageUid messageId3 = MessageUid.of(3);
+        MessageUid messageId4 = MessageUid.of(4);
+        MessageUid messageId5 = MessageUid.of(5);
+        when(mailbox.getMailboxId())
+            .thenReturn(MAILBOX_ID);
+
+        BulkResponse expectedBulkResponse = mock(BulkResponse.class);
+        when(indexer.deleteMessages(any(List.class)))
+            .thenReturn(expectedBulkResponse);
         
-        control.replay();
-        testee.delete(session, mailbox, Lists.newArrayList(messageUid1, 
messageUid2, messageUid3, messageUid4, messageUid5));
-        control.verify();
+        //When
+        testee.delete(session, mailbox, Lists.newArrayList(MESSAGE_UID, 
messageId2, messageId3, messageId4, messageId5));
+        
+        //Then
+        
verify(indexer).deleteMessages(eq(Lists.newArrayList(ELASTIC_SEARCH_ID, "12:2", 
"12:3", "12:4", "12:5")));
     }
-    
+
     @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);
+        //Given
+        Mailbox mailbox = mock(Mailbox.class);
+        when(mailbox.getMailboxId())
+            .thenReturn(MAILBOX_ID);
+        
+        when(indexer.deleteMessages(any(List.class)))
+            .thenThrow(new ElasticsearchException(""));
         
-        expect(indexer.deleteMessages(anyObject(List.class)))
-            .andThrow(new ElasticsearchException(""));
+        //When
+        testee.delete(session, mailbox, Lists.newArrayList(MESSAGE_UID));
         
-        control.replay();
-        testee.delete(session, mailbox, Lists.newArrayList(messageUid));
-        control.verify();
+        //Then
+        //No exception
     }
 
     @Test
-    @SuppressWarnings("unchecked")
     public void updateShouldWork() throws Exception {
-        MailboxSession session = control.createMock(MailboxSession.class);
-
-        Mailbox mailbox = control.createMock(Mailbox.class);
-
+        //Given
+        Mailbox mailbox = mock(Mailbox.class);
         Flags flags = new Flags();
-        MessageUid messageUid = MessageUid.of(1);
+
         UpdatedFlags updatedFlags = UpdatedFlags.builder()
-            .uid(messageUid)
+            .uid(MESSAGE_UID)
             .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);
+        when(mailbox.getMailboxId())
+            .thenReturn(MAILBOX_ID);
+
+        
when(messageToElasticSearchJson.getUpdatedJsonMessagePart(any(Flags.class), 
any(Long.class)))
+            .thenReturn("json updated content");
         
-        control.replay();
+        //When
         testee.update(session, mailbox, Lists.newArrayList(updatedFlags));
-        control.verify();
+        
+        //Then
+        ImmutableList<UpdatedRepresentation> expectedUpdatedRepresentations = 
ImmutableList.of(new UpdatedRepresentation(ELASTIC_SEARCH_ID, "json updated 
content"));
+        verify(indexer).updateMessages(expectedUpdatedRepresentations);
     }
 
     @Test
-    @SuppressWarnings("unchecked")
     public void updateShouldNotPropagateExceptionWhenExceptionOccurs() throws 
Exception {
-        MailboxSession session = control.createMock(MailboxSession.class);
-
-        Mailbox mailbox = control.createMock(Mailbox.class);
+        //Given
+        Mailbox mailbox = mock(Mailbox.class);
         Flags flags = new Flags();
-        MessageUid messageUid = MessageUid.of(1);
         UpdatedFlags updatedFlags = UpdatedFlags.builder()
-            .uid(messageUid)
+            .uid(MESSAGE_UID)
             .modSeq(MODSEQ)
             .oldFlags(flags)
             .newFlags(flags)
             .build();
-        TestId mailboxId = TestId.of(12);
-
-        expectLastCall();
-        expect(mailbox.getMailboxId()).andReturn(mailboxId).times(2);
+        when(mailbox.getMailboxId())
+            .thenReturn(MAILBOX_ID);
 
-        expect(indexer.updateMessages(anyObject(List.class)))
-            .andThrow(new ElasticsearchException(""));
+        ImmutableList<UpdatedRepresentation> expectedUpdatedRepresentations = 
ImmutableList.of(new UpdatedRepresentation(ELASTIC_SEARCH_ID, "json updated 
content"));
+        when(indexer.updateMessages(expectedUpdatedRepresentations))
+            .thenThrow(new ElasticsearchException(""));
         
-        control.replay();
+        //When
         testee.update(session, mailbox, Lists.newArrayList(updatedFlags));
-        control.verify();
+        
+        //Then
+        //No exception
     }
 
     @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();
+        //Given
+        Mailbox mailbox = mock(Mailbox.class);
+        when(mailbox.getMailboxId())
+            .thenReturn(MAILBOX_ID);
 
-        control.replay();
+        //When
         testee.deleteAll(session, mailbox);
-        control.verify();
+        
+        //Then
+        QueryBuilder expectedQueryBuilder = 
QueryBuilders.termQuery("mailboxId", "12");
+        verify(indexer).deleteAllMatchingQuery(refEq(expectedQueryBuilder));
     }
 
     @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();
+        //Given
+        Mailbox mailbox = mock(Mailbox.class);
+        when(mailbox.getMailboxId())
+            .thenReturn(MAILBOX_ID);
+   
+        doThrow(RuntimeException.class)
+            
.when(indexer).deleteAllMatchingQuery(QueryBuilders.termQuery("mailboxId", 
"12"));
+
+        //When
         testee.deleteAll(session, mailbox);
-        control.verify();
+        
+        //Then
+        //No Exception
     }
+
 }

http://git-wip-us.apache.org/repos/asf/james-project/blob/d87642ab/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
index 2b0a4de..7efb14a 100644
--- 
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
@@ -354,4 +354,32 @@ public class MessageToElasticSearchJsonTest {
             
.isEqualTo(IOUtils.toString(ClassLoader.getSystemResource("eml/nonTextual.json"),
 CHARSET));
     }
 
+    @Test
+    public void convertToJsonWithoutAttachmentShouldConvertEmailBoby() throws 
IOException {
+        // Given
+        MailboxMessage message = new SimpleMailboxMessage(MESSAGE_ID,
+            null,
+            SIZE,
+            BODY_START_OCTET,
+            new 
SharedByteArrayInputStream(IOUtils.toByteArray(ClassLoader.getSystemResourceAsStream("eml/emailWithNonIndexableAttachment.eml"))),
+            new FlagsBuilder().add(Flags.Flag.DELETED, 
Flags.Flag.SEEN).add("debian", "security").build(),
+            propertyBuilder,
+            MAILBOX_ID);
+        message.setModSeq(MOD_SEQ);
+        message.setUid(UID);
+
+        // When
+        MessageToElasticSearchJson messageToElasticSearchJson = new 
MessageToElasticSearchJson(
+                new DefaultTextExtractor(),
+                ZoneId.of("Europe/Paris"),
+                IndexAttachments.YES,
+                MessageSearchIndex.IndexMessageId.Required);
+        String convertToJsonWithoutAttachment = 
messageToElasticSearchJson.convertToJsonWithoutAttachment(message, 
ImmutableList.of(new MockMailboxSession("username").getUser()));
+
+        // Then
+        assertThatJson(convertToJsonWithoutAttachment)
+            .when(IGNORING_ARRAY_ORDER)
+            .when(IGNORING_VALUES)
+            
.isEqualTo(IOUtils.toString(ClassLoader.getSystemResource("eml/emailWithNonIndexableAttachmentWithoutAttachment.json")));
+    }
 }


---------------------------------------------------------------------
To unsubscribe, e-mail: server-dev-unsubscr...@james.apache.org
For additional commands, e-mail: server-dev-h...@james.apache.org

Reply via email to