JAMES-2345 JMAP Handle max quota
Project: http://git-wip-us.apache.org/repos/asf/james-project/repo Commit: http://git-wip-us.apache.org/repos/asf/james-project/commit/23a634c0 Tree: http://git-wip-us.apache.org/repos/asf/james-project/tree/23a634c0 Diff: http://git-wip-us.apache.org/repos/asf/james-project/diff/23a634c0 Branch: refs/heads/master Commit: 23a634c08cbfcaa88a055fc97c437a6f737674f8 Parents: bfd7185 Author: Raphael Ouazana <[email protected]> Authored: Tue Mar 6 15:28:04 2018 +0100 Committer: Raphael Ouazana <[email protected]> Committed: Thu Mar 8 09:29:52 2018 +0100 ---------------------------------------------------------------------- .../integration/GetMailboxesMethodTest.java | 50 ++++++++++++++++++-- .../apache/james/jmap/model/MailboxFactory.java | 35 ++++++++++++-- .../jmap/methods/GetMailboxesMethodTest.java | 9 +++- .../james/jmap/model/MailboxFactoryTest.java | 10 ++-- 4 files changed, 90 insertions(+), 14 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/james-project/blob/23a634c0/server/protocols/jmap-integration-testing/jmap-integration-testing-common/src/test/java/org/apache/james/jmap/methods/integration/GetMailboxesMethodTest.java ---------------------------------------------------------------------- diff --git a/server/protocols/jmap-integration-testing/jmap-integration-testing-common/src/test/java/org/apache/james/jmap/methods/integration/GetMailboxesMethodTest.java b/server/protocols/jmap-integration-testing/jmap-integration-testing-common/src/test/java/org/apache/james/jmap/methods/integration/GetMailboxesMethodTest.java index ed06dd6..820d6bf 100644 --- a/server/protocols/jmap-integration-testing/jmap-integration-testing-common/src/test/java/org/apache/james/jmap/methods/integration/GetMailboxesMethodTest.java +++ b/server/protocols/jmap-integration-testing/jmap-integration-testing-common/src/test/java/org/apache/james/jmap/methods/integration/GetMailboxesMethodTest.java @@ -40,6 +40,7 @@ import java.util.Date; import java.util.List; import java.util.Locale; import java.util.Map; +import java.util.Optional; import javax.mail.Flags; @@ -54,11 +55,16 @@ import org.apache.james.mailbox.model.MailboxACL.Right; 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.quota.QuotaCount; +import org.apache.james.mailbox.quota.QuotaSize; import org.apache.james.mailbox.store.mail.model.Mailbox; +import org.apache.james.mailbox.store.mail.model.SerializableQuotaValue; import org.apache.james.mailbox.store.probe.ACLProbe; import org.apache.james.mailbox.store.probe.MailboxProbe; +import org.apache.james.mailbox.store.probe.QuotaProbe; import org.apache.james.modules.ACLProbeImpl; import org.apache.james.modules.MailboxProbeImpl; +import org.apache.james.modules.QuotaProbesImpl; import org.apache.james.probe.DataProbe; import org.apache.james.utils.DataProbeImpl; import org.apache.james.utils.JmapGuiceProbe; @@ -91,6 +97,7 @@ public abstract class GetMailboxesMethodTest { private GuiceJamesServer jmapServer; private MailboxProbe mailboxProbe; private ACLProbe aclProbe; + private QuotaProbe quotaProbe; @Before public void setup() throws Throwable { @@ -98,6 +105,7 @@ public abstract class GetMailboxesMethodTest { jmapServer.start(); mailboxProbe = jmapServer.getProbe(MailboxProbeImpl.class); aclProbe = jmapServer.getProbe(ACLProbeImpl.class); + quotaProbe = jmapServer.getProbe(QuotaProbesImpl.class); RestAssured.requestSpecification = new RequestSpecBuilder() .setContentType(ContentType.JSON) @@ -778,8 +786,42 @@ public abstract class GetMailboxesMethodTest { .statusCode(200) .body(NAME, equalTo("mailboxes")) .body(ARGUMENTS + ".list", hasSize(1)) - .body(FIRST_MAILBOX + ".quotas['key']['STORAGE'].max", nullValue()) - .body(FIRST_MAILBOX + ".quotas['key']['MESSAGE'].max", nullValue()); + .body(FIRST_MAILBOX + ".quotas['#private&[email protected]']['STORAGE'].max", nullValue()) + .body(FIRST_MAILBOX + ".quotas['#private&[email protected]']['MESSAGE'].max", nullValue()); + } + + @Test + public void getMailboxesShouldReturnMaxStorageQuotasForInboxWhenSet() throws Exception { + quotaProbe.setDefaultMaxStorage(SerializableQuotaValue.valueOf(Optional.of(QuotaSize.size(42)))); + String mailboxId = mailboxProbe.createMailbox(MailboxConstants.USER_NAMESPACE, alice, DefaultMailboxes.INBOX).serialize(); + + given() + .header("Authorization", accessToken.serialize()) + .body("[[\"getMailboxes\", {\"ids\": [\"" + mailboxId + "\"]}, \"#0\"]]") + .when() + .post("/jmap") + .then() + .statusCode(200) + .body(NAME, equalTo("mailboxes")) + .body(ARGUMENTS + ".list", hasSize(1)) + .body(FIRST_MAILBOX + ".quotas['#private&[email protected]']['STORAGE'].max", equalTo(42)); + } + + @Test + public void getMailboxesShouldReturnMaxMessageQuotasForInboxWhenSet() throws Exception { + quotaProbe.setDefaultMaxMessageCount(SerializableQuotaValue.valueOf(Optional.of(QuotaCount.count(43)))); + String mailboxId = mailboxProbe.createMailbox(MailboxConstants.USER_NAMESPACE, alice, DefaultMailboxes.INBOX).serialize(); + + given() + .header("Authorization", accessToken.serialize()) + .body("[[\"getMailboxes\", {\"ids\": [\"" + mailboxId + "\"]}, \"#0\"]]") + .when() + .post("/jmap") + .then() + .statusCode(200) + .body(NAME, equalTo("mailboxes")) + .body(ARGUMENTS + ".list", hasSize(1)) + .body(FIRST_MAILBOX + ".quotas['#private&[email protected]']['MESSAGE'].max", equalTo(43)); } @Test @@ -795,7 +837,7 @@ public abstract class GetMailboxesMethodTest { .statusCode(200) .body(NAME, equalTo("mailboxes")) .body(ARGUMENTS + ".list", hasSize(1)) - .body(FIRST_MAILBOX + ".quotas['key']['STORAGE'].used", equalTo(0)) - .body(FIRST_MAILBOX + ".quotas['key']['MESSAGE'].used", equalTo(0)); + .body(FIRST_MAILBOX + ".quotas['#private&[email protected]']['STORAGE'].used", equalTo(0)) + .body(FIRST_MAILBOX + ".quotas['#private&[email protected]']['MESSAGE'].used", equalTo(0)); } } http://git-wip-us.apache.org/repos/asf/james-project/blob/23a634c0/server/protocols/jmap/src/main/java/org/apache/james/jmap/model/MailboxFactory.java ---------------------------------------------------------------------- diff --git a/server/protocols/jmap/src/main/java/org/apache/james/jmap/model/MailboxFactory.java b/server/protocols/jmap/src/main/java/org/apache/james/jmap/model/MailboxFactory.java index 8013ed3..665cbb0 100644 --- a/server/protocols/jmap/src/main/java/org/apache/james/jmap/model/MailboxFactory.java +++ b/server/protocols/jmap/src/main/java/org/apache/james/jmap/model/MailboxFactory.java @@ -42,6 +42,10 @@ import org.apache.james.mailbox.model.MailboxId; import org.apache.james.mailbox.model.MailboxMetaData; import org.apache.james.mailbox.model.MailboxPath; import org.apache.james.mailbox.model.QuotaRoot; +import org.apache.james.mailbox.quota.MaxQuotaManager; +import org.apache.james.mailbox.quota.QuotaCount; +import org.apache.james.mailbox.quota.QuotaRootResolver; +import org.apache.james.mailbox.quota.QuotaSize; import com.google.common.annotations.VisibleForTesting; import com.google.common.base.Preconditions; @@ -52,6 +56,8 @@ import com.google.common.collect.ImmutableMap; public class MailboxFactory { public static final boolean NO_RESET_RECENT = false; private final MailboxManager mailboxManager; + private final MaxQuotaManager maxQuotaManager; + private final QuotaRootResolver quotaRootResolver; public static class MailboxBuilder { private final MailboxFactory mailboxFactory; @@ -94,8 +100,10 @@ public class MailboxFactory { } @Inject - public MailboxFactory(MailboxManager mailboxManager) { + public MailboxFactory(MailboxManager mailboxManager, MaxQuotaManager maxQuotaManager, QuotaRootResolver quotaRootResolver) { this.mailboxManager = mailboxManager; + this.maxQuotaManager = maxQuotaManager; + this.quotaRootResolver = quotaRootResolver; } public MailboxBuilder builder() { @@ -111,11 +119,9 @@ public class MailboxFactory { MessageManager.MetaData metaData = messageManager.getMetaData(NO_RESET_RECENT, mailboxSession, MessageManager.MetaData.FetchGroup.NO_COUNT); Rights rights = Rights.fromACL(metaData.getACL()) - .removeEntriesFor(Username.forMailboxPath(messageManager.getMailboxPath())); + .removeEntriesFor(Username.forMailboxPath(mailboxPath)); Username username = Username.fromSession(mailboxSession); - Quotas quotas = new Quotas(ImmutableMap.of(new QuotaId(QuotaRoot.quotaRoot("key")), new Quotas.Quota(ImmutableMap.of( - Type.STORAGE, new Quotas.Value(Number.ZERO, Optional.empty()), - Type.MESSAGE, new Quotas.Value(Number.ZERO, Optional.empty()))))); + Quotas quotas = getQuotas(mailboxPath); return Mailbox.builder() .id(messageManager.getId()) @@ -137,6 +143,25 @@ public class MailboxFactory { .build(); } + private Quotas getQuotas(MailboxPath mailboxPath) throws MailboxException { + QuotaRoot quotaRoot = quotaRootResolver.getQuotaRoot(mailboxPath); + return new Quotas(ImmutableMap.of(new QuotaId(quotaRoot), new Quotas.Quota(ImmutableMap.of( + Type.STORAGE, new Quotas.Value(Number.ZERO, getStorageMaxQuota(quotaRoot)), + Type.MESSAGE, new Quotas.Value(Number.ZERO, getMessageMaxQuota(quotaRoot)))))); + } + + private Optional<Number> getStorageMaxQuota(QuotaRoot quotaRoot) throws MailboxException { + return maxQuotaManager.getMaxStorage(quotaRoot) + .map(QuotaSize::asLong) + .map(Number.BOUND_SANITIZING_FACTORY::from); + } + + private Optional<Number> getMessageMaxQuota(QuotaRoot quotaRoot) throws MailboxException { + return maxQuotaManager.getMaxMessage(quotaRoot) + .map(QuotaCount::asLong) + .map(Number.BOUND_SANITIZING_FACTORY::from); + } + private MailboxNamespace getNamespace(MailboxPath mailboxPath, boolean isOwner) { if (isOwner) { return MailboxNamespace.personal(); http://git-wip-us.apache.org/repos/asf/james-project/blob/23a634c0/server/protocols/jmap/src/test/java/org/apache/james/jmap/methods/GetMailboxesMethodTest.java ---------------------------------------------------------------------- diff --git a/server/protocols/jmap/src/test/java/org/apache/james/jmap/methods/GetMailboxesMethodTest.java b/server/protocols/jmap/src/test/java/org/apache/james/jmap/methods/GetMailboxesMethodTest.java index c750526..7350eb9 100644 --- a/server/protocols/jmap/src/test/java/org/apache/james/jmap/methods/GetMailboxesMethodTest.java +++ b/server/protocols/jmap/src/test/java/org/apache/james/jmap/methods/GetMailboxesMethodTest.java @@ -48,6 +48,9 @@ import org.apache.james.mailbox.inmemory.InMemoryId; import org.apache.james.mailbox.inmemory.manager.InMemoryIntegrationResources; import org.apache.james.mailbox.mock.MockMailboxSession; import org.apache.james.mailbox.model.MailboxPath; +import org.apache.james.mailbox.quota.MaxQuotaManager; +import org.apache.james.mailbox.quota.QuotaRootResolver; +import org.apache.james.mailbox.store.StoreMailboxManager; import org.apache.james.metrics.logger.DefaultMetricFactory; import org.assertj.core.groups.Tuple; import org.junit.Before; @@ -60,7 +63,7 @@ public class GetMailboxesMethodTest { private static final String USERNAME = "[email protected]"; private static final String USERNAME2 = "[email protected]"; - private MailboxManager mailboxManager; + private StoreMailboxManager mailboxManager; private GetMailboxesMethod getMailboxesMethod; private ClientId clientId; private MailboxFactory mailboxFactory; @@ -71,7 +74,9 @@ public class GetMailboxesMethodTest { InMemoryIntegrationResources inMemoryIntegrationResources = new InMemoryIntegrationResources(); GroupMembershipResolver groupMembershipResolver = inMemoryIntegrationResources.createGroupMembershipResolver(); mailboxManager = inMemoryIntegrationResources.createMailboxManager(groupMembershipResolver); - mailboxFactory = new MailboxFactory(mailboxManager); + MaxQuotaManager maxQuotaManager = inMemoryIntegrationResources.createMaxQuotaManager(); + QuotaRootResolver quotaRootResolver = inMemoryIntegrationResources.createQuotaRootResolver(mailboxManager); + mailboxFactory = new MailboxFactory(mailboxManager, maxQuotaManager, quotaRootResolver); getMailboxesMethod = new GetMailboxesMethod(mailboxManager, mailboxFactory, new DefaultMetricFactory()); } http://git-wip-us.apache.org/repos/asf/james-project/blob/23a634c0/server/protocols/jmap/src/test/java/org/apache/james/jmap/model/MailboxFactoryTest.java ---------------------------------------------------------------------- diff --git a/server/protocols/jmap/src/test/java/org/apache/james/jmap/model/MailboxFactoryTest.java b/server/protocols/jmap/src/test/java/org/apache/james/jmap/model/MailboxFactoryTest.java index 4d18bdb..e0f832c 100644 --- a/server/protocols/jmap/src/test/java/org/apache/james/jmap/model/MailboxFactoryTest.java +++ b/server/protocols/jmap/src/test/java/org/apache/james/jmap/model/MailboxFactoryTest.java @@ -24,7 +24,6 @@ import java.util.Optional; import org.apache.james.jmap.model.mailbox.Mailbox; import org.apache.james.jmap.model.mailbox.MailboxNamespace; -import org.apache.james.mailbox.MailboxManager; import org.apache.james.mailbox.MailboxSession; import org.apache.james.mailbox.inmemory.InMemoryId; import org.apache.james.mailbox.inmemory.manager.InMemoryIntegrationResources; @@ -32,7 +31,10 @@ import org.apache.james.mailbox.manager.ManagerTestResources; import org.apache.james.mailbox.model.MailboxACL; import org.apache.james.mailbox.model.MailboxId; import org.apache.james.mailbox.model.MailboxPath; +import org.apache.james.mailbox.quota.MaxQuotaManager; +import org.apache.james.mailbox.quota.QuotaRootResolver; import org.apache.james.mailbox.store.SimpleMailboxMetaData; +import org.apache.james.mailbox.store.StoreMailboxManager; import org.assertj.core.api.JUnitSoftAssertions; import org.junit.Before; import org.junit.Rule; @@ -46,7 +48,7 @@ public class MailboxFactoryTest { @Rule public final JUnitSoftAssertions softly = new JUnitSoftAssertions(); - private MailboxManager mailboxManager; + private StoreMailboxManager mailboxManager; private MailboxSession mailboxSession; private MailboxSession otherMailboxSession; private String user; @@ -57,11 +59,13 @@ public class MailboxFactoryTest { public void setup() throws Exception { InMemoryIntegrationResources inMemoryIntegrationResources = new InMemoryIntegrationResources(); mailboxManager = inMemoryIntegrationResources.createMailboxManager(inMemoryIntegrationResources.createGroupMembershipResolver()); + MaxQuotaManager maxQuotaManager = inMemoryIntegrationResources.createMaxQuotaManager(); + QuotaRootResolver quotaRootResolver = inMemoryIntegrationResources.createQuotaRootResolver(mailboxManager); user = ManagerTestResources.USER; otherUser = ManagerTestResources.OTHER_USER; mailboxSession = mailboxManager.login(user, ManagerTestResources.USER_PASS); otherMailboxSession = mailboxManager.login(otherUser, ManagerTestResources.OTHER_USER_PASS); - sut = new MailboxFactory(mailboxManager); + sut = new MailboxFactory(mailboxManager, maxQuotaManager, quotaRootResolver); } --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
