Repository: james-project Updated Branches: refs/heads/master 99c181977 -> f1d6f12f6
PROTOCOLS-117 General fixes: Don't rely on JMock for ACL related IMAP tests Mockito is more robust to future changes, and leads to more readable code. Future commits would have broke these tests. Project: http://git-wip-us.apache.org/repos/asf/james-project/repo Commit: http://git-wip-us.apache.org/repos/asf/james-project/commit/8ffa76db Tree: http://git-wip-us.apache.org/repos/asf/james-project/tree/8ffa76db Diff: http://git-wip-us.apache.org/repos/asf/james-project/diff/8ffa76db Branch: refs/heads/master Commit: 8ffa76dbf5552d01ce2314e3ea45e6494df3f2a3 Parents: 1cdb6b7 Author: benwa <[email protected]> Authored: Tue Oct 31 10:27:35 2017 +0700 Committer: benwa <[email protected]> Committed: Fri Nov 3 09:34:03 2017 +0700 ---------------------------------------------------------------------- .../imap/processor/DeleteACLProcessorTest.java | 236 ++++++++--------- .../imap/processor/GetACLProcessorTest.java | 218 +++++++--------- .../imap/processor/ListRightsProcessorTest.java | 218 +++++++--------- .../imap/processor/SetACLProcessorTest.java | 254 ++++++++----------- 4 files changed, 387 insertions(+), 539 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/james-project/blob/8ffa76db/protocols/imap/src/test/java/org/apache/james/imap/processor/DeleteACLProcessorTest.java ---------------------------------------------------------------------- diff --git a/protocols/imap/src/test/java/org/apache/james/imap/processor/DeleteACLProcessorTest.java b/protocols/imap/src/test/java/org/apache/james/imap/processor/DeleteACLProcessorTest.java index 4228736..aaa908d 100644 --- a/protocols/imap/src/test/java/org/apache/james/imap/processor/DeleteACLProcessorTest.java +++ b/protocols/imap/src/test/java/org/apache/james/imap/processor/DeleteACLProcessorTest.java @@ -19,11 +19,20 @@ package org.apache.james.imap.processor; +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.Matchers.any; +import static org.mockito.Matchers.anyBoolean; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.verifyNoMoreInteractions; +import static org.mockito.Mockito.when; + import org.apache.james.imap.api.ImapCommand; import org.apache.james.imap.api.ImapConstants; import org.apache.james.imap.api.ImapSessionState; import org.apache.james.imap.api.ImapSessionUtils; -import org.apache.james.imap.api.message.response.StatusResponse; +import org.apache.james.imap.api.message.response.ImapResponseMessage; import org.apache.james.imap.api.process.ImapProcessor; import org.apache.james.imap.api.process.ImapProcessor.Responder; import org.apache.james.imap.api.process.ImapSession; @@ -41,11 +50,9 @@ import org.apache.james.mailbox.model.MailboxACL; import org.apache.james.mailbox.model.MailboxACL.EntryKey; import org.apache.james.mailbox.model.MailboxPath; import org.apache.james.metrics.api.NoopMetricFactory; -import org.jmock.Expectations; -import org.jmock.Mockery; -import org.jmock.integration.junit4.JUnit4Mockery; import org.junit.Before; import org.junit.Test; +import org.mockito.ArgumentCaptor; /** * DeleteACLProcessor Test. @@ -57,163 +64,126 @@ public class DeleteACLProcessorTest { private static final String MAILBOX_NAME = ImapConstants.INBOX_NAME; private static final String USER_1 = "user1"; - ImapSession imapSessionStub; - MailboxManager mailboxManagerStub; - MailboxSession mailboxSessionStub; - MessageManager messageManagerStub; - MetaData metaDataStub; - Mockery mockery = new JUnit4Mockery(); - DeleteACLRequest deleteACLRequest; - UnpooledStatusResponseFactory statusResponseFactory; - DeleteACLProcessor subject; - User user1Stub; - EntryKey user1Key; - MailboxPath path; - - private Expectations prepareRightsExpectations() throws MailboxException { - return new Expectations() { - { - - allowing(imapSessionStub).getAttribute(ImapSessionUtils.MAILBOX_SESSION_ATTRIBUTE_SESSION_KEY); - will(returnValue(mailboxSessionStub)); - - allowing(imapSessionStub).getState(); - will(returnValue(ImapSessionState.AUTHENTICATED)); - - allowing(mailboxSessionStub).getUser(); - will(returnValue(user1Stub)); - - allowing(user1Stub).getUserName(); - will(returnValue(USER_1)); - - allowing(mailboxManagerStub).startProcessingRequest(with(same(mailboxSessionStub))); - allowing(mailboxManagerStub).endProcessingRequest(with(same(mailboxSessionStub))); - - allowing(messageManagerStub).getMetaData(with(any(Boolean.class)), with(same(mailboxSessionStub)), with(any(FetchGroup.class))); - will(returnValue(metaDataStub)); - - } - }; - } + private ImapSession imapSession; + private MailboxManager mailboxManager; + private MailboxSession mailboxSession; + private MetaData metaData; + private DeleteACLRequest deleteACLRequest; + private DeleteACLProcessor subject; + private EntryKey user1Key; + private MailboxPath path; + private Responder responder; + private ArgumentCaptor<ImapResponseMessage> argumentCaptor; @Before public void setUp() throws Exception { path = MailboxPath.forUser(USER_1, MAILBOX_NAME); - statusResponseFactory = new UnpooledStatusResponseFactory(); - mailboxManagerStub = mockery.mock(MailboxManager.class); - subject = new DeleteACLProcessor(mockery.mock(ImapProcessor.class), mailboxManagerStub, statusResponseFactory, new NoopMetricFactory()); - imapSessionStub = mockery.mock(ImapSession.class); - mailboxSessionStub = mockery.mock(MailboxSession.class); - user1Stub = mockery.mock(User.class); - messageManagerStub = mockery.mock(MessageManager.class); - metaDataStub = mockery.mock(MetaData.class); - - deleteACLRequest = new DeleteACLRequest("TAG", ImapCommand.anyStateCommand("Name"), MAILBOX_NAME, USER_1); + UnpooledStatusResponseFactory statusResponseFactory = new UnpooledStatusResponseFactory(); + mailboxManager = mock(MailboxManager.class); + subject = new DeleteACLProcessor(mock(ImapProcessor.class), mailboxManager, statusResponseFactory, new NoopMetricFactory()); + imapSession = mock(ImapSession.class); + mailboxSession = mock(MailboxSession.class); + User user1 = mock(User.class); + MessageManager messageManager = mock(MessageManager.class); + metaData = mock(MetaData.class); + responder = mock(Responder.class); + + when(imapSession.getAttribute(ImapSessionUtils.MAILBOX_SESSION_ATTRIBUTE_SESSION_KEY)) + .thenReturn(mailboxSession); + when(imapSession.getState()) + .thenReturn(ImapSessionState.AUTHENTICATED); + when(mailboxSession.getUser()) + .thenReturn(user1); + when(user1.getUserName()) + .thenReturn(USER_1); + when(messageManager.getMetaData(anyBoolean(), any(MailboxSession.class), any(FetchGroup.class))) + .thenReturn(metaData); + when(mailboxManager.getMailbox(any(MailboxPath.class), any(MailboxSession.class))) + .thenReturn(messageManager); + + deleteACLRequest = new DeleteACLRequest("TAG", + ImapCommand.anyStateCommand("Name"), + MAILBOX_NAME, + USER_1); user1Key = EntryKey.deserialize(USER_1); + + argumentCaptor = ArgumentCaptor.forClass(ImapResponseMessage.class); } @Test public void testNoListRight() throws Exception { + when(mailboxManager.hasRight(path, MailboxACL.Right.Lookup, mailboxSession)) + .thenReturn(false); - Expectations expectations = prepareRightsExpectations(); + subject.doProcess(deleteACLRequest, responder, imapSession); - expectations.allowing(mailboxManagerStub).hasRight(expectations.with(path), expectations.with(Expectations.equal(MailboxACL.Right.Lookup)), expectations.with(Expectations.same(mailboxSessionStub))); - expectations.will(Expectations.returnValue(false)); - - expectations.allowing(mailboxManagerStub).getMailbox(expectations.with(Expectations.any(MailboxPath.class)), expectations.with(Expectations.any(MailboxSession.class))); - expectations.will(Expectations.returnValue(messageManagerStub)); - - mockery.checking(expectations); - - final Responder responderMock = mockery.mock(Responder.class); - mockery.checking(new Expectations() { - { - oneOf(responderMock).respond(with(new StatusResponseTypeMatcher(StatusResponse.Type.NO))); - } - }); - - subject.doProcess(deleteACLRequest, responderMock, imapSessionStub); + verify(responder, times(1)).respond(argumentCaptor.capture()); + verifyNoMoreInteractions(responder); + assertThat(argumentCaptor.getAllValues()) + .hasSize(1); + assertThat(argumentCaptor.getAllValues().get(0)) + .matches(StatusResponseTypeMatcher.NO_RESPONSE_MATCHER::matches); } - + @Test public void testNoAdminRight() throws Exception { + when(mailboxManager.hasRight(path, MailboxACL.Right.Lookup, mailboxSession)) + .thenReturn(true); + when(mailboxManager.hasRight(path, MailboxACL.Right.Administer, mailboxSession)) + .thenReturn(false); - Expectations expectations = prepareRightsExpectations(); - expectations.allowing(mailboxManagerStub).hasRight(expectations.with(path), expectations.with(Expectations.equal(MailboxACL.Right.Lookup)), expectations.with(Expectations.same(mailboxSessionStub))); - expectations.will(Expectations.returnValue(true)); + subject.doProcess(deleteACLRequest, responder, imapSession); - expectations.allowing(mailboxManagerStub).hasRight(expectations.with(path), expectations.with(Expectations.equal(MailboxACL.Right.Administer)), expectations.with(Expectations.same(mailboxSessionStub))); - expectations.will(Expectations.returnValue(false)); + verify(responder, times(1)).respond(argumentCaptor.capture()); + verifyNoMoreInteractions(responder); - expectations.allowing(mailboxManagerStub).getMailbox(expectations.with(Expectations.any(MailboxPath.class)), expectations.with(Expectations.any(MailboxSession.class))); - expectations.will(Expectations.returnValue(messageManagerStub)); + assertThat(argumentCaptor.getAllValues()) + .hasSize(1); + assertThat(argumentCaptor.getAllValues().get(0)) + .matches(StatusResponseTypeMatcher.NO_RESPONSE_MATCHER::matches); + } - mockery.checking(expectations); + @Test + public void testNonExistentMailboxName() throws Exception { + when(mailboxManager.getMailbox(any(MailboxPath.class), any(MailboxSession.class))) + .thenThrow(new MailboxNotFoundException("")); - final Responder responderMock = mockery.mock(Responder.class); - mockery.checking(new Expectations() { - { - oneOf(responderMock).respond(with(new StatusResponseTypeMatcher(StatusResponse.Type.NO))); - } - }); + subject.doProcess(deleteACLRequest, responder, imapSession); - subject.doProcess(deleteACLRequest, responderMock, imapSessionStub); + verify(responder, times(1)).respond(argumentCaptor.capture()); + verifyNoMoreInteractions(responder); - } - - @Test - public void testInexistentMailboxName() throws Exception { - Expectations expectations = prepareRightsExpectations(); - - expectations.allowing(mailboxManagerStub).getMailbox(expectations.with(Expectations.any(MailboxPath.class)), expectations.with(Expectations.any(MailboxSession.class))); - expectations.will(Expectations.throwException(new MailboxNotFoundException(path))); - - mockery.checking(expectations); - - final Responder responderMock = mockery.mock(Responder.class); - mockery.checking(new Expectations() { - { - oneOf(responderMock).respond(with(new StatusResponseTypeMatcher(StatusResponse.Type.NO))); - } - }); - - subject.doProcess(deleteACLRequest, responderMock, imapSessionStub); + assertThat(argumentCaptor.getAllValues()) + .hasSize(1); + assertThat(argumentCaptor.getAllValues().get(0)) + .matches(StatusResponseTypeMatcher.NO_RESPONSE_MATCHER::matches); } @Test public void testDelete() throws MailboxException { - final MailboxACL acl = MailboxACL.OWNER_FULL_ACL; - - Expectations expectations = prepareRightsExpectations(); - - expectations.allowing(mailboxManagerStub).getMailbox(expectations.with(Expectations.any(MailboxPath.class)), expectations.with(Expectations.any(MailboxSession.class))); - expectations.will(Expectations.returnValue(messageManagerStub)); - - expectations.allowing(mailboxManagerStub).hasRight(expectations.with(path), expectations.with(Expectations.equal(MailboxACL.Right.Lookup)), expectations.with(Expectations.same(mailboxSessionStub))); - expectations.will(Expectations.returnValue(true)); - - expectations.allowing(mailboxManagerStub).hasRight(expectations.with(path), expectations.with(Expectations.equal(MailboxACL.Right.Administer)), expectations.with(Expectations.same(mailboxSessionStub))); - expectations.will(Expectations.returnValue(true)); - - expectations.allowing(mailboxManagerStub).applyRightsCommand(expectations.with(path), expectations.with(MailboxACL.command().key(user1Key).noRights().asReplacement()), expectations.with(mailboxSessionStub)); - - expectations.allowing(metaDataStub).getACL(); - expectations.will(Expectations.returnValue(acl)); - - mockery.checking(expectations); - - final Responder responderMock = mockery.mock(Responder.class); - mockery.checking(new Expectations() { - { - oneOf(responderMock).respond(with(new StatusResponseTypeMatcher(StatusResponse.Type.OK))); - } - }); - - DeleteACLRequest r = new DeleteACLRequest("TAG", ImapCommand.anyStateCommand("Name"), MAILBOX_NAME, USER_1); - subject.doProcess(r, responderMock, imapSessionStub); + MailboxACL acl = MailboxACL.OWNER_FULL_ACL; + when(mailboxManager.hasRight(path, MailboxACL.Right.Lookup, mailboxSession)) + .thenReturn(true); + when(mailboxManager.hasRight(path, MailboxACL.Right.Administer, mailboxSession)) + .thenReturn(true); + when(metaData.getACL()).thenReturn(acl); + + subject.doProcess(deleteACLRequest, responder, imapSession); + + verify(mailboxManager).applyRightsCommand(path, + MailboxACL.command().key(user1Key).noRights().asReplacement(), + mailboxSession); + + verify(responder, times(1)).respond(argumentCaptor.capture()); + verifyNoMoreInteractions(responder); + + assertThat(argumentCaptor.getAllValues()) + .hasSize(1); + assertThat(argumentCaptor.getAllValues().get(0)) + .matches(StatusResponseTypeMatcher.OK_RESPONSE_MATCHER::matches); } } http://git-wip-us.apache.org/repos/asf/james-project/blob/8ffa76db/protocols/imap/src/test/java/org/apache/james/imap/processor/GetACLProcessorTest.java ---------------------------------------------------------------------- diff --git a/protocols/imap/src/test/java/org/apache/james/imap/processor/GetACLProcessorTest.java b/protocols/imap/src/test/java/org/apache/james/imap/processor/GetACLProcessorTest.java index 40c47f0..5e46bfa 100644 --- a/protocols/imap/src/test/java/org/apache/james/imap/processor/GetACLProcessorTest.java +++ b/protocols/imap/src/test/java/org/apache/james/imap/processor/GetACLProcessorTest.java @@ -19,11 +19,20 @@ package org.apache.james.imap.processor; +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.Matchers.any; +import static org.mockito.Matchers.anyBoolean; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.verifyNoMoreInteractions; +import static org.mockito.Mockito.when; + import org.apache.james.imap.api.ImapCommand; import org.apache.james.imap.api.ImapConstants; import org.apache.james.imap.api.ImapSessionState; import org.apache.james.imap.api.ImapSessionUtils; -import org.apache.james.imap.api.message.response.StatusResponse; +import org.apache.james.imap.api.message.response.ImapResponseMessage; import org.apache.james.imap.api.process.ImapProcessor; import org.apache.james.imap.api.process.ImapProcessor.Responder; import org.apache.james.imap.api.process.ImapSession; @@ -36,16 +45,13 @@ import org.apache.james.mailbox.MailboxSession.User; import org.apache.james.mailbox.MessageManager; import org.apache.james.mailbox.MessageManager.MetaData; import org.apache.james.mailbox.MessageManager.MetaData.FetchGroup; -import org.apache.james.mailbox.exception.MailboxException; import org.apache.james.mailbox.exception.MailboxNotFoundException; import org.apache.james.mailbox.model.MailboxACL; import org.apache.james.mailbox.model.MailboxPath; import org.apache.james.metrics.api.NoopMetricFactory; -import org.jmock.Expectations; -import org.jmock.Mockery; -import org.jmock.integration.junit4.JUnit4Mockery; import org.junit.Before; import org.junit.Test; +import org.mockito.ArgumentCaptor; /** * GetACLProcessor Test. @@ -56,160 +62,118 @@ public class GetACLProcessorTest { private static final String MAILBOX_NAME = ImapConstants.INBOX_NAME; private static final String USER_1 = "user1"; - ImapSession imapSessionStub; - MailboxManager mailboxManagerStub; - MailboxSession mailboxSessionStub; - MessageManager messageManagerStub; - MetaData metaDataStub; - Mockery mockery = new JUnit4Mockery(); - GetACLRequest getACLRequest; - UnpooledStatusResponseFactory statusResponseFactory; - GetACLProcessor subject; - User user1Stub; - MailboxPath path; - - private Expectations prepareRightsExpectations() throws MailboxException { - return new Expectations() { - { - - allowing(imapSessionStub).getAttribute(ImapSessionUtils.MAILBOX_SESSION_ATTRIBUTE_SESSION_KEY); - will(returnValue(mailboxSessionStub)); - - allowing(imapSessionStub).getState(); - will(returnValue(ImapSessionState.AUTHENTICATED)); - - allowing(mailboxSessionStub).getUser(); - will(returnValue(user1Stub)); - - allowing(user1Stub).getUserName(); - will(returnValue(USER_1)); - - allowing(mailboxManagerStub).startProcessingRequest(with(same(mailboxSessionStub))); - allowing(mailboxManagerStub).endProcessingRequest(with(same(mailboxSessionStub))); - - allowing(messageManagerStub).getMetaData(with(any(Boolean.class)), with(same(mailboxSessionStub)), with(any(FetchGroup.class))); - will(returnValue(metaDataStub)); - - } - }; - } + private ImapSession imapSession; + private MailboxManager mailboxManager; + private MailboxSession mailboxSession; + private MetaData metaData; + private GetACLRequest getACLRequest; + private GetACLProcessor subject; + private MailboxPath path; + private ArgumentCaptor<ImapResponseMessage> argumentCaptor; + private Responder responder; @Before public void setUp() throws Exception { path = MailboxPath.forUser(USER_1, MAILBOX_NAME); - statusResponseFactory = new UnpooledStatusResponseFactory(); - mailboxManagerStub = mockery.mock(MailboxManager.class); - subject = new GetACLProcessor(mockery.mock(ImapProcessor.class), mailboxManagerStub, statusResponseFactory, new NoopMetricFactory()); - imapSessionStub = mockery.mock(ImapSession.class); - mailboxSessionStub = mockery.mock(MailboxSession.class); - user1Stub = mockery.mock(User.class); - messageManagerStub = mockery.mock(MessageManager.class); - metaDataStub = mockery.mock(MetaData.class); + UnpooledStatusResponseFactory statusResponseFactory = new UnpooledStatusResponseFactory(); + mailboxManager = mock(MailboxManager.class); + subject = new GetACLProcessor(mock(ImapProcessor.class), mailboxManager, statusResponseFactory, new NoopMetricFactory()); + imapSession = mock(ImapSession.class); + mailboxSession = mock(MailboxSession.class); + User user1 = mock(User.class); + MessageManager messageManager = mock(MessageManager.class); + metaData = mock(MetaData.class); + responder = mock(Responder.class); getACLRequest = new GetACLRequest("TAG", ImapCommand.anyStateCommand("Name"), MAILBOX_NAME); + when(imapSession.getAttribute(ImapSessionUtils.MAILBOX_SESSION_ATTRIBUTE_SESSION_KEY)) + .thenReturn(mailboxSession); + when(imapSession.getState()) + .thenReturn(ImapSessionState.AUTHENTICATED); + when(mailboxSession.getUser()) + .thenReturn(user1); + when(user1.getUserName()) + .thenReturn(USER_1); + when(messageManager.getMetaData(anyBoolean(), any(MailboxSession.class), any(FetchGroup.class))) + .thenReturn(metaData); + when(mailboxManager.getMailbox(any(MailboxPath.class), any(MailboxSession.class))) + .thenReturn(messageManager); + + argumentCaptor = ArgumentCaptor.forClass(ImapResponseMessage.class); } @Test public void testNoListRight() throws Exception { + when(mailboxManager.hasRight(path, MailboxACL.Right.Lookup, mailboxSession)) + .thenReturn(false); - Expectations expectations = prepareRightsExpectations(); - expectations.allowing(mailboxManagerStub).hasRight(expectations.with(path), expectations.with(Expectations.equal(MailboxACL.Right.Lookup)), expectations.with(Expectations.same(mailboxSessionStub))); - expectations.will(Expectations.returnValue(false)); - - expectations.allowing(mailboxManagerStub).getMailbox(expectations.with(Expectations.any(MailboxPath.class)), expectations.with(Expectations.any(MailboxSession.class))); - expectations.will(Expectations.returnValue(messageManagerStub)); - - mockery.checking(expectations); + subject.doProcess(getACLRequest, responder, imapSession); - final Responder responderMock = mockery.mock(Responder.class); - mockery.checking(new Expectations() { - { - oneOf(responderMock).respond(with(new StatusResponseTypeMatcher(StatusResponse.Type.NO))); - } - }); - - subject.doProcess(getACLRequest, responderMock, imapSessionStub); + verify(responder, times(1)).respond(argumentCaptor.capture()); + verifyNoMoreInteractions(responder); + assertThat(argumentCaptor.getAllValues()) + .hasSize(1); + assertThat(argumentCaptor.getAllValues().get(0)) + .matches(StatusResponseTypeMatcher.NO_RESPONSE_MATCHER::matches); } @Test public void testNoAdminRight() throws Exception { + when(mailboxManager.hasRight(path, MailboxACL.Right.Lookup, mailboxSession)) + .thenReturn(true); + when(mailboxManager.hasRight(path, MailboxACL.Right.Administer, mailboxSession)) + .thenReturn(false); - Expectations expectations = prepareRightsExpectations(); - expectations.allowing(mailboxManagerStub).hasRight(expectations.with(path), expectations.with(Expectations.equal(MailboxACL.Right.Lookup)), expectations.with(Expectations.same(mailboxSessionStub))); - expectations.will(Expectations.returnValue(true)); - - expectations.allowing(mailboxManagerStub).hasRight(expectations.with(path), expectations.with(Expectations.equal(MailboxACL.Right.Administer)), expectations.with(Expectations.same(mailboxSessionStub))); - expectations.will(Expectations.returnValue(false)); + subject.doProcess(getACLRequest, responder, imapSession); - expectations.allowing(mailboxManagerStub).getMailbox(expectations.with(Expectations.any(MailboxPath.class)), expectations.with(Expectations.any(MailboxSession.class))); - expectations.will(Expectations.returnValue(messageManagerStub)); - - mockery.checking(expectations); - - final Responder responderMock = mockery.mock(Responder.class); - mockery.checking(new Expectations() { - { - oneOf(responderMock).respond(with(new StatusResponseTypeMatcher(StatusResponse.Type.NO))); - } - }); - - subject.doProcess(getACLRequest, responderMock, imapSessionStub); + verify(responder, times(1)).respond(argumentCaptor.capture()); + verifyNoMoreInteractions(responder); + assertThat(argumentCaptor.getAllValues()) + .hasSize(1); + assertThat(argumentCaptor.getAllValues().get(0)) + .matches(StatusResponseTypeMatcher.NO_RESPONSE_MATCHER::matches); } @Test public void testInexistentMailboxName() throws Exception { - Expectations expectations = prepareRightsExpectations(); - - expectations.allowing(mailboxManagerStub).getMailbox(expectations.with(Expectations.any(MailboxPath.class)), expectations.with(Expectations.any(MailboxSession.class))); - expectations.will(Expectations.throwException(new MailboxNotFoundException(path))); + when(mailboxManager.getMailbox(any(MailboxPath.class), any(MailboxSession.class))) + .thenThrow(new MailboxNotFoundException("")); - mockery.checking(expectations); + subject.doProcess(getACLRequest, responder, imapSession); - final Responder responderMock = mockery.mock(Responder.class); - mockery.checking(new Expectations() { - { - oneOf(responderMock).respond(with(new StatusResponseTypeMatcher(StatusResponse.Type.NO))); - } - }); + verify(responder, times(1)).respond(argumentCaptor.capture()); + verifyNoMoreInteractions(responder); - subject.doProcess(getACLRequest, responderMock, imapSessionStub); + assertThat(argumentCaptor.getAllValues()) + .hasSize(1); + assertThat(argumentCaptor.getAllValues().get(0)) + .matches(StatusResponseTypeMatcher.NO_RESPONSE_MATCHER::matches); } @Test public void testSufficientRights() throws Exception { - - final MailboxACL acl = MailboxACL.OWNER_FULL_ACL; - - Expectations expectations = prepareRightsExpectations(); - - expectations.allowing(mailboxManagerStub).getMailbox(expectations.with(Expectations.any(MailboxPath.class)), expectations.with(Expectations.any(MailboxSession.class))); - expectations.will(Expectations.returnValue(messageManagerStub)); - - expectations.allowing(mailboxManagerStub).hasRight(expectations.with(path), expectations.with(Expectations.equal(MailboxACL.Right.Lookup)), expectations.with(Expectations.same(mailboxSessionStub))); - expectations.will(Expectations.returnValue(true)); - - expectations.allowing(mailboxManagerStub).hasRight(expectations.with(path), expectations.with(Expectations.equal(MailboxACL.Right.Administer)), expectations.with(Expectations.same(mailboxSessionStub))); - expectations.will(Expectations.returnValue(true)); - - - expectations.allowing(metaDataStub).getACL(); - expectations.will(Expectations.returnValue(acl)); - - mockery.checking(expectations); - - final ACLResponse response = new ACLResponse(MAILBOX_NAME, acl); - final Responder responderMock = mockery.mock(Responder.class); - mockery.checking(new Expectations() { - { - oneOf(responderMock).respond(with(equal(response))); - oneOf(responderMock).respond(with(new StatusResponseTypeMatcher(StatusResponse.Type.OK))); - } - }); - - subject.doProcess(getACLRequest, responderMock, imapSessionStub); + MailboxACL acl = MailboxACL.OWNER_FULL_ACL; + when(mailboxManager.hasRight(path, MailboxACL.Right.Lookup, mailboxSession)) + .thenReturn(true); + when(mailboxManager.hasRight(path, MailboxACL.Right.Administer, mailboxSession)) + .thenReturn(true); + when(metaData.getACL()).thenReturn(acl); + + ACLResponse response = new ACLResponse(MAILBOX_NAME, acl); + subject.doProcess(getACLRequest, responder, imapSession); + + verify(responder, times(2)).respond(argumentCaptor.capture()); + verifyNoMoreInteractions(responder); + + assertThat(argumentCaptor.getAllValues()) + .hasSize(2); + assertThat(argumentCaptor.getAllValues().get(0)) + .isEqualTo(response); + assertThat(argumentCaptor.getAllValues().get(1)) + .matches(StatusResponseTypeMatcher.OK_RESPONSE_MATCHER::matches); } } http://git-wip-us.apache.org/repos/asf/james-project/blob/8ffa76db/protocols/imap/src/test/java/org/apache/james/imap/processor/ListRightsProcessorTest.java ---------------------------------------------------------------------- diff --git a/protocols/imap/src/test/java/org/apache/james/imap/processor/ListRightsProcessorTest.java b/protocols/imap/src/test/java/org/apache/james/imap/processor/ListRightsProcessorTest.java index d14c900..f9844db 100644 --- a/protocols/imap/src/test/java/org/apache/james/imap/processor/ListRightsProcessorTest.java +++ b/protocols/imap/src/test/java/org/apache/james/imap/processor/ListRightsProcessorTest.java @@ -19,11 +19,20 @@ package org.apache.james.imap.processor; +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.Matchers.any; +import static org.mockito.Matchers.anyBoolean; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.verifyNoMoreInteractions; +import static org.mockito.Mockito.when; + import org.apache.james.imap.api.ImapCommand; import org.apache.james.imap.api.ImapConstants; import org.apache.james.imap.api.ImapSessionState; import org.apache.james.imap.api.ImapSessionUtils; -import org.apache.james.imap.api.message.response.StatusResponse; +import org.apache.james.imap.api.message.response.ImapResponseMessage; import org.apache.james.imap.api.process.ImapProcessor; import org.apache.james.imap.api.process.ImapProcessor.Responder; import org.apache.james.imap.api.process.ImapSession; @@ -35,7 +44,6 @@ import org.apache.james.mailbox.MailboxSession; import org.apache.james.mailbox.MailboxSession.User; import org.apache.james.mailbox.MessageManager; import org.apache.james.mailbox.MessageManager.MetaData; -import org.apache.james.mailbox.MessageManager.MetaData.FetchGroup; import org.apache.james.mailbox.exception.MailboxException; import org.apache.james.mailbox.exception.MailboxNotFoundException; import org.apache.james.mailbox.model.MailboxACL; @@ -43,11 +51,9 @@ import org.apache.james.mailbox.model.MailboxACL.EntryKey; import org.apache.james.mailbox.model.MailboxACL.Rfc4314Rights; import org.apache.james.mailbox.model.MailboxPath; import org.apache.james.metrics.api.NoopMetricFactory; -import org.jmock.Expectations; -import org.jmock.Mockery; -import org.jmock.integration.junit4.JUnit4Mockery; import org.junit.Before; import org.junit.Test; +import org.mockito.ArgumentCaptor; /** * ListRightsProcessor Test. @@ -59,57 +65,45 @@ public class ListRightsProcessorTest { private static final String MAILBOX_NAME = ImapConstants.INBOX_NAME; private static final String USER_1 = "user1"; - ImapSession imapSessionStub; - MailboxManager mailboxManagerStub; - MailboxSession mailboxSessionStub; - MessageManager messageManagerStub; - MetaData metaDataStub; - Mockery mockery = new JUnit4Mockery(); - ListRightsRequest listRightsRequest; - UnpooledStatusResponseFactory statusResponseFactory; - ListRightsProcessor subject; - User user1Stub; - EntryKey user1Key; - Rfc4314Rights[] listRights; - MailboxPath path; - - private Expectations prepareRightsExpectations() throws MailboxException { - return new Expectations() { - { - - allowing(imapSessionStub).getAttribute(ImapSessionUtils.MAILBOX_SESSION_ATTRIBUTE_SESSION_KEY); - will(returnValue(mailboxSessionStub)); - - allowing(imapSessionStub).getState(); - will(returnValue(ImapSessionState.AUTHENTICATED)); - - allowing(mailboxSessionStub).getUser(); - will(returnValue(user1Stub)); - - allowing(user1Stub).getUserName(); - will(returnValue(USER_1)); - - allowing(mailboxManagerStub).startProcessingRequest(with(same(mailboxSessionStub))); - allowing(mailboxManagerStub).endProcessingRequest(with(same(mailboxSessionStub))); - - allowing(messageManagerStub).getMetaData(with(any(Boolean.class)), with(same(mailboxSessionStub)), with(any(FetchGroup.class))); - will(returnValue(metaDataStub)); - - } - }; - } + private ImapSession imapSession; + private MailboxManager mailboxManager; + private MailboxSession mailboxSession; + private MetaData metaData; + private ListRightsRequest listRightsRequest; + private ListRightsProcessor subject; + private EntryKey user1Key; + private Rfc4314Rights[] listRights; + private MailboxPath path; + private Responder responder; + private ArgumentCaptor<ImapResponseMessage> argumentCaptor; @Before public void setUp() throws Exception { path = MailboxPath.forUser(USER_1, MAILBOX_NAME); - statusResponseFactory = new UnpooledStatusResponseFactory(); - mailboxManagerStub = mockery.mock(MailboxManager.class); - subject = new ListRightsProcessor(mockery.mock(ImapProcessor.class), mailboxManagerStub, statusResponseFactory, new NoopMetricFactory()); - imapSessionStub = mockery.mock(ImapSession.class); - mailboxSessionStub = mockery.mock(MailboxSession.class); - user1Stub = mockery.mock(User.class); - messageManagerStub = mockery.mock(MessageManager.class); - metaDataStub = mockery.mock(MetaData.class); + UnpooledStatusResponseFactory statusResponseFactory = new UnpooledStatusResponseFactory(); + mailboxManager = mock(MailboxManager.class); + subject = new ListRightsProcessor(mock(ImapProcessor.class), mailboxManager, statusResponseFactory, new NoopMetricFactory()); + imapSession = mock(ImapSession.class); + mailboxSession = mock(MailboxSession.class); + User user1 = mock(User.class); + MessageManager messageManager = mock(MessageManager.class); + metaData = mock(MetaData.class); + responder = mock(Responder.class); + + argumentCaptor = ArgumentCaptor.forClass(ImapResponseMessage.class); + + when(imapSession.getAttribute(ImapSessionUtils.MAILBOX_SESSION_ATTRIBUTE_SESSION_KEY)) + .thenReturn(mailboxSession); + when(imapSession.getState()) + .thenReturn(ImapSessionState.AUTHENTICATED); + when(mailboxSession.getUser()) + .thenReturn(user1); + when(user1.getUserName()) + .thenReturn(USER_1); + when(messageManager.getMetaData(anyBoolean(), any(MailboxSession.class), any(MetaData.FetchGroup.class))) + .thenReturn(metaData); + when(mailboxManager.getMailbox(any(MailboxPath.class), any(MailboxSession.class))) + .thenReturn(messageManager); listRightsRequest = new ListRightsRequest("TAG", ImapCommand.anyStateCommand("Name"), MAILBOX_NAME, USER_1); @@ -119,106 +113,80 @@ public class ListRightsProcessorTest { @Test public void testNoListRight() throws Exception { + when(mailboxManager.hasRight(path, MailboxACL.Right.Lookup, mailboxSession)) + .thenReturn(false); - Expectations expectations = prepareRightsExpectations(); - expectations.allowing(mailboxManagerStub).hasRight(expectations.with(path), expectations.with(Expectations.equal(MailboxACL.Right.Lookup)), expectations.with(Expectations.same(mailboxSessionStub))); - expectations.will(Expectations.returnValue(false)); - - expectations.allowing(mailboxManagerStub).getMailbox(expectations.with(Expectations.any(MailboxPath.class)), expectations.with(Expectations.any(MailboxSession.class))); - expectations.will(Expectations.returnValue(messageManagerStub)); + subject.doProcess(listRightsRequest, responder, imapSession); - mockery.checking(expectations); - - final Responder responderMock = mockery.mock(Responder.class); - mockery.checking(new Expectations() { - { - oneOf(responderMock).respond(with(new StatusResponseTypeMatcher(StatusResponse.Type.NO))); - } - }); - - subject.doProcess(listRightsRequest, responderMock, imapSessionStub); + verify(responder, times(1)).respond(argumentCaptor.capture()); + verifyNoMoreInteractions(responder); + assertThat(argumentCaptor.getAllValues()) + .hasSize(1); + assertThat(argumentCaptor.getAllValues().get(0)) + .matches(StatusResponseTypeMatcher.NO_RESPONSE_MATCHER::matches); } @Test public void testNoAdminRight() throws Exception { + when(mailboxManager.hasRight(path, MailboxACL.Right.Lookup, mailboxSession)) + .thenReturn(true); + when(mailboxManager.hasRight(path, MailboxACL.Right.Administer, mailboxSession)) + .thenReturn(false); - Expectations expectations = prepareRightsExpectations(); - expectations.allowing(mailboxManagerStub).hasRight(expectations.with(path), expectations.with(Expectations.equal(MailboxACL.Right.Lookup)), expectations.with(Expectations.same(mailboxSessionStub))); - expectations.will(Expectations.returnValue(true)); - - expectations.allowing(mailboxManagerStub).hasRight(expectations.with(path), expectations.with(Expectations.equal(MailboxACL.Right.Administer)), expectations.with(Expectations.same(mailboxSessionStub))); - expectations.will(Expectations.returnValue(false)); - - expectations.allowing(mailboxManagerStub).getMailbox(expectations.with(Expectations.any(MailboxPath.class)), expectations.with(Expectations.any(MailboxSession.class))); - expectations.will(Expectations.returnValue(messageManagerStub)); - - mockery.checking(expectations); - - final Responder responderMock = mockery.mock(Responder.class); - mockery.checking(new Expectations() { - { - oneOf(responderMock).respond(with(new StatusResponseTypeMatcher(StatusResponse.Type.NO))); - } - }); + subject.doProcess(listRightsRequest, responder, imapSession); - subject.doProcess(listRightsRequest, responderMock, imapSessionStub); + verify(responder, times(1)).respond(argumentCaptor.capture()); + verifyNoMoreInteractions(responder); + assertThat(argumentCaptor.getAllValues()) + .hasSize(1); + assertThat(argumentCaptor.getAllValues().get(0)) + .matches(StatusResponseTypeMatcher.NO_RESPONSE_MATCHER::matches); } @Test public void testInexistentMailboxName() throws Exception { - Expectations expectations = prepareRightsExpectations(); - - expectations.allowing(mailboxManagerStub).getMailbox(expectations.with(Expectations.any(MailboxPath.class)), expectations.with(Expectations.any(MailboxSession.class))); - expectations.will(Expectations.throwException(new MailboxNotFoundException(path))); + when(mailboxManager.getMailbox(any(MailboxPath.class), any(MailboxSession.class))) + .thenThrow(new MailboxNotFoundException("")); - mockery.checking(expectations); + subject.doProcess(listRightsRequest, responder, imapSession); - final Responder responderMock = mockery.mock(Responder.class); - mockery.checking(new Expectations() { - { - oneOf(responderMock).respond(with(new StatusResponseTypeMatcher(StatusResponse.Type.NO))); - } - }); + verify(responder, times(1)).respond(argumentCaptor.capture()); + verifyNoMoreInteractions(responder); - subject.doProcess(listRightsRequest, responderMock, imapSessionStub); + assertThat(argumentCaptor.getAllValues()) + .hasSize(1); + assertThat(argumentCaptor.getAllValues().get(0)) + .matches(StatusResponseTypeMatcher.NO_RESPONSE_MATCHER::matches); } @Test public void testListRights() throws MailboxException { - final MailboxACL acl = MailboxACL.OWNER_FULL_ACL; - - Expectations expectations = prepareRightsExpectations(); - - expectations.allowing(mailboxManagerStub).getMailbox(expectations.with(Expectations.any(MailboxPath.class)), expectations.with(Expectations.any(MailboxSession.class))); - expectations.will(Expectations.returnValue(messageManagerStub)); - - expectations.allowing(mailboxManagerStub).hasRight(expectations.with(path), expectations.with(Expectations.equal(MailboxACL.Right.Lookup)), expectations.with(Expectations.same(mailboxSessionStub))); - expectations.will(Expectations.returnValue(true)); - - expectations.allowing(mailboxManagerStub).hasRight(expectations.with(path), expectations.with(Expectations.equal(MailboxACL.Right.Administer)), expectations.with(Expectations.same(mailboxSessionStub))); - expectations.will(Expectations.returnValue(true)); + MailboxACL acl = MailboxACL.OWNER_FULL_ACL; + when(mailboxManager.hasRight(path, MailboxACL.Right.Lookup, mailboxSession)) + .thenReturn(true); + when(mailboxManager.hasRight(path, MailboxACL.Right.Administer, mailboxSession)) + .thenReturn(true); + when(metaData.getACL()).thenReturn(acl); - expectations.allowing(mailboxManagerStub).listRigths(expectations.with(path), expectations.with(Expectations.equal(user1Key)), expectations.with(Expectations.same(mailboxSessionStub))); - expectations.will(Expectations.returnValue(listRights)); + when(mailboxManager.listRigths(path, user1Key, mailboxSession)) + .thenReturn(listRights); - expectations.allowing(metaDataStub).getACL(); - expectations.will(Expectations.returnValue(acl)); - mockery.checking(expectations); + subject.doProcess(listRightsRequest, responder, imapSession); - final ListRightsResponse response = new ListRightsResponse(MAILBOX_NAME, USER_1, listRights); - final Responder responderMock = mockery.mock(Responder.class); - mockery.checking(new Expectations() { - { - oneOf(responderMock).respond(with(equal(response))); - oneOf(responderMock).respond(with(new StatusResponseTypeMatcher(StatusResponse.Type.OK))); - } - }); + ListRightsResponse response = new ListRightsResponse(MAILBOX_NAME, USER_1, listRights); + verify(responder, times(2)).respond(argumentCaptor.capture()); + verifyNoMoreInteractions(responder); - subject.doProcess(listRightsRequest, responderMock, imapSessionStub); + assertThat(argumentCaptor.getAllValues()) + .hasSize(2); + assertThat(argumentCaptor.getAllValues().get(0)) + .isEqualTo(response); + assertThat(argumentCaptor.getAllValues().get(1)) + .matches(StatusResponseTypeMatcher.OK_RESPONSE_MATCHER::matches); } } http://git-wip-us.apache.org/repos/asf/james-project/blob/8ffa76db/protocols/imap/src/test/java/org/apache/james/imap/processor/SetACLProcessorTest.java ---------------------------------------------------------------------- diff --git a/protocols/imap/src/test/java/org/apache/james/imap/processor/SetACLProcessorTest.java b/protocols/imap/src/test/java/org/apache/james/imap/processor/SetACLProcessorTest.java index 942cc69..0e5096e 100644 --- a/protocols/imap/src/test/java/org/apache/james/imap/processor/SetACLProcessorTest.java +++ b/protocols/imap/src/test/java/org/apache/james/imap/processor/SetACLProcessorTest.java @@ -19,11 +19,20 @@ package org.apache.james.imap.processor; +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.Matchers.any; +import static org.mockito.Matchers.anyBoolean; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.verifyNoMoreInteractions; +import static org.mockito.Mockito.when; + import org.apache.james.imap.api.ImapCommand; import org.apache.james.imap.api.ImapConstants; import org.apache.james.imap.api.ImapSessionState; import org.apache.james.imap.api.ImapSessionUtils; -import org.apache.james.imap.api.message.response.StatusResponse; +import org.apache.james.imap.api.message.response.ImapResponseMessage; import org.apache.james.imap.api.process.ImapProcessor; import org.apache.james.imap.api.process.ImapProcessor.Responder; import org.apache.james.imap.api.process.ImapSession; @@ -43,11 +52,9 @@ import org.apache.james.mailbox.model.MailboxACL.EntryKey; import org.apache.james.mailbox.model.MailboxACL.Rfc4314Rights; import org.apache.james.mailbox.model.MailboxPath; import org.apache.james.metrics.api.NoopMetricFactory; -import org.jmock.Expectations; -import org.jmock.Mockery; -import org.jmock.integration.junit4.JUnit4Mockery; import org.junit.Before; import org.junit.Test; +import org.mockito.ArgumentCaptor; /** * SetACLProcessor Test. @@ -61,59 +68,46 @@ public class SetACLProcessorTest { private static final String SET_RIGHTS = "aw"; private static final String UNSUPPORTED_RIGHT = "W"; - ImapSession imapSessionStub; - MailboxManager mailboxManagerStub; - MailboxSession mailboxSessionStub; - MessageManager messageManagerStub; - MetaData metaDataStub; - Mockery mockery = new JUnit4Mockery(); - SetACLRequest replaceACLRequest; - UnpooledStatusResponseFactory statusResponseFactory; - SetACLProcessor subject; - User user1Stub; - EntryKey user1Key; - Rfc4314Rights setRights; - MailboxPath path; - - private Expectations prepareRightsExpectations() throws MailboxException { - return new Expectations() { - { - - allowing(imapSessionStub).getAttribute(ImapSessionUtils.MAILBOX_SESSION_ATTRIBUTE_SESSION_KEY); - will(returnValue(mailboxSessionStub)); - - allowing(imapSessionStub).getState(); - will(returnValue(ImapSessionState.AUTHENTICATED)); - - allowing(mailboxSessionStub).getUser(); - will(returnValue(user1Stub)); - - allowing(user1Stub).getUserName(); - will(returnValue(USER_1)); - - allowing(mailboxManagerStub).startProcessingRequest(with(same(mailboxSessionStub))); - allowing(mailboxManagerStub).endProcessingRequest(with(same(mailboxSessionStub))); - - allowing(messageManagerStub).getMetaData(with(any(Boolean.class)), with(same(mailboxSessionStub)), with(any(FetchGroup.class))); - will(returnValue(metaDataStub)); - - } - }; - } + private ImapSession imapSession; + private MailboxManager mailboxManager; + private MailboxSession mailboxSession; + private SetACLProcessor subject; + private EntryKey user1Key; + private MailboxPath path; + private Responder responder; + private ArgumentCaptor<ImapResponseMessage> argumentCaptor; + private SetACLRequest replaceAclRequest; + private Rfc4314Rights setRights; @Before public void setUp() throws Exception { path = MailboxPath.forUser(USER_1, MAILBOX_NAME); - statusResponseFactory = new UnpooledStatusResponseFactory(); - mailboxManagerStub = mockery.mock(MailboxManager.class); - subject = new SetACLProcessor(mockery.mock(ImapProcessor.class), mailboxManagerStub, statusResponseFactory, new NoopMetricFactory()); - imapSessionStub = mockery.mock(ImapSession.class); - mailboxSessionStub = mockery.mock(MailboxSession.class); - user1Stub = mockery.mock(User.class); - messageManagerStub = mockery.mock(MessageManager.class); - metaDataStub = mockery.mock(MetaData.class); - - replaceACLRequest = new SetACLRequest("TAG", ImapCommand.anyStateCommand("Name"), MAILBOX_NAME, USER_1, SET_RIGHTS); + UnpooledStatusResponseFactory statusResponseFactory = new UnpooledStatusResponseFactory(); + mailboxManager = mock(MailboxManager.class); + subject = new SetACLProcessor(mock(ImapProcessor.class), mailboxManager, statusResponseFactory, new NoopMetricFactory()); + imapSession = mock(ImapSession.class); + mailboxSession = mock(MailboxSession.class); + User user1 = mock(User.class); + MessageManager messageManager = mock(MessageManager.class); + MetaData metaData = mock(MetaData.class); + responder = mock(Responder.class); + + argumentCaptor = ArgumentCaptor.forClass(ImapResponseMessage.class); + + when(imapSession.getAttribute(ImapSessionUtils.MAILBOX_SESSION_ATTRIBUTE_SESSION_KEY)) + .thenReturn(mailboxSession); + when(imapSession.getState()) + .thenReturn(ImapSessionState.AUTHENTICATED); + when(mailboxSession.getUser()) + .thenReturn(user1); + when(user1.getUserName()) + .thenReturn(USER_1); + when(messageManager.getMetaData(anyBoolean(), any(MailboxSession.class), any(FetchGroup.class))) + .thenReturn(metaData); + when(mailboxManager.getMailbox(any(MailboxPath.class), any(MailboxSession.class))) + .thenReturn(messageManager); + + replaceAclRequest = new SetACLRequest("TAG", ImapCommand.anyStateCommand("Name"), MAILBOX_NAME, USER_1, SET_RIGHTS); user1Key = EntryKey.deserialize(USER_1); setRights = Rfc4314Rights.fromSerializedRfc4314Rights(SET_RIGHTS); @@ -121,132 +115,61 @@ public class SetACLProcessorTest { @Test public void testUnsupportedRight() throws Exception { + SetACLRequest setACLRequest = new SetACLRequest("TAG", ImapCommand.anyStateCommand("Name"), MAILBOX_NAME, USER_1, UNSUPPORTED_RIGHT); - Expectations expectations = prepareRightsExpectations(); - expectations.allowing(mailboxManagerStub).hasRight(expectations.with(path), expectations.with(Expectations.equal(MailboxACL.Right.Lookup)), expectations.with(Expectations.same(mailboxSessionStub))); - expectations.will(Expectations.returnValue(false)); - - expectations.allowing(mailboxManagerStub).getMailbox(expectations.with(Expectations.any(MailboxPath.class)), expectations.with(Expectations.any(MailboxSession.class))); - expectations.will(Expectations.returnValue(messageManagerStub)); - - mockery.checking(expectations); - - final Responder responderMock = mockery.mock(Responder.class); - mockery.checking(new Expectations() { - { - oneOf(responderMock).respond(with(new StatusResponseTypeMatcher(StatusResponse.Type.BAD))); - } - }); - - subject.doProcess(new SetACLRequest("TAG", ImapCommand.anyStateCommand("Name"), MAILBOX_NAME, USER_1, UNSUPPORTED_RIGHT), responderMock, imapSessionStub); - - } - - @Test - public void testNoListRight() throws Exception { - - Expectations expectations = prepareRightsExpectations(); - expectations.allowing(mailboxManagerStub).hasRight(expectations.with(path), expectations.with(Expectations.equal(MailboxACL.Right.Lookup)), expectations.with(Expectations.same(mailboxSessionStub))); - expectations.will(Expectations.returnValue(false)); - - expectations.allowing(mailboxManagerStub).getMailbox(expectations.with(Expectations.any(MailboxPath.class)), expectations.with(Expectations.any(MailboxSession.class))); - expectations.will(Expectations.returnValue(messageManagerStub)); - - mockery.checking(expectations); + when(mailboxManager.hasRight(path, MailboxACL.Right.Lookup, mailboxSession)) + .thenReturn(false); - final Responder responderMock = mockery.mock(Responder.class); - mockery.checking(new Expectations() { - { - oneOf(responderMock).respond(with(new StatusResponseTypeMatcher(StatusResponse.Type.NO))); - } - }); + subject.doProcess(setACLRequest, responder, imapSession); - subject.doProcess(replaceACLRequest, responderMock, imapSessionStub); + verify(responder, times(1)).respond(argumentCaptor.capture()); + verifyNoMoreInteractions(responder); + assertThat(argumentCaptor.getAllValues()) + .hasSize(1); + assertThat(argumentCaptor.getAllValues().get(0)) + .matches(StatusResponseTypeMatcher.BAD_RESPONSE_MATCHER::matches); } @Test public void testNoAdminRight() throws Exception { + when(mailboxManager.hasRight(path, MailboxACL.Right.Lookup, mailboxSession)) + .thenReturn(true); + when(mailboxManager.hasRight(path, MailboxACL.Right.Administer, mailboxSession)) + .thenReturn(false); - Expectations expectations = prepareRightsExpectations(); - expectations.allowing(mailboxManagerStub).hasRight(expectations.with(path), expectations.with(Expectations.equal(MailboxACL.Right.Lookup)), expectations.with(Expectations.same(mailboxSessionStub))); - expectations.will(Expectations.returnValue(true)); + subject.doProcess(replaceAclRequest, responder, imapSession); - expectations.allowing(mailboxManagerStub).hasRight(expectations.with(path), expectations.with(Expectations.equal(MailboxACL.Right.Administer)), expectations.with(Expectations.same(mailboxSessionStub))); - expectations.will(Expectations.returnValue(false)); - - expectations.allowing(mailboxManagerStub).getMailbox(expectations.with(Expectations.any(MailboxPath.class)), expectations.with(Expectations.any(MailboxSession.class))); - expectations.will(Expectations.returnValue(messageManagerStub)); - - mockery.checking(expectations); - - final Responder responderMock = mockery.mock(Responder.class); - mockery.checking(new Expectations() { - { - oneOf(responderMock).respond(with(new StatusResponseTypeMatcher(StatusResponse.Type.NO))); - } - }); - - subject.doProcess(replaceACLRequest, responderMock, imapSessionStub); + verify(responder, times(1)).respond(argumentCaptor.capture()); + verifyNoMoreInteractions(responder); + assertThat(argumentCaptor.getAllValues()) + .hasSize(1); + assertThat(argumentCaptor.getAllValues().get(0)) + .matches(StatusResponseTypeMatcher.NO_RESPONSE_MATCHER::matches); } @Test public void testInexistentMailboxName() throws Exception { - Expectations expectations = prepareRightsExpectations(); - - expectations.allowing(mailboxManagerStub).getMailbox(expectations.with(Expectations.any(MailboxPath.class)), expectations.with(Expectations.any(MailboxSession.class))); - expectations.will(Expectations.throwException(new MailboxNotFoundException(path))); + when(mailboxManager.getMailbox(any(MailboxPath.class), any(MailboxSession.class))) + .thenThrow(new MailboxNotFoundException("")); - mockery.checking(expectations); + subject.doProcess(replaceAclRequest, responder, imapSession); - final Responder responderMock = mockery.mock(Responder.class); - mockery.checking(new Expectations() { - { - oneOf(responderMock).respond(with(new StatusResponseTypeMatcher(StatusResponse.Type.NO))); - } - }); + verify(responder, times(1)).respond(argumentCaptor.capture()); + verifyNoMoreInteractions(responder); - subject.doProcess(replaceACLRequest, responderMock, imapSessionStub); + assertThat(argumentCaptor.getAllValues()) + .hasSize(1); + assertThat(argumentCaptor.getAllValues().get(0)) + .matches(StatusResponseTypeMatcher.NO_RESPONSE_MATCHER::matches); } @Test public void testAddRights() throws Exception { testOp("+", EditMode.ADD); } - - private void testOp(String prefix, EditMode editMode) throws MailboxException { - final MailboxACL acl = MailboxACL.OWNER_FULL_ACL; - Expectations expectations = prepareRightsExpectations(); - - expectations.allowing(mailboxManagerStub).getMailbox(expectations.with(Expectations.any(MailboxPath.class)), expectations.with(Expectations.any(MailboxSession.class))); - expectations.will(Expectations.returnValue(messageManagerStub)); - - expectations.allowing(mailboxManagerStub).hasRight(expectations.with(path), expectations.with(Expectations.equal(MailboxACL.Right.Lookup)), expectations.with(Expectations.same(mailboxSessionStub))); - expectations.will(Expectations.returnValue(true)); - - expectations.allowing(mailboxManagerStub).hasRight(expectations.with(path), expectations.with(Expectations.equal(MailboxACL.Right.Administer)), expectations.with(Expectations.same(mailboxSessionStub))); - expectations.will(Expectations.returnValue(true)); - - expectations.allowing(mailboxManagerStub).applyRightsCommand(expectations.with(path), expectations.with(Expectations.equal(MailboxACL.command().key(user1Key).mode(editMode).rights(setRights).build())), expectations.with(mailboxSessionStub)); - - expectations.allowing(metaDataStub).getACL(); - expectations.will(Expectations.returnValue(acl)); - - mockery.checking(expectations); - - final Responder responderMock = mockery.mock(Responder.class); - mockery.checking(new Expectations() { - { - oneOf(responderMock).respond(with(new StatusResponseTypeMatcher(StatusResponse.Type.OK))); - } - }); - - SetACLRequest r = new SetACLRequest("TAG", ImapCommand.anyStateCommand("Name"), MAILBOX_NAME, USER_1, prefix + SET_RIGHTS); - subject.doProcess(r, responderMock, imapSessionStub); - } - @Test public void testRemoveRights() throws Exception { testOp("-", EditMode.REMOVE); @@ -256,5 +179,28 @@ public class SetACLProcessorTest { public void testReplaceRights() throws Exception { testOp("", EditMode.REPLACE); } + + private void testOp(String prefix, EditMode editMode) throws MailboxException { + when(mailboxManager.hasRight(path, MailboxACL.Right.Lookup, mailboxSession)) + .thenReturn(true); + when(mailboxManager.hasRight(path, MailboxACL.Right.Administer, mailboxSession)) + .thenReturn(true); + + + SetACLRequest r = new SetACLRequest("TAG", ImapCommand.anyStateCommand("Name"), MAILBOX_NAME, USER_1, prefix + SET_RIGHTS); + subject.doProcess(r, responder, imapSession); + + verify(mailboxManager).applyRightsCommand(path, + MailboxACL.command().key(user1Key).rights(setRights).mode(editMode).build(), + mailboxSession); + + verify(responder, times(1)).respond(argumentCaptor.capture()); + verifyNoMoreInteractions(responder); + + assertThat(argumentCaptor.getAllValues()) + .hasSize(1); + assertThat(argumentCaptor.getAllValues().get(0)) + .matches(StatusResponseTypeMatcher.OK_RESPONSE_MATCHER::matches); + } } --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
