public static <U extends QuotaValue<U>> SerializableQuota<U> newInstance(U used, U max) { return new SerializableQuota<>( new SerializableQuotaValue<>(used), new SerializableQuotaValue<>(max) ); }
@Override public SerializableQuotaValue<QuotaCount> getGlobalMaxMessageCount() throws MailboxException { return SerializableQuotaValue.valueOf(maxQuotaManager.getGlobalMaxMessage()); }
public Long encodeAsLong() { return max.encodeAsLong(); }
@Override public void setGlobalMaxMessageCount(SerializableQuotaValue<QuotaCount> maxGlobalMessageCount) throws MailboxException { maxGlobalMessageCount.toValue(QuotaCount::count, QuotaCount.unlimited()) .ifPresent(Throwing.consumer(maxQuotaManager::setGlobalMaxMessage).sneakyThrow()); }
@Override public void setGlobalMaxStorage(SerializableQuotaValue<QuotaSize> maxGlobalSize) throws MailboxException { maxGlobalSize.toValue(QuotaSize::size, QuotaSize.unlimited()) .ifPresent(Throwing.consumer(maxQuotaManager::setGlobalMaxStorage).sneakyThrow()); } }
public static <U extends QuotaValue<U>> SerializableQuotaValue<U> valueOf(Optional<U> input) { return new SerializableQuotaValue<>(input.orElse(null)); }
@Override public SerializableQuotaValue<QuotaSize> getGlobalMaxStorage() throws MailboxException { return SerializableQuotaValue.valueOf(maxQuotaManager.getGlobalMaxStorage()); }
@Override public void setMaxMessageCount(String quotaRoot, SerializableQuotaValue<QuotaCount> maxMessageCount) throws MailboxException { maxMessageCount.toValue(QuotaCount::count, QuotaCount.unlimited()) .ifPresent( Throwing.consumer( (QuotaCount value) -> maxQuotaManager.setMaxMessage(quotaRootResolver.fromString(quotaRoot), value)) .sneakyThrow()); }
public SerializableQuotaValue(T value) { this(encodeAsLong(value)); }
@Test public void getGlobalMaxStorageCommandShouldWork() throws Exception { String[] arguments = { "-h", "127.0.0.1", "-p", "9999", CmdType.GETGLOBALMAXSTORAGEQUOTA.getCommand()}; CommandLine commandLine = ServerCmd.parseCommandLine(arguments); when(quotaProbe.getGlobalMaxStorage()).thenReturn(new SerializableQuotaValue<>(QuotaSize.size(1024L * 1024L * 1024L))); testee.executeCommandLine(commandLine); }
@Override public SerializableQuotaValue<QuotaCount> getMaxMessageCount(String quotaRoot) throws MailboxException { return SerializableQuotaValue.valueOf(maxQuotaManager.getMaxMessage(quotaRootResolver.fromString(quotaRoot))); }
@Override public void setMaxStorage(String quotaRoot, SerializableQuotaValue<QuotaSize> maxSize) throws MailboxException { maxSize.toValue(QuotaSize::size, QuotaSize.unlimited()) .ifPresent( Throwing.consumer( (QuotaSize value) -> maxQuotaManager.setMaxStorage(quotaRootResolver.fromString(quotaRoot), value)) .sneakyThrow()); }
public Optional<T> toValue(Function<Long, T> factory, T unlimited) { Long longValue = encodeAsLong(); if (longValue == null) { return Optional.empty(); } if (longValue == UNLIMITED) { return Optional.of(unlimited); } return Optional.of(factory.apply(longValue)); } }
@Test public void getMaxStorageQuotaCommandShouldWork() throws Exception { String quotaroot = "#private&user@domain"; String[] arguments = { "-h", "127.0.0.1", "-p", "9999", CmdType.GETMAXSTORAGEQUOTA.getCommand(), quotaroot}; CommandLine commandLine = ServerCmd.parseCommandLine(arguments); when(quotaProbe.getMaxStorage(quotaroot)).thenReturn(new SerializableQuotaValue<>(QuotaSize.unlimited())); testee.executeCommandLine(commandLine); }
@Override public SerializableQuotaValue<QuotaSize> getMaxStorage(String quotaRoot) throws MailboxException { return SerializableQuotaValue.valueOf(maxQuotaManager.getMaxStorage(quotaRootResolver.fromString(quotaRoot))); }
@Test public void getGlobalMaxMessageCountCommandShouldWork() throws Exception { String[] arguments = { "-h", "127.0.0.1", "-p", "9999", CmdType.GETGLOBALMAXMESSAGECOUNTQUOTA.getCommand()}; CommandLine commandLine = ServerCmd.parseCommandLine(arguments); when(quotaProbe.getGlobalMaxMessageCount()).thenReturn(new SerializableQuotaValue<>(QuotaCount.count(1024L * 1024L))); testee.executeCommandLine(commandLine); }
@Test public void getMailboxesShouldReturnMaxMessageQuotasForInboxWhenSet() throws Exception { quotaProbe.setGlobalMaxMessageCount(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&alice@domain.tld']['MESSAGE'].max", equalTo(43)); }
@Test public void getMaxMessageCountCommandShouldWork() throws Exception { String quotaroot = "#private&user@domain"; String[] arguments = { "-h", "127.0.0.1", "-p", "9999", CmdType.GETMAXMESSAGECOUNTQUOTA.getCommand(), quotaroot}; CommandLine commandLine = ServerCmd.parseCommandLine(arguments); when(quotaProbe.getMaxMessageCount(quotaroot)).thenReturn(new SerializableQuotaValue<>(QuotaCount.unlimited())); testee.executeCommandLine(commandLine); }
@Test public void getMailboxesShouldReturnMaxStorageQuotasForInboxWhenSet() throws Exception { quotaProbe.setGlobalMaxStorage(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&alice@domain.tld']['STORAGE'].max", equalTo(42)); }
@Test public void setMaxMessageCountCommandShouldWork() throws Exception { String quotaroot = "#private&user@domain"; String[] arguments = { "-h", "127.0.0.1", "-p", "9999", CmdType.SETMAXMESSAGECOUNTQUOTA.getCommand(), quotaroot, "1000"}; CommandLine commandLine = ServerCmd.parseCommandLine(arguments); testee.executeCommandLine(commandLine); verify(quotaProbe).setMaxMessageCount(quotaroot, new SerializableQuotaValue<>(QuotaCount.count(1000))); }