Author: matthieu
Date: Mon Jan 25 15:22:46 2016
New Revision: 1726644

URL: http://svn.apache.org/viewvc?rev=1726644&view=rev
Log:
JAMES-1661 Define a builder for MailboxQuery to ease request construction

Modified:
    james/project/trunk/mailbox/api/pom.xml
    
james/project/trunk/mailbox/api/src/main/java/org/apache/james/mailbox/model/MailboxQuery.java
    
james/project/trunk/mailbox/api/src/test/java/org/apache/james/mailbox/model/MailboxQueryTest.java
    
james/project/trunk/server/protocols/jmap/src/main/java/org/apache/james/jmap/methods/GetMailboxesMethod.java

Modified: james/project/trunk/mailbox/api/pom.xml
URL: 
http://svn.apache.org/viewvc/james/project/trunk/mailbox/api/pom.xml?rev=1726644&r1=1726643&r2=1726644&view=diff
==============================================================================
--- james/project/trunk/mailbox/api/pom.xml (original)
+++ james/project/trunk/mailbox/api/pom.xml Mon Jan 25 15:22:46 2016
@@ -33,6 +33,10 @@
 
     <dependencies>
         <dependency>
+            <groupId>com.google.guava</groupId>
+            <artifactId>guava</artifactId>
+        </dependency>
+        <dependency>
             <groupId>${javax.mail.groupId}</groupId>
             <artifactId>${javax.mail.artifactId}</artifactId>
         </dependency>
@@ -66,5 +70,10 @@
             <version>${assertj-1.version}</version>
             <scope>test</scope>
         </dependency>
+        <dependency>
+            <groupId>org.mockito</groupId>
+            <artifactId>mockito-core</artifactId>
+            <scope>test</scope>
+        </dependency>
     </dependencies>
 </project>

Modified: 
james/project/trunk/mailbox/api/src/main/java/org/apache/james/mailbox/model/MailboxQuery.java
URL: 
http://svn.apache.org/viewvc/james/project/trunk/mailbox/api/src/main/java/org/apache/james/mailbox/model/MailboxQuery.java?rev=1726644&r1=1726643&r2=1726644&view=diff
==============================================================================
--- 
james/project/trunk/mailbox/api/src/main/java/org/apache/james/mailbox/model/MailboxQuery.java
 (original)
+++ 
james/project/trunk/mailbox/api/src/main/java/org/apache/james/mailbox/model/MailboxQuery.java
 Mon Jan 25 15:22:46 2016
@@ -22,6 +22,12 @@ package org.apache.james.mailbox.model;
 import java.util.StringTokenizer;
 import java.util.regex.Pattern;
 
+import org.apache.james.mailbox.MailboxSession;
+
+import com.google.common.annotations.VisibleForTesting;
+import com.google.common.base.Optional;
+import com.google.common.base.Preconditions;
+
 
 /**
  * Expresses select criteria for mailboxes.
@@ -46,6 +52,79 @@ public final class MailboxQuery {
      * Use this wildcard to match every char except the hierarchy delimiter
      */
     public final static char LOCALWILDCARD = '%';
+    
+    public static Builder builder() {
+        return new Builder();
+    }
+
+    public static Builder builder(MailboxSession session) {
+        return 
builder().pathDelimiter(session.getPathDelimiter()).username(session.getUser().getUserName());
+    }
+
+    public static class Builder {
+        private static final String EMPTY_PATH_NAME = "";
+        private MailboxPath base;
+        private String expression;
+        @VisibleForTesting char pathDelimiter;
+        @VisibleForTesting String username;
+        @VisibleForTesting Optional<String> pathName;
+        @VisibleForTesting Optional<String> namespace;
+        
+        private Builder() {
+            this.pathName = Optional.absent();
+            this.namespace = Optional.absent();
+        }
+        
+        public Builder base(MailboxPath base) {
+            this.base = base;
+            return this;
+        }
+        
+        public Builder username(String username) {
+            this.username = username;
+            return this;
+        }
+        
+        public Builder privateUserMailboxes() {
+            Preconditions.checkState(!pathName.isPresent());
+            Preconditions.checkState(!namespace.isPresent());
+            Preconditions.checkState(base == null);
+            this.namespace = Optional.of(MailboxConstants.USER_NAMESPACE);
+            this.pathName = Optional.of(EMPTY_PATH_NAME);
+            return matchesAll();
+        }
+        
+        public Builder expression(String expression) {
+            this.expression = expression;
+            return this;
+        }
+        
+        public Builder matchesAll() {
+            this.expression = String.valueOf(FREEWILDCARD);
+            return this;
+        }
+        
+        public Builder pathDelimiter(char pathDelimiter) {
+            this.pathDelimiter = pathDelimiter;
+            return this;
+        }
+        
+        public MailboxQuery build() {
+            Preconditions.checkState(base != null || username != null);
+            if (base != null && username != null) {
+                throw new IllegalStateException("'base' and 'username' are 
exclusives");
+            }
+            return new MailboxQuery(buildBase(), expression, pathDelimiter);
+        }
+
+        private MailboxPath buildBase() {
+            if (base != null) {
+                return base;
+            } else {
+                return new 
MailboxPath(namespace.or(MailboxConstants.USER_NAMESPACE), username, 
pathName.or(EMPTY_PATH_NAME));
+            }
+        }
+    }
 
     /**
      * Constructs an expression determining a set of mailbox names.

Modified: 
james/project/trunk/mailbox/api/src/test/java/org/apache/james/mailbox/model/MailboxQueryTest.java
URL: 
http://svn.apache.org/viewvc/james/project/trunk/mailbox/api/src/test/java/org/apache/james/mailbox/model/MailboxQueryTest.java?rev=1726644&r1=1726643&r2=1726644&view=diff
==============================================================================
--- 
james/project/trunk/mailbox/api/src/test/java/org/apache/james/mailbox/model/MailboxQueryTest.java
 (original)
+++ 
james/project/trunk/mailbox/api/src/test/java/org/apache/james/mailbox/model/MailboxQueryTest.java
 Mon Jan 25 15:22:46 2016
@@ -25,6 +25,13 @@ import org.junit.Test;
 
 import static org.assertj.core.api.Assertions.assertThat;
 
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+import org.apache.james.mailbox.MailboxSession;
+import org.apache.james.mailbox.MailboxSession.User;
+import org.apache.james.mailbox.model.MailboxQuery.Builder;
+
 public class MailboxQueryTest {
 
     MailboxPath path;
@@ -248,6 +255,11 @@ public class MailboxQueryTest {
         assertThat(new MailboxQuery(path, null, 
'.').isExpressionMatch("folder")).isFalse();
     }
 
+    @Test(expected=IllegalStateException.class)
+    public void buildShouldThrowWhenNoBaseDefined() {
+        MailboxQuery.builder().expression("abc").pathDelimiter('/').build();
+    }
+    
     @Test
     public void freeWildcardAreNotEscapedWithDotSeparator() {
         assertThat(new MailboxQuery(path, "folder\\*", 
'.').isExpressionMatch("folder\\123")).isTrue();
@@ -258,4 +270,68 @@ public class MailboxQueryTest {
         assertThat(new MailboxQuery(path, "folder\\%", 
'.').isExpressionMatch("folder\\123")).isTrue();
     }
 
+    @Test
+    public void buildShouldMatchAllValuesWhenAll() {
+        MailboxQuery query = MailboxQuery.builder()
+            .base(path)
+            .matchesAll()
+            .pathDelimiter('.')
+            .build();
+        assertThat(query.isExpressionMatch("folder")).isTrue();
+    }
+
+    @Test
+    public void buildShouldConstructMailboxPathWhenPrivateUserMailboxes() {
+        MailboxPath expected = new 
MailboxPath(MailboxConstants.USER_NAMESPACE, "user", "");
+        MailboxPath actual = MailboxQuery.builder()
+                .username("user")
+                .privateUserMailboxes()
+                .pathDelimiter('.')
+                .build().getBase();
+        assertThat(actual).isEqualTo(expected);
+    }
+
+    @Test
+    public void buildShouldMatchAllWhenPrivateUserMailboxes() {
+        MailboxQuery query = MailboxQuery.builder()
+                .username("user")
+                .privateUserMailboxes()
+                .pathDelimiter('.')
+                .build();
+        assertThat(query.isExpressionMatch("folder")).isTrue();
+    }
+    
+    @Test
+    public void builderShouldInitFromSessionWhenGiven() {
+        MailboxSession mailboxSession = mock(MailboxSession.class);
+        when(mailboxSession.getPathDelimiter()).thenReturn('#');
+        User user = mock(User.class);
+        when(user.getUserName()).thenReturn("little bobby table");
+        when(mailboxSession.getUser()).thenReturn(user);
+        Builder query = MailboxQuery.builder(mailboxSession);
+        assertThat(query.pathDelimiter).isEqualTo('#');
+        assertThat(query.username).isEqualTo("little bobby table");
+    }
+    
+    @Test(expected=IllegalStateException.class)
+    public void builderShouldThrowWhenConflictingBase() {
+        
MailboxQuery.builder().base(mock(MailboxPath.class)).username("user").build();
+    }
+    
+    @Test(expected=IllegalStateException.class)
+    public void builderShouldThrowWhenOverwritingBaseParams() {
+        
MailboxQuery.builder().base(mock(MailboxPath.class)).privateUserMailboxes().build();
+    }
+    
+    @Test(expected=IllegalStateException.class)
+    public void builderShouldThrowWhenMissingUsername() {
+        MailboxQuery.builder().privateUserMailboxes().build();
+    }
+    
+    @Test
+    public void builderShouldUseBaseWhenGiven() {
+        MailboxPath base = new MailboxPath("a", "b", "c");
+        MailboxQuery actual = MailboxQuery.builder().base(base).build();
+        assertThat(actual.getBase()).isSameAs(base);
+    }
 }

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=1726644&r1=1726643&r2=1726644&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
 Mon Jan 25 15:22:46 2016
@@ -40,7 +40,6 @@ import org.apache.james.mailbox.MailboxS
 import org.apache.james.mailbox.MessageManager;
 import org.apache.james.mailbox.MessageManager.MetaData.FetchGroup;
 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;
@@ -112,12 +111,9 @@ public class GetMailboxesMethod<Id exten
     }
     
     private List<MailboxMetaData> retrieveUserMailboxes(MailboxSession 
session) throws MailboxException {
-        String username = session.getUser().getUserName();
         return mailboxManager.search(
-            new MailboxQuery(new MailboxPath(MailboxConstants.USER_NAMESPACE, 
username, ""),
-                "*",
-                session.getPathDelimiter()),
-            session);
+                MailboxQuery.builder(session).privateUserMailboxes().build(),
+                session);
     }
 
     private Optional<Mailbox> mailboxFromMailboxPath(MailboxPath mailboxPath, 
MailboxSession mailboxSession) {



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

Reply via email to