Author: matthieu
Date: Fri Dec 11 12:35:08 2015
New Revision: 1719393

URL: http://svn.apache.org/viewvc?rev=1719393&view=rev
Log:
JAMES-1644 Handle simple GetMessageRequest and return only message ids

Added:
    
james/project/trunk/server/protocols/jmap/src/main/java/org/apache/james/jmap/methods/GetMessagesMethod.java
    
james/project/trunk/server/protocols/jmap/src/main/java/org/apache/james/jmap/model/GetMessagesRequest.java
    
james/project/trunk/server/protocols/jmap/src/main/java/org/apache/james/jmap/model/GetMessagesResponse.java
    
james/project/trunk/server/protocols/jmap/src/main/java/org/apache/james/jmap/model/MessageId.java
    
james/project/trunk/server/protocols/jmap/src/main/java/org/apache/james/jmap/model/Property.java
    
james/project/trunk/server/protocols/jmap/src/test/java/org/apache/james/jmap/methods/GetMessagesMethodTest.java
    
james/project/trunk/server/protocols/jmap/src/test/java/org/apache/james/jmap/model/GetMessagesRequestTest.java
Modified:
    james/project/trunk/server/protocols/jmap/pom.xml
    
james/project/trunk/server/protocols/jmap/src/main/java/org/apache/james/jmap/model/Message.java
    
james/project/trunk/server/protocols/jmap/src/test/java/org/apache/james/jmap/model/MessageTest.java

Modified: james/project/trunk/server/protocols/jmap/pom.xml
URL: 
http://svn.apache.org/viewvc/james/project/trunk/server/protocols/jmap/pom.xml?rev=1719393&r1=1719392&r2=1719393&view=diff
==============================================================================
--- james/project/trunk/server/protocols/jmap/pom.xml (original)
+++ james/project/trunk/server/protocols/jmap/pom.xml Fri Dec 11 12:35:08 2015
@@ -271,6 +271,11 @@
                     <scope>test</scope>
                 </dependency>
                 <dependency>
+                    <groupId>org.javatuples</groupId>
+                    <artifactId>javatuples</artifactId>
+                    <version>1.2</version>
+                </dependency>
+                <dependency>
                     <groupId>org.mockito</groupId>
                     <artifactId>mockito-core</artifactId>
                     <scope>test</scope>

Added: 
james/project/trunk/server/protocols/jmap/src/main/java/org/apache/james/jmap/methods/GetMessagesMethod.java
URL: 
http://svn.apache.org/viewvc/james/project/trunk/server/protocols/jmap/src/main/java/org/apache/james/jmap/methods/GetMessagesMethod.java?rev=1719393&view=auto
==============================================================================
--- 
james/project/trunk/server/protocols/jmap/src/main/java/org/apache/james/jmap/methods/GetMessagesMethod.java
 (added)
+++ 
james/project/trunk/server/protocols/jmap/src/main/java/org/apache/james/jmap/methods/GetMessagesMethod.java
 Fri Dec 11 12:35:08 2015
@@ -0,0 +1,126 @@
+/****************************************************************
+ * 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.jmap.methods;
+
+import java.util.Iterator;
+import java.util.List;
+import java.util.function.Function;
+import java.util.stream.Collectors;
+import java.util.stream.Stream;
+import java.util.stream.StreamSupport;
+
+import javax.inject.Inject;
+
+import org.apache.james.jmap.model.GetMessagesRequest;
+import org.apache.james.jmap.model.GetMessagesResponse;
+import org.apache.james.jmap.model.Message;
+import org.apache.james.jmap.model.MessageId;
+import org.apache.james.mailbox.MailboxSession;
+import org.apache.james.mailbox.model.MailboxPath;
+import org.apache.james.mailbox.model.MessageRange;
+import org.apache.james.mailbox.store.mail.MailboxMapperFactory;
+import org.apache.james.mailbox.store.mail.MessageMapper;
+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.MailboxId;
+import org.javatuples.Pair;
+
+import com.github.fge.lambdas.Throwing;
+import com.google.common.annotations.VisibleForTesting;
+import com.google.common.base.Preconditions;
+
+public class GetMessagesMethod<Id extends MailboxId> implements Method {
+
+    private static final Method.Name METHOD_NAME = Method.name("getMessages");
+    private final MessageMapperFactory<Id> messageMapperFactory;
+    private final MailboxMapperFactory<Id> mailboxMapperFactory;
+    
+
+    @Inject
+    @VisibleForTesting GetMessagesMethod(
+            MessageMapperFactory<Id> messageMapperFactory, 
+            MailboxMapperFactory<Id> mailboxMapperFactory) {
+        this.messageMapperFactory = messageMapperFactory;
+        this.mailboxMapperFactory = mailboxMapperFactory;
+    }
+    
+    @Override
+    public Method.Name methodName() {
+        return METHOD_NAME;
+    }
+    
+    @Override
+    public Class<? extends JmapRequest> requestType() {
+        return GetMessagesRequest.class;
+    }
+    
+    @Override
+    public GetMessagesResponse process(JmapRequest request, MailboxSession 
mailboxSession) {
+        Preconditions.checkNotNull(request);
+        Preconditions.checkNotNull(mailboxSession);
+        Preconditions.checkArgument(request instanceof GetMessagesRequest);
+        GetMessagesRequest getMessagesRequest = (GetMessagesRequest) request;
+        
+        Function<MessageId, 
Stream<Pair<org.apache.james.mailbox.store.mail.model.Message<Id>, 
MailboxPath>>> loadMessages = loadMessage(mailboxSession);
+        Function<Pair<org.apache.james.mailbox.store.mail.model.Message<Id>, 
MailboxPath>, Message> toJmapMessage = toJmapMessage(mailboxSession);
+        
+        List<Message> result = getMessagesRequest.getIds().stream()
+            .flatMap(loadMessages)
+            .map(toJmapMessage)
+            .collect(Collectors.toList());
+
+        return new GetMessagesResponse(result);
+    }
+
+    private 
Function<Pair<org.apache.james.mailbox.store.mail.model.Message<Id>, 
MailboxPath>, Message> toJmapMessage(MailboxSession mailboxSession) {
+        return (value) -> {
+            org.apache.james.mailbox.store.mail.model.Message<Id> 
messageResult = value.getValue0();
+            MailboxPath mailboxPath = value.getValue1();
+            return Message.fromMailboxMessage(messageResult, uid -> new 
MessageId(mailboxSession.getUser(), mailboxPath , uid));
+        };
+    }
+
+    private Function<MessageId, Stream<
+                                    
Pair<org.apache.james.mailbox.store.mail.model.Message<Id>, 
+                                         MailboxPath>>> 
+                loadMessage(MailboxSession mailboxSession) {
+        
+        return Throwing
+                .function((MessageId messageId) -> {
+                     MailboxPath mailboxPath = 
messageId.getMailboxPath(mailboxSession);
+                     MessageMapper<Id> messageMapper = 
messageMapperFactory.getMessageMapper(mailboxSession);
+                     Mailbox<Id> mailbox = 
mailboxMapperFactory.getMailboxMapper(mailboxSession).findMailboxByPath(mailboxPath);
+                     return Pair.with(
+                             messageMapper.findInMailbox(mailbox, 
MessageRange.one(messageId.getUid()), MessageMapper.FetchType.Full, 1),
+                             mailboxPath
+                             );
+         })
+                .andThen(this::iteratorToStream);
+    }
+    
+    private Stream<Pair<org.apache.james.mailbox.store.mail.model.Message<Id>, 
MailboxPath>> 
iteratorToStream(Pair<Iterator<org.apache.james.mailbox.store.mail.model.Message<Id>>,
 MailboxPath> value) {
+        Iterable<org.apache.james.mailbox.store.mail.model.Message<Id>> 
iterable = () -> value.getValue0();
+        Stream<org.apache.james.mailbox.store.mail.model.Message<Id>> 
targetStream = StreamSupport.stream(iterable.spliterator(), false);
+        
+        MailboxPath mailboxPath = value.getValue1();
+        return targetStream.map(x -> Pair.with(x, mailboxPath));
+    }
+
+}

Added: 
james/project/trunk/server/protocols/jmap/src/main/java/org/apache/james/jmap/model/GetMessagesRequest.java
URL: 
http://svn.apache.org/viewvc/james/project/trunk/server/protocols/jmap/src/main/java/org/apache/james/jmap/model/GetMessagesRequest.java?rev=1719393&view=auto
==============================================================================
--- 
james/project/trunk/server/protocols/jmap/src/main/java/org/apache/james/jmap/model/GetMessagesRequest.java
 (added)
+++ 
james/project/trunk/server/protocols/jmap/src/main/java/org/apache/james/jmap/model/GetMessagesRequest.java
 Fri Dec 11 12:35:08 2015
@@ -0,0 +1,87 @@
+/****************************************************************
+ * 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.jmap.model;
+
+import java.util.Arrays;
+import java.util.Optional;
+
+import org.apache.james.jmap.methods.JmapRequest;
+
+import com.google.common.collect.ImmutableList;
+
+public class GetMessagesRequest implements JmapRequest {
+
+    public static Builder builder() {
+        return new Builder();
+    }
+    
+    public static class Builder {
+        
+        private Optional<String> accountId;
+        private ImmutableList.Builder<MessageId> ids;
+        private Optional<ImmutableList<Property>> properties;
+
+        private Builder() {
+            accountId = Optional.empty();
+            ids = ImmutableList.builder();
+            properties = Optional.empty();
+        }
+        
+        public Builder accountId(String accountId) {
+            this.accountId = Optional.of(accountId);
+            return this;
+        }
+
+        public Builder ids(MessageId... ids) {
+            this.ids.addAll(Arrays.asList(ids));
+            return this;
+        }
+
+        public Builder properties(Property... properties) {
+            this.properties = Optional.of(ImmutableList.copyOf(properties));
+            return this;
+        }
+        
+        public GetMessagesRequest build() {
+            return new GetMessagesRequest(accountId, ids.build(), properties);
+        }
+    }
+
+    private final Optional<String> accountId;
+    private final ImmutableList<MessageId> ids;
+    private final Optional<ImmutableList<Property>> properties;
+
+    public GetMessagesRequest(Optional<String> accountId, 
ImmutableList<MessageId> ids, Optional<ImmutableList<Property>> properties) {
+        this.accountId = accountId;
+        this.ids = ids;
+        this.properties = properties;
+    }
+    
+    public Optional<String> getAccountId() {
+        return accountId;
+    }
+    
+    public ImmutableList<MessageId> getIds() {
+        return ids;
+    }
+    
+    public Optional<ImmutableList<Property>> getProperties() {
+        return properties;
+    }
+}

Added: 
james/project/trunk/server/protocols/jmap/src/main/java/org/apache/james/jmap/model/GetMessagesResponse.java
URL: 
http://svn.apache.org/viewvc/james/project/trunk/server/protocols/jmap/src/main/java/org/apache/james/jmap/model/GetMessagesResponse.java?rev=1719393&view=auto
==============================================================================
--- 
james/project/trunk/server/protocols/jmap/src/main/java/org/apache/james/jmap/model/GetMessagesResponse.java
 (added)
+++ 
james/project/trunk/server/protocols/jmap/src/main/java/org/apache/james/jmap/model/GetMessagesResponse.java
 Fri Dec 11 12:35:08 2015
@@ -0,0 +1,37 @@
+/****************************************************************
+ * 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.jmap.model;
+
+import java.util.List;
+
+import org.apache.james.jmap.methods.Method;
+
+public class GetMessagesResponse implements Method.Response {
+
+    private final List<Message> messages;
+
+    public GetMessagesResponse(List<Message> messages) {
+        this.messages = messages;
+    }
+    
+    public List<Message> list() {
+        return messages;
+    }
+
+}

Modified: 
james/project/trunk/server/protocols/jmap/src/main/java/org/apache/james/jmap/model/Message.java
URL: 
http://svn.apache.org/viewvc/james/project/trunk/server/protocols/jmap/src/main/java/org/apache/james/jmap/model/Message.java?rev=1719393&r1=1719392&r2=1719393&view=diff
==============================================================================
--- 
james/project/trunk/server/protocols/jmap/src/main/java/org/apache/james/jmap/model/Message.java
 (original)
+++ 
james/project/trunk/server/protocols/jmap/src/main/java/org/apache/james/jmap/model/Message.java
 Fri Dec 11 12:35:08 2015
@@ -25,6 +25,7 @@ import java.util.List;
 import java.util.Map;
 import java.util.Optional;
 import java.util.Set;
+import java.util.function.Function;
 import java.util.stream.Collectors;
 
 import org.apache.commons.lang.NotImplementedException;
@@ -53,13 +54,14 @@ public class Message {
         return new Builder();
     }
 
-    public static Message 
fromMailboxMessage(org.apache.james.mailbox.store.mail.model.Message<? extends 
MailboxId> mailboxMessage) {
+    public static Message 
fromMailboxMessage(org.apache.james.mailbox.store.mail.model.Message<? extends 
MailboxId> mailboxMessage,
+            Function<Long, MessageId> uidToMessageId) {
         IndexableMessage im = IndexableMessage.from(mailboxMessage, new 
DefaultTextExtractor(), UTC_ZONE_ID);
         if (im.getHasAttachment()) {
             throw new NotImplementedException();
         }
         return builder()
-                .id(String.valueOf(im.getId()))
+                .id(uidToMessageId.apply(im.getId()))
                 .blobId(String.valueOf(im.getId()))
                 .threadId(String.valueOf(im.getId()))
                 .mailboxIds(ImmutableList.of(im.getMailboxId()))
@@ -149,7 +151,7 @@ public class Message {
 
     @JsonPOJOBuilder(withPrefix = "")
     public static class Builder {
-        private String id;
+        private MessageId id;
         private String blobId;
         private String threadId;
         private ImmutableList<String> mailboxIds;
@@ -183,7 +185,7 @@ public class Message {
             attachedMessages = ImmutableMap.builder();
         }
 
-        public Builder id(String id) {
+        public Builder id(MessageId id) {
             this.id = id;
             return this;
         }
@@ -304,7 +306,7 @@ public class Message {
         }
 
         public Message build() {
-            Preconditions.checkState(!Strings.isNullOrEmpty(id), "'id' is 
mandatory");
+            Preconditions.checkState(id != null, "'id' is mandatory");
             Preconditions.checkState(!Strings.isNullOrEmpty(blobId), "'blobId' 
is mandatory");
             Preconditions.checkState(!Strings.isNullOrEmpty(threadId), 
"'threadId' is mandatory");
             Preconditions.checkState(mailboxIds != null, "'mailboxIds' is 
mandatory");
@@ -328,7 +330,7 @@ public class Message {
                 .allMatch(attachedMessages::containsKey);
     }
 
-    private final String id;
+    private final MessageId id;
     private final String blobId;
     private final String threadId;
     private final ImmutableList<String> mailboxIds;
@@ -353,7 +355,7 @@ public class Message {
     private final ImmutableList<Attachment> attachments;
     private final ImmutableMap<String, SubMessage> attachedMessages;
 
-    @VisibleForTesting Message(String id, String blobId, String threadId, 
ImmutableList<String> mailboxIds, Optional<String> inReplyToMessageId, boolean 
isUnread, boolean isFlagged, boolean isAnswered, boolean isDraft, boolean 
hasAttachment, ImmutableMap<String, String> headers, Optional<Emailer> from,
+    @VisibleForTesting Message(MessageId id, String blobId, String threadId, 
ImmutableList<String> mailboxIds, Optional<String> inReplyToMessageId, boolean 
isUnread, boolean isFlagged, boolean isAnswered, boolean isDraft, boolean 
hasAttachment, ImmutableMap<String, String> headers, Optional<Emailer> from,
             ImmutableList<Emailer> to, ImmutableList<Emailer> cc, 
ImmutableList<Emailer> bcc, ImmutableList<Emailer> replyTo, String subject, 
ZonedDateTime date, long size, String preview, Optional<String> textBody, 
Optional<String> htmlBody, ImmutableList<Attachment> attachments,
             ImmutableMap<String, SubMessage> attachedMessages) {
         this.id = id;
@@ -382,7 +384,7 @@ public class Message {
         this.attachedMessages = attachedMessages;
     }
 
-    public String getId() {
+    public MessageId getId() {
         return id;
     }
 
@@ -477,4 +479,5 @@ public class Message {
     public ImmutableMap<String, SubMessage> getAttachedMessages() {
         return attachedMessages;
     }
+
 }

Added: 
james/project/trunk/server/protocols/jmap/src/main/java/org/apache/james/jmap/model/MessageId.java
URL: 
http://svn.apache.org/viewvc/james/project/trunk/server/protocols/jmap/src/main/java/org/apache/james/jmap/model/MessageId.java?rev=1719393&view=auto
==============================================================================
--- 
james/project/trunk/server/protocols/jmap/src/main/java/org/apache/james/jmap/model/MessageId.java
 (added)
+++ 
james/project/trunk/server/protocols/jmap/src/main/java/org/apache/james/jmap/model/MessageId.java
 Fri Dec 11 12:35:08 2015
@@ -0,0 +1,99 @@
+/****************************************************************
+ * 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.jmap.model;
+
+import java.util.Objects;
+
+import org.apache.james.mailbox.MailboxSession;
+import org.apache.james.mailbox.MailboxSession.User;
+import org.apache.james.mailbox.model.MailboxPath;
+import org.javatuples.Triplet;
+
+import com.fasterxml.jackson.annotation.JsonValue;
+import com.google.common.base.Joiner;
+import com.google.common.base.Splitter;
+
+public class MessageId {
+
+    private static final String SEPARATOR = "-";
+
+    public static MessageId of(String id) {
+        Triplet<String, String, String> parts = 
Triplet.fromIterable(Splitter.on(SEPARATOR).split(id));
+        return new MessageId(parts.getValue0(), parts.getValue1(), 
Long.valueOf(parts.getValue2()));
+    }
+
+    private final String mailboxPath;
+    private final long uid;
+    private final String username;
+
+    public MessageId(User username, MailboxPath mailboxPath, long uid) {
+        this.username = username.getUserName();
+        this.mailboxPath = mailboxPath.getName();
+        this.uid = uid;
+    }
+    
+    private MessageId(String username, String mailboxPath, long uid) {
+        this.username = username;
+        this.mailboxPath = mailboxPath;
+        this.uid = uid;
+    }
+    
+    public String getUsername() {
+        return username;
+    }
+    
+    public long getUid() {
+        return uid;
+    }
+    
+    public MailboxPath getMailboxPath(MailboxSession mailboxSession) {
+        return new MailboxPath("", username, mailboxPath);
+    }
+    
+    @JsonValue
+    public String serialize() {
+        return Joiner.on(SEPARATOR).join(username, mailboxPath, uid);
+    }
+    
+    @Override
+    public boolean equals(Object obj) {
+        if (obj instanceof MessageId) {
+            MessageId other = (MessageId) obj;
+            return Objects.equals(username, other.username)
+                 && Objects.equals(mailboxPath, other.mailboxPath)
+                 && Objects.equals(uid, other.uid);
+        }
+        return false;
+    }
+
+    @Override
+    public int hashCode() {
+        return Objects.hash(username, mailboxPath, uid);
+    }
+    
+    @Override
+    public String toString() {
+        return com.google.common.base.Objects
+                .toStringHelper(getClass())
+                .add("username", username)
+                .add("mailboxPath", mailboxPath)
+                .add("uid", uid)
+                .toString();
+    }
+}

Added: 
james/project/trunk/server/protocols/jmap/src/main/java/org/apache/james/jmap/model/Property.java
URL: 
http://svn.apache.org/viewvc/james/project/trunk/server/protocols/jmap/src/main/java/org/apache/james/jmap/model/Property.java?rev=1719393&view=auto
==============================================================================
--- 
james/project/trunk/server/protocols/jmap/src/main/java/org/apache/james/jmap/model/Property.java
 (added)
+++ 
james/project/trunk/server/protocols/jmap/src/main/java/org/apache/james/jmap/model/Property.java
 Fri Dec 11 12:35:08 2015
@@ -0,0 +1,23 @@
+/****************************************************************
+ * 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.jmap.model;
+
+public class Property {
+
+}

Added: 
james/project/trunk/server/protocols/jmap/src/test/java/org/apache/james/jmap/methods/GetMessagesMethodTest.java
URL: 
http://svn.apache.org/viewvc/james/project/trunk/server/protocols/jmap/src/test/java/org/apache/james/jmap/methods/GetMessagesMethodTest.java?rev=1719393&view=auto
==============================================================================
--- 
james/project/trunk/server/protocols/jmap/src/test/java/org/apache/james/jmap/methods/GetMessagesMethodTest.java
 (added)
+++ 
james/project/trunk/server/protocols/jmap/src/test/java/org/apache/james/jmap/methods/GetMessagesMethodTest.java
 Fri Dec 11 12:35:08 2015
@@ -0,0 +1,141 @@
+/****************************************************************
+ * 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.jmap.methods;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
+import static org.mockito.Mockito.mock;
+
+import java.io.ByteArrayInputStream;
+import java.util.Date;
+import java.util.List;
+import java.util.Locale;
+
+import org.apache.james.jmap.model.GetMessagesRequest;
+import org.apache.james.jmap.model.GetMessagesResponse;
+import org.apache.james.jmap.model.MessageId;
+import org.apache.james.mailbox.MailboxSession;
+import org.apache.james.mailbox.MessageManager;
+import org.apache.james.mailbox.acl.SimpleGroupMembershipResolver;
+import org.apache.james.mailbox.acl.UnionMailboxACLResolver;
+import org.apache.james.mailbox.exception.MailboxException;
+import org.apache.james.mailbox.inmemory.InMemoryId;
+import org.apache.james.mailbox.inmemory.InMemoryMailboxSessionMapperFactory;
+import org.apache.james.mailbox.model.MailboxPath;
+import org.apache.james.mailbox.store.MockAuthenticator;
+import org.apache.james.mailbox.store.StoreMailboxManager;
+import org.junit.Before;
+import org.junit.Test;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import com.google.common.base.Charsets;
+import com.google.common.collect.ImmutableList;
+
+public class GetMessagesMethodTest {
+
+    private static final Logger LOGGER = 
LoggerFactory.getLogger(GetMessagesMethodTest.class);
+    
+    private StoreMailboxManager<InMemoryId> mailboxManager;
+
+    private static class User implements 
org.apache.james.mailbox.MailboxSession.User {
+        final String username;
+        final String password;
+
+        public User(String username, String password) {
+            this.username = username;
+            this.password = password;
+        }
+        
+        @Override
+        public String getUserName() {
+            return username;
+        }
+
+        @Override
+        public String getPassword() {
+            return password;
+        }
+        
+        @Override
+        public List<Locale> getLocalePreferences() {
+            return ImmutableList.of();
+        }
+    }
+    
+    private static final User ROBERT = new User("robert", "secret");
+
+    private MailboxSession session;
+    private MailboxPath inboxPath;
+
+    private InMemoryMailboxSessionMapperFactory mailboxSessionMapperFactory;
+    
+    @Before
+    public void setup() throws MailboxException {
+        
+        mailboxSessionMapperFactory = new 
InMemoryMailboxSessionMapperFactory();
+        MockAuthenticator authenticator = new MockAuthenticator();
+        authenticator.addUser(ROBERT.username, ROBERT.password);
+        UnionMailboxACLResolver aclResolver = new UnionMailboxACLResolver();
+        SimpleGroupMembershipResolver groupMembershipResolver = new 
SimpleGroupMembershipResolver();
+        mailboxManager = new 
StoreMailboxManager<>(mailboxSessionMapperFactory, authenticator, aclResolver, 
groupMembershipResolver);
+        mailboxManager.init();
+        
+
+        session = mailboxManager.login(ROBERT.username, ROBERT.password, 
LOGGER);
+        inboxPath = MailboxPath.inbox(session);
+        mailboxManager.createMailbox(inboxPath, session);
+    }
+    
+    @Test
+    public void processShouldThrowWhenNullRequest() {
+        GetMessagesMethod<InMemoryId> testee = new 
GetMessagesMethod<>(mailboxSessionMapperFactory, mailboxSessionMapperFactory);
+        GetMessagesRequest request = null;
+        assertThatThrownBy(() -> testee.process(request, 
mock(MailboxSession.class))).isInstanceOf(NullPointerException.class);
+    }
+
+    @Test
+    public void processShouldThrowWhenNullSession() {
+        GetMessagesMethod<InMemoryId> testee = new 
GetMessagesMethod<>(mailboxSessionMapperFactory, mailboxSessionMapperFactory);
+        MailboxSession mailboxSession = null;
+        assertThatThrownBy(() -> 
testee.process(mock(GetMessagesRequest.class), 
mailboxSession)).isInstanceOf(NullPointerException.class);
+    }
+    
+    @Test
+    public void processShouldFetchMessages() throws MailboxException {
+        MessageManager inbox = mailboxManager.getMailbox(inboxPath, session);
+        ByteArrayInputStream messageContent = new ByteArrayInputStream("my 
message".getBytes(Charsets.UTF_8));
+        Date now = new Date();
+        long message1Uid = inbox.appendMessage(messageContent, now, session, 
false, null);
+        long message2Uid = inbox.appendMessage(messageContent, now, session, 
false, null);
+        long message3Uid = inbox.appendMessage(messageContent, now, session, 
false, null);
+        
+        GetMessagesRequest request = GetMessagesRequest.builder()
+                .ids(new MessageId(ROBERT, inboxPath, message1Uid),
+                          new MessageId(ROBERT, inboxPath, message2Uid),
+                          new MessageId(ROBERT, inboxPath, message3Uid))
+                .build();
+
+        GetMessagesMethod<InMemoryId> testee = new 
GetMessagesMethod<>(mailboxSessionMapperFactory, mailboxSessionMapperFactory);
+        GetMessagesResponse result = testee.process(request, session);
+        
+        assertThat(result.list()).extracting(message -> 
message.getId().getUid()).containsOnly(message1Uid, message2Uid, message3Uid);
+    }
+    
+}

Added: 
james/project/trunk/server/protocols/jmap/src/test/java/org/apache/james/jmap/model/GetMessagesRequestTest.java
URL: 
http://svn.apache.org/viewvc/james/project/trunk/server/protocols/jmap/src/test/java/org/apache/james/jmap/model/GetMessagesRequestTest.java?rev=1719393&view=auto
==============================================================================
--- 
james/project/trunk/server/protocols/jmap/src/test/java/org/apache/james/jmap/model/GetMessagesRequestTest.java
 (added)
+++ 
james/project/trunk/server/protocols/jmap/src/test/java/org/apache/james/jmap/model/GetMessagesRequestTest.java
 Fri Dec 11 12:35:08 2015
@@ -0,0 +1,62 @@
+/****************************************************************
+ * 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.jmap.model;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
+
+import org.junit.Test;
+
+import com.google.common.collect.ImmutableList;
+
+public class GetMessagesRequestTest {
+
+    @Test
+    public void shouldAllowOptionalAccountId() {
+        GetMessagesRequest result = 
GetMessagesRequest.builder().ids(MessageId.of("user-inbox-1")).properties(new 
Property()).build();
+        assertThat(result).isNotNull();
+        assertThat(result.getAccountId()).isEmpty();
+    }
+
+    @Test
+    public void shouldThrowWhenAccountIdIsNull() {
+        assertThatThrownBy(() -> 
GetMessagesRequest.builder().accountId(null)).isInstanceOf(NullPointerException.class);
+    }
+
+    @Test
+    public void shouldAllowEmptyMessagesList() {
+        GetMessagesRequest result = 
GetMessagesRequest.builder().accountId("accountId").ids().properties(new 
Property()).build();
+        assertThat(result).isNotNull();
+        assertThat(result.getIds()).isEmpty();
+    }
+
+    @Test
+    public void shouldAllowAbsentPropertyList() {
+        GetMessagesRequest result = 
GetMessagesRequest.builder().accountId("accountId").ids().build();
+        assertThat(result).isNotNull();
+        assertThat(result.getProperties()).isEmpty();
+    }
+
+    @Test
+    public void shouldAllowEmptyPropertyList() {
+        GetMessagesRequest result = 
GetMessagesRequest.builder().accountId("accountId").ids().properties(new 
Property[0]).build();
+        assertThat(result).isNotNull();
+        assertThat(result.getProperties()).contains(ImmutableList.of());
+    }
+}

Modified: 
james/project/trunk/server/protocols/jmap/src/test/java/org/apache/james/jmap/model/MessageTest.java
URL: 
http://svn.apache.org/viewvc/james/project/trunk/server/protocols/jmap/src/test/java/org/apache/james/jmap/model/MessageTest.java?rev=1719393&r1=1719392&r2=1719393&view=diff
==============================================================================
--- 
james/project/trunk/server/protocols/jmap/src/test/java/org/apache/james/jmap/model/MessageTest.java
 (original)
+++ 
james/project/trunk/server/protocols/jmap/src/test/java/org/apache/james/jmap/model/MessageTest.java
 Fri Dec 11 12:35:08 2015
@@ -52,82 +52,77 @@ public class MessageTest {
     }
 
     @Test(expected=IllegalStateException.class)
-    public void buildShouldThrowWhenIdIsEmpty() {
-        Message.builder().id("").build();
-    }
-
-    @Test(expected=IllegalStateException.class)
     public void buildShouldThrowWhenBlobIdIsNull() {
-        Message.builder().id("id").build();
+        Message.builder().id(MessageId.of("user-box-1")).build();
     }
 
     @Test(expected=IllegalStateException.class)
     public void buildShouldThrowWhenBlobIdIsEmpty() {
-        Message.builder().id("id").blobId("").build();
+        Message.builder().id(MessageId.of("user-box-1")).blobId("").build();
     }
 
     @Test(expected=IllegalStateException.class)
     public void buildShouldThrowWhenThreadIdIsNull() {
-        Message.builder().id("id").blobId("blobId").build();
+        
Message.builder().id(MessageId.of("user-box-1")).blobId("blobId").build();
     }
 
     @Test(expected=IllegalStateException.class)
     public void buildShouldThrowWhenThreadIdIsEmpty() {
-        Message.builder().id("id").blobId("blobId").threadId("").build();
+        
Message.builder().id(MessageId.of("user-box-1")).blobId("blobId").threadId("").build();
     }
 
     @Test(expected=IllegalStateException.class)
     public void buildShouldThrowWhenMailboxIdsIsNull() {
-        
Message.builder().id("id").blobId("blobId").threadId("threadId").build();
+        
Message.builder().id(MessageId.of("user-box-1")).blobId("blobId").threadId("threadId").build();
     }
 
     @Test(expected=IllegalStateException.class)
     public void buildShouldThrowWhenHeadersIsNull() {
-        
Message.builder().id("id").blobId("blobId").threadId("threadId").mailboxIds(ImmutableList.of()).build();
+        
Message.builder().id(MessageId.of("user-box-1")).blobId("blobId").threadId("threadId").mailboxIds(ImmutableList.of()).build();
     }
 
     @Test(expected=IllegalStateException.class)
     public void buildShouldThrowWhenSubjectIsNull() {
-        
Message.builder().id("id").blobId("blobId").threadId("threadId").mailboxIds(ImmutableList.of()).headers(ImmutableMap.of()).build();
+        
Message.builder().id(MessageId.of("user-box-1")).blobId("blobId").threadId("threadId").mailboxIds(ImmutableList.of()).headers(ImmutableMap.of()).build();
     }
 
     @Test(expected=IllegalStateException.class)
     public void buildShouldThrowWhenSubjectIsEmpty() {
-        
Message.builder().id("id").blobId("blobId").threadId("threadId").mailboxIds(ImmutableList.of()).headers(ImmutableMap.of())
+        
Message.builder().id(MessageId.of("user-box-1")).blobId("blobId").threadId("threadId").mailboxIds(ImmutableList.of()).headers(ImmutableMap.of())
             .subject("").build();
     }
 
     @Test(expected=IllegalStateException.class)
     public void buildShouldThrowWhenSizeIsNull() {
-        
Message.builder().id("id").blobId("blobId").threadId("threadId").mailboxIds(ImmutableList.of()).headers(ImmutableMap.of())
+        
Message.builder().id(MessageId.of("user-box-1")).blobId("blobId").threadId("threadId").mailboxIds(ImmutableList.of()).headers(ImmutableMap.of())
             .subject("subject").build();
     }
 
     @Test(expected=IllegalStateException.class)
     public void buildShouldThrowWhenDateIsNull() {
-        
Message.builder().id("id").blobId("blobId").threadId("threadId").mailboxIds(ImmutableList.of()).headers(ImmutableMap.of())
+        
Message.builder().id(MessageId.of("user-box-1")).blobId("blobId").threadId("threadId").mailboxIds(ImmutableList.of()).headers(ImmutableMap.of())
             .subject("subject").size(123).build();
     }
 
     @Test(expected=IllegalStateException.class)
     public void buildShouldThrowWhenPreviewIsNull() {
-        
Message.builder().id("id").blobId("blobId").threadId("threadId").mailboxIds(ImmutableList.of()).headers(ImmutableMap.of())
+        
Message.builder().id(MessageId.of("user-box-1")).blobId("blobId").threadId("threadId").mailboxIds(ImmutableList.of()).headers(ImmutableMap.of())
             .subject("subject").size(123).date(ZonedDateTime.now()).build();
     }
 
     @Test(expected=IllegalStateException.class)
     public void buildShouldThrowWhenPreviewIsEmpty() {
-        
Message.builder().id("id").blobId("blobId").threadId("threadId").mailboxIds(ImmutableList.of()).headers(ImmutableMap.of())
+        
Message.builder().id(MessageId.of("user-box-1")).blobId("blobId").threadId("threadId").mailboxIds(ImmutableList.of()).headers(ImmutableMap.of())
             
.subject("subject").size(123).date(ZonedDateTime.now()).preview("").build();
     }
 
     @Test
     public void buildShouldWorkWhenMandatoryFieldsArePresent() {
         ZonedDateTime currentDate = ZonedDateTime.now();
-        Message expected = new Message("id", "blobId", "threadId", 
ImmutableList.of("mailboxId"), Optional.empty(), false, false, false, false, 
false, ImmutableMap.of("key", "value"), Optional.empty(),
+        Message expected = new Message(MessageId.of("user-box-1"), "blobId", 
"threadId", ImmutableList.of("mailboxId"), Optional.empty(), false, false, 
false, false, false, ImmutableMap.of("key", "value"), Optional.empty(),
                 ImmutableList.of(), ImmutableList.of(), ImmutableList.of(), 
ImmutableList.of(), "subject", currentDate, 123, "preview", Optional.empty(), 
Optional.empty(), ImmutableList.of(), ImmutableMap.of());
         Message tested = Message.builder()
-                .id("id")
+                .id(MessageId.of("user-box-1"))
                 .blobId("blobId")
                 .threadId("threadId")
                 .mailboxIds(ImmutableList.of("mailboxId"))
@@ -151,7 +146,7 @@ public class MessageTest {
                 .build();
         ImmutableMap<String, SubMessage> attachedMessages = 
ImmutableMap.of("differentBlobId", simpleMessage);
         Message.builder()
-            .id("id")
+            .id(MessageId.of("user-box-1"))
             .blobId("blobId")
             .threadId("threadId")
             .mailboxIds(ImmutableList.of("mailboxId"))
@@ -182,7 +177,7 @@ public class MessageTest {
                 .build();
         ImmutableMap<String, SubMessage> attachedMessages = 
ImmutableMap.of("blobId", simpleMessage);
         Message expected = new Message(
-                "id",
+                MessageId.of("user-box-1"),
                 "blobId",
                 "threadId",
                 ImmutableList.of("mailboxId"),
@@ -207,7 +202,7 @@ public class MessageTest {
                 attachments,
                 attachedMessages);
         Message tested = Message.builder()
-            .id("id")
+            .id(MessageId.of("user-box-1"))
             .blobId("blobId")
             .threadId("threadId")
             .mailboxIds(ImmutableList.of("mailboxId"))
@@ -247,9 +242,9 @@ public class MessageTest {
                 MAILBOX_ID);
         testMail.setModSeq(MOD_SEQ);
         
-        Message testee = Message.fromMailboxMessage(testMail);
+        Message testee = Message.fromMailboxMessage(testMail, x -> 
MessageId.of("user-box-" + x));
         Message expected = Message.builder()
-                .id("0")
+                .id(MessageId.of("user-box-0"))
                 .blobId("0")
                 .threadId("0")
                 .mailboxIds(ImmutableList.of(MAILBOX_ID.serialize()))
@@ -278,9 +273,9 @@ public class MessageTest {
                 MAILBOX_ID);
         testMail.setModSeq(MOD_SEQ);
         
-        Message testee = Message.fromMailboxMessage(testMail);
+        Message testee = Message.fromMailboxMessage(testMail, x -> 
MessageId.of("user-box-" + x));
         Message expected = Message.builder()
-                .id("0")
+                .id(MessageId.of("user-box-0"))
                 .blobId("0")
                 .threadId("0")
                 .mailboxIds(ImmutableList.of(MAILBOX_ID.serialize()))
@@ -333,9 +328,9 @@ public class MessageTest {
                 .put("in-reply-to", 
"<[email protected]>")
                 .put("other-header", "other header value")
                 .build();
-        Message testee = Message.fromMailboxMessage(testMail);
+        Message testee = Message.fromMailboxMessage(testMail, x -> 
MessageId.of("user-box-" + x));
         Message expected = Message.builder()
-                .id("0")
+                .id(MessageId.of("user-box-0"))
                 .blobId("0")
                 .threadId("0")
                 .mailboxIds(ImmutableList.of(MAILBOX_ID.serialize()))
@@ -369,9 +364,9 @@ public class MessageTest {
                 MAILBOX_ID);
         testMail.setModSeq(MOD_SEQ);
         
-        Message testee = Message.fromMailboxMessage(testMail);
+        Message testee = Message.fromMailboxMessage(testMail, x -> 
MessageId.of("user-box-" + x));
         Message expected = Message.builder()
-                .id("0")
+                .id(MessageId.of("user-box-0"))
                 .blobId("0")
                 .threadId("0")
                 .mailboxIds(ImmutableList.of(MAILBOX_ID.serialize()))
@@ -429,9 +424,9 @@ public class MessageTest {
                 MAILBOX_ID);
         testMail.setModSeq(MOD_SEQ);
         
-        Message testee = Message.fromMailboxMessage(testMail);
+        Message testee = Message.fromMailboxMessage(testMail, x -> 
MessageId.of("user-box-" + x));
         Message expected = Message.builder()
-                .id("0")
+                .id(MessageId.of("user-box-0"))
                 .blobId("0")
                 .threadId("0")
                 .mailboxIds(ImmutableList.of(MAILBOX_ID.serialize()))
@@ -457,6 +452,6 @@ public class MessageTest {
                 MAILBOX_ID);
         testMail.setModSeq(MOD_SEQ);
         
-        Message.fromMailboxMessage(testMail);
+        Message.fromMailboxMessage(testMail, x -> MessageId.of("user-box-" + 
x));
     }
 }




---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to