james-server-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From adup...@apache.org
Subject james-project git commit: JAMES-1680 Support ids parameter in getMailboxes query
Date Thu, 04 Feb 2016 13:13:36 GMT
Repository: james-project
Updated Branches:
  refs/heads/master c1d028086 -> 409344c12


JAMES-1680 Support ids parameter in getMailboxes query


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

Branch: refs/heads/master
Commit: 409344c12fe326fca6830cd68e609fe16d25f1e1
Parents: c1d0280
Author: Antoine Duprat <antduprat@gmail.com>
Authored: Tue Feb 2 14:41:49 2016 +0100
Committer: Antoine Duprat <antduprat@gmail.com>
Committed: Thu Feb 4 14:12:18 2016 +0100

----------------------------------------------------------------------
 .../apache/james/utils/ExtendedServerProbe.java |   4 +
 .../apache/james/utils/GuiceServerProbe.java    |  22 +++-
 .../jmap/methods/GetMailboxesMethodTest.java    | 102 ++++++++++++++++++-
 .../james/jmap/methods/GetMailboxesMethod.java  |  17 +++-
 .../james/jmap/model/GetMailboxesRequest.java   |  27 ++---
 .../jmap/model/GetMailboxesRequestTest.java     |  22 +++-
 6 files changed, 170 insertions(+), 24 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/james-project/blob/409344c1/server/container/cassandra-guice/src/main/java/org/apache/james/utils/ExtendedServerProbe.java
----------------------------------------------------------------------
diff --git a/server/container/cassandra-guice/src/main/java/org/apache/james/utils/ExtendedServerProbe.java
b/server/container/cassandra-guice/src/main/java/org/apache/james/utils/ExtendedServerProbe.java
index b4ce40d..fc27b61 100644
--- a/server/container/cassandra-guice/src/main/java/org/apache/james/utils/ExtendedServerProbe.java
+++ b/server/container/cassandra-guice/src/main/java/org/apache/james/utils/ExtendedServerProbe.java
@@ -25,12 +25,16 @@ import java.util.Date;
 import javax.mail.Flags;
 
 import org.apache.james.cli.probe.ServerProbe;
+import org.apache.james.mailbox.cassandra.CassandraId;
 import org.apache.james.mailbox.exception.BadCredentialsException;
 import org.apache.james.mailbox.exception.MailboxException;
 import org.apache.james.mailbox.model.MailboxPath;
+import org.apache.james.mailbox.store.mail.model.Mailbox;
 
 public interface ExtendedServerProbe extends ServerProbe {
 
     void appendMessage(String username, MailboxPath mailboxPath, InputStream message, Date
internalDate, boolean isRecent, Flags flags) 
             throws BadCredentialsException, MailboxException;
+
+    Mailbox<CassandraId> getMailbox(String namespace, String user, String name);
 }

http://git-wip-us.apache.org/repos/asf/james-project/blob/409344c1/server/container/cassandra-guice/src/main/java/org/apache/james/utils/GuiceServerProbe.java
----------------------------------------------------------------------
diff --git a/server/container/cassandra-guice/src/main/java/org/apache/james/utils/GuiceServerProbe.java
b/server/container/cassandra-guice/src/main/java/org/apache/james/utils/GuiceServerProbe.java
index 82ed1a8..151410f 100644
--- a/server/container/cassandra-guice/src/main/java/org/apache/james/utils/GuiceServerProbe.java
+++ b/server/container/cassandra-guice/src/main/java/org/apache/james/utils/GuiceServerProbe.java
@@ -36,12 +36,16 @@ import org.apache.james.domainlist.api.DomainList;
 import org.apache.james.mailbox.MailboxManager;
 import org.apache.james.mailbox.MailboxSession;
 import org.apache.james.mailbox.MessageManager;
+import org.apache.james.mailbox.cassandra.CassandraId;
 import org.apache.james.mailbox.exception.BadCredentialsException;
 import org.apache.james.mailbox.exception.MailboxException;
 import org.apache.james.mailbox.model.MailboxConstants;
 import org.apache.james.mailbox.model.MailboxMetaData;
 import org.apache.james.mailbox.model.MailboxPath;
 import org.apache.james.mailbox.model.MailboxQuery;
+import org.apache.james.mailbox.store.mail.MailboxMapper;
+import org.apache.james.mailbox.store.mail.MailboxMapperFactory;
+import org.apache.james.mailbox.store.mail.model.Mailbox;
 import org.apache.james.rrt.lib.Mappings;
 import org.apache.james.user.api.UsersRepository;
 import org.slf4j.Logger;
@@ -56,12 +60,14 @@ public class GuiceServerProbe implements ExtendedServerProbe {
     private static final Logger LOGGER = LoggerFactory.getLogger(GuiceServerProbe.class);
 
     private final MailboxManager mailboxManager;
+    private final MailboxMapperFactory<CassandraId> mailboxMapperFactory;
     private final DomainList domainList;
     private final UsersRepository usersRepository;
 
     @Inject
-    private GuiceServerProbe(MailboxManager mailboxManager, DomainList domainList, UsersRepository
usersRepository) {
+    private GuiceServerProbe(MailboxManager mailboxManager, MailboxMapperFactory<CassandraId>
mailboxMapperFactory, DomainList domainList, UsersRepository usersRepository) {
         this.mailboxManager = mailboxManager;
+        this.mailboxMapperFactory = mailboxMapperFactory;
         this.domainList = domainList;
         this.usersRepository = usersRepository;
     }
@@ -164,6 +170,20 @@ public class GuiceServerProbe implements ExtendedServerProbe {
         }
     }
 
+    @Override
+    public Mailbox<CassandraId> getMailbox(String namespace, String user, String name)
{
+        MailboxSession mailboxSession = null;
+        try {
+            mailboxSession = mailboxManager.createSystemSession(user, LOGGER);
+            MailboxMapper<CassandraId> mailboxMapper = mailboxMapperFactory.getMailboxMapper(mailboxSession);
+            return mailboxMapper.findMailboxByPath(new MailboxPath(namespace, user, name));
+        } catch (MailboxException e) {
+            throw Throwables.propagate(e);
+        } finally {
+            closeSession(mailboxSession);
+        }
+    }
+
     private void closeSession(MailboxSession session) {
         if (session != null) {
             mailboxManager.endProcessingRequest(session);

http://git-wip-us.apache.org/repos/asf/james-project/blob/409344c1/server/protocols/jmap-integration-testing/src/test/java/org/apache/james/jmap/methods/GetMailboxesMethodTest.java
----------------------------------------------------------------------
diff --git a/server/protocols/jmap-integration-testing/src/test/java/org/apache/james/jmap/methods/GetMailboxesMethodTest.java
b/server/protocols/jmap-integration-testing/src/test/java/org/apache/james/jmap/methods/GetMailboxesMethodTest.java
index 3ed4ec2..7b23ab2 100644
--- a/server/protocols/jmap-integration-testing/src/test/java/org/apache/james/jmap/methods/GetMailboxesMethodTest.java
+++ b/server/protocols/jmap-integration-testing/src/test/java/org/apache/james/jmap/methods/GetMailboxesMethodTest.java
@@ -39,9 +39,11 @@ import org.apache.james.backends.cassandra.EmbeddedCassandra;
 import org.apache.james.jmap.JmapAuthentication;
 import org.apache.james.jmap.JmapServer;
 import org.apache.james.jmap.api.access.AccessToken;
+import org.apache.james.mailbox.cassandra.CassandraId;
 import org.apache.james.mailbox.elasticsearch.EmbeddedElasticSearch;
 import org.apache.james.mailbox.model.MailboxConstants;
 import org.apache.james.mailbox.model.MailboxPath;
+import org.apache.james.mailbox.store.mail.model.Mailbox;
 import org.junit.Before;
 import org.junit.Rule;
 import org.junit.Test;
@@ -106,9 +108,76 @@ public abstract class GetMailboxesMethodTest {
             .content(equalTo("[[\"error\",{\"type\":\"Not yet implemented\"},\"#0\"]]"));
     }
 
-    
     @Test
-    public void getMailboxesShouldErrorNotSupportedWhenRequestContainsNonNullIds() throws
Exception {
+    public void getMailboxesShouldReturnEmptyWhenIdsDoesntMatch() throws Exception {
+        jmapServer.serverProbe().createMailbox(MailboxConstants.USER_NAMESPACE, username,
"name");
+
+        given()
+            .accept(ContentType.JSON)
+            .contentType(ContentType.JSON)
+            .header("Authorization", accessToken.serialize())
+            .body("[[\"getMailboxes\", {\"ids\": [\"notAMailboxId\"]}, \"#0\"]]")
+        .when()
+            .post("/jmap")
+        .then()
+            .statusCode(200)
+            .body("[0][0]", equalTo("mailboxes"))
+            .body("[0][1].list", hasSize(0));
+    }
+
+    @Test
+    public void getMailboxesShouldReturnMailboxesWhenIdsMatch() throws Exception {
+        jmapServer.serverProbe().createMailbox(MailboxConstants.USER_NAMESPACE, username,
"INBOX");
+        jmapServer.serverProbe().createMailbox(MailboxConstants.USER_NAMESPACE, username,
"myMailbox");
+
+        Mailbox<CassandraId> mailbox = jmapServer.serverProbe().getMailbox(MailboxConstants.USER_NAMESPACE,
username, "INBOX");
+        Mailbox<CassandraId> mailbox2 = jmapServer.serverProbe().getMailbox(MailboxConstants.USER_NAMESPACE,
username, "myMailbox");
+
+        String mailboxId = mailbox.getMailboxId().serialize();
+        String mailboxId2 = mailbox2.getMailboxId().serialize();
+
+        given()
+            .accept(ContentType.JSON)
+            .contentType(ContentType.JSON)
+            .header("Authorization", accessToken.serialize())
+            .body("[[\"getMailboxes\", {\"ids\": [\"" + mailboxId + "\", \"" + mailboxId2
+ "\"]}, \"#0\"]]")
+        .when()
+            .post("/jmap")
+        .then()
+            .statusCode(200)
+            .body("[0][0]", equalTo("mailboxes"))
+            .body("[0][1].list", hasSize(2))
+            .body("[0][1].list[0].id", equalTo(mailboxId))
+            .body("[0][1].list[1].id", equalTo(mailboxId2));
+    }
+
+    @Test
+    public void getMailboxesShouldReturnOnlyMatchingMailboxesWhenIdsGiven() throws Exception
{
+        jmapServer.serverProbe().createMailbox(MailboxConstants.USER_NAMESPACE, username,
"INBOX");
+        jmapServer.serverProbe().createMailbox(MailboxConstants.USER_NAMESPACE, username,
"myMailbox");
+
+        Mailbox<CassandraId> mailbox = jmapServer.serverProbe().getMailbox(MailboxConstants.USER_NAMESPACE,
username, "INBOX");
+
+        String mailboxId = mailbox.getMailboxId().serialize();
+
+        given()
+            .accept(ContentType.JSON)
+            .contentType(ContentType.JSON)
+            .header("Authorization", accessToken.serialize())
+            .body("[[\"getMailboxes\", {\"ids\": [\"" + mailboxId + "\"]}, \"#0\"]]")
+        .when()
+            .post("/jmap")
+        .then()
+            .statusCode(200)
+            .body("[0][0]", equalTo("mailboxes"))
+            .body("[0][1].list", hasSize(1))
+            .body("[0][1].list[0].id", equalTo(mailboxId));
+    }
+
+    @Test
+    public void getMailboxesShouldReturnEmptyWhenIdsIsEmpty() throws Exception {
+        jmapServer.serverProbe().createMailbox(MailboxConstants.USER_NAMESPACE, username,
"INBOX");
+
         given()
             .accept(ContentType.JSON)
             .contentType(ContentType.JSON)
@@ -118,7 +187,34 @@ public abstract class GetMailboxesMethodTest {
             .post("/jmap")
         .then()
             .statusCode(200)
-            .content(equalTo("[[\"error\",{\"type\":\"Not yet implemented\"},\"#0\"]]"));
+            .body("[0][0]", equalTo("mailboxes"))
+            .body("[0][1].list", hasSize(0));
+    }
+
+    @Test
+    public void getMailboxesShouldReturnAllMailboxesWhenIdsIsNull() throws Exception {
+        jmapServer.serverProbe().createMailbox(MailboxConstants.USER_NAMESPACE, username,
"INBOX");
+        jmapServer.serverProbe().createMailbox(MailboxConstants.USER_NAMESPACE, username,
"myMailbox");
+
+        Mailbox<CassandraId> mailbox = jmapServer.serverProbe().getMailbox(MailboxConstants.USER_NAMESPACE,
username, "INBOX");
+        Mailbox<CassandraId> mailbox2 = jmapServer.serverProbe().getMailbox(MailboxConstants.USER_NAMESPACE,
username, "myMailbox");
+
+        String mailboxId = mailbox.getMailboxId().serialize();
+        String mailboxId2 = mailbox2.getMailboxId().serialize();
+
+        given()
+            .accept(ContentType.JSON)
+            .contentType(ContentType.JSON)
+            .header("Authorization", accessToken.serialize())
+            .body("[[\"getMailboxes\", {\"ids\": null}, \"#0\"]]")
+        .when()
+            .post("/jmap")
+        .then()
+            .statusCode(200)
+            .body("[0][0]", equalTo("mailboxes"))
+            .body("[0][1].list", hasSize(2))
+            .body("[0][1].list[0].id", equalTo(mailboxId))
+            .body("[0][1].list[1].id", equalTo(mailboxId2));
     }
     
     @Test

http://git-wip-us.apache.org/repos/asf/james-project/blob/409344c1/server/protocols/jmap/src/main/java/org/apache/james/jmap/methods/GetMailboxesMethod.java
----------------------------------------------------------------------
diff --git a/server/protocols/jmap/src/main/java/org/apache/james/jmap/methods/GetMailboxesMethod.java
b/server/protocols/jmap/src/main/java/org/apache/james/jmap/methods/GetMailboxesMethod.java
index 7051006..2285ff6 100644
--- a/server/protocols/jmap/src/main/java/org/apache/james/jmap/methods/GetMailboxesMethod.java
+++ b/server/protocols/jmap/src/main/java/org/apache/james/jmap/methods/GetMailboxesMethod.java
@@ -22,12 +22,11 @@ package org.apache.james.jmap.methods;
 import java.util.List;
 import java.util.Optional;
 import java.util.Set;
+import java.util.function.Predicate;
 import java.util.stream.Stream;
 
 import javax.inject.Inject;
 
-import com.google.common.collect.ImmutableSet;
-import com.google.common.collect.Sets;
 import org.apache.james.jmap.model.ClientId;
 import org.apache.james.jmap.model.GetMailboxesRequest;
 import org.apache.james.jmap.model.GetMailboxesResponse;
@@ -51,6 +50,9 @@ import org.slf4j.LoggerFactory;
 import com.google.common.annotations.VisibleForTesting;
 import com.google.common.base.Preconditions;
 import com.google.common.base.Throwables;
+import com.google.common.collect.ImmutableList;
+import com.google.common.collect.ImmutableSet;
+import com.google.common.collect.Sets;
 
 public class GetMailboxesMethod<Id extends MailboxId> implements Method {
 
@@ -83,7 +85,7 @@ public class GetMailboxesMethod<Id extends MailboxId> implements Method
{
         GetMailboxesRequest mailboxesRequest = (GetMailboxesRequest) request;
         return Stream.of(
                 JmapResponse.builder().clientId(clientId)
-                .response(getMailboxesResponse(mailboxSession))
+                .response(getMailboxesResponse(mailboxesRequest, mailboxSession))
                 .properties(mailboxesRequest.getProperties().map(this::ensureContainsId))
                 .responseName(RESPONSE_NAME)
                 .build());
@@ -93,7 +95,7 @@ public class GetMailboxesMethod<Id extends MailboxId> implements Method
{
         return Sets.union(input, ImmutableSet.of(MailboxProperty.ID)).immutableCopy();
     }
 
-    private GetMailboxesResponse getMailboxesResponse(MailboxSession mailboxSession) {
+    private GetMailboxesResponse getMailboxesResponse(GetMailboxesRequest mailboxesRequest,
MailboxSession mailboxSession) {
         GetMailboxesResponse.Builder builder = GetMailboxesResponse.builder();
         try {
             retrieveUserMailboxes(mailboxSession)
@@ -102,6 +104,7 @@ public class GetMailboxesMethod<Id extends MailboxId> implements
Method {
                 .map(mailboxPath -> mailboxFromMailboxPath(mailboxPath, mailboxSession))
                 .filter(Optional::isPresent)
                 .map(Optional::get)
+                .filter(filterMailboxesById(mailboxesRequest.getIds()))
                 .sorted((m1, m2) -> Integer.compare(m1.getSortOrder(), m2.getSortOrder()))
                 .forEach(mailbox -> builder.add(mailbox));
             return builder.build();
@@ -109,7 +112,11 @@ public class GetMailboxesMethod<Id extends MailboxId> implements
Method {
             throw Throwables.propagate(e);
         }
     }
-    
+
+    private Predicate<? super Mailbox> filterMailboxesById(Optional<ImmutableList<String>>
ids) {
+        return (mailbox -> ids.map(list -> list.contains(mailbox.getId())).orElse(true));
+    }
+
     private List<MailboxMetaData> retrieveUserMailboxes(MailboxSession session) throws
MailboxException {
         return mailboxManager.search(
                 MailboxQuery.builder(session).privateUserMailboxes().build(),

http://git-wip-us.apache.org/repos/asf/james-project/blob/409344c1/server/protocols/jmap/src/main/java/org/apache/james/jmap/model/GetMailboxesRequest.java
----------------------------------------------------------------------
diff --git a/server/protocols/jmap/src/main/java/org/apache/james/jmap/model/GetMailboxesRequest.java
b/server/protocols/jmap/src/main/java/org/apache/james/jmap/model/GetMailboxesRequest.java
index 9d3f24a..722d86b 100644
--- a/server/protocols/jmap/src/main/java/org/apache/james/jmap/model/GetMailboxesRequest.java
+++ b/server/protocols/jmap/src/main/java/org/apache/james/jmap/model/GetMailboxesRequest.java
@@ -18,16 +18,17 @@
  ****************************************************************/
 package org.apache.james.jmap.model;
 
-import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
-import com.fasterxml.jackson.databind.annotation.JsonPOJOBuilder;
-import com.google.common.collect.ImmutableList;
-import com.google.common.collect.ImmutableSet;
+import java.util.List;
+import java.util.Optional;
+
 import org.apache.commons.lang.NotImplementedException;
 import org.apache.james.jmap.methods.JmapRequest;
 import org.apache.james.util.streams.Collectors;
 
-import java.util.List;
-import java.util.Optional;
+import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
+import com.fasterxml.jackson.databind.annotation.JsonPOJOBuilder;
+import com.google.common.collect.ImmutableList;
+import com.google.common.collect.ImmutableSet;
 
 @JsonDeserialize(builder = GetMailboxesRequest.Builder.class)
 public class GetMailboxesRequest implements JmapRequest {
@@ -41,10 +42,10 @@ public class GetMailboxesRequest implements JmapRequest {
 
         private String accountId;
         private Optional<ImmutableSet<MailboxProperty>> properties;
-        private final ImmutableList.Builder<String> ids;
+        private Optional<ImmutableList<String>> ids;
 
         private Builder() {
-            ids = ImmutableList.builder();
+            ids = Optional.empty();
             properties = Optional.empty();
         }
 
@@ -57,7 +58,7 @@ public class GetMailboxesRequest implements JmapRequest {
 
         public Builder ids(List<String> ids) {
             if (ids != null) {
-                throw new NotImplementedException();
+                this.ids = Optional.of(ImmutableList.copyOf(ids));
             }
             return this;
         }
@@ -73,15 +74,15 @@ public class GetMailboxesRequest implements JmapRequest {
         }
         
         public GetMailboxesRequest build() {
-            return new GetMailboxesRequest(Optional.ofNullable(accountId), ids.build(), properties);
+            return new GetMailboxesRequest(Optional.ofNullable(accountId), ids, properties);
         }
     }
 
     private final Optional<String> accountId;
-    private final List<String> ids;
+    private final Optional<ImmutableList<String>> ids;
     private final Optional<ImmutableSet<MailboxProperty>> properties;
 
-    private GetMailboxesRequest(Optional<String> accountId, List<String> ids,
Optional<ImmutableSet<MailboxProperty>> properties) {
+    private GetMailboxesRequest(Optional<String> accountId, Optional<ImmutableList<String>>
ids, Optional<ImmutableSet<MailboxProperty>> properties) {
         this.accountId = accountId;
         this.ids = ids;
         this.properties = properties;
@@ -91,7 +92,7 @@ public class GetMailboxesRequest implements JmapRequest {
         return accountId;
     }
 
-    public List<String> getIds() {
+    public Optional<ImmutableList<String>> getIds() {
         return ids;
     }
 

http://git-wip-us.apache.org/repos/asf/james-project/blob/409344c1/server/protocols/jmap/src/test/java/org/apache/james/jmap/model/GetMailboxesRequestTest.java
----------------------------------------------------------------------
diff --git a/server/protocols/jmap/src/test/java/org/apache/james/jmap/model/GetMailboxesRequestTest.java
b/server/protocols/jmap/src/test/java/org/apache/james/jmap/model/GetMailboxesRequestTest.java
index 6a5dc8a..ca78fd8 100644
--- a/server/protocols/jmap/src/test/java/org/apache/james/jmap/model/GetMailboxesRequestTest.java
+++ b/server/protocols/jmap/src/test/java/org/apache/james/jmap/model/GetMailboxesRequestTest.java
@@ -33,12 +33,30 @@ public class GetMailboxesRequestTest {
         GetMailboxesRequest.builder().accountId("1");
     }
 
-    @Test(expected=NotImplementedException.class)
-    public void builderShouldThrowWhenIds() {
+    @Test
+    public void builderShouldNotThrowWhenIds() {
         GetMailboxesRequest.builder().ids(ImmutableList.of());
     }
 
     @Test
+    public void idsShouldBeEmptyListWhenEmptyList() {
+        GetMailboxesRequest getMailboxesRequest = GetMailboxesRequest.builder()
+            .ids(ImmutableList.of())
+            .build();
+        assertThat(getMailboxesRequest.getIds()).isPresent();
+        assertThat(getMailboxesRequest.getIds().get()).hasSize(0);
+    }
+
+    @Test
+    public void idsShouldBePresentWhenListIsNotEmpty() {
+        GetMailboxesRequest getMailboxesRequest = GetMailboxesRequest.builder()
+            .ids(ImmutableList.of("123"))
+            .build();
+        assertThat(getMailboxesRequest.getIds()).isPresent();
+        assertThat(getMailboxesRequest.getIds().get()).containsExactly("123");
+    }
+
+    @Test
     public void propertiesShouldBeEmptyWhenNotGiven() {
         GetMailboxesRequest getMailboxesRequest = GetMailboxesRequest.builder().build();
         assertThat(getMailboxesRequest.getProperties()).isEmpty();


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


Mime
View raw message