Author: matthieu
Date: Thu Jan 21 15:52:09 2016
New Revision: 1725995

URL: http://svn.apache.org/viewvc?rev=1725995&view=rev
Log:
JAMES-1662 implement properties filtering on JMAP getMailboxes

Added:
    
james/project/trunk/server/protocols/jmap/src/main/java/org/apache/james/jmap/model/MailboxProperty.java
      - copied, changed from r1725994, 
james/project/trunk/server/protocols/jmap/src/test/java/org/apache/james/jmap/model/GetMailboxesRequestTest.java
    
james/project/trunk/server/protocols/jmap/src/test/java/org/apache/james/jmap/model/MailboxPropertyTest.java
      - copied, changed from r1725994, 
james/project/trunk/server/protocols/jmap/src/test/java/org/apache/james/jmap/model/GetMailboxesRequestTest.java
Modified:
    
james/project/trunk/server/protocols/jmap-integration-testing/src/test/java/org/apache/james/jmap/methods/GetMailboxesMethodTest.java
    
james/project/trunk/server/protocols/jmap/src/main/java/org/apache/james/jmap/methods/GetMailboxesMethod.java
    
james/project/trunk/server/protocols/jmap/src/main/java/org/apache/james/jmap/methods/JmapResponse.java
    
james/project/trunk/server/protocols/jmap/src/main/java/org/apache/james/jmap/model/GetMailboxesRequest.java
    
james/project/trunk/server/protocols/jmap/src/main/java/org/apache/james/jmap/model/mailbox/Mailbox.java
    
james/project/trunk/server/protocols/jmap/src/test/java/org/apache/james/jmap/model/GetMailboxesRequestTest.java

Modified: 
james/project/trunk/server/protocols/jmap-integration-testing/src/test/java/org/apache/james/jmap/methods/GetMailboxesMethodTest.java
URL: 
http://svn.apache.org/viewvc/james/project/trunk/server/protocols/jmap-integration-testing/src/test/java/org/apache/james/jmap/methods/GetMailboxesMethodTest.java?rev=1725995&r1=1725994&r2=1725995&view=diff
==============================================================================
--- 
james/project/trunk/server/protocols/jmap-integration-testing/src/test/java/org/apache/james/jmap/methods/GetMailboxesMethodTest.java
 (original)
+++ 
james/project/trunk/server/protocols/jmap-integration-testing/src/test/java/org/apache/james/jmap/methods/GetMailboxesMethodTest.java
 Thu Jan 21 15:52:09 2016
@@ -121,20 +121,6 @@ public abstract class GetMailboxesMethod
     }
     
     @Test
-    public void 
getMailboxesShouldErrorNotSupportedWhenRequestContainsNonNullProperties() 
throws Exception {
-        given()
-            .accept(ContentType.JSON)
-            .contentType(ContentType.JSON)
-            .header("Authorization", accessToken.serialize())
-            .body("[[\"getMailboxes\", {\"properties\": []}, \"#0\"]]")
-        .when()
-            .post("/jmap")
-        .then()
-            .statusCode(200)
-            .content(equalTo("[[\"error\",{\"type\":\"Not yet 
implemented\"},\"#0\"]]"));
-    }
-
-    @Test
     public void getMailboxesShouldErrorInvalidArgumentsWhenRequestIsInvalid() 
throws Exception {
         given()
             .accept(ContentType.JSON)
@@ -252,6 +238,88 @@ public abstract class GetMailboxesMethod
         assertThat(jsonPath.parse(response).<Integer>read(firstMailboxPath + 
".unreadThreads")).isEqualTo(0);
     }
 
+    @Test
+    public void 
getMailboxesShouldReturnFilteredMailboxesPropertiesWhenRequestContainsFilterProperties()
 throws Exception {
+        String user = "user";
+        
jmapServer.serverProbe().createMailbox(MailboxConstants.USER_NAMESPACE, user, 
"name");
+
+        String response = given()
+            .accept(ContentType.JSON)
+            .contentType(ContentType.JSON)
+            .header("Authorization", accessToken.serialize())
+            .body("[[\"getMailboxes\", {\"properties\" : [\"unreadMessages\", 
\"sortOrder\"]}, \"#0\"]]")
+            .when()
+            .post("/jmap")
+            .then()
+            .statusCode(200)
+            .content(startsWith("[[\"mailboxes\","))
+            .extract()
+            .asString();
+
+        String firstMailboxPath = "$.[0].[1].list.[0]";
+        assertThat(jsonPath.parse(response).<String>read(firstMailboxPath + 
".id")).isNotEmpty();
+        assertThat(jsonPath.parse(response).<String>read(firstMailboxPath + 
".name")).isNull();
+        assertThat(jsonPath.parse(response).<String>read(firstMailboxPath + 
".parentId")).isNull();
+        assertThat(jsonPath.parse(response).<String>read(firstMailboxPath + 
".role")).isNull();
+        assertThat(jsonPath.parse(response).<Integer>read(firstMailboxPath + 
".sortOrder")).isEqualTo(1000);
+        assertThat(jsonPath.parse(response).<Boolean>read(firstMailboxPath + 
".mustBeOnlyMailbox")).isNull();
+        assertThat(jsonPath.parse(response).<Boolean>read(firstMailboxPath + 
".mayReadItems")).isNull();
+        assertThat(jsonPath.parse(response).<Boolean>read(firstMailboxPath + 
".mayAddItems")).isNull();
+        assertThat(jsonPath.parse(response).<Boolean>read(firstMailboxPath + 
".mayRemoveItems")).isNull();
+        assertThat(jsonPath.parse(response).<Boolean>read(firstMailboxPath + 
".mayCreateChild")).isNull();
+        assertThat(jsonPath.parse(response).<Boolean>read(firstMailboxPath + 
".mayRename")).isNull();
+        assertThat(jsonPath.parse(response).<Boolean>read(firstMailboxPath + 
".mayDelete")).isNull();
+        assertThat(jsonPath.parse(response).<Integer>read(firstMailboxPath + 
".totalMessages")).isNull();
+        assertThat(jsonPath.parse(response).<Integer>read(firstMailboxPath + 
".unreadMessages")).isEqualTo(0);
+        assertThat(jsonPath.parse(response).<Integer>read(firstMailboxPath + 
".unreadThreads")).isNull();
+    }
+
+    @Test
+    public void 
getMailboxesShouldReturnIdWhenRequestContainsEmptyPropertyListFilter() throws 
Exception {
+        String user = "user";
+        
jmapServer.serverProbe().createMailbox(MailboxConstants.USER_NAMESPACE, user, 
"name");
+
+        String response = given()
+            .accept(ContentType.JSON)
+            .contentType(ContentType.JSON)
+            .header("Authorization", accessToken.serialize())
+            .body("[[\"getMailboxes\", {\"properties\" : []}, \"#0\"]]")
+            .when()
+            .post("/jmap")
+            .then()
+            .statusCode(200)
+            .content(startsWith("[[\"mailboxes\","))
+            .extract()
+            .asString();
+
+        String firstMailboxPath = "$.[0].[1].list.[0]";
+        assertThat(jsonPath.parse(response).<String>read(firstMailboxPath + 
".id")).isNotEmpty();
+        assertThat(jsonPath.parse(response).<String>read(firstMailboxPath + 
".name")).isNull();
+    }
+
+    @Test
+    public void 
getMailboxesShouldIgnoreUnknownPropertiesWhenRequestContainsUnknownPropertyListFilter()
 throws Exception {
+        String user = "user";
+        
jmapServer.serverProbe().createMailbox(MailboxConstants.USER_NAMESPACE, user, 
"name");
+
+        String response = given()
+            .accept(ContentType.JSON)
+            .contentType(ContentType.JSON)
+            .header("Authorization", accessToken.serialize())
+            .body("[[\"getMailboxes\", {\"properties\" : [\"unknown\"]}, 
\"#0\"]]")
+            .when()
+            .post("/jmap")
+            .then()
+            .statusCode(200)
+            .content(startsWith("[[\"mailboxes\","))
+            .extract()
+            .asString();
+
+        String firstMailboxPath = "$.[0].[1].list.[0]";
+        assertThat(jsonPath.parse(response).<String>read(firstMailboxPath + 
".id")).isNotEmpty();
+        assertThat(jsonPath.parse(response).<String>read(firstMailboxPath + 
".name")).isNull();
+    }
+
     @SuppressWarnings("unchecked")
     @Test
     public void getMailboxesShouldReturnMailboxesWithSortOrder() throws 
Exception {

Modified: 
james/project/trunk/server/protocols/jmap/src/main/java/org/apache/james/jmap/methods/GetMailboxesMethod.java
URL: 
http://svn.apache.org/viewvc/james/project/trunk/server/protocols/jmap/src/main/java/org/apache/james/jmap/methods/GetMailboxesMethod.java?rev=1725995&r1=1725994&r2=1725995&view=diff
==============================================================================
--- 
james/project/trunk/server/protocols/jmap/src/main/java/org/apache/james/jmap/methods/GetMailboxesMethod.java
 (original)
+++ 
james/project/trunk/server/protocols/jmap/src/main/java/org/apache/james/jmap/methods/GetMailboxesMethod.java
 Thu Jan 21 15:52:09 2016
@@ -20,13 +20,17 @@
 package org.apache.james.jmap.methods;
 
 import java.util.Optional;
+import java.util.Set;
 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;
+import org.apache.james.jmap.model.MailboxProperty;
 import org.apache.james.jmap.model.mailbox.Mailbox;
 import org.apache.james.jmap.model.mailbox.Role;
 import org.apache.james.jmap.model.mailbox.SortOrder;
@@ -73,13 +77,19 @@ public class GetMailboxesMethod<Id exten
 
     public Stream<JmapResponse> process(JmapRequest request, ClientId 
clientId, MailboxSession mailboxSession) {
         Preconditions.checkArgument(request instanceof GetMailboxesRequest);
+        GetMailboxesRequest mailboxesRequest = (GetMailboxesRequest) request;
         return Stream.of(
                 JmapResponse.builder().clientId(clientId)
                 .response(getMailboxesResponse(mailboxSession))
+                
.properties(mailboxesRequest.getProperties().map(this::ensureContainsId))
                 .responseName(RESPONSE_NAME)
                 .build());
     }
 
+    private Set<MailboxProperty> ensureContainsId(Set<MailboxProperty> input) {
+        return Sets.union(input, 
ImmutableSet.of(MailboxProperty.ID)).immutableCopy();
+    }
+
     private GetMailboxesResponse getMailboxesResponse(MailboxSession 
mailboxSession) {
         GetMailboxesResponse.Builder builder = GetMailboxesResponse.builder();
         try {

Modified: 
james/project/trunk/server/protocols/jmap/src/main/java/org/apache/james/jmap/methods/JmapResponse.java
URL: 
http://svn.apache.org/viewvc/james/project/trunk/server/protocols/jmap/src/main/java/org/apache/james/jmap/methods/JmapResponse.java?rev=1725995&r1=1725994&r2=1725995&view=diff
==============================================================================
--- 
james/project/trunk/server/protocols/jmap/src/main/java/org/apache/james/jmap/methods/JmapResponse.java
 (original)
+++ 
james/project/trunk/server/protocols/jmap/src/main/java/org/apache/james/jmap/methods/JmapResponse.java
 Thu Jan 21 15:52:09 2016
@@ -27,6 +27,7 @@ import org.apache.james.jmap.model.Prope
 
 import com.fasterxml.jackson.databind.ser.impl.SimpleFilterProvider;
 import com.google.common.annotations.VisibleForTesting;
+import com.google.common.collect.ImmutableSet;
 
 public class JmapResponse {
 
@@ -61,13 +62,12 @@ public class JmapResponse {
         }
 
         public Builder properties(Optional<? extends Set<? extends Property>> 
properties) {
-            this.properties = properties;
+            this.properties = properties.map(ImmutableSet::copyOf);
             return this;
         }
 
         public Builder properties(Set<? extends Property> properties) {
-            this.properties = Optional.ofNullable(properties);
-            return this;
+            return properties(Optional.ofNullable(properties));
         }
         
         public Builder filterProvider(Optional<SimpleFilterProvider> 
filterProvider) {

Modified: 
james/project/trunk/server/protocols/jmap/src/main/java/org/apache/james/jmap/model/GetMailboxesRequest.java
URL: 
http://svn.apache.org/viewvc/james/project/trunk/server/protocols/jmap/src/main/java/org/apache/james/jmap/model/GetMailboxesRequest.java?rev=1725995&r1=1725994&r2=1725995&view=diff
==============================================================================
--- 
james/project/trunk/server/protocols/jmap/src/main/java/org/apache/james/jmap/model/GetMailboxesRequest.java
 (original)
+++ 
james/project/trunk/server/protocols/jmap/src/main/java/org/apache/james/jmap/model/GetMailboxesRequest.java
 Thu Jan 21 15:52:09 2016
@@ -18,15 +18,16 @@
  ****************************************************************/
 package org.apache.james.jmap.model;
 
-import java.util.List;
-import java.util.Optional;
-
-import org.apache.commons.lang.NotImplementedException;
-import org.apache.james.jmap.methods.JmapRequest;
-
 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 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;
 
 @JsonDeserialize(builder = GetMailboxesRequest.Builder.class)
 public class GetMailboxesRequest implements JmapRequest {
@@ -40,11 +41,11 @@ public class GetMailboxesRequest impleme
 
         private String accountId;
         private ImmutableList.Builder<String> ids;
-        private ImmutableList.Builder<String> properties;
+        private Optional<ImmutableSet<MailboxProperty>> properties;
 
         private Builder() {
             ids = ImmutableList.builder();
-            properties = ImmutableList.builder();
+            properties = Optional.empty();
         }
 
         public Builder accountId(String accountId) {
@@ -62,22 +63,25 @@ public class GetMailboxesRequest impleme
         }
 
         public Builder properties(List<String> properties) {
-            if (properties != null) {
-                throw new NotImplementedException();
-            }
+            this.properties = Optional.of(
+                properties.stream()
+                    .map(MailboxProperty::findProperty)
+                    .filter(Optional::isPresent)
+                    .map(Optional::get)
+                    .collect(Collectors.toImmutableSet()));
             return this;
         }
         
         public GetMailboxesRequest build() {
-            return new GetMailboxesRequest(Optional.ofNullable(accountId), 
ids.build(), properties.build());
+            return new GetMailboxesRequest(Optional.ofNullable(accountId), 
ids.build(), properties);
         }
     }
 
     private final Optional<String> accountId;
     private final List<String> ids;
-    private final List<String> properties;
+    private final Optional<ImmutableSet<MailboxProperty>> properties;
 
-    private GetMailboxesRequest(Optional<String> accountId, List<String> ids, 
List<String> properties) {
+    private GetMailboxesRequest(Optional<String> accountId, List<String> ids, 
Optional<ImmutableSet<MailboxProperty>> properties) {
         this.accountId = accountId;
         this.ids = ids;
         this.properties = properties;
@@ -91,7 +95,7 @@ public class GetMailboxesRequest impleme
         return ids;
     }
 
-    public List<String> getProperties() {
+    public Optional<ImmutableSet<MailboxProperty>> getProperties() {
         return properties;
     }
 }

Copied: 
james/project/trunk/server/protocols/jmap/src/main/java/org/apache/james/jmap/model/MailboxProperty.java
 (from r1725994, 
james/project/trunk/server/protocols/jmap/src/test/java/org/apache/james/jmap/model/GetMailboxesRequestTest.java)
URL: 
http://svn.apache.org/viewvc/james/project/trunk/server/protocols/jmap/src/main/java/org/apache/james/jmap/model/MailboxProperty.java?p2=james/project/trunk/server/protocols/jmap/src/main/java/org/apache/james/jmap/model/MailboxProperty.java&p1=james/project/trunk/server/protocols/jmap/src/test/java/org/apache/james/jmap/model/GetMailboxesRequestTest.java&r1=1725994&r2=1725995&rev=1725995&view=diff
==============================================================================
--- 
james/project/trunk/server/protocols/jmap/src/test/java/org/apache/james/jmap/model/GetMailboxesRequestTest.java
 (original)
+++ 
james/project/trunk/server/protocols/jmap/src/main/java/org/apache/james/jmap/model/MailboxProperty.java
 Thu Jan 21 15:52:09 2016
@@ -16,28 +16,45 @@
  * specific language governing permissions and limitations      *
  * under the License.                                           *
  ****************************************************************/
-
 package org.apache.james.jmap.model;
 
-import org.apache.commons.lang.NotImplementedException;
-import org.junit.Test;
+import com.google.common.base.Preconditions;
+
+import java.util.Arrays;
+import java.util.Optional;
 
-import com.google.common.collect.ImmutableList;
+public enum MailboxProperty implements Property {
+    ID("id"),
+    NAME("name"),
+    PARENT_ID("parentId"),
+    ROLE("role"),
+    SORT_ORDER("sortOrder"),
+    MUST_BE_ONLY_MAILBOX("mustBeOnlyMailbox"),
+    MAY_READ_ITEMS("mayReadItems"),
+    MAY_ADD_ITEMS("mayAddItems"),
+    MAY_REMOVE_ITEMS("mayRemoveItems"),
+    MAY_CREATE_CHILD("mayCreateChild"),
+    MAY_RENAME("mayRename"),
+    MAY_DELETE("mayDelete"),
+    TOTAL_MESSAGES("totalMessages"),
+    UNREAD_MESSAGES("unreadMessages"),
+    TOTAL_THREADS("totalThreads"),
+    UNREAD_THREADS("unreadThreads");
 
-public class GetMailboxesRequestTest {
+    private final String fieldName;
 
-    @Test(expected=NotImplementedException.class)
-    public void builderShouldThrowWhenAccountId() {
-        GetMailboxesRequest.builder().accountId("1");
+    MailboxProperty(String fieldName) {
+        this.fieldName = fieldName;
     }
 
-    @Test(expected=NotImplementedException.class)
-    public void builderShouldThrowWhenIds() {
-        GetMailboxesRequest.builder().ids(ImmutableList.of());
+    public String asFieldName() {
+        return fieldName;
     }
 
-    @Test(expected=NotImplementedException.class)
-    public void builderShouldThrowWhenProperties() {
-        GetMailboxesRequest.builder().properties(ImmutableList.of());
+    public static Optional<MailboxProperty> findProperty(String value) {
+        Preconditions.checkNotNull(value);
+        return Arrays.stream(values())
+            .filter(element -> element.fieldName.equals(value))
+            .findAny();
     }
 }

Modified: 
james/project/trunk/server/protocols/jmap/src/main/java/org/apache/james/jmap/model/mailbox/Mailbox.java
URL: 
http://svn.apache.org/viewvc/james/project/trunk/server/protocols/jmap/src/main/java/org/apache/james/jmap/model/mailbox/Mailbox.java?rev=1725995&r1=1725994&r2=1725995&view=diff
==============================================================================
--- 
james/project/trunk/server/protocols/jmap/src/main/java/org/apache/james/jmap/model/mailbox/Mailbox.java
 (original)
+++ 
james/project/trunk/server/protocols/jmap/src/main/java/org/apache/james/jmap/model/mailbox/Mailbox.java
 Thu Jan 21 15:52:09 2016
@@ -22,13 +22,16 @@ package org.apache.james.jmap.model.mail
 import java.util.Objects;
 import java.util.Optional;
 
+import com.fasterxml.jackson.annotation.JsonFilter;
 import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
 import com.fasterxml.jackson.databind.annotation.JsonPOJOBuilder;
 import com.google.common.annotations.VisibleForTesting;
 import com.google.common.base.Preconditions;
 import com.google.common.base.Strings;
+import org.apache.james.jmap.methods.JmapResponseWriterImpl;
 
 @JsonDeserialize(builder = Mailbox.Builder.class)
+@JsonFilter(JmapResponseWriterImpl.PROPERTIES_FILTER)
 public class Mailbox {
 
     public static Builder builder() {

Modified: 
james/project/trunk/server/protocols/jmap/src/test/java/org/apache/james/jmap/model/GetMailboxesRequestTest.java
URL: 
http://svn.apache.org/viewvc/james/project/trunk/server/protocols/jmap/src/test/java/org/apache/james/jmap/model/GetMailboxesRequestTest.java?rev=1725995&r1=1725994&r2=1725995&view=diff
==============================================================================
--- 
james/project/trunk/server/protocols/jmap/src/test/java/org/apache/james/jmap/model/GetMailboxesRequestTest.java
 (original)
+++ 
james/project/trunk/server/protocols/jmap/src/test/java/org/apache/james/jmap/model/GetMailboxesRequestTest.java
 Thu Jan 21 15:52:09 2016
@@ -36,8 +36,4 @@ public class GetMailboxesRequestTest {
         GetMailboxesRequest.builder().ids(ImmutableList.of());
     }
 
-    @Test(expected=NotImplementedException.class)
-    public void builderShouldThrowWhenProperties() {
-        GetMailboxesRequest.builder().properties(ImmutableList.of());
-    }
 }

Copied: 
james/project/trunk/server/protocols/jmap/src/test/java/org/apache/james/jmap/model/MailboxPropertyTest.java
 (from r1725994, 
james/project/trunk/server/protocols/jmap/src/test/java/org/apache/james/jmap/model/GetMailboxesRequestTest.java)
URL: 
http://svn.apache.org/viewvc/james/project/trunk/server/protocols/jmap/src/test/java/org/apache/james/jmap/model/MailboxPropertyTest.java?p2=james/project/trunk/server/protocols/jmap/src/test/java/org/apache/james/jmap/model/MailboxPropertyTest.java&p1=james/project/trunk/server/protocols/jmap/src/test/java/org/apache/james/jmap/model/GetMailboxesRequestTest.java&r1=1725994&r2=1725995&rev=1725995&view=diff
==============================================================================
--- 
james/project/trunk/server/protocols/jmap/src/test/java/org/apache/james/jmap/model/GetMailboxesRequestTest.java
 (original)
+++ 
james/project/trunk/server/protocols/jmap/src/test/java/org/apache/james/jmap/model/MailboxPropertyTest.java
 Thu Jan 21 15:52:09 2016
@@ -16,28 +16,31 @@
  * specific language governing permissions and limitations      *
  * under the License.                                           *
  ****************************************************************/
-
 package org.apache.james.jmap.model;
 
-import org.apache.commons.lang.NotImplementedException;
 import org.junit.Test;
 
-import com.google.common.collect.ImmutableList;
+import java.util.Optional;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
 
-public class GetMailboxesRequestTest {
+public class MailboxPropertyTest {
 
-    @Test(expected=NotImplementedException.class)
-    public void builderShouldThrowWhenAccountId() {
-        GetMailboxesRequest.builder().accountId("1");
+    @Test
+    public void findPropertyShouldReturnEmptyWhenNoEnumEntryMatchGivenString() 
{
+        assertThat(MailboxProperty.findProperty("should not match any 
entry")).isEmpty();
     }
 
-    @Test(expected=NotImplementedException.class)
-    public void builderShouldThrowWhenIds() {
-        GetMailboxesRequest.builder().ids(ImmutableList.of());
+    @Test
+    public void findPropertyShouldThrowWhenNullString() {
+        assertThatThrownBy(() -> MailboxProperty.findProperty(null))
+            .isInstanceOf(NullPointerException.class);
     }
 
-    @Test(expected=NotImplementedException.class)
-    public void builderShouldThrowWhenProperties() {
-        GetMailboxesRequest.builder().properties(ImmutableList.of());
+    @Test
+    public void findPropertyShouldReturnMatchingEnumEntryWhenExistingValue() {
+        
assertThat(MailboxProperty.findProperty("name")).isEqualTo(Optional.of(MailboxProperty.NAME));
     }
-}
+
+}
\ No newline at end of file



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

Reply via email to