JAMES-1840 Accept null user when resolving quota root.

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

Branch: refs/heads/master
Commit: 72bc076c1884dcd4c9349cc2747b3a7df3144596
Parents: 7488a2e
Author: Edgar Asatryan <nst...@gmail.com>
Authored: Fri Jun 8 21:04:25 2018 +0400
Committer: benwa <btell...@linagora.com>
Committed: Wed Jun 13 09:48:48 2018 +0700

----------------------------------------------------------------------
 .../quota/DefaultUserQuotaRootResolver.java     | 12 ++-
 .../quota/DefaultQuotaRootResolverTest.java     | 85 ------------------
 .../quota/DefaultUserQuotaRootResolverTest.java | 92 ++++++++++++++++++++
 3 files changed, 100 insertions(+), 89 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/james-project/blob/72bc076c/mailbox/store/src/main/java/org/apache/james/mailbox/store/quota/DefaultUserQuotaRootResolver.java
----------------------------------------------------------------------
diff --git 
a/mailbox/store/src/main/java/org/apache/james/mailbox/store/quota/DefaultUserQuotaRootResolver.java
 
b/mailbox/store/src/main/java/org/apache/james/mailbox/store/quota/DefaultUserQuotaRootResolver.java
index bbf90a2..09a55c9 100644
--- 
a/mailbox/store/src/main/java/org/apache/james/mailbox/store/quota/DefaultUserQuotaRootResolver.java
+++ 
b/mailbox/store/src/main/java/org/apache/james/mailbox/store/quota/DefaultUserQuotaRootResolver.java
@@ -20,6 +20,7 @@
 package org.apache.james.mailbox.store.quota;
 
 import java.util.List;
+import java.util.Optional;
 
 import javax.inject.Inject;
 
@@ -57,10 +58,13 @@ public class DefaultUserQuotaRootResolver implements 
UserQuotaRootResolver {
     @Override
     public QuotaRoot getQuotaRoot(MailboxPath mailboxPath) {
         
Preconditions.checkArgument(!mailboxPath.getNamespace().contains(SEPARATOR), 
"Namespace should not contain " + SEPARATOR);
-        
Preconditions.checkArgument(!mailboxPath.getUser().contains(SEPARATOR), 
"Username should not contain " + SEPARATOR);
-        User user = User.fromUsername(mailboxPath.getUser());
-        return QuotaRoot.quotaRoot(mailboxPath.getNamespace() + SEPARATOR + 
user.asString(),
-            user.getDomainPart());
+        return Optional.ofNullable(mailboxPath.getUser())
+                .map(user -> {
+                    
Preconditions.checkArgument(!mailboxPath.getUser().contains(SEPARATOR), 
"Username should not contain " + SEPARATOR);
+                    return User.fromUsername(mailboxPath.getUser());
+                })
+                .map(user -> QuotaRoot.quotaRoot(mailboxPath.getNamespace() + 
SEPARATOR + user.asString(), user.getDomainPart()))
+                .orElseGet(() -> 
QuotaRoot.quotaRoot(mailboxPath.getNamespace(), Optional.empty()));
     }
 
     @Override

http://git-wip-us.apache.org/repos/asf/james-project/blob/72bc076c/mailbox/store/src/test/java/org/apache/james/mailbox/store/quota/DefaultQuotaRootResolverTest.java
----------------------------------------------------------------------
diff --git 
a/mailbox/store/src/test/java/org/apache/james/mailbox/store/quota/DefaultQuotaRootResolverTest.java
 
b/mailbox/store/src/test/java/org/apache/james/mailbox/store/quota/DefaultQuotaRootResolverTest.java
deleted file mode 100644
index 4c855bf..0000000
--- 
a/mailbox/store/src/test/java/org/apache/james/mailbox/store/quota/DefaultQuotaRootResolverTest.java
+++ /dev/null
@@ -1,85 +0,0 @@
-/****************************************************************
- * 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.mailbox.store.quota;
-
-import static org.assertj.core.api.Assertions.assertThat;
-import static org.mockito.Mockito.mock;
-import static org.mockito.Mockito.when;
-
-import java.util.Optional;
-
-import org.apache.james.mailbox.exception.MailboxException;
-import org.apache.james.mailbox.model.MailboxPath;
-import org.apache.james.mailbox.model.QuotaRoot;
-import org.apache.james.mailbox.store.MailboxSessionMapperFactory;
-import org.apache.james.mailbox.store.mail.MailboxMapper;
-import org.apache.james.mailbox.store.mail.model.impl.SimpleMailbox;
-import org.junit.Before;
-import org.junit.Test;
-
-import com.google.common.collect.Lists;
-
-public class DefaultQuotaRootResolverTest {
-
-    public static final MailboxPath MAILBOX_PATH = 
MailboxPath.forUser("benwa", "INBOX");
-    public static final SimpleMailbox MAILBOX = new 
SimpleMailbox(MAILBOX_PATH, 10);
-    public static final MailboxPath PATH_LIKE = MailboxPath.forUser("benwa", 
"%");
-    public static final MailboxPath MAILBOX_PATH_2 = 
MailboxPath.forUser("benwa", "test");
-    public static final SimpleMailbox MAILBOX_2 = new 
SimpleMailbox(MAILBOX_PATH_2, 10);
-    public static final QuotaRoot QUOTA_ROOT = 
QuotaRoot.quotaRoot("#private&benwa", Optional.empty());
-
-    private DefaultUserQuotaRootResolver testee;
-    private MailboxSessionMapperFactory mockedFactory;
-
-    @Before
-    public void setUp() {
-        mockedFactory = mock(MailboxSessionMapperFactory.class);
-        testee = new DefaultUserQuotaRootResolver(mockedFactory);
-    }
-
-    @Test
-    public void getQuotaRootShouldReturnUserRelatedQuotaRoot() throws 
Exception {
-        assertThat(testee.getQuotaRoot(MAILBOX_PATH)).isEqualTo(QUOTA_ROOT);
-    }
-
-    @Test(expected = IllegalArgumentException.class)
-    public void getQuotaRootShouldThrowWhenNamespaceContainsSeparator() throws 
Exception {
-        testee.getQuotaRoot(new MailboxPath("#pr&ivate", "benwa", "INBOX"));
-    }
-
-    @Test(expected = IllegalArgumentException.class)
-    public void getQuotaRootShouldThrowWhenUserContainsSeparator() throws 
Exception {
-        testee.getQuotaRoot(MailboxPath.forUser("ben&wa", "INBOX"));
-    }
-
-    @Test
-    public void retrieveAssociatedMailboxesShouldWork() throws Exception {
-        final MailboxMapper mockedMapper = mock(MailboxMapper.class);
-        when(mockedFactory.getMailboxMapper(null)).thenReturn(mockedMapper);
-        
when(mockedMapper.findMailboxWithPathLike(PATH_LIKE)).thenReturn(Lists.newArrayList(MAILBOX,
 MAILBOX_2));
-        assertThat(testee.retrieveAssociatedMailboxes(QUOTA_ROOT, 
null)).containsOnly(MAILBOX_PATH, MAILBOX_PATH_2);
-    }
-
-    @Test(expected = MailboxException.class)
-    public void 
retrieveAssociatedMailboxesShouldThrowWhenQuotaRootContainsSeparator2Times() 
throws Exception {
-        
testee.retrieveAssociatedMailboxes(QuotaRoot.quotaRoot("#private&be&nwa", 
Optional.empty()), null);
-    }
-
-}

http://git-wip-us.apache.org/repos/asf/james-project/blob/72bc076c/mailbox/store/src/test/java/org/apache/james/mailbox/store/quota/DefaultUserQuotaRootResolverTest.java
----------------------------------------------------------------------
diff --git 
a/mailbox/store/src/test/java/org/apache/james/mailbox/store/quota/DefaultUserQuotaRootResolverTest.java
 
b/mailbox/store/src/test/java/org/apache/james/mailbox/store/quota/DefaultUserQuotaRootResolverTest.java
new file mode 100644
index 0000000..92b9f6c
--- /dev/null
+++ 
b/mailbox/store/src/test/java/org/apache/james/mailbox/store/quota/DefaultUserQuotaRootResolverTest.java
@@ -0,0 +1,92 @@
+/****************************************************************
+ * 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.mailbox.store.quota;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+import java.util.Optional;
+
+import org.apache.james.mailbox.exception.MailboxException;
+import org.apache.james.mailbox.model.MailboxPath;
+import org.apache.james.mailbox.model.QuotaRoot;
+import org.apache.james.mailbox.store.MailboxSessionMapperFactory;
+import org.apache.james.mailbox.store.mail.MailboxMapper;
+import org.apache.james.mailbox.store.mail.model.impl.SimpleMailbox;
+import org.junit.Before;
+import org.junit.Test;
+
+import com.google.common.collect.Lists;
+
+public class DefaultUserQuotaRootResolverTest {
+
+    public static final MailboxPath MAILBOX_PATH = 
MailboxPath.forUser("benwa", "INBOX");
+    public static final SimpleMailbox MAILBOX = new 
SimpleMailbox(MAILBOX_PATH, 10);
+    public static final MailboxPath PATH_LIKE = MailboxPath.forUser("benwa", 
"%");
+    public static final MailboxPath MAILBOX_PATH_2 = 
MailboxPath.forUser("benwa", "test");
+    public static final SimpleMailbox MAILBOX_2 = new 
SimpleMailbox(MAILBOX_PATH_2, 10);
+    public static final QuotaRoot QUOTA_ROOT = 
QuotaRoot.quotaRoot("#private&benwa", Optional.empty());
+
+    private DefaultUserQuotaRootResolver testee;
+    private MailboxSessionMapperFactory mockedFactory;
+
+    @Before
+    public void setUp() {
+        mockedFactory = mock(MailboxSessionMapperFactory.class);
+        testee = new DefaultUserQuotaRootResolver(mockedFactory);
+    }
+
+    @Test
+    public void getQuotaRootShouldReturnUserRelatedQuotaRoot() throws 
Exception {
+        assertThat(testee.getQuotaRoot(MAILBOX_PATH)).isEqualTo(QUOTA_ROOT);
+    }
+
+    @Test(expected = IllegalArgumentException.class)
+    public void getQuotaRootShouldThrowWhenNamespaceContainsSeparator() throws 
Exception {
+        testee.getQuotaRoot(new MailboxPath("#pr&ivate", "benwa", "INBOX"));
+    }
+
+    @Test(expected = IllegalArgumentException.class)
+    public void getQuotaRootShouldThrowWhenUserContainsSeparator() throws 
Exception {
+        testee.getQuotaRoot(MailboxPath.forUser("ben&wa", "INBOX"));
+    }
+
+    @Test
+    public void getQuotaRootShouldWorkWhenUserIsNull() throws Exception {
+        QuotaRoot quotaRoot = testee.getQuotaRoot(new MailboxPath("#private", 
null, "INBOX"));
+
+        assertThat(quotaRoot).isEqualTo(QuotaRoot.quotaRoot("#private", 
Optional.empty()));
+    }
+
+    @Test
+    public void retrieveAssociatedMailboxesShouldWork() throws Exception {
+        final MailboxMapper mockedMapper = mock(MailboxMapper.class);
+        when(mockedFactory.getMailboxMapper(null)).thenReturn(mockedMapper);
+        
when(mockedMapper.findMailboxWithPathLike(PATH_LIKE)).thenReturn(Lists.newArrayList(MAILBOX,
 MAILBOX_2));
+        assertThat(testee.retrieveAssociatedMailboxes(QUOTA_ROOT, 
null)).containsOnly(MAILBOX_PATH, MAILBOX_PATH_2);
+    }
+
+    @Test(expected = MailboxException.class)
+    public void 
retrieveAssociatedMailboxesShouldThrowWhenQuotaRootContainsSeparator2Times() 
throws Exception {
+        
testee.retrieveAssociatedMailboxes(QuotaRoot.quotaRoot("#private&be&nwa", 
Optional.empty()), null);
+    }
+
+}


---------------------------------------------------------------------
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