You are viewing a plain text version of this content. The canonical link for it is here.
Posted to server-dev@james.apache.org by ad...@apache.org on 2017/11/13 15:27:42 UTC

[5/8] james-project git commit: PROTOCOLS-117 Rewrite StoreManagerTests without mock

PROTOCOLS-117 Rewrite StoreManagerTests without mock


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

Branch: refs/heads/master
Commit: 2b4626fc7e689ba13f5e70a45cf177f6956ec613
Parents: 7e8aaea
Author: benwa <bt...@linagora.com>
Authored: Thu Nov 9 10:06:31 2017 +0700
Committer: Antoine Duprat <ad...@linagora.com>
Committed: Mon Nov 13 16:25:59 2017 +0100

----------------------------------------------------------------------
 .../mailbox/store/StoreRightManagerTest.java    | 85 ++++++++++----------
 1 file changed, 42 insertions(+), 43 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/james-project/blob/2b4626fc/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 265bfe8..9d765d5 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
@@ -30,8 +30,6 @@ import static org.mockito.Mockito.when;
 
 import javax.mail.Flags;
 
-import org.apache.james.mailbox.acl.GroupMembershipResolver;
-import org.apache.james.mailbox.acl.MailboxACLResolver;
 import org.apache.james.mailbox.acl.SimpleGroupMembershipResolver;
 import org.apache.james.mailbox.acl.UnionMailboxACLResolver;
 import org.apache.james.mailbox.exception.DifferentDomainException;
@@ -43,25 +41,21 @@ import org.apache.james.mailbox.mock.MockMailboxSession;
 import org.apache.james.mailbox.model.MailboxACL;
 import org.apache.james.mailbox.model.MailboxACL.ACLCommand;
 import org.apache.james.mailbox.model.MailboxACL.Right;
+import org.apache.james.mailbox.model.MailboxConstants;
 import org.apache.james.mailbox.model.MailboxPath;
 import org.apache.james.mailbox.store.mail.MailboxMapper;
 import org.apache.james.mailbox.store.mail.model.Mailbox;
 import org.apache.james.mailbox.store.mail.model.impl.SimpleMailbox;
 import org.junit.Before;
-import org.junit.Rule;
+import org.junit.Ignore;
 import org.junit.Test;
-import org.junit.rules.ExpectedException;
 
 public class StoreRightManagerTest {
 
-    public static final long UID_VALIDITY = 3421l;
+    private static final long UID_VALIDITY = 3421L;
     private StoreRightManager storeRightManager;
-    private MailboxACLResolver mailboxAclResolver;
-    private GroupMembershipResolver groupMembershipResolver;
     private MockMailboxSession aliceSession;
 
-    @Rule
-    public ExpectedException expectedException = ExpectedException.none();
     private MailboxMapper mockedMailboxMapper;
 
     @Before
@@ -69,29 +63,29 @@ public class StoreRightManagerTest {
         aliceSession = new MockMailboxSession(MailboxFixture.ALICE);
         MailboxSessionMapperFactory mockedMapperFactory = mock(MailboxSessionMapperFactory.class);
         mockedMailboxMapper = mock(MailboxMapper.class);
-        mailboxAclResolver = new UnionMailboxACLResolver();
-        groupMembershipResolver = new SimpleGroupMembershipResolver();
         when(mockedMapperFactory.getMailboxMapper(aliceSession))
             .thenReturn(mockedMailboxMapper);
+
         storeRightManager = new StoreRightManager(mockedMapperFactory,
-                                                  mailboxAclResolver,
-                                                  groupMembershipResolver);
+            new UnionMailboxACLResolver(),
+            new SimpleGroupMembershipResolver());
     }
 
     @Test
     public void hasRightShouldThrowMailboxNotFoundExceptionWhenMailboxDoesNotExist() throws MailboxException {
-        expectedException.expect(MailboxNotFoundException.class);
-
         MailboxPath mailboxPath = MailboxPath.forUser(MailboxFixture.ALICE, "unexisting mailbox");
         when(mockedMailboxMapper.findMailboxByPath(mailboxPath))
             .thenThrow(new MailboxNotFoundException(""));
-        storeRightManager.hasRight(mailboxPath, Right.Read, aliceSession);
+
+        assertThatThrownBy(() ->
+            storeRightManager.hasRight(mailboxPath, Right.Read, aliceSession))
+            .isInstanceOf(MailboxNotFoundException.class);
     }
 
+    @Ignore("PROTOCOLS-117 Will be solved in next commit")
     @Test
     public void hasRightShouldReturnTrueWhenTheUserOwnTheMailbox() throws MailboxException {
-        Mailbox mailbox = mock(Mailbox.class);
-        when(mailbox.getUser()).thenReturn(MailboxFixture.ALICE);
+        Mailbox mailbox = new SimpleMailbox(MailboxPath.forUser(ALICE, MailboxConstants.INBOX), UID_VALIDITY);
 
         assertThat(storeRightManager.hasRight(mailbox, Right.Write, aliceSession))
             .isFalse();
@@ -99,9 +93,8 @@ public class StoreRightManagerTest {
 
     @Test
     public void hasRightShouldReturnTrueWhenTheUserDoesnotOwnTheMailboxButHaveTheCorrectRightOnIt() throws MailboxException {
-        Mailbox mailbox = mock(Mailbox.class);
-        when(mailbox.getUser()).thenReturn(MailboxFixture.BOB);
-        when(mailbox.getACL()).thenReturn(new MailboxACL(new MailboxACL.Entry(MailboxFixture.ALICE, Right.Write)));
+        Mailbox mailbox = new SimpleMailbox(MailboxPath.forUser(BOB, MailboxConstants.INBOX), UID_VALIDITY);
+        mailbox.setACL(new MailboxACL(new MailboxACL.Entry(MailboxFixture.ALICE, Right.Write)));
 
         assertThat(storeRightManager.hasRight(mailbox, Right.Write, aliceSession))
             .isTrue();
@@ -109,9 +102,8 @@ public class StoreRightManagerTest {
 
     @Test
     public void hasRightShouldReturnTrueWhenTheUserDoesnotOwnTheMailboxButHasAtLeastTheCorrectRightOnIt() throws MailboxException {
-        Mailbox mailbox = mock(Mailbox.class);
-        when(mailbox.getUser()).thenReturn(MailboxFixture.BOB);
-        when(mailbox.getACL()).thenReturn(new MailboxACL(new MailboxACL.Entry(MailboxFixture.ALICE, Right.Write, Right.Lookup)));
+        Mailbox mailbox = new SimpleMailbox(MailboxPath.forUser(BOB, MailboxConstants.INBOX), UID_VALIDITY);
+        mailbox.setACL(new MailboxACL(new MailboxACL.Entry(MailboxFixture.ALICE, Right.Write, Right.Lookup)));
 
         assertThat(storeRightManager.hasRight(mailbox, Right.Write, aliceSession))
             .isTrue();
@@ -119,8 +111,7 @@ public class StoreRightManagerTest {
 
     @Test
     public void hasRightShouldReturnFalseWhenTheUserDoesNotOwnTheMailboxAndHasNoRightOnIt() throws MailboxException {
-        Mailbox mailbox = mock(Mailbox.class);
-        when(mailbox.getUser()).thenReturn(MailboxFixture.BOB);
+        Mailbox mailbox = new SimpleMailbox(MailboxPath.forUser(BOB, MailboxConstants.INBOX), UID_VALIDITY);
 
         assertThat(storeRightManager.hasRight(mailbox, Right.Write, aliceSession))
             .isFalse();
@@ -128,71 +119,79 @@ public class StoreRightManagerTest {
 
     @Test
     public void isReadWriteShouldReturnTrueWhenUserHasInsertRightOnMailbox() throws Exception {
-        Mailbox mailbox = mock(Mailbox.class);
         Flags flags = new Flags();
-        when(mailbox.getACL()).thenReturn(new MailboxACL(new MailboxACL.Entry(MailboxFixture.ALICE, Right.Insert)));
+        Mailbox mailbox = new SimpleMailbox(MailboxPath.forUser(BOB, MailboxConstants.INBOX), UID_VALIDITY);
+        mailbox.setACL(new MailboxACL(new MailboxACL.Entry(MailboxFixture.ALICE, Right.Insert)));
+
         assertThat(storeRightManager.isReadWrite(aliceSession, mailbox, flags))
             .isTrue();
     }
 
     @Test
     public void isReadWriteShouldReturnTrueWhenUserHasPerformExpungeRightOnMailbox() throws Exception {
-        Mailbox mailbox = mock(Mailbox.class);
         Flags flags = new Flags();
-        when(mailbox.getACL()).thenReturn(new MailboxACL(new MailboxACL.Entry(MailboxFixture.ALICE, Right.PerformExpunge)));
+        Mailbox mailbox = new SimpleMailbox(MailboxPath.forUser(BOB, MailboxConstants.INBOX), UID_VALIDITY);
+        mailbox.setACL(new MailboxACL(new MailboxACL.Entry(MailboxFixture.ALICE, Right.PerformExpunge)));
+
         assertThat(storeRightManager.isReadWrite(aliceSession, mailbox, flags))
             .isTrue();
     }
 
     @Test
     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(MailboxFixture.ALICE, Right.DeleteMessages)));
+        Mailbox mailbox = new SimpleMailbox(MailboxPath.forUser(BOB, MailboxConstants.INBOX), UID_VALIDITY);
+        mailbox.setACL(new MailboxACL(new MailboxACL.Entry(MailboxFixture.ALICE, Right.DeleteMessages)));
+
         assertThat(storeRightManager.isReadWrite(aliceSession, mailbox, flags))
             .isTrue();
     }
 
     @Test
     public void isReadWriteShouldReturnFalseWhenUserHasDeleteMessagesRightOnMailboxButFlagsDoesNotContainDeletedFlag() throws Exception {
-        Mailbox mailbox = mock(Mailbox.class);
         Flags flags = new Flags();
-        when(mailbox.getACL()).thenReturn(new MailboxACL(new MailboxACL.Entry(MailboxFixture.ALICE, Right.DeleteMessages)));
+        Mailbox mailbox = new SimpleMailbox(MailboxPath.forUser(BOB, MailboxConstants.INBOX), UID_VALIDITY);
+        mailbox.setACL(new MailboxACL(new MailboxACL.Entry(MailboxFixture.ALICE, Right.DeleteMessages)));
+
         assertThat(storeRightManager.isReadWrite(aliceSession, mailbox, flags))
             .isFalse();
     }
 
     @Test
     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(MailboxFixture.ALICE, Right.WriteSeenFlag)));
+        Mailbox mailbox = new SimpleMailbox(MailboxPath.forUser(BOB, MailboxConstants.INBOX), UID_VALIDITY);
+        mailbox.setACL(new MailboxACL(new MailboxACL.Entry(MailboxFixture.ALICE, Right.WriteSeenFlag)));
+
         assertThat(storeRightManager.isReadWrite(aliceSession, mailbox, flags))
             .isTrue();
     }
 
     @Test
     public void isReadWriteShouldReturnFalseWhenUserHasWriteSeenFlagRightOnMailboxAndFlagsDoesNotContainSeenFlag() throws Exception {
-        Mailbox mailbox = mock(Mailbox.class);
         Flags flags = new Flags();
-        when(mailbox.getACL()).thenReturn(new MailboxACL(new MailboxACL.Entry(MailboxFixture.ALICE, Right.WriteSeenFlag)));
+        Mailbox mailbox = new SimpleMailbox(MailboxPath.forUser(BOB, MailboxConstants.INBOX), UID_VALIDITY);
+        mailbox.setACL(new MailboxACL(new MailboxACL.Entry(MailboxFixture.ALICE, Right.WriteSeenFlag)));
+
         assertThat(storeRightManager.isReadWrite(aliceSession, mailbox, flags))
             .isFalse();
     }
 
     @Test
     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(MailboxFixture.ALICE, Right.Write)));
+        Mailbox mailbox = new SimpleMailbox(MailboxPath.forUser(BOB, MailboxConstants.INBOX), UID_VALIDITY);
+        mailbox.setACL(new MailboxACL(new MailboxACL.Entry(MailboxFixture.ALICE, Right.Write)));
+
         assertThat(storeRightManager.isReadWrite(aliceSession, mailbox, flags))
             .isTrue();
     }
 
     @Test
     public void isReadWriteShouldReturnFalseWhenUserDoesNotHaveInsertOrPerformExpungeRightOnMailboxAndNullFlag() throws Exception {
-        Mailbox mailbox = mock(Mailbox.class);
-        when(mailbox.getACL()).thenReturn(new MailboxACL(new MailboxACL.Entry(MailboxFixture.ALICE, Right.Administer)));
+        Mailbox mailbox = new SimpleMailbox(MailboxPath.forUser(BOB, MailboxConstants.INBOX), UID_VALIDITY);
+        mailbox.setACL(new MailboxACL(new MailboxACL.Entry(MailboxFixture.ALICE, Right.Administer)));
+
         assertThat(storeRightManager.isReadWrite(aliceSession, mailbox, new Flags()))
             .isFalse();
     }
@@ -203,7 +202,7 @@ public class StoreRightManagerTest {
             .apply(MailboxACL.command().rights(Right.Read, Right.Write).forUser(BOB).asAddition())
             .apply(MailboxACL.command().rights(Right.Read, Right.Write, Right.Administer).forUser(CEDRIC).asAddition());
         MailboxACL actual = StoreRightManager.filteredForSession(
-            new SimpleMailbox(INBOX_ALICE, UID_VALIDITY), acl, new MockMailboxSession(ALICE));
+            new SimpleMailbox(INBOX_ALICE, UID_VALIDITY), acl, aliceSession);
         assertThat(actual).isEqualTo(acl);
     }
 


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