JAMES-2151 Update REST API endpoints for Sieve quota Differentiate /sieve/quota/default from /sieve/quota/users/[email protected]
Also DELETEs should be idempotent. Project: http://git-wip-us.apache.org/repos/asf/james-project/repo Commit: http://git-wip-us.apache.org/repos/asf/james-project/commit/3caa3d6c Tree: http://git-wip-us.apache.org/repos/asf/james-project/tree/3caa3d6c Diff: http://git-wip-us.apache.org/repos/asf/james-project/diff/3caa3d6c Branch: refs/heads/master Commit: 3caa3d6c3ae8366f21769000fe4c25b1666d8254 Parents: 685a13b Author: benwa <[email protected]> Authored: Fri Jun 22 11:34:10 2018 +0700 Committer: benwa <[email protected]> Committed: Tue Jun 26 16:06:31 2018 +0700 ---------------------------------------------------------------------- .../james/webadmin/routes/SieveQuotaRoutes.java | 29 ++++------ .../webadmin/routes/SieveQuotaRoutesTest.java | 58 ++++++++++---------- 2 files changed, 40 insertions(+), 47 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/james-project/blob/3caa3d6c/server/protocols/webadmin/webadmin-data/src/main/java/org/apache/james/webadmin/routes/SieveQuotaRoutes.java ---------------------------------------------------------------------- diff --git a/server/protocols/webadmin/webadmin-data/src/main/java/org/apache/james/webadmin/routes/SieveQuotaRoutes.java b/server/protocols/webadmin/webadmin-data/src/main/java/org/apache/james/webadmin/routes/SieveQuotaRoutes.java index a1fb584..1b99b72 100644 --- a/server/protocols/webadmin/webadmin-data/src/main/java/org/apache/james/webadmin/routes/SieveQuotaRoutes.java +++ b/server/protocols/webadmin/webadmin-data/src/main/java/org/apache/james/webadmin/routes/SieveQuotaRoutes.java @@ -42,6 +42,8 @@ import org.eclipse.jetty.http.HttpStatus; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import com.google.common.base.Joiner; + import io.swagger.annotations.Api; import io.swagger.annotations.ApiImplicitParam; import io.swagger.annotations.ApiImplicitParams; @@ -57,8 +59,9 @@ import spark.Service; public class SieveQuotaRoutes implements Routes { static final String ROOT_PATH = "/sieve/quota"; + public static final String DEFAULT_QUOTA_PATH = ROOT_PATH + SEPARATOR + "default"; private static final String USER_ID = "userId"; - private static final String USER_SIEVE_QUOTA_PATH = ROOT_PATH + SEPARATOR + ":" + USER_ID; + private static final String USER_SIEVE_QUOTA_PATH = Joiner.on(SEPARATOR).join(ROOT_PATH, "users", ":" + USER_ID); private static final String REQUESTED_SIZE = "requestedSize"; private static final Logger LOGGER = LoggerFactory.getLogger(SieveQuotaRoutes.class); @@ -92,7 +95,7 @@ public class SieveQuotaRoutes implements Routes { @ApiResponse(code = 500, message = "Internal server error - Something went bad on the server side.") }) public void defineGetGlobalSieveQuota(Service service) { - service.get(ROOT_PATH, (request, response) -> { + service.get(DEFAULT_QUOTA_PATH, (request, response) -> { try { QuotaSize sieveQuota = sieveQuotaRepository.getDefaultQuota(); response.status(HttpStatus.OK_200); @@ -119,7 +122,7 @@ public class SieveQuotaRoutes implements Routes { @ApiResponse(code = 500, message = "Internal server error - Something went bad on the server side.") }) public void defineUpdateGlobalSieveQuota(Service service) { - service.put(ROOT_PATH, (request, response) -> { + service.put(DEFAULT_QUOTA_PATH, (request, response) -> { try { QuotaSize requestedSize = extractRequestedQuotaSizeFromRequest(request); sieveQuotaRepository.setDefaultQuota(requestedSize); @@ -145,18 +148,13 @@ public class SieveQuotaRoutes implements Routes { @ApiResponse(code = 500, message = "Internal server error - Something went bad on the server side.") }) public void defineRemoveGlobalSieveQuota(Service service) { - service.delete(ROOT_PATH, (request, response) -> { + service.delete(DEFAULT_QUOTA_PATH, (request, response) -> { try { sieveQuotaRepository.removeQuota(); - response.status(HttpStatus.NO_CONTENT_204); } catch (QuotaNotFoundException e) { - LOGGER.info("Global sieve quota not set", e); - throw ErrorResponder.builder() - .type(ErrorResponder.ErrorType.NOT_FOUND) - .statusCode(HttpStatus.NOT_FOUND_404) - .message("Global sieve quota not set") - .haltError(); + // Do nothing } + response.status(HttpStatus.NO_CONTENT_204); return Constants.EMPTY_BODY; }); } @@ -235,15 +233,10 @@ public class SieveQuotaRoutes implements Routes { User userId = User.fromUsername(request.params(USER_ID)); try { sieveQuotaRepository.removeQuota(userId); - response.status(HttpStatus.NO_CONTENT_204); } catch (QuotaNotFoundException e) { - LOGGER.info("User sieve quota not set", e); - throw ErrorResponder.builder() - .type(ErrorResponder.ErrorType.NOT_FOUND) - .statusCode(HttpStatus.NOT_FOUND_404) - .message("User sieve quota not set") - .haltError(); + // Do nothing } + response.status(HttpStatus.NO_CONTENT_204); return Constants.EMPTY_BODY; }); } http://git-wip-us.apache.org/repos/asf/james-project/blob/3caa3d6c/server/protocols/webadmin/webadmin-data/src/test/java/org/apache/james/webadmin/routes/SieveQuotaRoutesTest.java ---------------------------------------------------------------------- diff --git a/server/protocols/webadmin/webadmin-data/src/test/java/org/apache/james/webadmin/routes/SieveQuotaRoutesTest.java b/server/protocols/webadmin/webadmin-data/src/test/java/org/apache/james/webadmin/routes/SieveQuotaRoutesTest.java index 41be8cc..80b3891 100644 --- a/server/protocols/webadmin/webadmin-data/src/test/java/org/apache/james/webadmin/routes/SieveQuotaRoutesTest.java +++ b/server/protocols/webadmin/webadmin-data/src/test/java/org/apache/james/webadmin/routes/SieveQuotaRoutesTest.java @@ -20,11 +20,11 @@ package org.apache.james.webadmin.routes; import static com.jayway.restassured.RestAssured.given; -import static org.apache.james.webadmin.Constants.SEPARATOR; import static org.apache.james.webadmin.WebAdminServer.NO_CONFIGURATION; -import static org.apache.james.webadmin.routes.SieveQuotaRoutes.ROOT_PATH; import static org.assertj.core.api.Assertions.assertThat; +import org.apache.james.core.User; +import org.apache.james.core.quota.QuotaSize; import org.apache.james.metrics.logger.DefaultMetricFactory; import org.apache.james.sieverepository.api.SieveQuotaRepository; import org.apache.james.sieverepository.memory.InMemorySieveQuotaRepository; @@ -41,7 +41,7 @@ import com.jayway.restassured.http.ContentType; public class SieveQuotaRoutesTest { - private static final String USER_A = "userA"; + private static final User USER_A = User.fromUsername("userA"); private WebAdminServer webAdminServer; private SieveQuotaRepository sieveRepository; @@ -67,47 +67,47 @@ public class SieveQuotaRoutesTest { @Test public void getGlobalSieveQuotaShouldReturn404WhenNoQuotaSet() { given() - .get(SieveQuotaRoutes.ROOT_PATH) + .get("/sieve/quota/default") .then() .statusCode(404); } @Test public void getGlobalSieveQuotaShouldReturnStoredValue() throws Exception { - long value = 1000L; + QuotaSize value = QuotaSize.size(1000L); sieveRepository.setDefaultQuota(value); long actual = given() - .get(SieveQuotaRoutes.ROOT_PATH) + .get("/sieve/quota/default") .then() .statusCode(HttpStatus.OK_200) .contentType(ContentType.JSON) .extract() .as(Long.class); - assertThat(actual).isEqualTo(value); + assertThat(actual).isEqualTo(value.asLong()); } @Test public void updateGlobalSieveQuotaShouldUpdateStoredValue() throws Exception { - sieveRepository.setDefaultQuota(500L); + sieveRepository.setDefaultQuota(QuotaSize.size(500L)); long requiredSize = 1024L; given() .body(requiredSize) - .put(SieveQuotaRoutes.ROOT_PATH) + .put("/sieve/quota/default") .then() .statusCode(HttpStatus.NO_CONTENT_204); - assertThat(sieveRepository.getDefaultQuota()).isEqualTo(requiredSize); + assertThat(sieveRepository.getDefaultQuota().asLong()).isEqualTo(requiredSize); } @Test public void updateGlobalSieveQuotaShouldReturn400WhenMalformedJSON() { given() .body("invalid") - .put(SieveQuotaRoutes.ROOT_PATH) + .put("/sieve/quota/default") .then() .statusCode(HttpStatus.BAD_REQUEST_400); } @@ -116,7 +116,7 @@ public class SieveQuotaRoutesTest { public void updateGlobalSieveQuotaShouldReturn400WhenRequestedSizeNotPositiveInteger() { given() .body(-100L) - .put(SieveQuotaRoutes.ROOT_PATH) + .put("/sieve/quota/default") .then() .statusCode(HttpStatus.BAD_REQUEST_400); } @@ -124,17 +124,17 @@ public class SieveQuotaRoutesTest { @Test public void removeGlobalSieveQuotaShouldReturn404WhenNoQuotaSet() { given() - .delete(SieveQuotaRoutes.ROOT_PATH) + .delete("/sieve/quota/default") .then() - .statusCode(HttpStatus.NOT_FOUND_404); + .statusCode(HttpStatus.NO_CONTENT_204); } @Test public void removeGlobalSieveQuotaShouldRemoveGlobalSieveQuota() throws Exception { - sieveRepository.setDefaultQuota(1024L); + sieveRepository.setDefaultQuota(QuotaSize.size(1024L)); given() - .delete(SieveQuotaRoutes.ROOT_PATH) + .delete("/sieve/quota/default") .then() .statusCode(HttpStatus.NO_CONTENT_204); } @@ -142,47 +142,47 @@ public class SieveQuotaRoutesTest { @Test public void getPerUserQuotaShouldReturn404WhenNoQuotaSetForUser() { given() - .get(ROOT_PATH + SEPARATOR + USER_A) + .get("/sieve/quota/users/" + USER_A.asString()) .then() .statusCode(HttpStatus.NOT_FOUND_404); } @Test public void getPerUserSieveQuotaShouldReturnedStoredValue() throws Exception { - long value = 1024L; + QuotaSize value = QuotaSize.size(1024L); sieveRepository.setQuota(USER_A, value); long actual = given() - .get(ROOT_PATH + SEPARATOR + USER_A) + .get("/sieve/quota/users/" + USER_A.asString()) .then() .statusCode(HttpStatus.OK_200) .contentType(ContentType.JSON) .extract() .as(Long.class); - assertThat(actual).isEqualTo(value); + assertThat(actual).isEqualTo(value.asLong()); } @Test public void updatePerUserSieveQuotaShouldUpdateStoredValue() throws Exception { - sieveRepository.setQuota(USER_A, 500L); + sieveRepository.setQuota(USER_A, QuotaSize.size(500L)); long requiredSize = 1024L; given() .body(requiredSize) - .put(ROOT_PATH + SEPARATOR + USER_A) + .put("/sieve/quota/users/" + USER_A.asString()) .then() .statusCode(HttpStatus.NO_CONTENT_204); - assertThat(sieveRepository.getQuota(USER_A)).isEqualTo(requiredSize); + assertThat(sieveRepository.getQuota(USER_A).asLong()).isEqualTo(requiredSize); } @Test public void updatePerUserSieveQuotaShouldReturn400WhenMalformedJSON() { given() .body("invalid") - .put(ROOT_PATH + SEPARATOR + USER_A) + .put("/sieve/quota/users/" + USER_A.asString()) .then() .statusCode(HttpStatus.BAD_REQUEST_400); } @@ -191,7 +191,7 @@ public class SieveQuotaRoutesTest { public void updatePerUserSieveQuotaShouldReturn400WhenRequestedSizeNotPositiveInteger() { given() .body(-100L) - .put(ROOT_PATH + SEPARATOR + USER_A) + .put("/sieve/quota/users/" + USER_A.asString()) .then() .statusCode(HttpStatus.BAD_REQUEST_400); } @@ -199,17 +199,17 @@ public class SieveQuotaRoutesTest { @Test public void removePerUserSieveQuotaShouldReturn404WhenNoQuotaSetForUser() { given() - .delete(ROOT_PATH + SEPARATOR + USER_A) + .delete("/sieve/quota/users/" + USER_A.asString()) .then() - .statusCode(HttpStatus.NOT_FOUND_404); + .statusCode(HttpStatus.NO_CONTENT_204); } @Test public void removePerUserSieveQuotaShouldRemoveQuotaForUser() throws Exception { - sieveRepository.setQuota(USER_A, 1024L); + sieveRepository.setQuota(USER_A, QuotaSize.size(1024)); given() - .delete(ROOT_PATH + SEPARATOR + USER_A) + .delete("/sieve/quota/users/" + USER_A.asString()) .then() .statusCode(HttpStatus.NO_CONTENT_204); } --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
