Repository: james-project
Updated Branches:
  refs/heads/master 2649a5c6b -> 2140f42df


JAMES-2186 StoreRightManagerTest: use MailboxFixture properly


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

Branch: refs/heads/master
Commit: 703f2d1678962ff184592aeb3d1aa8cfdac7f8e6
Parents: 52e3f6c
Author: Luc DUZAN <[email protected]>
Authored: Mon Oct 23 15:40:33 2017 +0700
Committer: Raphael Ouazana <[email protected]>
Committed: Wed Oct 25 17:40:11 2017 +0200

----------------------------------------------------------------------
 .../james/mailbox/store/StoreRightManager.java  | 41 ++++++++++++++------
 .../mailbox/store/StoreRightManagerTest.java    | 39 +++++++++----------
 2 files changed, 48 insertions(+), 32 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/james-project/blob/703f2d16/mailbox/store/src/main/java/org/apache/james/mailbox/store/StoreRightManager.java
----------------------------------------------------------------------
diff --git 
a/mailbox/store/src/main/java/org/apache/james/mailbox/store/StoreRightManager.java
 
b/mailbox/store/src/main/java/org/apache/james/mailbox/store/StoreRightManager.java
index 226510c..85dacc1 100644
--- 
a/mailbox/store/src/main/java/org/apache/james/mailbox/store/StoreRightManager.java
+++ 
b/mailbox/store/src/main/java/org/apache/james/mailbox/store/StoreRightManager.java
@@ -22,6 +22,7 @@ package org.apache.james.mailbox.store;
 import javax.inject.Inject;
 import javax.mail.Flags;
 
+import com.github.fge.lambdas.Throwing;
 import org.apache.james.mailbox.MailboxSession;
 import org.apache.james.mailbox.RightManager;
 import org.apache.james.mailbox.acl.GroupMembershipResolver;
@@ -42,6 +43,9 @@ import org.apache.james.mailbox.store.transaction.Mapper;
 import com.google.common.annotations.VisibleForTesting;
 import com.google.common.collect.ImmutableMap;
 
+import java.util.Optional;
+import java.util.function.Function;
+
 public class StoreRightManager implements RightManager {
 
     private final MailboxSessionMapperFactory mailboxSessionMapperFactory;
@@ -87,11 +91,17 @@ public class StoreRightManager implements RightManager {
 
     public Rfc4314Rights myRights(Mailbox mailbox, MailboxSession session) 
throws UnsupportedRightException {
         MailboxSession.User user = session.getUser();
-        if (user != null) {
-            return aclResolver.resolveRights(user.getUserName(), 
groupMembershipResolver, mailbox.getACL(), mailbox.getUser(), new 
GroupFolderResolver(session).isGroupFolder(mailbox));
-        } else {
-            return MailboxACL.NO_RIGHTS;
-        }
+
+        return Optional.ofNullable(user)
+            .map(Throwing.function(value ->
+                aclResolver.resolveRights(
+                    user.getUserName(),
+                    groupMembershipResolver,
+                    mailbox.getACL(),
+                    mailbox.getUser(),
+                    new GroupFolderResolver(session).isGroupFolder(mailbox)))
+                .sneakyThrow())
+            .orElse(MailboxACL.NO_RIGHTS);
     }
 
     @Override
@@ -108,13 +118,6 @@ public class StoreRightManager implements RightManager {
         mapper.execute(Mapper.toTransaction(() -> mapper.updateACL(mailbox, 
mailboxACLCommand)));
     }
 
-    @Override
-    public void setRights(MailboxPath mailboxPath, MailboxACL mailboxACL, 
MailboxSession session) throws MailboxException {
-        MailboxMapper mapper = 
mailboxSessionMapperFactory.getMailboxMapper(session);
-        Mailbox mailbox = mapper.findMailboxByPath(mailboxPath);
-        mapper.execute(Mapper.toTransaction(() -> mapper.setACL(mailbox, 
mailboxACL)));
-    }
-
     public boolean isReadWrite(MailboxSession session, Mailbox mailbox, Flags 
sharedPermanentFlags) throws UnsupportedRightException {
         Rfc4314Rights rights = myRights(mailbox, session);
 
@@ -153,9 +156,22 @@ public class StoreRightManager implements RightManager {
     public void setRights(MailboxId mailboxId, MailboxACL mailboxACL, 
MailboxSession session) throws MailboxException {
         MailboxMapper mapper = 
mailboxSessionMapperFactory.getMailboxMapper(session);
         Mailbox mailbox = mapper.findMailboxById(mailboxId);
+
         setRights(mailbox.generateAssociatedPath(), mailboxACL, session);
     }
 
+    @Override
+    public void setRights(MailboxPath mailboxPath, MailboxACL mailboxACL, 
MailboxSession session) throws MailboxException {
+        MailboxMapper mapper = 
mailboxSessionMapperFactory.getMailboxMapper(session);
+        Mailbox mailbox = mapper.findMailboxByPath(mailboxPath);
+
+        setRights(mailboxACL, mapper, mailbox);
+    }
+
+    private void setRights(MailboxACL mailboxACL, MailboxMapper mapper, 
Mailbox mailbox) throws MailboxException {
+        mapper.execute(Mapper.toTransaction(() -> mapper.setACL(mailbox, 
mailboxACL)));
+    }
+
     /**
      * Applies the global ACL (if there are any) to the mailbox ACL.
      *
@@ -182,6 +198,7 @@ public class StoreRightManager implements RightManager {
         if (mailboxSession.getUser().isSameUser(mailbox.getUser())) {
             return acl;
         }
+
         MailboxACL.EntryKey userAsKey = 
MailboxACL.EntryKey.createUserEntryKey(mailboxSession.getUser().getUserName());
         Rfc4314Rights rights = acl.getEntries().getOrDefault(userAsKey, new 
Rfc4314Rights());
         if (rights.contains(MailboxACL.Right.Administer)) {

http://git-wip-us.apache.org/repos/asf/james-project/blob/703f2d16/mailbox/store/src/test/java/org/apache/james/mailbox/store/StoreRightManagerTest.java
----------------------------------------------------------------------
diff --git 
a/mailbox/store/src/test/java/org/apache/james/mailbox/store/StoreRightManagerTest.java
 
b/mailbox/store/src/test/java/org/apache/james/mailbox/store/StoreRightManagerTest.java
index c89c74e..2b9a81b 100644
--- 
a/mailbox/store/src/test/java/org/apache/james/mailbox/store/StoreRightManagerTest.java
+++ 
b/mailbox/store/src/test/java/org/apache/james/mailbox/store/StoreRightManagerTest.java
@@ -29,7 +29,6 @@ import static org.mockito.Mockito.when;
 
 import javax.mail.Flags;
 
-import com.google.common.collect.ImmutableMap;
 import org.apache.james.mailbox.acl.GroupMembershipResolver;
 import org.apache.james.mailbox.acl.MailboxACLResolver;
 import org.apache.james.mailbox.acl.SimpleGroupMembershipResolver;
@@ -37,6 +36,7 @@ import org.apache.james.mailbox.acl.UnionMailboxACLResolver;
 import org.apache.james.mailbox.exception.MailboxException;
 import org.apache.james.mailbox.exception.MailboxNotFoundException;
 import org.apache.james.mailbox.exception.UnsupportedRightException;
+import org.apache.james.mailbox.fixture.MailboxFixture;
 import org.apache.james.mailbox.mock.MockMailboxSession;
 import org.apache.james.mailbox.model.MailboxACL;
 import org.apache.james.mailbox.model.MailboxACL.Right;
@@ -55,7 +55,6 @@ public class StoreRightManagerTest {
     private StoreRightManager storeRightManager;
     private MailboxACLResolver mailboxAclResolver;
     private GroupMembershipResolver groupMembershipResolver;
-    private String alice;
     private MockMailboxSession aliceSession;
 
     @Rule
@@ -64,8 +63,7 @@ public class StoreRightManagerTest {
 
     @Before
     public void setup() throws MailboxException {
-        alice = "Alice";
-        aliceSession = new MockMailboxSession(alice);
+        aliceSession = new MockMailboxSession(MailboxFixture.ALICE);
         MailboxSessionMapperFactory mockedMapperFactory = 
mock(MailboxSessionMapperFactory.class);
         mockedMailboxMapper = mock(MailboxMapper.class);
         mailboxAclResolver = new UnionMailboxACLResolver();
@@ -81,15 +79,16 @@ public class StoreRightManagerTest {
     public void 
hasRightShouldThrowMailboxNotFoundExceptionWhenMailboxDoesNotExist() throws 
MailboxException {
         expectedException.expect(MailboxNotFoundException.class);
 
-        MailboxPath mailboxPath = MailboxPath.forUser(alice, "unexisting 
mailbox");
-        
when(mockedMailboxMapper.findMailboxByPath(mailboxPath)).thenThrow(MailboxNotFoundException.class);
+        MailboxPath mailboxPath = MailboxPath.forUser(MailboxFixture.ALICE, 
"unexisting mailbox");
+        when(mockedMailboxMapper.findMailboxByPath(mailboxPath))
+            .thenThrow(new MailboxNotFoundException(""));
         storeRightManager.hasRight(mailboxPath, Right.Read, aliceSession);
     }
 
     @Test
     public void hasRightShouldReturnTrueWhenTheUserOwnTheMailbox() throws 
MailboxException {
         Mailbox mailbox = mock(Mailbox.class);
-        when(mailbox.getUser()).thenReturn(alice);
+        when(mailbox.getUser()).thenReturn(MailboxFixture.ALICE);
 
         assertThat(storeRightManager.hasRight(mailbox, Right.Write, 
aliceSession))
             .isFalse();
@@ -98,8 +97,8 @@ public class StoreRightManagerTest {
     @Test
     public void 
hasRightShouldReturnTrueWhenTheUserDoesnotOwnTheMailboxButHaveTheCorrectRightOnIt()
 throws MailboxException {
         Mailbox mailbox = mock(Mailbox.class);
-        when(mailbox.getUser()).thenReturn("bob");
-        when(mailbox.getACL()).thenReturn(new MailboxACL(new 
MailboxACL.Entry(alice, Right.Write)));
+        when(mailbox.getUser()).thenReturn(MailboxFixture.BOB);
+        when(mailbox.getACL()).thenReturn(new MailboxACL(new 
MailboxACL.Entry(MailboxFixture.ALICE, Right.Write)));
 
         assertThat(storeRightManager.hasRight(mailbox, Right.Write, 
aliceSession))
             .isTrue();
@@ -108,8 +107,8 @@ public class StoreRightManagerTest {
     @Test
     public void 
hasRightShouldReturnTrueWhenTheUserDoesnotOwnTheMailboxButHasAtLeastTheCorrectRightOnIt()
 throws MailboxException {
         Mailbox mailbox = mock(Mailbox.class);
-        when(mailbox.getUser()).thenReturn("bob");
-        when(mailbox.getACL()).thenReturn(new MailboxACL(new 
MailboxACL.Entry(alice, Right.Write, Right.Lookup)));
+        when(mailbox.getUser()).thenReturn(MailboxFixture.BOB);
+        when(mailbox.getACL()).thenReturn(new MailboxACL(new 
MailboxACL.Entry(MailboxFixture.ALICE, Right.Write, Right.Lookup)));
 
         assertThat(storeRightManager.hasRight(mailbox, Right.Write, 
aliceSession))
             .isTrue();
@@ -118,7 +117,7 @@ public class StoreRightManagerTest {
     @Test
     public void 
hasRightShouldReturnFalseWhenTheUserDoesNotOwnTheMailboxAndHasNoRightOnIt() 
throws MailboxException {
         Mailbox mailbox = mock(Mailbox.class);
-        when(mailbox.getUser()).thenReturn("bob");
+        when(mailbox.getUser()).thenReturn(MailboxFixture.BOB);
 
         assertThat(storeRightManager.hasRight(mailbox, Right.Write, 
aliceSession))
             .isFalse();
@@ -128,7 +127,7 @@ public class StoreRightManagerTest {
     public void isReadWriteShouldReturnTrueWhenUserHasInsertRightOnMailbox() 
throws Exception {
         Mailbox mailbox = mock(Mailbox.class);
         Flags flags = new Flags();
-        when(mailbox.getACL()).thenReturn(new MailboxACL(new 
MailboxACL.Entry(alice, Right.Insert)));
+        when(mailbox.getACL()).thenReturn(new MailboxACL(new 
MailboxACL.Entry(MailboxFixture.ALICE, Right.Insert)));
         assertThat(storeRightManager.isReadWrite(aliceSession, mailbox, flags))
             .isTrue();
     }
@@ -137,7 +136,7 @@ public class StoreRightManagerTest {
     public void 
isReadWriteShouldReturnTrueWhenUserHasPerformExpungeRightOnMailbox() throws 
Exception {
         Mailbox mailbox = mock(Mailbox.class);
         Flags flags = new Flags();
-        when(mailbox.getACL()).thenReturn(new MailboxACL(new 
MailboxACL.Entry(alice, Right.PerformExpunge)));
+        when(mailbox.getACL()).thenReturn(new MailboxACL(new 
MailboxACL.Entry(MailboxFixture.ALICE, Right.PerformExpunge)));
         assertThat(storeRightManager.isReadWrite(aliceSession, mailbox, flags))
             .isTrue();
     }
@@ -146,7 +145,7 @@ public class StoreRightManagerTest {
     public void 
isReadWriteShouldReturnTrueWhenUserHasDeleteMessagesRightOnMailboxAndFlagsContainDeletedFlag()
 throws Exception {
         Mailbox mailbox = mock(Mailbox.class);
         Flags flags = new Flags(Flags.Flag.DELETED);
-        when(mailbox.getACL()).thenReturn(new MailboxACL(new 
MailboxACL.Entry(alice, Right.DeleteMessages)));
+        when(mailbox.getACL()).thenReturn(new MailboxACL(new 
MailboxACL.Entry(MailboxFixture.ALICE, Right.DeleteMessages)));
         assertThat(storeRightManager.isReadWrite(aliceSession, mailbox, flags))
             .isTrue();
     }
@@ -155,7 +154,7 @@ public class StoreRightManagerTest {
     public void 
isReadWriteShouldReturnFalseWhenUserHasDeleteMessagesRightOnMailboxButFlagsDoesNotContainDeletedFlag()
 throws Exception {
         Mailbox mailbox = mock(Mailbox.class);
         Flags flags = new Flags();
-        when(mailbox.getACL()).thenReturn(new MailboxACL(new 
MailboxACL.Entry(alice, Right.DeleteMessages)));
+        when(mailbox.getACL()).thenReturn(new MailboxACL(new 
MailboxACL.Entry(MailboxFixture.ALICE, Right.DeleteMessages)));
         assertThat(storeRightManager.isReadWrite(aliceSession, mailbox, flags))
             .isFalse();
     }
@@ -164,7 +163,7 @@ public class StoreRightManagerTest {
     public void 
isReadWriteShouldReturnTrueWhenUserHasWriteSeenFlagRightOnMailboxAndFlagsContainSeenFlag()
 throws Exception {
         Mailbox mailbox = mock(Mailbox.class);
         Flags flags = new Flags(Flags.Flag.SEEN);
-        when(mailbox.getACL()).thenReturn(new MailboxACL(new 
MailboxACL.Entry(alice, Right.WriteSeenFlag)));
+        when(mailbox.getACL()).thenReturn(new MailboxACL(new 
MailboxACL.Entry(MailboxFixture.ALICE, Right.WriteSeenFlag)));
         assertThat(storeRightManager.isReadWrite(aliceSession, mailbox, flags))
             .isTrue();
     }
@@ -173,7 +172,7 @@ public class StoreRightManagerTest {
     public void 
isReadWriteShouldReturnFalseWhenUserHasWriteSeenFlagRightOnMailboxAndFlagsDoesNotContainSeenFlag()
 throws Exception {
         Mailbox mailbox = mock(Mailbox.class);
         Flags flags = new Flags();
-        when(mailbox.getACL()).thenReturn(new MailboxACL(new 
MailboxACL.Entry(alice, Right.WriteSeenFlag)));
+        when(mailbox.getACL()).thenReturn(new MailboxACL(new 
MailboxACL.Entry(MailboxFixture.ALICE, Right.WriteSeenFlag)));
         assertThat(storeRightManager.isReadWrite(aliceSession, mailbox, flags))
             .isFalse();
     }
@@ -182,7 +181,7 @@ public class StoreRightManagerTest {
     public void 
isReadWriteShouldReturnTrueWhenUserHasWriteRightOnMailboxAndFlagsContainAnsweredFlag()
 throws Exception {
         Mailbox mailbox = mock(Mailbox.class);
         Flags flags = new Flags(Flags.Flag.ANSWERED);
-        when(mailbox.getACL()).thenReturn(new MailboxACL(new 
MailboxACL.Entry(alice, Right.Write)));
+        when(mailbox.getACL()).thenReturn(new MailboxACL(new 
MailboxACL.Entry(MailboxFixture.ALICE, Right.Write)));
         assertThat(storeRightManager.isReadWrite(aliceSession, mailbox, flags))
             .isTrue();
     }
@@ -190,7 +189,7 @@ public class StoreRightManagerTest {
     @Test
     public void 
isReadWriteShouldReturnFalseWhenUserDoesNotHaveInsertOrPerformExpungeRightOnMailboxAndNullFlag()
 throws Exception {
         Mailbox mailbox = mock(Mailbox.class);
-        when(mailbox.getACL()).thenReturn(new MailboxACL(new 
MailboxACL.Entry(alice, Right.Administer)));
+        when(mailbox.getACL()).thenReturn(new MailboxACL(new 
MailboxACL.Entry(MailboxFixture.ALICE, Right.Administer)));
         assertThat(storeRightManager.isReadWrite(aliceSession, mailbox, new 
Flags()))
             .isFalse();
     }


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

Reply via email to