JAMES-1715 Add Mailbox utility methods: mailboxFromMailboxId, getMailboxPath & 
hasChildren


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

Branch: refs/heads/master
Commit: ebeabded139b5f70258fdc3da29c551a362b1942
Parents: ee19df8
Author: Antoine Duprat <[email protected]>
Authored: Thu Mar 31 15:15:09 2016 +0200
Committer: Antoine Duprat <[email protected]>
Committed: Tue Apr 5 14:00:40 2016 +0200

----------------------------------------------------------------------
 .../apache/james/jmap/utils/MailboxUtils.java   |  32 +++++
 .../james/jmap/utils/MailboxUtilsTest.java      | 140 +++++++++++++++++--
 2 files changed, 159 insertions(+), 13 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/james-project/blob/ebeabded/server/protocols/jmap/src/main/java/org/apache/james/jmap/utils/MailboxUtils.java
----------------------------------------------------------------------
diff --git 
a/server/protocols/jmap/src/main/java/org/apache/james/jmap/utils/MailboxUtils.java
 
b/server/protocols/jmap/src/main/java/org/apache/james/jmap/utils/MailboxUtils.java
index b8128e3..46d552f 100644
--- 
a/server/protocols/jmap/src/main/java/org/apache/james/jmap/utils/MailboxUtils.java
+++ 
b/server/protocols/jmap/src/main/java/org/apache/james/jmap/utils/MailboxUtils.java
@@ -37,6 +37,7 @@ import org.apache.james.mailbox.store.mail.model.MailboxId;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
+import com.github.fge.lambdas.Throwing;
 import com.google.common.annotations.VisibleForTesting;
 import com.google.common.base.Splitter;
 
@@ -116,4 +117,35 @@ public class MailboxUtils<Id extends MailboxId> {
         MailboxPath parent = levels.get(levels.size() - 2);
         return getMailboxId(parent, mailboxSession);
     }
+
+    public Optional<Mailbox> mailboxFromMailboxId(String mailboxId, 
MailboxSession mailboxSession) {
+        try {
+            return getMailboxFromId(mailboxId, mailboxSession)
+                .flatMap(jamesMailbox ->
+                    mailboxFromMailboxPath(new 
MailboxPath(jamesMailbox.getNamespace(), 
mailboxSession.getUser().getUserName(), jamesMailbox.getName()), 
+                            mailboxSession)
+                );
+        } catch (MailboxException e) {
+            return Optional.empty();
+        }
+    }
+
+    public MailboxPath getMailboxPath(Mailbox mailbox, MailboxSession 
mailboxSession) {
+        return new MailboxPath(mailboxSession.getPersonalSpace(), 
mailboxSession.getUser().getUserName(), getMailboxName(mailbox, 
mailboxSession));
+    }
+
+    private String getMailboxName(Mailbox mailbox, MailboxSession 
mailboxSession) {
+        if (mailbox.getParentId().isPresent()) {
+            return 
getMailboxName(mailboxFromMailboxId(mailbox.getParentId().get(), 
mailboxSession).get(), mailboxSession) +
+                    mailboxSession.getPathDelimiter() + mailbox.getName();
+        }
+        return mailbox.getName();
+    }
+
+    public boolean hasChildren(String mailboxId, MailboxSession 
mailboxSession) throws MailboxException {
+        return getMailboxFromId(mailboxId, mailboxSession)
+                .map(Throwing.function(mailbox -> 
+                    
mailboxMapperFactory.getMailboxMapper(mailboxSession).hasChildren(mailbox, 
mailboxSession.getPathDelimiter())))
+                .orElse(false);
+    }
 }

http://git-wip-us.apache.org/repos/asf/james-project/blob/ebeabded/server/protocols/jmap/src/test/java/org/apache/james/jmap/utils/MailboxUtilsTest.java
----------------------------------------------------------------------
diff --git 
a/server/protocols/jmap/src/test/java/org/apache/james/jmap/utils/MailboxUtilsTest.java
 
b/server/protocols/jmap/src/test/java/org/apache/james/jmap/utils/MailboxUtilsTest.java
index f6a4a89..6accb7a 100644
--- 
a/server/protocols/jmap/src/test/java/org/apache/james/jmap/utils/MailboxUtilsTest.java
+++ 
b/server/protocols/jmap/src/test/java/org/apache/james/jmap/utils/MailboxUtilsTest.java
@@ -43,6 +43,7 @@ public class MailboxUtilsTest {
     private MailboxManager mailboxManager;
     private MailboxMapperFactory<InMemoryId> mailboxMapperFactory;
     private MailboxSession mailboxSession;
+    private String user;
     private MailboxUtils<InMemoryId> sut;
 
     @Before
@@ -50,13 +51,14 @@ public class MailboxUtilsTest {
         InMemoryIntegrationResources inMemoryIntegrationResources = new 
InMemoryIntegrationResources();
         mailboxManager = 
inMemoryIntegrationResources.createMailboxManager(inMemoryIntegrationResources.createGroupMembershipResolver());
         mailboxMapperFactory = new InMemoryMailboxSessionMapperFactory();
-        mailboxSession = mailboxManager.login("[email protected]", "pass", 
LOGGER);
+        user = "[email protected]";
+        mailboxSession = mailboxManager.login(user, "pass", LOGGER);
         sut = new MailboxUtils<>(mailboxManager, mailboxMapperFactory);
     }
 
     @Test
     public void mailboxFromMailboxPathShouldReturnNotEmptyWhenMailboxExists() 
throws Exception {
-        MailboxPath mailboxPath = new MailboxPath("#private", "user", 
"mailbox");
+        MailboxPath mailboxPath = new MailboxPath("#private", user, "mailbox");
         mailboxManager.createMailbox(mailboxPath, mailboxSession);
 
         Optional<Mailbox> optionalMailbox = 
sut.mailboxFromMailboxPath(mailboxPath, mailboxSession);
@@ -67,7 +69,7 @@ public class MailboxUtilsTest {
 
     @Test
     public void 
mailboxFromMailboxPathShouldReturnEmptyWhenMailboxDoesntExist() throws 
Exception {
-        MailboxPath mailboxPath = new MailboxPath("#private", "user", 
"mailbox");
+        MailboxPath mailboxPath = new MailboxPath("#private", user, "mailbox");
 
         Optional<Mailbox> optionalMailbox = 
sut.mailboxFromMailboxPath(mailboxPath, mailboxSession);
         assertThat(optionalMailbox).isEmpty();
@@ -76,7 +78,7 @@ public class MailboxUtilsTest {
     @Test
     public void getNameShouldReturnMailboxNameWhenRootMailbox() throws 
Exception {
         String expected = "mailbox";
-        MailboxPath mailboxPath = new MailboxPath("#private", "user", 
expected);
+        MailboxPath mailboxPath = new MailboxPath("#private", user, expected);
 
         String name = sut.getName(mailboxPath, mailboxSession);
         assertThat(name).isEqualTo(expected);
@@ -85,7 +87,7 @@ public class MailboxUtilsTest {
     @Test
     public void getNameShouldReturnMailboxNameWhenChildMailbox() throws 
Exception {
         String expected = "mailbox";
-        MailboxPath mailboxPath = new MailboxPath("#private", "user", "inbox." 
+ expected);
+        MailboxPath mailboxPath = new MailboxPath("#private", user, "inbox." + 
expected);
 
         String name = sut.getName(mailboxPath, mailboxSession);
         assertThat(name).isEqualTo(expected);
@@ -94,7 +96,7 @@ public class MailboxUtilsTest {
     @Test
     public void getNameShouldReturnMailboxNameWhenChildOfChildMailbox() throws 
Exception {
         String expected = "mailbox";
-        MailboxPath mailboxPath = new MailboxPath("#private", "user", 
"inbox.children." + expected);
+        MailboxPath mailboxPath = new MailboxPath("#private", user, 
"inbox.children." + expected);
 
         String name = sut.getName(mailboxPath, mailboxSession);
         assertThat(name).isEqualTo(expected);
@@ -103,7 +105,7 @@ public class MailboxUtilsTest {
     @Test
     public void getMailboxNameFromIdShouldReturnNotEmptyWhenMailboxExists() 
throws Exception {
         String expected = "mailbox";
-        MailboxPath mailboxPath = new MailboxPath("#private", "user", 
expected);
+        MailboxPath mailboxPath = new MailboxPath("#private", user, expected);
         mailboxManager.createMailbox(mailboxPath, mailboxSession);
         InMemoryId mailboxId = 
mailboxMapperFactory.getMailboxMapper(mailboxSession)
             .findMailboxByPath(mailboxPath)
@@ -123,7 +125,7 @@ public class MailboxUtilsTest {
 
     @Test
     public void getParentIdFromMailboxPathShouldReturNullWhenRootMailbox() 
throws Exception {
-        MailboxPath mailboxPath = new MailboxPath("#private", "user", 
"mailbox");
+        MailboxPath mailboxPath = new MailboxPath("#private", user, "mailbox");
         mailboxManager.createMailbox(mailboxPath, mailboxSession);
 
         String id = sut.getParentIdFromMailboxPath(mailboxPath, 
mailboxSession);
@@ -132,13 +134,13 @@ public class MailboxUtilsTest {
 
     @Test
     public void 
getParentIdFromMailboxPathShouldReturnParentIdWhenChildMailbox() throws 
Exception {
-        MailboxPath parentMailboxPath = new MailboxPath("#private", "user", 
"inbox");
+        MailboxPath parentMailboxPath = new MailboxPath("#private", user, 
"inbox");
         mailboxManager.createMailbox(parentMailboxPath, mailboxSession);
         InMemoryId parentId = 
mailboxMapperFactory.getMailboxMapper(mailboxSession)
                 .findMailboxByPath(parentMailboxPath)
                 .getMailboxId();
 
-        MailboxPath mailboxPath = new MailboxPath("#private", "user", 
"inbox.mailbox");
+        MailboxPath mailboxPath = new MailboxPath("#private", user, 
"inbox.mailbox");
         mailboxManager.createMailbox(mailboxPath, mailboxSession);
 
         String id = sut.getParentIdFromMailboxPath(mailboxPath, 
mailboxSession);
@@ -147,10 +149,10 @@ public class MailboxUtilsTest {
 
     @Test
     public void 
getParentIdFromMailboxPathShouldReturnParentIdWhenChildOfChildMailbox() throws 
Exception {
-        MailboxPath mailboxPath = new MailboxPath("#private", "user", 
"inbox.children.mailbox");
-        mailboxManager.createMailbox(new MailboxPath("#private", "user", 
"inbox"), mailboxSession);
+        MailboxPath mailboxPath = new MailboxPath("#private", user, 
"inbox.children.mailbox");
+        mailboxManager.createMailbox(new MailboxPath("#private", user, 
"inbox"), mailboxSession);
 
-        MailboxPath parentMailboxPath = new MailboxPath("#private", "user", 
"inbox.children");
+        MailboxPath parentMailboxPath = new MailboxPath("#private", user, 
"inbox.children");
         mailboxManager.createMailbox(parentMailboxPath, mailboxSession);
         InMemoryId parentId = 
mailboxMapperFactory.getMailboxMapper(mailboxSession)
                 .findMailboxByPath(parentMailboxPath)
@@ -161,4 +163,116 @@ public class MailboxUtilsTest {
         String id = sut.getParentIdFromMailboxPath(mailboxPath, 
mailboxSession);
         assertThat(id).isEqualTo(parentId.serialize());
     }
+
+    @Test
+    public void mailboxFromMailboxIdShouldReturnPresentWhenExists() throws 
Exception {
+        MailboxPath mailboxPath = new MailboxPath("#private", user, "myBox");
+        mailboxManager.createMailbox(mailboxPath, mailboxSession);
+        InMemoryId mailboxId = 
mailboxMapperFactory.getMailboxMapper(mailboxSession)
+                .findMailboxByPath(mailboxPath)
+                .getMailboxId();
+
+        Optional<Mailbox> mailbox = 
sut.mailboxFromMailboxId(mailboxId.serialize(), mailboxSession);
+        assertThat(mailbox).isPresent();
+        assertThat(mailbox.get().getId()).isEqualTo(mailboxId.serialize());
+    }
+
+    @Test
+    public void mailboxFromMailboxIdShouldReturnAbsentWhenDoesntExist() throws 
Exception {
+        Optional<Mailbox> mailbox = sut.mailboxFromMailboxId("123", 
mailboxSession);
+        assertThat(mailbox).isEmpty();
+    }
+
+    @Test
+    public void getMailboxPathShouldReturnThePathWhenRootMailbox() throws 
Exception {
+        MailboxPath expected = new MailboxPath("#private", user, "myBox");
+        mailboxManager.createMailbox(expected, mailboxSession);
+        InMemoryId mailboxId = 
mailboxMapperFactory.getMailboxMapper(mailboxSession)
+                .findMailboxByPath(expected)
+                .getMailboxId();
+
+        Mailbox mailbox = Mailbox.builder()
+                .id(mailboxId.serialize())
+                .name("myBox")
+                .build();
+
+        MailboxPath mailboxPath = sut.getMailboxPath(mailbox, mailboxSession);
+        assertThat(mailboxPath).isEqualTo(expected);
+    }
+
+    @Test
+    public void getMailboxPathShouldReturnThePathWhenChildMailbox() throws 
Exception {
+        MailboxPath parentMailboxPath = new MailboxPath("#private", user, 
"inbox");
+        mailboxManager.createMailbox(parentMailboxPath, mailboxSession);
+        InMemoryId parentId = 
mailboxMapperFactory.getMailboxMapper(mailboxSession)
+                .findMailboxByPath(parentMailboxPath)
+                .getMailboxId();
+
+        MailboxPath expected = new MailboxPath("#private", user, 
"inbox.myBox");
+        mailboxManager.createMailbox(expected, mailboxSession);
+        InMemoryId mailboxId = 
mailboxMapperFactory.getMailboxMapper(mailboxSession)
+                .findMailboxByPath(expected)
+                .getMailboxId();
+
+        Mailbox mailbox = Mailbox.builder()
+                .id(mailboxId.serialize())
+                .name("myBox")
+                .parentId(parentId.serialize())
+                .build();
+
+        MailboxPath mailboxPath = sut.getMailboxPath(mailbox, mailboxSession);
+        assertThat(mailboxPath).isEqualTo(expected);
+    }
+
+    @Test
+    public void getMailboxPathShouldReturnThePathWhenChildOfChildMailbox() 
throws Exception {
+        MailboxPath parentMailboxPath = new MailboxPath("#private", user, 
"inbox");
+        mailboxManager.createMailbox(parentMailboxPath, mailboxSession);
+
+        MailboxPath childMailboxPath = new MailboxPath("#private", user, 
"inbox.child");
+        mailboxManager.createMailbox(childMailboxPath, mailboxSession);
+        InMemoryId childId = 
mailboxMapperFactory.getMailboxMapper(mailboxSession)
+                .findMailboxByPath(childMailboxPath)
+                .getMailboxId();
+
+        MailboxPath expected = new MailboxPath("#private", user, 
"inbox.child.myBox");
+        mailboxManager.createMailbox(expected, mailboxSession);
+        InMemoryId mailboxId = 
mailboxMapperFactory.getMailboxMapper(mailboxSession)
+                .findMailboxByPath(expected)
+                .getMailboxId();
+
+        Mailbox mailbox = Mailbox.builder()
+                .id(mailboxId.serialize())
+                .name("myBox")
+                .parentId(childId.serialize())
+                .build();
+
+        MailboxPath mailboxPath = sut.getMailboxPath(mailbox, mailboxSession);
+        assertThat(mailboxPath).isEqualTo(expected);
+    }
+
+    @Test
+    public void hasChildrenShouldReturnFalseWhenNoChild() throws Exception {
+        MailboxPath mailboxPath = new MailboxPath("#private", user, "myBox");
+        mailboxManager.createMailbox(mailboxPath, mailboxSession);
+        InMemoryId mailboxId = 
mailboxMapperFactory.getMailboxMapper(mailboxSession)
+                .findMailboxByPath(mailboxPath)
+                .getMailboxId();
+
+        assertThat(sut.hasChildren(mailboxId.serialize(), 
mailboxSession)).isFalse();
+    }
+
+    @Test
+    public void hasChildrenShouldReturnTrueWhenHasAChild() throws Exception {
+        MailboxPath parentMailboxPath = new MailboxPath("#private", user, 
"inbox");
+        mailboxManager.createMailbox(parentMailboxPath, mailboxSession);
+        InMemoryId parentId = 
mailboxMapperFactory.getMailboxMapper(mailboxSession)
+                .findMailboxByPath(parentMailboxPath)
+                .getMailboxId();
+
+        MailboxPath mailboxPath = new MailboxPath("#private", user, 
"inbox.myBox");
+        mailboxManager.createMailbox(mailboxPath, mailboxSession);
+
+        assertThat(sut.hasChildren(parentId.serialize(), 
mailboxSession)).isTrue();
+    }
 }


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

Reply via email to