You are viewing a plain text version of this content. The canonical link for it is here.
Posted to server-dev@james.apache.org by bt...@apache.org on 2018/01/25 05:02:24 UTC
[13/16] james-project git commit: JAMES-2292 GET mailQueue
JAMES-2292 GET mailQueue
change swagger documentation
we should not log + throw Exception
refactor getMailQueue
Project: http://git-wip-us.apache.org/repos/asf/james-project/repo
Commit: http://git-wip-us.apache.org/repos/asf/james-project/commit/2ecbdea3
Tree: http://git-wip-us.apache.org/repos/asf/james-project/tree/2ecbdea3
Diff: http://git-wip-us.apache.org/repos/asf/james-project/diff/2ecbdea3
Branch: refs/heads/master
Commit: 2ecbdea32b8b76b796106a9752016b8e91f23782
Parents: 0e1719d
Author: Antoine Duprat <ad...@linagora.com>
Authored: Tue Jan 23 13:29:12 2018 +0100
Committer: benwa <bt...@linagora.com>
Committed: Thu Jan 25 11:46:04 2018 +0700
----------------------------------------------------------------------
.../webadmin/webadmin-mailqueue/pom.xml | 12 +++
.../apache/james/webadmin/dto/MailQueueDTO.java | 80 ++++++++++++++++++++
.../james/webadmin/routes/MailQueueRoutes.java | 70 ++++++++++++++++-
.../james/webadmin/dto/MailQueueDTOTest.java | 57 ++++++++++++++
.../webadmin/routes/MailQueueRoutesTest.java | 32 +++++++-
5 files changed, 247 insertions(+), 4 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/james-project/blob/2ecbdea3/server/protocols/webadmin/webadmin-mailqueue/pom.xml
----------------------------------------------------------------------
diff --git a/server/protocols/webadmin/webadmin-mailqueue/pom.xml b/server/protocols/webadmin/webadmin-mailqueue/pom.xml
index 4bcd366..bdb1860 100644
--- a/server/protocols/webadmin/webadmin-mailqueue/pom.xml
+++ b/server/protocols/webadmin/webadmin-mailqueue/pom.xml
@@ -35,10 +35,22 @@
<dependencies>
<dependency>
<groupId>${project.groupId}</groupId>
+ <artifactId>apache-mailet-base</artifactId>
+ <type>test-jar</type>
+ <scope>test</scope>
+ </dependency>
+ <dependency>
+ <groupId>${project.groupId}</groupId>
<artifactId>james-server-queue-api</artifactId>
</dependency>
<dependency>
<groupId>${project.groupId}</groupId>
+ <artifactId>james-server-queue-api</artifactId>
+ <type>test-jar</type>
+ <scope>test</scope>
+ </dependency>
+ <dependency>
+ <groupId>${project.groupId}</groupId>
<artifactId>james-server-queue-memory</artifactId>
<scope>test</scope>
</dependency>
http://git-wip-us.apache.org/repos/asf/james-project/blob/2ecbdea3/server/protocols/webadmin/webadmin-mailqueue/src/main/java/org/apache/james/webadmin/dto/MailQueueDTO.java
----------------------------------------------------------------------
diff --git a/server/protocols/webadmin/webadmin-mailqueue/src/main/java/org/apache/james/webadmin/dto/MailQueueDTO.java b/server/protocols/webadmin/webadmin-mailqueue/src/main/java/org/apache/james/webadmin/dto/MailQueueDTO.java
new file mode 100644
index 0000000..39198d0
--- /dev/null
+++ b/server/protocols/webadmin/webadmin-mailqueue/src/main/java/org/apache/james/webadmin/dto/MailQueueDTO.java
@@ -0,0 +1,80 @@
+/****************************************************************
+ * 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.queue.api.MailQueue.MailQueueException;
+import org.apache.james.queue.api.ManageableMailQueue;
+
+import com.google.common.base.Preconditions;
+import com.google.common.base.Strings;
+
+public class MailQueueDTO {
+
+ public static Builder builder() {
+ return new Builder();
+ }
+
+ public static MailQueueDTO from(ManageableMailQueue mailQueue) throws MailQueueException {
+ return builder()
+ .name(mailQueue.getName())
+ .size(mailQueue.getSize())
+ .build();
+ }
+
+ public static class Builder {
+
+ private String name;
+ private long size;
+
+ private Builder() {
+ }
+
+ public Builder name(String name) {
+ this.name = name;
+ return this;
+ }
+
+ public Builder size(long size) {
+ this.size = size;
+ return this;
+ }
+
+ public MailQueueDTO build() {
+ Preconditions.checkArgument(!Strings.isNullOrEmpty(name), "name is mandatory");
+ return new MailQueueDTO(name, size);
+ }
+ }
+
+ private final String name;
+ private final long size;
+
+ private MailQueueDTO(String name, long size) {
+ this.name = name;
+ this.size = size;
+ }
+
+ public String getName() {
+ return name;
+ }
+
+ public long getSize() {
+ return size;
+ }
+}
http://git-wip-us.apache.org/repos/asf/james-project/blob/2ecbdea3/server/protocols/webadmin/webadmin-mailqueue/src/main/java/org/apache/james/webadmin/routes/MailQueueRoutes.java
----------------------------------------------------------------------
diff --git a/server/protocols/webadmin/webadmin-mailqueue/src/main/java/org/apache/james/webadmin/routes/MailQueueRoutes.java b/server/protocols/webadmin/webadmin-mailqueue/src/main/java/org/apache/james/webadmin/routes/MailQueueRoutes.java
index b2ef8f6..5a6aa15 100644
--- a/server/protocols/webadmin/webadmin-mailqueue/src/main/java/org/apache/james/webadmin/routes/MailQueueRoutes.java
+++ b/server/protocols/webadmin/webadmin-mailqueue/src/main/java/org/apache/james/webadmin/routes/MailQueueRoutes.java
@@ -19,27 +19,49 @@
package org.apache.james.webadmin.routes;
+import static org.apache.james.webadmin.Constants.SEPARATOR;
+
import java.util.List;
+import java.util.Optional;
import javax.inject.Inject;
import javax.ws.rs.GET;
+import javax.ws.rs.Path;
+import javax.ws.rs.Produces;
+import org.apache.james.queue.api.MailQueue.MailQueueException;
import org.apache.james.queue.api.MailQueueFactory;
import org.apache.james.queue.api.ManageableMailQueue;
import org.apache.james.webadmin.Routes;
+import org.apache.james.webadmin.dto.MailQueueDTO;
+import org.apache.james.webadmin.utils.ErrorResponder;
+import org.apache.james.webadmin.utils.ErrorResponder.ErrorType;
import org.apache.james.webadmin.utils.JsonTransformer;
import org.eclipse.jetty.http.HttpStatus;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
import com.github.steveash.guavate.Guavate;
import com.google.common.annotations.VisibleForTesting;
+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 spark.Request;
import spark.Service;
+
+@Api(tags = "MailQueues")
+@Path(MailQueueRoutes.BASE_URL)
+@Produces("application/json")
public class MailQueueRoutes implements Routes {
- @VisibleForTesting static final String BASE_URL = "/mailQueues";
+ @VisibleForTesting static final String BASE_URL = "/mailQueues";
+ @VisibleForTesting static final String MAIL_QUEUE_NAME = ":mailQueueName";
+ private static final Logger LOGGER = LoggerFactory.getLogger(MailQueueRoutes.class);
+
private final MailQueueFactory<ManageableMailQueue> mailQueueFactory;
private final JsonTransformer jsonTransformer;
@@ -52,6 +74,8 @@ public class MailQueueRoutes implements Routes {
@Override
public void define(Service service) {
defineListQueues(service);
+
+ getMailQueue(service);
}
@GET
@@ -72,4 +96,48 @@ public class MailQueueRoutes implements Routes {
.collect(Guavate.toImmutableList()),
jsonTransformer);
}
+
+ @GET
+ @Path("/{mailQueueName}")
+ @ApiImplicitParams({
+ @ApiImplicitParam(required = true, dataType = "string", name = "mailQueueName", paramType = "path")
+ })
+ @ApiOperation(
+ value = "Get a MailQueue details"
+ )
+ @ApiResponses(value = {
+ @ApiResponse(code = HttpStatus.OK_200, message = "OK", response = MailQueueDTO.class),
+ @ApiResponse(code = HttpStatus.BAD_REQUEST_400, message = "Invalid request for getting the mail queue."),
+ @ApiResponse(code = HttpStatus.NOT_FOUND_404, message = "The MailQueue does not exist."),
+ @ApiResponse(code = HttpStatus.INTERNAL_SERVER_ERROR_500, message = "Internal server error - Something went bad on the server side.")
+ })
+ public void getMailQueue(Service service) {
+ service.get(BASE_URL + SEPARATOR + MAIL_QUEUE_NAME,
+ (request, response) -> getMailQueue(request),
+ jsonTransformer);
+ }
+
+ private MailQueueDTO getMailQueue(Request request) {
+ String mailQueueName = request.params(MAIL_QUEUE_NAME);
+ return mailQueueFactory.getQueue(mailQueueName).map(this::toDTO)
+ .orElseThrow(
+ () -> ErrorResponder.builder()
+ .message(String.format("%s can not be found", mailQueueName))
+ .statusCode(HttpStatus.NOT_FOUND_404)
+ .type(ErrorResponder.ErrorType.NOT_FOUND)
+ .haltError());
+ }
+
+ private MailQueueDTO toDTO(ManageableMailQueue queue) {
+ try {
+ return MailQueueDTO.from(queue);
+ } catch (MailQueueException e) {
+ throw ErrorResponder.builder()
+ .statusCode(HttpStatus.BAD_REQUEST_400)
+ .type(ErrorType.INVALID_ARGUMENT)
+ .message("Invalid request for getting the mail queue " + queue)
+ .cause(e)
+ .haltError();
+ }
+ }
}
http://git-wip-us.apache.org/repos/asf/james-project/blob/2ecbdea3/server/protocols/webadmin/webadmin-mailqueue/src/test/java/org/apache/james/webadmin/dto/MailQueueDTOTest.java
----------------------------------------------------------------------
diff --git a/server/protocols/webadmin/webadmin-mailqueue/src/test/java/org/apache/james/webadmin/dto/MailQueueDTOTest.java b/server/protocols/webadmin/webadmin-mailqueue/src/test/java/org/apache/james/webadmin/dto/MailQueueDTOTest.java
new file mode 100644
index 0000000..898b228
--- /dev/null
+++ b/server/protocols/webadmin/webadmin-mailqueue/src/test/java/org/apache/james/webadmin/dto/MailQueueDTOTest.java
@@ -0,0 +1,57 @@
+/****************************************************************
+ * 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 static org.assertj.core.api.Assertions.assertThatThrownBy;
+
+import org.assertj.core.api.JUnitSoftAssertions;
+import org.junit.Rule;
+import org.junit.Test;
+
+public class MailQueueDTOTest {
+
+ @Rule
+ public final JUnitSoftAssertions softly = new JUnitSoftAssertions();
+
+ @Test
+ public void buildShouldThrowWhenNameIsNull() {
+ assertThatThrownBy(() -> MailQueueDTO.builder().build())
+ .isInstanceOf(IllegalArgumentException.class);
+ }
+
+ @Test
+ public void buildShouldThrowWhenNameIsEmpty() {
+ assertThatThrownBy(() -> MailQueueDTO.builder().name("").build())
+ .isInstanceOf(IllegalArgumentException.class);
+ }
+
+ @Test
+ public void builderShouldCreateTheRightObject() {
+ String name = "name";
+ int size = 123;
+
+ MailQueueDTO mailQueueDTO = MailQueueDTO.builder()
+ .name(name)
+ .size(size)
+ .build();
+
+ softly.assertThat(mailQueueDTO.getName()).isEqualTo(name);
+ softly.assertThat(mailQueueDTO.getSize()).isEqualTo(size);
+ }
+}
http://git-wip-us.apache.org/repos/asf/james-project/blob/2ecbdea3/server/protocols/webadmin/webadmin-mailqueue/src/test/java/org/apache/james/webadmin/routes/MailQueueRoutesTest.java
----------------------------------------------------------------------
diff --git a/server/protocols/webadmin/webadmin-mailqueue/src/test/java/org/apache/james/webadmin/routes/MailQueueRoutesTest.java b/server/protocols/webadmin/webadmin-mailqueue/src/test/java/org/apache/james/webadmin/routes/MailQueueRoutesTest.java
index 73a24a7..d45a438 100644
--- a/server/protocols/webadmin/webadmin-mailqueue/src/test/java/org/apache/james/webadmin/routes/MailQueueRoutesTest.java
+++ b/server/protocols/webadmin/webadmin-mailqueue/src/test/java/org/apache/james/webadmin/routes/MailQueueRoutesTest.java
@@ -19,17 +19,21 @@
package org.apache.james.webadmin.routes;
+import static com.jayway.restassured.RestAssured.when;
import static com.jayway.restassured.config.EncoderConfig.encoderConfig;
import static com.jayway.restassured.config.RestAssuredConfig.newConfig;
import static org.apache.james.webadmin.WebAdminServer.NO_CONFIGURATION;
import static org.assertj.core.api.Assertions.assertThat;
+import static org.hamcrest.Matchers.equalTo;
import java.nio.charset.StandardCharsets;
import java.util.List;
import org.apache.james.metrics.api.NoopMetricFactory;
+import org.apache.james.queue.api.Mails;
import org.apache.james.queue.api.RawMailQueueItemDecoratorFactory;
import org.apache.james.queue.memory.MemoryMailQueueFactory;
+import org.apache.james.queue.memory.MemoryMailQueueFactory.MemoryMailQueue;
import org.apache.james.webadmin.WebAdminServer;
import org.apache.james.webadmin.WebAdminUtils;
import org.apache.james.webadmin.utils.JsonTransformer;
@@ -77,6 +81,7 @@ public class MailQueueRoutesTest {
mailQueueFactory = new MemoryMailQueueFactory(new RawMailQueueItemDecoratorFactory());
webAdminServer = createServer(mailQueueFactory);
RestAssured.requestSpecification = buildRequestSpecification(webAdminServer);
+ RestAssured.enableLoggingOfRequestAndResponseIfValidationFails();
}
@After
@@ -86,7 +91,7 @@ public class MailQueueRoutesTest {
@Test
public void listAllMailQueuesShouldReturnEmptyWhenNone() {
- List<String> actual = RestAssured.when()
+ List<String> actual = when()
.get()
.then()
.statusCode(HttpStatus.OK_200)
@@ -103,7 +108,7 @@ public class MailQueueRoutesTest {
public void listAllMailQueuesShouldReturnSingleElementListWhenOnlyOneMailQueue() {
mailQueueFactory.createQueue(FIRST_QUEUE);
- List<String> actual = RestAssured.when()
+ List<String> actual = when()
.get()
.then()
.statusCode(HttpStatus.OK_200)
@@ -123,7 +128,7 @@ public class MailQueueRoutesTest {
mailQueueFactory.createQueue(THIRD_QUEUE);
mailQueueFactory.createQueue(FOURTH_QUEUE);
- List<String> actual = RestAssured.when()
+ List<String> actual = when()
.get()
.then()
.statusCode(HttpStatus.OK_200)
@@ -136,4 +141,25 @@ public class MailQueueRoutesTest {
assertThat(actual).containsOnly(FIRST_QUEUE, SECOND_QUEUE, THIRD_QUEUE, FOURTH_QUEUE);
}
+ @Test
+ public void getMailQueueShouldReturnTheMailQueueDataWhenMailQueueExists() throws Exception {
+ MemoryMailQueue queue = mailQueueFactory.createQueue(FIRST_QUEUE);
+ queue.enQueue(Mails.defaultMail().build());
+
+ when()
+ .get(FIRST_QUEUE)
+ .then()
+ .statusCode(HttpStatus.OK_200)
+ .body("name", equalTo(FIRST_QUEUE))
+ .body("size", equalTo(1));
+ }
+
+ @Test
+ public void getMailQueueShouldReturnNotFoundWhenMailQueueDoesntExist() {
+ when()
+ .get(FIRST_QUEUE)
+ .then()
+ .statusCode(HttpStatus.NOT_FOUND_404);
+ }
+
}
---------------------------------------------------------------------
To unsubscribe, e-mail: server-dev-unsubscribe@james.apache.org
For additional commands, e-mail: server-dev-help@james.apache.org