JAMES-2138 Top-level entities are usually in plural form
Project: http://git-wip-us.apache.org/repos/asf/james-project/repo Commit: http://git-wip-us.apache.org/repos/asf/james-project/commit/840a885c Tree: http://git-wip-us.apache.org/repos/asf/james-project/tree/840a885c Diff: http://git-wip-us.apache.org/repos/asf/james-project/diff/840a885c Branch: refs/heads/master Commit: 840a885caf0bc5761e4d52f835a3df4626dea429 Parents: 73b650e Author: Matthieu Baechler <matth...@apache.org> Authored: Mon Sep 4 10:17:42 2017 +0200 Committer: benwa <btell...@linagora.com> Committed: Sat Sep 9 10:40:29 2017 +0700 ---------------------------------------------------------------------- .../james/modules/server/DataRoutesModules.java | 4 +- .../integration/JwtFilterIntegrationTest.java | 4 +- .../WebAdminServerIntegrationTest.java | 4 +- .../james/webadmin/routes/DomainRoutes.java | 191 ----------- .../james/webadmin/routes/DomainsRoutes.java | 191 +++++++++++ .../james/webadmin/routes/DomainRoutesTest.java | 319 ------------------- .../webadmin/routes/DomainsRoutesTest.java | 319 +++++++++++++++++++ 7 files changed, 516 insertions(+), 516 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/james-project/blob/840a885c/server/container/guice/protocols/webadmin-data/src/main/java/org/apache/james/modules/server/DataRoutesModules.java ---------------------------------------------------------------------- diff --git a/server/container/guice/protocols/webadmin-data/src/main/java/org/apache/james/modules/server/DataRoutesModules.java b/server/container/guice/protocols/webadmin-data/src/main/java/org/apache/james/modules/server/DataRoutesModules.java index 3fb6669..48233d7 100644 --- a/server/container/guice/protocols/webadmin-data/src/main/java/org/apache/james/modules/server/DataRoutesModules.java +++ b/server/container/guice/protocols/webadmin-data/src/main/java/org/apache/james/modules/server/DataRoutesModules.java @@ -20,7 +20,7 @@ package org.apache.james.modules.server; import org.apache.james.webadmin.Routes; -import org.apache.james.webadmin.routes.DomainRoutes; +import org.apache.james.webadmin.routes.DomainsRoutes; import org.apache.james.webadmin.routes.UserRoutes; import com.google.inject.AbstractModule; @@ -31,7 +31,7 @@ public class DataRoutesModules extends AbstractModule { @Override protected void configure() { Multibinder<Routes> routesMultibinder = Multibinder.newSetBinder(binder(), Routes.class); - routesMultibinder.addBinding().to(DomainRoutes.class); + routesMultibinder.addBinding().to(DomainsRoutes.class); routesMultibinder.addBinding().to(UserRoutes.class); } } http://git-wip-us.apache.org/repos/asf/james-project/blob/840a885c/server/protocols/webadmin-integration-test/src/test/java/org/apache/james/webadmin/integration/JwtFilterIntegrationTest.java ---------------------------------------------------------------------- diff --git a/server/protocols/webadmin-integration-test/src/test/java/org/apache/james/webadmin/integration/JwtFilterIntegrationTest.java b/server/protocols/webadmin-integration-test/src/test/java/org/apache/james/webadmin/integration/JwtFilterIntegrationTest.java index e15f178..97e6deb 100644 --- a/server/protocols/webadmin-integration-test/src/test/java/org/apache/james/webadmin/integration/JwtFilterIntegrationTest.java +++ b/server/protocols/webadmin-integration-test/src/test/java/org/apache/james/webadmin/integration/JwtFilterIntegrationTest.java @@ -36,7 +36,7 @@ import org.apache.james.utils.DataProbeImpl; import org.apache.james.utils.WebAdminGuiceProbe; import org.apache.james.webadmin.authentication.AuthenticationFilter; import org.apache.james.webadmin.authentication.JwtFilter; -import org.apache.james.webadmin.routes.DomainRoutes; +import org.apache.james.webadmin.routes.DomainsRoutes; import org.junit.After; import org.junit.Before; import org.junit.ClassRule; @@ -52,7 +52,7 @@ import com.jayway.restassured.response.Header; public class JwtFilterIntegrationTest { private static final String DOMAIN = "domain"; - private static final String SPECIFIC_DOMAIN = DomainRoutes.DOMAINS + SEPARATOR + DOMAIN; + private static final String SPECIFIC_DOMAIN = DomainsRoutes.DOMAINS + SEPARATOR + DOMAIN; private static final String VALID_TOKEN_ADMIN_TRUE = "eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJhZG1pbkBvcGVuL" + "XBhYXMub3JnIiwiYWRtaW4iOnRydWUsImlhdCI6MTQ4OTAzODQzOH0.rgxCkdWEa-92a4R-72a9Z49k4LRvQDShgci5Y7qWRUP9IGJCK-lMkrHF" + "4H0a6L87BYppxVW701zaZ6dNxRMvHnjLBBWnPsC2B0rkkr2hEL2zfz7sb-iNGV-J4ICx97t8-TfQ5rz3VOX0FwdusPL_rJtmlGEGRivPkR6_aBe1" + http://git-wip-us.apache.org/repos/asf/james-project/blob/840a885c/server/protocols/webadmin-integration-test/src/test/java/org/apache/james/webadmin/integration/WebAdminServerIntegrationTest.java ---------------------------------------------------------------------- diff --git a/server/protocols/webadmin-integration-test/src/test/java/org/apache/james/webadmin/integration/WebAdminServerIntegrationTest.java b/server/protocols/webadmin-integration-test/src/test/java/org/apache/james/webadmin/integration/WebAdminServerIntegrationTest.java index bb243cc..028f8b3 100644 --- a/server/protocols/webadmin-integration-test/src/test/java/org/apache/james/webadmin/integration/WebAdminServerIntegrationTest.java +++ b/server/protocols/webadmin-integration-test/src/test/java/org/apache/james/webadmin/integration/WebAdminServerIntegrationTest.java @@ -35,7 +35,7 @@ import org.apache.james.modules.MailboxProbeImpl; import org.apache.james.probe.DataProbe; import org.apache.james.utils.DataProbeImpl; import org.apache.james.utils.WebAdminGuiceProbe; -import org.apache.james.webadmin.routes.DomainRoutes; +import org.apache.james.webadmin.routes.DomainsRoutes; import org.apache.james.webadmin.routes.UserMailboxesRoutes; import org.apache.james.webadmin.routes.UserRoutes; import org.apache.james.webadmin.swagger.routes.SwaggerRoutes; @@ -54,7 +54,7 @@ public class WebAdminServerIntegrationTest { public static final String DOMAIN = "domain"; public static final String USERNAME = "username@" + DOMAIN; - public static final String SPECIFIC_DOMAIN = DomainRoutes.DOMAINS + SEPARATOR + DOMAIN; + public static final String SPECIFIC_DOMAIN = DomainsRoutes.DOMAINS + SEPARATOR + DOMAIN; public static final String SPECIFIC_USER = UserRoutes.USERS + SEPARATOR + USERNAME; public static final String MAILBOX = "mailbox"; public static final String SPECIFIC_MAILBOX = SPECIFIC_USER + SEPARATOR + UserMailboxesRoutes.MAILBOXES + SEPARATOR + MAILBOX; http://git-wip-us.apache.org/repos/asf/james-project/blob/840a885c/server/protocols/webadmin/webadmin-data/src/main/java/org/apache/james/webadmin/routes/DomainRoutes.java ---------------------------------------------------------------------- diff --git a/server/protocols/webadmin/webadmin-data/src/main/java/org/apache/james/webadmin/routes/DomainRoutes.java b/server/protocols/webadmin/webadmin-data/src/main/java/org/apache/james/webadmin/routes/DomainRoutes.java deleted file mode 100644 index 60c8952..0000000 --- a/server/protocols/webadmin/webadmin-data/src/main/java/org/apache/james/webadmin/routes/DomainRoutes.java +++ /dev/null @@ -1,191 +0,0 @@ -/**************************************************************** - * 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.routes; - -import static org.apache.james.webadmin.Constants.SEPARATOR; - -import java.util.List; -import javax.inject.Inject; -import javax.ws.rs.DELETE; -import javax.ws.rs.GET; -import javax.ws.rs.PUT; -import javax.ws.rs.Path; -import javax.ws.rs.Produces; - -import org.apache.james.domainlist.api.DomainList; -import org.apache.james.domainlist.api.DomainListException; -import org.apache.james.webadmin.Constants; -import org.apache.james.webadmin.Routes; -import org.apache.james.webadmin.utils.JsonTransformer; - -import io.swagger.annotations.Api; -import io.swagger.annotations.ApiImplicitParam; -import io.swagger.annotations.ApiImplicitParams; -import io.swagger.annotations.ApiOperation; -import io.swagger.annotations.ApiResponse; -import io.swagger.annotations.ApiResponses; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import com.google.common.base.Preconditions; -import com.google.common.base.Strings; - -import spark.Request; -import spark.Response; -import spark.Service; - -@Api(tags = "Domains") -@Path(DomainRoutes.DOMAINS) -@Produces("application/json") -public class DomainRoutes implements Routes { - - private static final String DOMAIN_NAME = ":domainName"; - private static final Logger LOGGER = LoggerFactory.getLogger(DomainRoutes.class); - - public static final String DOMAINS = "/domains"; - public static final String SPECIFIC_DOMAIN = DOMAINS + SEPARATOR + DOMAIN_NAME; - public static final int MAXIMUM_DOMAIN_SIZE = 256; - - - private final DomainList domainList; - private final JsonTransformer jsonTransformer; - private Service service; - - @Inject - public DomainRoutes(DomainList domainList, JsonTransformer jsonTransformer) { - this.domainList = domainList; - this.jsonTransformer = jsonTransformer; - } - - @Override - public void define(Service service) { - this.service = service; - - defineGetDomains(); - - defineDomainExists(); - - defineAddDomain(); - - defineDeleteDomain(); - } - - @DELETE - @Path("/{domainName}") - @ApiOperation(value = "Deleting a domain") - @ApiImplicitParams({ - @ApiImplicitParam(required = true, dataType = "string", name = "domainName", paramType = "path") - }) - @ApiResponses(value = { - @ApiResponse(code = 204, message = "OK. Domain is removed."), - @ApiResponse(code = 500, message = "Internal server error - Something went bad on the server side.") - }) - public void defineDeleteDomain() { - service.delete(SPECIFIC_DOMAIN, this::removeDomain); - } - - @PUT - @Path("/{domainName}") - @ApiOperation(value = "Creating new domain") - @ApiImplicitParams({ - @ApiImplicitParam(required = true, dataType = "string", name = "domainName", paramType = "path") - }) - @ApiResponses(value = { - @ApiResponse(code = 204, message = "OK. New domain is created."), - @ApiResponse(code = 400, message = "Invalid request for domain creation"), - @ApiResponse(code = 500, message = "Internal server error - Something went bad on the server side.") - }) - public void defineAddDomain() { - service.put(SPECIFIC_DOMAIN, this::addDomain); - } - - @GET - @Path("/{domainName}") - @ApiImplicitParams({ - @ApiImplicitParam(required = true, dataType = "string", name = "domainName", paramType = "path") - }) - @ApiOperation(value = "Testing existence of a domain.") - @ApiResponses(value = { - @ApiResponse(code = 204, message = "The domain exists", response = String.class), - @ApiResponse(code = 404, message = "The domain does not exist."), - @ApiResponse(code = 500, message = "Internal server error - Something went bad on the server side.") - }) - public void defineDomainExists() { - service.get(SPECIFIC_DOMAIN, this::exists); - } - - @GET - @ApiOperation(value = "Getting all domains") - @ApiResponses(value = { - @ApiResponse(code = 204, message = "OK", response = List.class), - @ApiResponse(code = 500, message = "Internal server error - Something went bad on the server side.") - }) - public void defineGetDomains() { - service.get(DOMAINS, - (request, response) -> domainList.getDomains(), - jsonTransformer); - } - - private String removeDomain(Request request, Response response) { - try { - String domain = request.params(DOMAIN_NAME); - removeDomain(domain); - } catch (DomainListException e) { - LOGGER.info("{} did not exists", request.params(DOMAIN_NAME)); - } - response.status(204); - return Constants.EMPTY_BODY; - } - - private void removeDomain(String domain) throws DomainListException { - Preconditions.checkArgument(!Strings.isNullOrEmpty(domain)); - domainList.removeDomain(domain); - } - - private String addDomain(Request request, Response response) { - try { - addDomain(request.params(DOMAIN_NAME)); - response.status(204); - } catch (DomainListException e) { - LOGGER.info("{} already exists", request.params(DOMAIN_NAME)); - response.status(204); - } catch (IllegalArgumentException e) { - LOGGER.info("Invalid request for domain creation"); - response.status(400); - } - return Constants.EMPTY_BODY; - } - - private void addDomain(String domain) throws DomainListException { - Preconditions.checkArgument(!Strings.isNullOrEmpty(domain)); - Preconditions.checkArgument(!domain.contains("@")); - Preconditions.checkArgument(domain.length() < MAXIMUM_DOMAIN_SIZE); - domainList.addDomain(domain); - } - - private String exists(Request request, Response response) throws DomainListException { - if (!domainList.containsDomain(request.params(DOMAIN_NAME))) { - response.status(404); - } else { - response.status(204); - } - return Constants.EMPTY_BODY; - } -} http://git-wip-us.apache.org/repos/asf/james-project/blob/840a885c/server/protocols/webadmin/webadmin-data/src/main/java/org/apache/james/webadmin/routes/DomainsRoutes.java ---------------------------------------------------------------------- diff --git a/server/protocols/webadmin/webadmin-data/src/main/java/org/apache/james/webadmin/routes/DomainsRoutes.java b/server/protocols/webadmin/webadmin-data/src/main/java/org/apache/james/webadmin/routes/DomainsRoutes.java new file mode 100644 index 0000000..6b6fb9a --- /dev/null +++ b/server/protocols/webadmin/webadmin-data/src/main/java/org/apache/james/webadmin/routes/DomainsRoutes.java @@ -0,0 +1,191 @@ +/**************************************************************** + * 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.routes; + +import static org.apache.james.webadmin.Constants.SEPARATOR; + +import java.util.List; +import javax.inject.Inject; +import javax.ws.rs.DELETE; +import javax.ws.rs.GET; +import javax.ws.rs.PUT; +import javax.ws.rs.Path; +import javax.ws.rs.Produces; + +import org.apache.james.domainlist.api.DomainList; +import org.apache.james.domainlist.api.DomainListException; +import org.apache.james.webadmin.Constants; +import org.apache.james.webadmin.Routes; +import org.apache.james.webadmin.utils.JsonTransformer; + +import io.swagger.annotations.Api; +import io.swagger.annotations.ApiImplicitParam; +import io.swagger.annotations.ApiImplicitParams; +import io.swagger.annotations.ApiOperation; +import io.swagger.annotations.ApiResponse; +import io.swagger.annotations.ApiResponses; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import com.google.common.base.Preconditions; +import com.google.common.base.Strings; + +import spark.Request; +import spark.Response; +import spark.Service; + +@Api(tags = "Domains") +@Path(DomainsRoutes.DOMAINS) +@Produces("application/json") +public class DomainsRoutes implements Routes { + + private static final String DOMAIN_NAME = ":domainName"; + private static final Logger LOGGER = LoggerFactory.getLogger(DomainsRoutes.class); + + public static final String DOMAINS = "/domains"; + public static final String SPECIFIC_DOMAIN = DOMAINS + SEPARATOR + DOMAIN_NAME; + public static final int MAXIMUM_DOMAIN_SIZE = 256; + + + private final DomainList domainList; + private final JsonTransformer jsonTransformer; + private Service service; + + @Inject + public DomainsRoutes(DomainList domainList, JsonTransformer jsonTransformer) { + this.domainList = domainList; + this.jsonTransformer = jsonTransformer; + } + + @Override + public void define(Service service) { + this.service = service; + + defineGetDomains(); + + defineDomainExists(); + + defineAddDomain(); + + defineDeleteDomain(); + } + + @DELETE + @Path("/{domainName}") + @ApiOperation(value = "Deleting a domain") + @ApiImplicitParams({ + @ApiImplicitParam(required = true, dataType = "string", name = "domainName", paramType = "path") + }) + @ApiResponses(value = { + @ApiResponse(code = 204, message = "OK. Domain is removed."), + @ApiResponse(code = 500, message = "Internal server error - Something went bad on the server side.") + }) + public void defineDeleteDomain() { + service.delete(SPECIFIC_DOMAIN, this::removeDomain); + } + + @PUT + @Path("/{domainName}") + @ApiOperation(value = "Creating new domain") + @ApiImplicitParams({ + @ApiImplicitParam(required = true, dataType = "string", name = "domainName", paramType = "path") + }) + @ApiResponses(value = { + @ApiResponse(code = 204, message = "OK. New domain is created."), + @ApiResponse(code = 400, message = "Invalid request for domain creation"), + @ApiResponse(code = 500, message = "Internal server error - Something went bad on the server side.") + }) + public void defineAddDomain() { + service.put(SPECIFIC_DOMAIN, this::addDomain); + } + + @GET + @Path("/{domainName}") + @ApiImplicitParams({ + @ApiImplicitParam(required = true, dataType = "string", name = "domainName", paramType = "path") + }) + @ApiOperation(value = "Testing existence of a domain.") + @ApiResponses(value = { + @ApiResponse(code = 204, message = "The domain exists", response = String.class), + @ApiResponse(code = 404, message = "The domain does not exist."), + @ApiResponse(code = 500, message = "Internal server error - Something went bad on the server side.") + }) + public void defineDomainExists() { + service.get(SPECIFIC_DOMAIN, this::exists); + } + + @GET + @ApiOperation(value = "Getting all domains") + @ApiResponses(value = { + @ApiResponse(code = 204, message = "OK", response = List.class), + @ApiResponse(code = 500, message = "Internal server error - Something went bad on the server side.") + }) + public void defineGetDomains() { + service.get(DOMAINS, + (request, response) -> domainList.getDomains(), + jsonTransformer); + } + + private String removeDomain(Request request, Response response) { + try { + String domain = request.params(DOMAIN_NAME); + removeDomain(domain); + } catch (DomainListException e) { + LOGGER.info("{} did not exists", request.params(DOMAIN_NAME)); + } + response.status(204); + return Constants.EMPTY_BODY; + } + + private void removeDomain(String domain) throws DomainListException { + Preconditions.checkArgument(!Strings.isNullOrEmpty(domain)); + domainList.removeDomain(domain); + } + + private String addDomain(Request request, Response response) { + try { + addDomain(request.params(DOMAIN_NAME)); + response.status(204); + } catch (DomainListException e) { + LOGGER.info("{} already exists", request.params(DOMAIN_NAME)); + response.status(204); + } catch (IllegalArgumentException e) { + LOGGER.info("Invalid request for domain creation"); + response.status(400); + } + return Constants.EMPTY_BODY; + } + + private void addDomain(String domain) throws DomainListException { + Preconditions.checkArgument(!Strings.isNullOrEmpty(domain)); + Preconditions.checkArgument(!domain.contains("@")); + Preconditions.checkArgument(domain.length() < MAXIMUM_DOMAIN_SIZE); + domainList.addDomain(domain); + } + + private String exists(Request request, Response response) throws DomainListException { + if (!domainList.containsDomain(request.params(DOMAIN_NAME))) { + response.status(404); + } else { + response.status(204); + } + return Constants.EMPTY_BODY; + } +} http://git-wip-us.apache.org/repos/asf/james-project/blob/840a885c/server/protocols/webadmin/webadmin-data/src/test/java/org/apache/james/webadmin/routes/DomainRoutesTest.java ---------------------------------------------------------------------- diff --git a/server/protocols/webadmin/webadmin-data/src/test/java/org/apache/james/webadmin/routes/DomainRoutesTest.java b/server/protocols/webadmin/webadmin-data/src/test/java/org/apache/james/webadmin/routes/DomainRoutesTest.java deleted file mode 100644 index f17acd3..0000000 --- a/server/protocols/webadmin/webadmin-data/src/test/java/org/apache/james/webadmin/routes/DomainRoutesTest.java +++ /dev/null @@ -1,319 +0,0 @@ -/**************************************************************** - * 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.routes; - -import static com.jayway.restassured.RestAssured.given; -import static com.jayway.restassured.RestAssured.when; -import static com.jayway.restassured.RestAssured.with; -import static com.jayway.restassured.config.EncoderConfig.encoderConfig; -import static com.jayway.restassured.config.RestAssuredConfig.newConfig; -import static org.apache.james.webadmin.Constants.SEPARATOR; -import static org.apache.james.webadmin.WebAdminServer.NO_CONFIGURATION; -import static org.hamcrest.CoreMatchers.containsString; -import static org.hamcrest.CoreMatchers.is; -import static org.mockito.Matchers.any; -import static org.mockito.Mockito.doThrow; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; - -import java.net.InetAddress; -import java.nio.charset.StandardCharsets; - -import org.apache.james.dnsservice.api.DNSService; -import org.apache.james.domainlist.api.DomainList; -import org.apache.james.domainlist.api.DomainListException; -import org.apache.james.domainlist.memory.MemoryDomainList; -import org.apache.james.metrics.logger.DefaultMetricFactory; -import org.apache.james.webadmin.WebAdminServer; -import org.apache.james.webadmin.utils.JsonTransformer; -import org.junit.After; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; - -import com.jayway.restassured.RestAssured; -import com.jayway.restassured.builder.RequestSpecBuilder; -import com.jayway.restassured.http.ContentType; -import de.bechte.junit.runners.context.HierarchicalContextRunner; - -@RunWith(HierarchicalContextRunner.class) -public class DomainRoutesTest { - public static final String DOMAIN = "domain"; - - private WebAdminServer webAdminServer; - - private void createServer(DomainList domainList) throws Exception { - webAdminServer = new WebAdminServer( - new DefaultMetricFactory(), - new DomainRoutes(domainList, new JsonTransformer())); - webAdminServer.configure(NO_CONFIGURATION); - webAdminServer.await(); - - RestAssured.requestSpecification = new RequestSpecBuilder() - .setContentType(ContentType.JSON) - .setAccept(ContentType.JSON) - .setConfig(newConfig().encoderConfig(encoderConfig().defaultContentCharset(StandardCharsets.UTF_8))) - .setPort(webAdminServer.getPort().toInt()) - .setBasePath(DomainRoutes.DOMAINS) - .build(); - - } - - @After - public void stop() { - webAdminServer.destroy(); - } - - public class NormalBehaviour { - - @Before - public void setUp() throws Exception { - DNSService dnsService = mock(DNSService.class); - when(dnsService.getHostName(any())).thenReturn("localhost"); - when(dnsService.getLocalHost()).thenReturn(InetAddress.getByName("localhost")); - - MemoryDomainList domainList = new MemoryDomainList(dnsService); - domainList.setAutoDetectIP(false); - createServer(domainList); - } - - @Test - public void getDomainsShouldBeEmptyByDefault() { - given() - .get() - .then() - .statusCode(200) - .body(is("[]")); - } - - @Test - public void putShouldReturnErrorWhenUsedWithEmptyDomain() { - given() - .put(SEPARATOR) - .then() - .statusCode(404); - } - - @Test - public void deleteShouldReturnErrorWhenUsedWithEmptyDomain() { - given() - .delete(SEPARATOR) - .then() - .statusCode(404); - } - - @Test - public void putShouldBeOk() { - given() - .put(DOMAIN) - .then() - .statusCode(204); - } - - @Test - public void getDomainsShouldDisplayAddedDomains() { - with() - .put(DOMAIN); - - when() - .get() - .then() - .statusCode(200) - .body(containsString(DOMAIN)); - } - - @Test - public void putShouldReturnUserErrorWhenNameContainsAT() { - when() - .put(DOMAIN + "@" + DOMAIN) - .then() - .statusCode(400); - } - - @Test - public void putShouldReturnUserErrorWhenNameContainsUrlSeparator() { - when() - .put(DOMAIN + "/" + DOMAIN) - .then() - .statusCode(404); - } - - @Test - public void putShouldReturnUserErrorWhenNameIsTooLong() { - when() - .put(DOMAIN + "0123456789.0123456789.0123456789.0123456789.0123456789.0123456789.0123456789.0123456789.0123456789.0123456789." + - "0123456789.0123456789.0123456789.0123456789.0123456789.0123456789.0123456789.0123456789.0123456789.0123456789." + - "0123456789.0123456789.0123456789.") - .then() - .statusCode(400); - } - - @Test - public void putShouldWorkOnTheSecondTimeForAGivenValue() { - with() - .put(DOMAIN); - - when() - .put(DOMAIN) - .then() - .statusCode(204); - } - - @Test - public void deleteShouldRemoveTheGivenDomain() { - with() - .put(DOMAIN); - - when() - .delete(DOMAIN) - .then() - .statusCode(204); - - when() - .get() - .then() - .statusCode(200) - .body(is("[]")); - } - - @Test - public void deleteShouldBeOkWhenTheDomainIsNotPresent() { - given() - .delete(DOMAIN) - .then() - .statusCode(204); - } - - @Test - public void getDomainShouldReturnOkWhenTheDomainIsPresent() { - with() - .put(DOMAIN); - - when() - .get(DOMAIN) - .then() - .statusCode(204); - } - - @Test - public void getDomainShouldReturnNotFoundWhenTheDomainIsAbsent() { - given() - .get(DOMAIN) - .then() - .statusCode(404); - } - - } - - public class ExceptionHandling { - - private DomainList domainList; - private String domain; - - @Before - public void setUp() throws Exception { - domainList = mock(DomainList.class); - createServer(domainList); - domain = "domain"; - } - - @Test - public void deleteShouldReturnErrorOnUnknownException() throws Exception { - doThrow(new RuntimeException()).when(domainList).removeDomain(domain); - - when() - .delete(DOMAIN) - .then() - .statusCode(500); - } - - @Test - public void putShouldReturnErrorOnUnknownException() throws Exception { - doThrow(new RuntimeException()).when(domainList).addDomain(domain); - - when() - .put(DOMAIN) - .then() - .statusCode(500); - } - - @Test - public void getDomainShouldReturnErrorOnUnknownException() throws Exception { - when(domainList.containsDomain(domain)).thenThrow(new RuntimeException()); - - when() - .get(DOMAIN) - .then() - .statusCode(500); - } - - @Test - public void getDomainsShouldReturnErrorOnUnknownException() throws Exception { - when(domainList.getDomains()).thenThrow(new RuntimeException()); - - when() - .get() - .then() - .statusCode(500); - } - - @Test - public void deleteShouldReturnOkWhenDomainListException() throws Exception { - doThrow(new DomainListException("message")).when(domainList).removeDomain(domain); - - when() - .delete(DOMAIN) - .then() - .statusCode(204); - } - - @Test - public void putShouldReturnOkWhenDomainListException() throws Exception { - doThrow(new DomainListException("message")).when(domainList).addDomain(domain); - - when() - .put(DOMAIN) - .then() - .statusCode(204); - } - - @Test - public void getDomainShouldReturnErrorOnDomainListException() throws Exception { - when(domainList.containsDomain(domain)).thenThrow(new DomainListException("message")); - - when() - .get(DOMAIN) - .then() - .statusCode(500); - } - - @Test - public void getDomainsShouldReturnErrorOnDomainListException() throws Exception { - when(domainList.getDomains()).thenThrow(new DomainListException("message")); - - when() - .get() - .then() - .statusCode(500); - } - - } - -} http://git-wip-us.apache.org/repos/asf/james-project/blob/840a885c/server/protocols/webadmin/webadmin-data/src/test/java/org/apache/james/webadmin/routes/DomainsRoutesTest.java ---------------------------------------------------------------------- diff --git a/server/protocols/webadmin/webadmin-data/src/test/java/org/apache/james/webadmin/routes/DomainsRoutesTest.java b/server/protocols/webadmin/webadmin-data/src/test/java/org/apache/james/webadmin/routes/DomainsRoutesTest.java new file mode 100644 index 0000000..23739f9 --- /dev/null +++ b/server/protocols/webadmin/webadmin-data/src/test/java/org/apache/james/webadmin/routes/DomainsRoutesTest.java @@ -0,0 +1,319 @@ +/**************************************************************** + * 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.routes; + +import static com.jayway.restassured.RestAssured.given; +import static com.jayway.restassured.RestAssured.when; +import static com.jayway.restassured.RestAssured.with; +import static com.jayway.restassured.config.EncoderConfig.encoderConfig; +import static com.jayway.restassured.config.RestAssuredConfig.newConfig; +import static org.apache.james.webadmin.Constants.SEPARATOR; +import static org.apache.james.webadmin.WebAdminServer.NO_CONFIGURATION; +import static org.hamcrest.CoreMatchers.containsString; +import static org.hamcrest.CoreMatchers.is; +import static org.mockito.Matchers.any; +import static org.mockito.Mockito.doThrow; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +import java.net.InetAddress; +import java.nio.charset.StandardCharsets; + +import org.apache.james.dnsservice.api.DNSService; +import org.apache.james.domainlist.api.DomainList; +import org.apache.james.domainlist.api.DomainListException; +import org.apache.james.domainlist.memory.MemoryDomainList; +import org.apache.james.metrics.logger.DefaultMetricFactory; +import org.apache.james.webadmin.WebAdminServer; +import org.apache.james.webadmin.utils.JsonTransformer; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; + +import com.jayway.restassured.RestAssured; +import com.jayway.restassured.builder.RequestSpecBuilder; +import com.jayway.restassured.http.ContentType; +import de.bechte.junit.runners.context.HierarchicalContextRunner; + +@RunWith(HierarchicalContextRunner.class) +public class DomainsRoutesTest { + public static final String DOMAIN = "domain"; + + private WebAdminServer webAdminServer; + + private void createServer(DomainList domainList) throws Exception { + webAdminServer = new WebAdminServer( + new DefaultMetricFactory(), + new DomainsRoutes(domainList, new JsonTransformer())); + webAdminServer.configure(NO_CONFIGURATION); + webAdminServer.await(); + + RestAssured.requestSpecification = new RequestSpecBuilder() + .setContentType(ContentType.JSON) + .setAccept(ContentType.JSON) + .setConfig(newConfig().encoderConfig(encoderConfig().defaultContentCharset(StandardCharsets.UTF_8))) + .setPort(webAdminServer.getPort().toInt()) + .setBasePath(DomainsRoutes.DOMAINS) + .build(); + + } + + @After + public void stop() { + webAdminServer.destroy(); + } + + public class NormalBehaviour { + + @Before + public void setUp() throws Exception { + DNSService dnsService = mock(DNSService.class); + when(dnsService.getHostName(any())).thenReturn("localhost"); + when(dnsService.getLocalHost()).thenReturn(InetAddress.getByName("localhost")); + + MemoryDomainList domainList = new MemoryDomainList(dnsService); + domainList.setAutoDetectIP(false); + createServer(domainList); + } + + @Test + public void getDomainsShouldBeEmptyByDefault() { + given() + .get() + .then() + .statusCode(200) + .body(is("[]")); + } + + @Test + public void putShouldReturnErrorWhenUsedWithEmptyDomain() { + given() + .put(SEPARATOR) + .then() + .statusCode(404); + } + + @Test + public void deleteShouldReturnErrorWhenUsedWithEmptyDomain() { + given() + .delete(SEPARATOR) + .then() + .statusCode(404); + } + + @Test + public void putShouldBeOk() { + given() + .put(DOMAIN) + .then() + .statusCode(204); + } + + @Test + public void getDomainsShouldDisplayAddedDomains() { + with() + .put(DOMAIN); + + when() + .get() + .then() + .statusCode(200) + .body(containsString(DOMAIN)); + } + + @Test + public void putShouldReturnUserErrorWhenNameContainsAT() { + when() + .put(DOMAIN + "@" + DOMAIN) + .then() + .statusCode(400); + } + + @Test + public void putShouldReturnUserErrorWhenNameContainsUrlSeparator() { + when() + .put(DOMAIN + "/" + DOMAIN) + .then() + .statusCode(404); + } + + @Test + public void putShouldReturnUserErrorWhenNameIsTooLong() { + when() + .put(DOMAIN + "0123456789.0123456789.0123456789.0123456789.0123456789.0123456789.0123456789.0123456789.0123456789.0123456789." + + "0123456789.0123456789.0123456789.0123456789.0123456789.0123456789.0123456789.0123456789.0123456789.0123456789." + + "0123456789.0123456789.0123456789.") + .then() + .statusCode(400); + } + + @Test + public void putShouldWorkOnTheSecondTimeForAGivenValue() { + with() + .put(DOMAIN); + + when() + .put(DOMAIN) + .then() + .statusCode(204); + } + + @Test + public void deleteShouldRemoveTheGivenDomain() { + with() + .put(DOMAIN); + + when() + .delete(DOMAIN) + .then() + .statusCode(204); + + when() + .get() + .then() + .statusCode(200) + .body(is("[]")); + } + + @Test + public void deleteShouldBeOkWhenTheDomainIsNotPresent() { + given() + .delete(DOMAIN) + .then() + .statusCode(204); + } + + @Test + public void getDomainShouldReturnOkWhenTheDomainIsPresent() { + with() + .put(DOMAIN); + + when() + .get(DOMAIN) + .then() + .statusCode(204); + } + + @Test + public void getDomainShouldReturnNotFoundWhenTheDomainIsAbsent() { + given() + .get(DOMAIN) + .then() + .statusCode(404); + } + + } + + public class ExceptionHandling { + + private DomainList domainList; + private String domain; + + @Before + public void setUp() throws Exception { + domainList = mock(DomainList.class); + createServer(domainList); + domain = "domain"; + } + + @Test + public void deleteShouldReturnErrorOnUnknownException() throws Exception { + doThrow(new RuntimeException()).when(domainList).removeDomain(domain); + + when() + .delete(DOMAIN) + .then() + .statusCode(500); + } + + @Test + public void putShouldReturnErrorOnUnknownException() throws Exception { + doThrow(new RuntimeException()).when(domainList).addDomain(domain); + + when() + .put(DOMAIN) + .then() + .statusCode(500); + } + + @Test + public void getDomainShouldReturnErrorOnUnknownException() throws Exception { + when(domainList.containsDomain(domain)).thenThrow(new RuntimeException()); + + when() + .get(DOMAIN) + .then() + .statusCode(500); + } + + @Test + public void getDomainsShouldReturnErrorOnUnknownException() throws Exception { + when(domainList.getDomains()).thenThrow(new RuntimeException()); + + when() + .get() + .then() + .statusCode(500); + } + + @Test + public void deleteShouldReturnOkWhenDomainListException() throws Exception { + doThrow(new DomainListException("message")).when(domainList).removeDomain(domain); + + when() + .delete(DOMAIN) + .then() + .statusCode(204); + } + + @Test + public void putShouldReturnOkWhenDomainListException() throws Exception { + doThrow(new DomainListException("message")).when(domainList).addDomain(domain); + + when() + .put(DOMAIN) + .then() + .statusCode(204); + } + + @Test + public void getDomainShouldReturnErrorOnDomainListException() throws Exception { + when(domainList.containsDomain(domain)).thenThrow(new DomainListException("message")); + + when() + .get(DOMAIN) + .then() + .statusCode(500); + } + + @Test + public void getDomainsShouldReturnErrorOnDomainListException() throws Exception { + when(domainList.getDomains()).thenThrow(new DomainListException("message")); + + when() + .get() + .then() + .statusCode(500); + } + + } + +} --------------------------------------------------------------------- To unsubscribe, e-mail: server-dev-unsubscr...@james.apache.org For additional commands, e-mail: server-dev-h...@james.apache.org