MAILBOX-355 ListeningCurrentQuotaUpdater should rely on MailboxId
Project: http://git-wip-us.apache.org/repos/asf/james-project/repo Commit: http://git-wip-us.apache.org/repos/asf/james-project/commit/5b05b944 Tree: http://git-wip-us.apache.org/repos/asf/james-project/tree/5b05b944 Diff: http://git-wip-us.apache.org/repos/asf/james-project/diff/5b05b944 Branch: refs/heads/master Commit: 5b05b9445f46e0b9421dd00b1db6e4b57c4111d3 Parents: e7e5ba6 Author: Benoit Tellier <btell...@linagora.com> Authored: Tue Dec 4 10:28:50 2018 +0700 Committer: Benoit Tellier <btell...@linagora.com> Committed: Wed Dec 5 16:34:25 2018 +0700 ---------------------------------------------------------------------- .../james/mailbox/quota/QuotaRootResolver.java | 3 ++ .../quota/DefaultUserQuotaRootResolver.java | 12 +++++ .../quota/ListeningCurrentQuotaUpdater.java | 4 +- .../quota/DefaultUserQuotaRootResolverTest.java | 21 ++++++-- .../quota/ListeningCurrentQuotaUpdaterTest.java | 54 ++++++++++++++------ 5 files changed, 73 insertions(+), 21 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/james-project/blob/5b05b944/mailbox/api/src/main/java/org/apache/james/mailbox/quota/QuotaRootResolver.java ---------------------------------------------------------------------- diff --git a/mailbox/api/src/main/java/org/apache/james/mailbox/quota/QuotaRootResolver.java b/mailbox/api/src/main/java/org/apache/james/mailbox/quota/QuotaRootResolver.java index 83d5f64..38aa4e6 100644 --- a/mailbox/api/src/main/java/org/apache/james/mailbox/quota/QuotaRootResolver.java +++ b/mailbox/api/src/main/java/org/apache/james/mailbox/quota/QuotaRootResolver.java @@ -23,6 +23,7 @@ import java.util.List; import org.apache.james.mailbox.MailboxSession; import org.apache.james.mailbox.exception.MailboxException; +import org.apache.james.mailbox.model.MailboxId; import org.apache.james.mailbox.model.MailboxPath; import org.apache.james.mailbox.model.QuotaRoot; @@ -37,6 +38,8 @@ public interface QuotaRootResolver { */ QuotaRoot getQuotaRoot(MailboxPath mailboxPath) throws MailboxException; + QuotaRoot getQuotaRoot(MailboxId mailboxId, MailboxSession mailboxSession) throws MailboxException; + QuotaRoot fromString(String serializedQuotaRoot) throws MailboxException; List<MailboxPath> retrieveAssociatedMailboxes(QuotaRoot quotaRoot, MailboxSession mailboxSession) throws MailboxException; http://git-wip-us.apache.org/repos/asf/james-project/blob/5b05b944/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 09a55c9..2340ff1 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 @@ -28,6 +28,7 @@ import org.apache.james.core.User; import org.apache.james.mailbox.MailboxSession; import org.apache.james.mailbox.exception.MailboxException; import org.apache.james.mailbox.model.MailboxConstants; +import org.apache.james.mailbox.model.MailboxId; import org.apache.james.mailbox.model.MailboxPath; import org.apache.james.mailbox.model.QuotaRoot; import org.apache.james.mailbox.quota.UserQuotaRootResolver; @@ -68,6 +69,17 @@ public class DefaultUserQuotaRootResolver implements UserQuotaRootResolver { } @Override + public QuotaRoot getQuotaRoot(MailboxId mailboxId, MailboxSession mailboxSession) throws MailboxException { + User user = User.fromUsername( + factory.getMailboxMapper(mailboxSession) + .findMailboxById(mailboxId) + .generateAssociatedPath() + .getUser()); + + return forUser(user); + } + + @Override public QuotaRoot fromString(String serializedQuotaRoot) throws MailboxException { List<String> parts = toParts(serializedQuotaRoot); User user = User.fromUsername(parts.get(1)); http://git-wip-us.apache.org/repos/asf/james-project/blob/5b05b944/mailbox/store/src/main/java/org/apache/james/mailbox/store/quota/ListeningCurrentQuotaUpdater.java ---------------------------------------------------------------------- diff --git a/mailbox/store/src/main/java/org/apache/james/mailbox/store/quota/ListeningCurrentQuotaUpdater.java b/mailbox/store/src/main/java/org/apache/james/mailbox/store/quota/ListeningCurrentQuotaUpdater.java index 86d67c3..5f9da05 100644 --- a/mailbox/store/src/main/java/org/apache/james/mailbox/store/quota/ListeningCurrentQuotaUpdater.java +++ b/mailbox/store/src/main/java/org/apache/james/mailbox/store/quota/ListeningCurrentQuotaUpdater.java @@ -60,11 +60,11 @@ public class ListeningCurrentQuotaUpdater implements MailboxListener, QuotaUpdat try { if (event instanceof Added) { Added addedEvent = (Added) event; - QuotaRoot quotaRoot = quotaRootResolver.getQuotaRoot(addedEvent.getMailboxPath()); + QuotaRoot quotaRoot = quotaRootResolver.getQuotaRoot(addedEvent.getMailboxId(), event.getSession()); handleAddedEvent(addedEvent, quotaRoot); } else if (event instanceof Expunged) { Expunged expungedEvent = (Expunged) event; - QuotaRoot quotaRoot = quotaRootResolver.getQuotaRoot(expungedEvent.getMailboxPath()); + QuotaRoot quotaRoot = quotaRootResolver.getQuotaRoot(expungedEvent.getMailboxId(), event.getSession()); handleExpungedEvent(expungedEvent, quotaRoot); } else if (event instanceof MailboxDeletion) { MailboxDeletion mailboxDeletionEvent = (MailboxDeletion) event; http://git-wip-us.apache.org/repos/asf/james-project/blob/5b05b944/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 index 367d476..f5f98ff 100644 --- 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 @@ -25,9 +25,12 @@ import static org.mockito.Mockito.when; import java.util.Optional; +import org.apache.james.mailbox.MailboxSession; import org.apache.james.mailbox.exception.MailboxException; +import org.apache.james.mailbox.model.MailboxId; import org.apache.james.mailbox.model.MailboxPath; import org.apache.james.mailbox.model.QuotaRoot; +import org.apache.james.mailbox.model.TestId; 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; @@ -44,6 +47,8 @@ public class DefaultUserQuotaRootResolverTest { private static final MailboxPath MAILBOX_PATH_2 = MailboxPath.forUser("benwa", "test"); private static final SimpleMailbox MAILBOX_2 = new SimpleMailbox(MAILBOX_PATH_2, 10); private static final QuotaRoot QUOTA_ROOT = QuotaRoot.quotaRoot("#private&benwa", Optional.empty()); + private static final MailboxId MAILBOX_ID = TestId.of(42); + public static final MailboxSession MAILBOX_SESSION = null; private DefaultUserQuotaRootResolver testee; private MailboxSessionMapperFactory mockedFactory; @@ -79,14 +84,24 @@ public class DefaultUserQuotaRootResolverTest { @Test public void retrieveAssociatedMailboxesShouldWork() throws Exception { MailboxMapper mockedMapper = mock(MailboxMapper.class); - when(mockedFactory.getMailboxMapper(null)).thenReturn(mockedMapper); + when(mockedFactory.getMailboxMapper(MAILBOX_SESSION)).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); + + assertThat(testee.retrieveAssociatedMailboxes(QUOTA_ROOT, MAILBOX_SESSION)).containsOnly(MAILBOX_PATH, MAILBOX_PATH_2); + } + + @Test + public void getQuotaRootShouldReturnUserValueWhenCalledWithMailboxId() throws Exception { + MailboxMapper mockedMapper = mock(MailboxMapper.class); + when(mockedFactory.getMailboxMapper(MAILBOX_SESSION)).thenReturn(mockedMapper); + when(mockedMapper.findMailboxById(MAILBOX_ID)).thenReturn(MAILBOX); + + assertThat(testee.getQuotaRoot(MAILBOX_ID, MAILBOX_SESSION)).isEqualTo(QUOTA_ROOT); } @Test(expected = MailboxException.class) public void retrieveAssociatedMailboxesShouldThrowWhenQuotaRootContainsSeparator2Times() throws Exception { - testee.retrieveAssociatedMailboxes(QuotaRoot.quotaRoot("#private&be&nwa", Optional.empty()), null); + testee.retrieveAssociatedMailboxes(QuotaRoot.quotaRoot("#private&be&nwa", Optional.empty()), MAILBOX_SESSION); } } http://git-wip-us.apache.org/repos/asf/james-project/blob/5b05b944/mailbox/store/src/test/java/org/apache/james/mailbox/store/quota/ListeningCurrentQuotaUpdaterTest.java ---------------------------------------------------------------------- diff --git a/mailbox/store/src/test/java/org/apache/james/mailbox/store/quota/ListeningCurrentQuotaUpdaterTest.java b/mailbox/store/src/test/java/org/apache/james/mailbox/store/quota/ListeningCurrentQuotaUpdaterTest.java index 2c5de61..a14dd30 100644 --- a/mailbox/store/src/test/java/org/apache/james/mailbox/store/quota/ListeningCurrentQuotaUpdaterTest.java +++ b/mailbox/store/src/test/java/org/apache/james/mailbox/store/quota/ListeningCurrentQuotaUpdaterTest.java @@ -19,6 +19,8 @@ package org.apache.james.mailbox.store.quota; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.eq; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.never; import static org.mockito.Mockito.verify; @@ -33,9 +35,12 @@ import javax.mail.Flags; import org.apache.james.core.quota.QuotaCount; import org.apache.james.core.quota.QuotaSize; import org.apache.james.mailbox.MailboxListener; +import org.apache.james.mailbox.MailboxSession; import org.apache.james.mailbox.MessageUid; -import org.apache.james.mailbox.model.MailboxPath; +import org.apache.james.mailbox.mock.MockMailboxSession; +import org.apache.james.mailbox.model.MailboxId; import org.apache.james.mailbox.model.QuotaRoot; +import org.apache.james.mailbox.model.TestId; import org.apache.james.mailbox.quota.QuotaManager; import org.apache.james.mailbox.quota.QuotaRootResolver; import org.apache.james.mailbox.store.SimpleMessageMetaData; @@ -48,9 +53,10 @@ import com.google.common.collect.Lists; public class ListeningCurrentQuotaUpdaterTest { - public static final int SIZE = 45; - public static final MailboxPath MAILBOX_PATH = MailboxPath.forUser("benwa", "INBOX"); - public static final QuotaRoot QUOTA_ROOT = QuotaRoot.quotaRoot("benwa", Optional.empty()); + private static final int SIZE = 45; + private static final MailboxId MAILBOX_ID = TestId.of(42); + private static final QuotaRoot QUOTA_ROOT = QuotaRoot.quotaRoot("benwa", Optional.empty()); + private static final MailboxSession MAILBOX_SESSION = new MockMailboxSession("benwa"); private StoreCurrentQuotaManager mockedCurrentQuotaManager; private QuotaRootResolver mockedQuotaRootResolver; @@ -70,9 +76,12 @@ public class ListeningCurrentQuotaUpdaterTest { when(added.getMetaData(MessageUid.of(36))).thenReturn(new SimpleMessageMetaData(MessageUid.of(36),0,new Flags(), SIZE, new Date(), new DefaultMessageId())); when(added.getMetaData(MessageUid.of(38))).thenReturn(new SimpleMessageMetaData(MessageUid.of(38),0,new Flags(), SIZE, new Date(), new DefaultMessageId())); when(added.getUids()).thenReturn(Lists.newArrayList(MessageUid.of(36), MessageUid.of(38))); - when(added.getMailboxPath()).thenReturn(MAILBOX_PATH); - when(mockedQuotaRootResolver.getQuotaRoot(MAILBOX_PATH)).thenReturn(QUOTA_ROOT); + when(added.getMailboxId()).thenReturn(MAILBOX_ID); + when(added.getSession()).thenReturn(MAILBOX_SESSION); + when(mockedQuotaRootResolver.getQuotaRoot(eq(MAILBOX_ID), any(MailboxSession.class))).thenReturn(QUOTA_ROOT); + testee.event(added); + verify(mockedCurrentQuotaManager).increase(QUOTA_ROOT, 2, 2 * SIZE); } @@ -82,9 +91,12 @@ public class ListeningCurrentQuotaUpdaterTest { when(expunged.getMetaData(MessageUid.of(36))).thenReturn(new SimpleMessageMetaData(MessageUid.of(36),0,new Flags(), SIZE, new Date(), new DefaultMessageId())); when(expunged.getMetaData(MessageUid.of(38))).thenReturn(new SimpleMessageMetaData(MessageUid.of(38),0,new Flags(), SIZE, new Date(), new DefaultMessageId())); when(expunged.getUids()).thenReturn(Lists.newArrayList(MessageUid.of(36), MessageUid.of(38))); - when(expunged.getMailboxPath()).thenReturn(MAILBOX_PATH); - when(mockedQuotaRootResolver.getQuotaRoot(MAILBOX_PATH)).thenReturn(QUOTA_ROOT); + when(expunged.getMailboxId()).thenReturn(MAILBOX_ID); + when(expunged.getSession()).thenReturn(MAILBOX_SESSION); + when(mockedQuotaRootResolver.getQuotaRoot(eq(MAILBOX_ID), any(MailboxSession.class))).thenReturn(QUOTA_ROOT); + testee.event(expunged); + verify(mockedCurrentQuotaManager).decrease(QUOTA_ROOT, 2, 2 * SIZE); } @@ -92,9 +104,12 @@ public class ListeningCurrentQuotaUpdaterTest { public void emptyExpungedEventShouldNotTriggerDecrease() throws Exception { MailboxListener.Expunged expunged = mock(MailboxListener.Expunged.class); when(expunged.getUids()).thenReturn(Lists.<MessageUid>newArrayList()); - when(expunged.getMailboxPath()).thenReturn(MAILBOX_PATH); - when(mockedQuotaRootResolver.getQuotaRoot(MAILBOX_PATH)).thenReturn(QUOTA_ROOT); + when(expunged.getMailboxId()).thenReturn(MAILBOX_ID); + when(expunged.getSession()).thenReturn(MAILBOX_SESSION); + when(mockedQuotaRootResolver.getQuotaRoot(eq(MAILBOX_ID), any(MailboxSession.class))).thenReturn(QUOTA_ROOT); + testee.event(expunged); + verify(mockedCurrentQuotaManager, never()).decrease(QUOTA_ROOT, 0, 0); } @@ -102,32 +117,39 @@ public class ListeningCurrentQuotaUpdaterTest { public void emptyAddedEventShouldNotTriggerDecrease() throws Exception { MailboxListener.Added added = mock(MailboxListener.Added.class); when(added.getUids()).thenReturn(Lists.<MessageUid>newArrayList()); - when(added.getMailboxPath()).thenReturn(MAILBOX_PATH); - when(mockedQuotaRootResolver.getQuotaRoot(MAILBOX_PATH)).thenReturn(QUOTA_ROOT); + when(added.getMailboxId()).thenReturn(MAILBOX_ID); + when(added.getSession()).thenReturn(MAILBOX_SESSION); + when(mockedQuotaRootResolver.getQuotaRoot(eq(MAILBOX_ID), any(MailboxSession.class))).thenReturn(QUOTA_ROOT); + testee.event(added); + verify(mockedCurrentQuotaManager, never()).increase(QUOTA_ROOT, 0, 0); } @Test public void mailboxDeletionEventShouldDecreaseCurrentQuotaValues() throws Exception { MailboxListener.MailboxDeletion deletion = mock(MailboxListener.MailboxDeletion.class); - when(deletion.getMailboxPath()).thenReturn(MAILBOX_PATH); when(deletion.getQuotaRoot()).thenReturn(QUOTA_ROOT); when(deletion.getDeletedMessageCount()).thenReturn(QuotaCount.count(10)); when(deletion.getTotalDeletedSize()).thenReturn(QuotaSize.size(5)); - when(mockedQuotaRootResolver.getQuotaRoot(MAILBOX_PATH)).thenReturn(QUOTA_ROOT); + when(deletion.getMailboxId()).thenReturn(MAILBOX_ID); + when(deletion.getSession()).thenReturn(MAILBOX_SESSION); + when(mockedQuotaRootResolver.getQuotaRoot(eq(MAILBOX_ID), any(MailboxSession.class))).thenReturn(QUOTA_ROOT); + testee.event(deletion); + verify(mockedCurrentQuotaManager).decrease(QUOTA_ROOT, 10, 5); } @Test public void mailboxDeletionEventShouldDoNothingWhenEmptyMailbox() throws Exception { MailboxListener.MailboxDeletion deletion = mock(MailboxListener.MailboxDeletion.class); - when(deletion.getMailboxPath()).thenReturn(MAILBOX_PATH); when(deletion.getQuotaRoot()).thenReturn(QUOTA_ROOT); when(deletion.getDeletedMessageCount()).thenReturn(QuotaCount.count(0)); when(deletion.getTotalDeletedSize()).thenReturn(QuotaSize.size(0)); - when(mockedQuotaRootResolver.getQuotaRoot(MAILBOX_PATH)).thenReturn(QUOTA_ROOT); + when(deletion.getMailboxId()).thenReturn(MAILBOX_ID); + when(deletion.getSession()).thenReturn(MAILBOX_SESSION); + when(mockedQuotaRootResolver.getQuotaRoot(eq(MAILBOX_ID), any(MailboxSession.class))).thenReturn(QUOTA_ROOT); testee.event(deletion); --------------------------------------------------------------------- To unsubscribe, e-mail: server-dev-unsubscr...@james.apache.org For additional commands, e-mail: server-dev-h...@james.apache.org