JAMES-2401 Getting user details should return occupation
Project: http://git-wip-us.apache.org/repos/asf/james-project/repo Commit: http://git-wip-us.apache.org/repos/asf/james-project/commit/4c1093dd Tree: http://git-wip-us.apache.org/repos/asf/james-project/tree/4c1093dd Diff: http://git-wip-us.apache.org/repos/asf/james-project/diff/4c1093dd Branch: refs/heads/master Commit: 4c1093dd2cfdad3edbc4d78313252c66768881f9 Parents: bb01282 Author: benwa <[email protected]> Authored: Fri May 18 16:23:08 2018 +0700 Committer: benwa <[email protected]> Committed: Fri May 18 16:41:16 2018 +0700 ---------------------------------------------------------------------- .../org/apache/james/mailbox/model/Quota.java | 7 +++ .../apache/james/mailbox/model/QuotaTest.java | 23 +++++++ .../james/webadmin/dto/OccupationDTO.java | 55 +++++++++++++++++ .../james/webadmin/dto/OccupationRatioDTO.java | 53 ++++++++++++++++ .../james/webadmin/dto/QuotaDetailsDTO.java | 21 ++++++- .../webadmin/service/UserQuotaService.java | 9 ++- .../webadmin/routes/UserQuotaRoutesTest.java | 63 +++++++++++++++++++- 7 files changed, 226 insertions(+), 5 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/james-project/blob/4c1093dd/mailbox/api/src/main/java/org/apache/james/mailbox/model/Quota.java ---------------------------------------------------------------------- diff --git a/mailbox/api/src/main/java/org/apache/james/mailbox/model/Quota.java b/mailbox/api/src/main/java/org/apache/james/mailbox/model/Quota.java index cabd05a..8b2a2a8 100644 --- a/mailbox/api/src/main/java/org/apache/james/mailbox/model/Quota.java +++ b/mailbox/api/src/main/java/org/apache/james/mailbox/model/Quota.java @@ -94,6 +94,13 @@ public class Quota<T extends QuotaValue<T>> { return used; } + public double getRatio() { + if (limit.isUnlimited()) { + return 0; + } + return Double.valueOf(used.asLong()) / Double.valueOf(limit.asLong()); + } + public ImmutableMap<Scope, T> getLimitByScope() { return limitByScope; } http://git-wip-us.apache.org/repos/asf/james-project/blob/4c1093dd/mailbox/api/src/test/java/org/apache/james/mailbox/model/QuotaTest.java ---------------------------------------------------------------------- diff --git a/mailbox/api/src/test/java/org/apache/james/mailbox/model/QuotaTest.java b/mailbox/api/src/test/java/org/apache/james/mailbox/model/QuotaTest.java index ef5a1d6..bcf360b 100644 --- a/mailbox/api/src/test/java/org/apache/james/mailbox/model/QuotaTest.java +++ b/mailbox/api/src/test/java/org/apache/james/mailbox/model/QuotaTest.java @@ -23,6 +23,7 @@ import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatThrownBy; import org.apache.james.mailbox.quota.QuotaCount; +import org.apache.james.mailbox.quota.QuotaSize; import org.junit.Test; public class QuotaTest { @@ -93,4 +94,26 @@ public class QuotaTest { assertThat(actual).isNotNull(); } + @Test + public void getRatioShouldReturnUsedDividedByLimit() { + assertThat( + Quota.<QuotaSize>builder() + .used(QuotaSize.size(15)) + .computedLimit(QuotaSize.size(60)) + .build() + .getRatio()) + .isEqualTo(0.25); + } + + @Test + public void getRatioShouldReturnZeroWhenUnlimited() { + assertThat( + Quota.<QuotaSize>builder() + .used(QuotaSize.size(15)) + .computedLimit(QuotaSize.unlimited()) + .build() + .getRatio()) + .isEqualTo(0); + } + } http://git-wip-us.apache.org/repos/asf/james-project/blob/4c1093dd/server/protocols/webadmin/webadmin-mailbox/src/main/java/org/apache/james/webadmin/dto/OccupationDTO.java ---------------------------------------------------------------------- diff --git a/server/protocols/webadmin/webadmin-mailbox/src/main/java/org/apache/james/webadmin/dto/OccupationDTO.java b/server/protocols/webadmin/webadmin-mailbox/src/main/java/org/apache/james/webadmin/dto/OccupationDTO.java new file mode 100644 index 0000000..ca85a37 --- /dev/null +++ b/server/protocols/webadmin/webadmin-mailbox/src/main/java/org/apache/james/webadmin/dto/OccupationDTO.java @@ -0,0 +1,55 @@ +/**************************************************************** + * Licensed to the Apache Software Foundation (ASF) under one * + * or more contributor license agreements. See the NOTICE file * + * distributed with this work for additional information * + * regarding copyright ownership. The ASF licenses this file * + * to you under the Apache License, Version 2.0 (the * + * "License"); you may not use this file except in compliance * + * with the License. You may obtain a copy of the License at * + * * + * http://www.apache.org/licenses/LICENSE-2.0 * + * * + * Unless required by applicable law or agreed to in writing, * + * software distributed under the License is distributed on an * + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * + * KIND, either express or implied. See the License for the * + * specific language governing permissions and limitations * + * under the License. * + ****************************************************************/ + +package org.apache.james.webadmin.dto; + +import org.apache.james.mailbox.model.Quota; +import org.apache.james.mailbox.quota.QuotaCount; +import org.apache.james.mailbox.quota.QuotaSize; + +public class OccupationDTO { + public static OccupationDTO from(Quota<QuotaSize> sizeQuota, Quota<QuotaCount> countQuota) { + return new OccupationDTO( + sizeQuota.getUsed().asLong(), + countQuota.getUsed().asLong(), + OccupationRatioDTO.from(sizeQuota, countQuota)); + } + + private final long size; + private final long count; + private final OccupationRatioDTO ratio; + + private OccupationDTO(long size, long count, OccupationRatioDTO ratio) { + this.size = size; + this.count = count; + this.ratio = ratio; + } + + public long getSize() { + return size; + } + + public long getCount() { + return count; + } + + public OccupationRatioDTO getRatio() { + return ratio; + } +} http://git-wip-us.apache.org/repos/asf/james-project/blob/4c1093dd/server/protocols/webadmin/webadmin-mailbox/src/main/java/org/apache/james/webadmin/dto/OccupationRatioDTO.java ---------------------------------------------------------------------- diff --git a/server/protocols/webadmin/webadmin-mailbox/src/main/java/org/apache/james/webadmin/dto/OccupationRatioDTO.java b/server/protocols/webadmin/webadmin-mailbox/src/main/java/org/apache/james/webadmin/dto/OccupationRatioDTO.java new file mode 100644 index 0000000..1358f13 --- /dev/null +++ b/server/protocols/webadmin/webadmin-mailbox/src/main/java/org/apache/james/webadmin/dto/OccupationRatioDTO.java @@ -0,0 +1,53 @@ +/**************************************************************** + * Licensed to the Apache Software Foundation (ASF) under one * + * or more contributor license agreements. See the NOTICE file * + * distributed with this work for additional information * + * regarding copyright ownership. The ASF licenses this file * + * to you under the Apache License, Version 2.0 (the * + * "License"); you may not use this file except in compliance * + * with the License. You may obtain a copy of the License at * + * * + * http://www.apache.org/licenses/LICENSE-2.0 * + * * + * Unless required by applicable law or agreed to in writing, * + * software distributed under the License is distributed on an * + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * + * KIND, either express or implied. See the License for the * + * specific language governing permissions and limitations * + * under the License. * + ****************************************************************/ + +package org.apache.james.webadmin.dto; + +import org.apache.james.mailbox.model.Quota; +import org.apache.james.mailbox.quota.QuotaCount; +import org.apache.james.mailbox.quota.QuotaSize; + +public class OccupationRatioDTO { + + public static OccupationRatioDTO from(Quota<QuotaSize> sizeQuota, Quota<QuotaCount> countQuota) { + return new OccupationRatioDTO( + sizeQuota.getRatio(), + countQuota.getRatio()); + } + + private final double size; + private final double count; + + private OccupationRatioDTO(double size, double count) { + this.size = size; + this.count = count; + } + + public double getSize() { + return size; + } + + public double getCount() { + return count; + } + + public double getMax() { + return Math.max(size, count); + } +} http://git-wip-us.apache.org/repos/asf/james-project/blob/4c1093dd/server/protocols/webadmin/webadmin-mailbox/src/main/java/org/apache/james/webadmin/dto/QuotaDetailsDTO.java ---------------------------------------------------------------------- diff --git a/server/protocols/webadmin/webadmin-mailbox/src/main/java/org/apache/james/webadmin/dto/QuotaDetailsDTO.java b/server/protocols/webadmin/webadmin-mailbox/src/main/java/org/apache/james/webadmin/dto/QuotaDetailsDTO.java index 0519c43..e365ecc 100644 --- a/server/protocols/webadmin/webadmin-mailbox/src/main/java/org/apache/james/webadmin/dto/QuotaDetailsDTO.java +++ b/server/protocols/webadmin/webadmin-mailbox/src/main/java/org/apache/james/webadmin/dto/QuotaDetailsDTO.java @@ -23,6 +23,10 @@ package org.apache.james.webadmin.dto; import java.util.Optional; import org.apache.james.mailbox.model.Quota; +import org.apache.james.mailbox.quota.QuotaCount; +import org.apache.james.mailbox.quota.QuotaSize; + +import com.google.common.base.Preconditions; public class QuotaDetailsDTO { @@ -35,6 +39,7 @@ public class QuotaDetailsDTO { private Optional<QuotaDTO> domain; private Optional<QuotaDTO> user; private Optional<QuotaDTO> computed; + private OccupationDTO occupation; private Builder() { global = Optional.empty(); @@ -62,6 +67,11 @@ public class QuotaDetailsDTO { return this; } + public Builder occupation(Quota<QuotaSize> sizeQuota, Quota<QuotaCount> countQuota) { + this.occupation = OccupationDTO.from(sizeQuota, countQuota); + return this; + } + public Builder valueForScope(Quota.Scope scope, QuotaDTO value) { switch (scope) { case Global: @@ -75,7 +85,8 @@ public class QuotaDetailsDTO { } public QuotaDetailsDTO build() { - return new QuotaDetailsDTO(global, domain, user, computed); + Preconditions.checkNotNull(occupation); + return new QuotaDetailsDTO(global, domain, user, computed, occupation); } } @@ -83,12 +94,14 @@ public class QuotaDetailsDTO { private final Optional<QuotaDTO> domain; private final Optional<QuotaDTO> user; private final Optional<QuotaDTO> computed; + private final OccupationDTO occupation; - private QuotaDetailsDTO(Optional<QuotaDTO> global, Optional<QuotaDTO> domain, Optional<QuotaDTO> user, Optional<QuotaDTO> computed) { + private QuotaDetailsDTO(Optional<QuotaDTO> global, Optional<QuotaDTO> domain, Optional<QuotaDTO> user, Optional<QuotaDTO> computed, OccupationDTO occupation) { this.global = global; this.domain = domain; this.user = user; this.computed = computed; + this.occupation = occupation; } public Optional<QuotaDTO> getGlobal() { @@ -106,4 +119,8 @@ public class QuotaDetailsDTO { public Optional<QuotaDTO> getComputed() { return computed; } + + public OccupationDTO getOccupation() { + return occupation; + } } http://git-wip-us.apache.org/repos/asf/james-project/blob/4c1093dd/server/protocols/webadmin/webadmin-mailbox/src/main/java/org/apache/james/webadmin/service/UserQuotaService.java ---------------------------------------------------------------------- diff --git a/server/protocols/webadmin/webadmin-mailbox/src/main/java/org/apache/james/webadmin/service/UserQuotaService.java b/server/protocols/webadmin/webadmin-mailbox/src/main/java/org/apache/james/webadmin/service/UserQuotaService.java index 7e48bba..31a934f 100644 --- a/server/protocols/webadmin/webadmin-mailbox/src/main/java/org/apache/james/webadmin/service/UserQuotaService.java +++ b/server/protocols/webadmin/webadmin-mailbox/src/main/java/org/apache/james/webadmin/service/UserQuotaService.java @@ -31,6 +31,7 @@ import org.apache.james.mailbox.model.Quota; 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.QuotaManager; import org.apache.james.mailbox.quota.QuotaSize; import org.apache.james.mailbox.quota.UserQuotaRootResolver; import org.apache.james.webadmin.dto.QuotaDTO; @@ -42,11 +43,13 @@ import com.google.common.collect.Sets; public class UserQuotaService { private final MaxQuotaManager maxQuotaManager; + private final QuotaManager quotaManager; private final UserQuotaRootResolver userQuotaRootResolver; @Inject - public UserQuotaService(MaxQuotaManager maxQuotaManager, UserQuotaRootResolver userQuotaRootResolver) { + public UserQuotaService(MaxQuotaManager maxQuotaManager, QuotaManager quotaManager, UserQuotaRootResolver userQuotaRootResolver) { this.maxQuotaManager = maxQuotaManager; + this.quotaManager = quotaManager; this.userQuotaRootResolver = userQuotaRootResolver; } @@ -60,7 +63,9 @@ public class UserQuotaService { public QuotaDetailsDTO getQuota(User user) throws MailboxException { QuotaRoot quotaRoot = userQuotaRootResolver.forUser(user); - QuotaDetailsDTO.Builder quotaDetails = QuotaDetailsDTO.builder(); + QuotaDetailsDTO.Builder quotaDetails = QuotaDetailsDTO.builder() + .occupation(quotaManager.getStorageQuota(quotaRoot), + quotaManager.getMessageQuota(quotaRoot)); mergeMaps( maxQuotaManager.listMaxMessagesDetails(quotaRoot), http://git-wip-us.apache.org/repos/asf/james-project/blob/4c1093dd/server/protocols/webadmin/webadmin-mailbox/src/test/java/org/apache/james/webadmin/routes/UserQuotaRoutesTest.java ---------------------------------------------------------------------- diff --git a/server/protocols/webadmin/webadmin-mailbox/src/test/java/org/apache/james/webadmin/routes/UserQuotaRoutesTest.java b/server/protocols/webadmin/webadmin-mailbox/src/test/java/org/apache/james/webadmin/routes/UserQuotaRoutesTest.java index 2d877e3..b3c7204 100644 --- a/server/protocols/webadmin/webadmin-mailbox/src/test/java/org/apache/james/webadmin/routes/UserQuotaRoutesTest.java +++ b/server/protocols/webadmin/webadmin-mailbox/src/test/java/org/apache/james/webadmin/routes/UserQuotaRoutesTest.java @@ -23,6 +23,8 @@ import static com.jayway.restassured.RestAssured.given; import static com.jayway.restassured.RestAssured.when; import static org.apache.james.webadmin.WebAdminServer.NO_CONFIGURATION; import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.Matchers.any; +import static org.mockito.Mockito.mock; import java.util.Map; @@ -31,10 +33,13 @@ import org.apache.james.core.User; import org.apache.james.dnsservice.api.InMemoryDNSService; import org.apache.james.domainlist.memory.MemoryDomainList; import org.apache.james.mailbox.inmemory.quota.InMemoryPerUserMaxQuotaManager; +import org.apache.james.mailbox.quota.CurrentQuotaManager; import org.apache.james.mailbox.quota.QuotaCount; +import org.apache.james.mailbox.quota.QuotaManager; import org.apache.james.mailbox.quota.QuotaSize; import org.apache.james.mailbox.store.MailboxSessionMapperFactory; import org.apache.james.mailbox.store.quota.DefaultUserQuotaRootResolver; +import org.apache.james.mailbox.store.quota.StoreQuotaManager; import org.apache.james.metrics.api.NoopMetricFactory; import org.apache.james.user.memory.MemoryUsersRepository; import org.apache.james.webadmin.WebAdminServer; @@ -48,6 +53,7 @@ import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Disabled; import org.junit.jupiter.api.Test; +import org.mockito.Mockito; import com.google.common.collect.ImmutableSet; import com.jayway.restassured.RestAssured; @@ -67,6 +73,7 @@ class UserQuotaRoutesTest { private WebAdminServer webAdminServer; private InMemoryPerUserMaxQuotaManager maxQuotaManager; private DefaultUserQuotaRootResolver userQuotaRootResolver; + private CurrentQuotaManager currentQuotaManager; @BeforeEach void setUp() throws Exception { @@ -79,7 +86,13 @@ class UserQuotaRoutesTest { usersRepository.addUser(BOB.asString(), PASSWORD); MailboxSessionMapperFactory factory = null; userQuotaRootResolver = new DefaultUserQuotaRootResolver(factory); - UserQuotaService userQuotaService = new UserQuotaService(maxQuotaManager, userQuotaRootResolver); + + currentQuotaManager = mock(CurrentQuotaManager.class); + Mockito.when(currentQuotaManager.getCurrentMessageCount(any())).thenReturn(QuotaCount.count(0)); + Mockito.when(currentQuotaManager.getCurrentStorage(any())).thenReturn(QuotaSize.size(0)); + + QuotaManager quotaManager = new StoreQuotaManager(currentQuotaManager, maxQuotaManager); + UserQuotaService userQuotaService = new UserQuotaService(maxQuotaManager, quotaManager, userQuotaRootResolver); QuotaModule quotaModule = new QuotaModule(); UserQuotaRoutes userQuotaRoutes = new UserQuotaRoutes(usersRepository, userQuotaService, new JsonTransformer(quotaModule), ImmutableSet.of(quotaModule)); webAdminServer = WebAdminUtils.createWebAdminServer( @@ -422,6 +435,54 @@ class UserQuotaRoutesTest { } @Test + public void getQuotaShouldReturnOccupation() throws Exception { + maxQuotaManager.setMaxStorage(userQuotaRootResolver.forUser(BOB), QuotaSize.size(80)); + maxQuotaManager.setMaxMessage(userQuotaRootResolver.forUser(BOB), QuotaCount.count(100)); + Mockito.when(currentQuotaManager.getCurrentStorage(any())).thenReturn(QuotaSize.size(40)); + Mockito.when(currentQuotaManager.getCurrentMessageCount(any())).thenReturn(QuotaCount.count(20)); + + JsonPath jsonPath = + given() + .get(QUOTA_USERS + "/" + BOB.asString()) + .then() + .statusCode(HttpStatus.OK_200) + .contentType(ContentType.JSON) + .extract() + .jsonPath(); + + SoftAssertions softly = new SoftAssertions(); + softly.assertThat(jsonPath.getLong("occupation.count")).isEqualTo(20); + softly.assertThat(jsonPath.getLong("occupation.size")).isEqualTo(40); + softly.assertThat(jsonPath.getDouble("occupation.ratio.count")).isEqualTo(0.2); + softly.assertThat(jsonPath.getDouble("occupation.ratio.size")).isEqualTo(0.5); + softly.assertThat(jsonPath.getDouble("occupation.ratio.max")).isEqualTo(0.5); + } + + @Test + public void getQuotaShouldReturnOccupationWhenUnlimited() throws Exception { + maxQuotaManager.setMaxStorage(userQuotaRootResolver.forUser(BOB), QuotaSize.unlimited()); + maxQuotaManager.setMaxMessage(userQuotaRootResolver.forUser(BOB), QuotaCount.unlimited()); + Mockito.when(currentQuotaManager.getCurrentStorage(any())).thenReturn(QuotaSize.size(40)); + Mockito.when(currentQuotaManager.getCurrentMessageCount(any())).thenReturn(QuotaCount.count(20)); + + JsonPath jsonPath = + given() + .get(QUOTA_USERS + "/" + BOB.asString()) + .then() + .statusCode(HttpStatus.OK_200) + .contentType(ContentType.JSON) + .extract() + .jsonPath(); + + SoftAssertions softly = new SoftAssertions(); + softly.assertThat(jsonPath.getLong("occupation.count")).isEqualTo(20); + softly.assertThat(jsonPath.getLong("occupation.size")).isEqualTo(40); + softly.assertThat(jsonPath.getDouble("occupation.ratio.count")).isEqualTo(0); + softly.assertThat(jsonPath.getDouble("occupation.ratio.size")).isEqualTo(0); + softly.assertThat(jsonPath.getDouble("occupation.ratio.max")).isEqualTo(0); + } + + @Test public void getQuotaShouldReturnOnlySpecifiedValues() throws Exception { maxQuotaManager.setGlobalMaxStorage(QuotaSize.size(1111)); maxQuotaManager.setMaxMessage(userQuotaRootResolver.forUser(BOB), QuotaCount.count(18)); --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
