james-server-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From btell...@apache.org
Subject [james-project] 03/07: JAMES-2868 Get list of all mails from MockSmtpServer
Date Tue, 03 Sep 2019 10:30:00 GMT
This is an automated email from the ASF dual-hosted git repository.

btellier pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/james-project.git

commit 9fd99bee6d99b0f99d2076ad2c07a8c0bfa007c0
Author: Rene Cordier <rcordier@linagora.com>
AuthorDate: Fri Aug 30 09:52:44 2019 +0700

    JAMES-2868 Get list of all mails from MockSmtpServer
---
 .../mock/smtp/server/HTTPConfigurationServer.java  |  48 +++-
 .../mock/smtp/server/jackson/JsonSimpleModule.java |  26 --
 .../smtp/server/jackson/MailAddressModule.java     |  18 +-
 .../org/apache/james/mock/smtp/server/Fixture.java |   4 +-
 .../smtp/server/HTTPConfigurationServerTest.java   | 263 +++++++++++++++------
 .../smtp/server/jackson/MailAddressModuleTest.java |   2 +-
 6 files changed, 231 insertions(+), 130 deletions(-)

diff --git a/server/mailet/mock-smtp-server/src/main/java/org/apache/james/mock/smtp/server/HTTPConfigurationServer.java
b/server/mailet/mock-smtp-server/src/main/java/org/apache/james/mock/smtp/server/HTTPConfigurationServer.java
index 95d65a1..08f0f8c 100644
--- a/server/mailet/mock-smtp-server/src/main/java/org/apache/james/mock/smtp/server/HTTPConfigurationServer.java
+++ b/server/mailet/mock-smtp-server/src/main/java/org/apache/james/mock/smtp/server/HTTPConfigurationServer.java
@@ -31,6 +31,8 @@ import javax.servlet.http.HttpServletResponse;
 
 import org.apache.james.http.jetty.Configuration;
 import org.apache.james.http.jetty.JettyHttpServer;
+import org.apache.james.mock.smtp.server.jackson.MailAddressModule;
+import org.apache.james.mock.smtp.server.model.Mails;
 import org.apache.james.mock.smtp.server.model.MockSMTPBehaviorInformation;
 import org.apache.james.mock.smtp.server.model.MockSmtpBehaviors;
 import org.apache.james.util.Port;
@@ -41,21 +43,17 @@ import com.fasterxml.jackson.datatype.jdk8.Jdk8Module;
 import com.github.steveash.guavate.Guavate;
 
 public class HTTPConfigurationServer {
-    static class HTTPConfigurationServlet extends HttpServlet {
-        private final ObjectMapper objectMapper;
+    static class SMTPBehaviorsServlet extends HttpServlet {
         private final SMTPBehaviorRepository smtpBehaviorRepository;
 
-        HTTPConfigurationServlet(SMTPBehaviorRepository smtpBehaviorRepository) {
-            this.objectMapper = new ObjectMapper()
-                .registerModule(new Jdk8Module())
-                .registerModule(new GuavaModule());
+        SMTPBehaviorsServlet(SMTPBehaviorRepository smtpBehaviorRepository) {
             this.smtpBehaviorRepository = smtpBehaviorRepository;
         }
 
         @Override
         protected void doPut(HttpServletRequest req, HttpServletResponse resp) {
             try {
-                MockSmtpBehaviors behaviors = objectMapper.readValue(req.getInputStream(),
MockSmtpBehaviors.class);
+                MockSmtpBehaviors behaviors = OBJECT_MAPPER.readValue(req.getInputStream(),
MockSmtpBehaviors.class);
                 smtpBehaviorRepository.setBehaviors(behaviors);
                 resp.setStatus(SC_NO_CONTENT);
             } catch (IOException e) {
@@ -71,7 +69,7 @@ public class HTTPConfigurationServer {
 
             resp.setStatus(SC_OK);
             resp.setContentType("application/json");
-            objectMapper.writeValue(resp.getOutputStream(), mockSmtpBehaviors);
+            OBJECT_MAPPER.writeValue(resp.getOutputStream(), mockSmtpBehaviors);
         }
 
         @Override
@@ -81,23 +79,49 @@ public class HTTPConfigurationServer {
         }
     }
 
+    static class SMTPMailsServlet extends HttpServlet {
+        private final ReceivedMailRepository receivedMailRepository;
+
+        SMTPMailsServlet(ReceivedMailRepository receivedMailRepository) {
+            this.receivedMailRepository = receivedMailRepository;
+        }
+
+        @Override
+        protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException
{
+            Mails mails = new Mails(receivedMailRepository.list());
+            resp.setStatus(SC_OK);
+            resp.setContentType("application/json");
+            OBJECT_MAPPER.writeValue(resp.getOutputStream(), mails);
+        }
+    }
+
     private static final String SMTP_BEHAVIORS = "/smtpBehaviors";
+    private static final String SMTP_MAILS = "/smtpMails";
+
+    private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper()
+        .registerModule(new Jdk8Module())
+        .registerModule(new GuavaModule())
+        .registerModule(MailAddressModule.MODULE);
 
-    public static HTTPConfigurationServer onRandomPort(SMTPBehaviorRepository smtpBehaviorRepository)
{
+    public static HTTPConfigurationServer onRandomPort(SMTPBehaviorRepository smtpBehaviorRepository,
ReceivedMailRepository receivedMailRepository) {
         return new HTTPConfigurationServer(smtpBehaviorRepository,
+            receivedMailRepository,
             Configuration.builder().randomPort());
     }
 
-    public static HTTPConfigurationServer onPort(SMTPBehaviorRepository smtpBehaviorRepository,
Port port) {
+    public static HTTPConfigurationServer onPort(SMTPBehaviorRepository smtpBehaviorRepository,
ReceivedMailRepository receivedMailRepository, Port port) {
         return new HTTPConfigurationServer(smtpBehaviorRepository,
+            receivedMailRepository,
             Configuration.builder().port(port.getValue()));
     }
 
     private final JettyHttpServer jettyHttpServer;
 
-    private HTTPConfigurationServer(SMTPBehaviorRepository smtpBehaviorRepository, Configuration.Builder
configurationBuilder) {
+    private HTTPConfigurationServer(SMTPBehaviorRepository smtpBehaviorRepository, ReceivedMailRepository
receivedMailRepository, Configuration.Builder configurationBuilder) {
         jettyHttpServer = JettyHttpServer.create(configurationBuilder.serve(SMTP_BEHAVIORS)
-            .with(new HTTPConfigurationServlet(smtpBehaviorRepository))
+            .with(new SMTPBehaviorsServlet(smtpBehaviorRepository))
+            .serve(SMTP_MAILS)
+            .with(new SMTPMailsServlet(receivedMailRepository))
             .build());
     }
 
diff --git a/server/mailet/mock-smtp-server/src/main/java/org/apache/james/mock/smtp/server/jackson/JsonSimpleModule.java
b/server/mailet/mock-smtp-server/src/main/java/org/apache/james/mock/smtp/server/jackson/JsonSimpleModule.java
deleted file mode 100644
index 4e00dc2..0000000
--- a/server/mailet/mock-smtp-server/src/main/java/org/apache/james/mock/smtp/server/jackson/JsonSimpleModule.java
+++ /dev/null
@@ -1,26 +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.mock.smtp.server.jackson;
-
-import com.fasterxml.jackson.databind.Module;
-
-public interface JsonSimpleModule {
-    Module asJacksonModule();
-}
diff --git a/server/mailet/mock-smtp-server/src/main/java/org/apache/james/mock/smtp/server/jackson/MailAddressModule.java
b/server/mailet/mock-smtp-server/src/main/java/org/apache/james/mock/smtp/server/jackson/MailAddressModule.java
index 9ecec8a..a83579f 100644
--- a/server/mailet/mock-smtp-server/src/main/java/org/apache/james/mock/smtp/server/jackson/MailAddressModule.java
+++ b/server/mailet/mock-smtp-server/src/main/java/org/apache/james/mock/smtp/server/jackson/MailAddressModule.java
@@ -21,20 +21,10 @@ package org.apache.james.mock.smtp.server.jackson;
 
 import org.apache.james.core.MailAddress;
 
-import com.fasterxml.jackson.databind.Module;
 import com.fasterxml.jackson.databind.module.SimpleModule;
 
-public class MailAddressModule implements JsonSimpleModule {
-    private final SimpleModule simpleModule;
-
-    public MailAddressModule() {
-        this.simpleModule = new SimpleModule()
-            .addSerializer(MailAddress.class, new MailAddressSerializer())
-            .addDeserializer(MailAddress.class, new MailAddressDeserializer());
-    }
-
-    @Override
-    public Module asJacksonModule() {
-        return simpleModule;
-    }
+public interface MailAddressModule {
+    SimpleModule MODULE = new SimpleModule()
+        .addSerializer(MailAddress.class, new MailAddressSerializer())
+        .addDeserializer(MailAddress.class, new MailAddressDeserializer());
 }
diff --git a/server/mailet/mock-smtp-server/src/test/java/org/apache/james/mock/smtp/server/Fixture.java
b/server/mailet/mock-smtp-server/src/test/java/org/apache/james/mock/smtp/server/Fixture.java
index 6f3cc4e..65b6c22 100644
--- a/server/mailet/mock-smtp-server/src/test/java/org/apache/james/mock/smtp/server/Fixture.java
+++ b/server/mailet/mock-smtp-server/src/test/java/org/apache/james/mock/smtp/server/Fixture.java
@@ -41,7 +41,7 @@ public interface Fixture {
     ObjectMapper OBJECT_MAPPER = new ObjectMapper()
         .registerModule(new Jdk8Module())
         .registerModule(new GuavaModule())
-        .registerModule(new MailAddressModule().asJacksonModule());
+        .registerModule(MailAddressModule.MODULE);
 
     Response RESPONSE = Response.serverAccept(Response.SMTPStatusCode.of(250), "message");
 
@@ -97,4 +97,6 @@ public interface Fixture {
         "  {\"from\":\"bob@james.org\",\"recipients\":[\"alice@james.org\", \"jack@james.org\"],\"message\":\"bob
to alice and jack\"}," +
         "  {\"from\":\"alice@james.org\",\"recipients\":[\"bob@james.org\"],\"message\":\"alice
to bob\"}" +
         "]";
+
+    String JSON_MAIL = "[{\"from\":\"bob@james.org\",\"recipients\":[\"alice@james.org\",
\"jack@james.org\"],\"message\":\"bob to alice and jack\"}]";
 }
diff --git a/server/mailet/mock-smtp-server/src/test/java/org/apache/james/mock/smtp/server/HTTPConfigurationServerTest.java
b/server/mailet/mock-smtp-server/src/test/java/org/apache/james/mock/smtp/server/HTTPConfigurationServerTest.java
index 64f422a..65435b4 100644
--- a/server/mailet/mock-smtp-server/src/test/java/org/apache/james/mock/smtp/server/HTTPConfigurationServerTest.java
+++ b/server/mailet/mock-smtp-server/src/test/java/org/apache/james/mock/smtp/server/HTTPConfigurationServerTest.java
@@ -25,16 +25,26 @@ import static io.restassured.RestAssured.with;
 import static io.restassured.config.EncoderConfig.encoderConfig;
 import static io.restassured.config.RestAssuredConfig.newConfig;
 import static net.javacrumbs.jsonunit.assertj.JsonAssertions.assertThatJson;
+import static org.apache.james.mock.smtp.server.Fixture.ALICE;
+import static org.apache.james.mock.smtp.server.Fixture.BOB;
+import static org.apache.james.mock.smtp.server.Fixture.JACK;
 import static org.apache.james.mock.smtp.server.Fixture.JSON_BEHAVIORS;
+import static org.apache.james.mock.smtp.server.Fixture.JSON_MAIL;
+import static org.apache.james.mock.smtp.server.Fixture.JSON_MAILS_LIST;
 import static org.hamcrest.Matchers.hasSize;
 
 import java.nio.charset.StandardCharsets;
 
+import org.apache.james.core.MailAddress;
+import org.apache.james.mock.smtp.server.model.Mail;
 import org.eclipse.jetty.http.HttpStatus;
 import org.junit.jupiter.api.AfterEach;
 import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Nested;
 import org.junit.jupiter.api.Test;
 
+import com.google.common.collect.ImmutableList;
+
 import io.restassured.RestAssured;
 import io.restassured.builder.RequestSpecBuilder;
 import io.restassured.http.ContentType;
@@ -44,87 +54,188 @@ import net.javacrumbs.jsonunit.core.internal.Options;
 class HTTPConfigurationServerTest {
     private HTTPConfigurationServer server;
 
-    @BeforeEach
-    void setUp() throws Exception {
-        server = HTTPConfigurationServer.onRandomPort(new SMTPBehaviorRepository());
-        server.start();
-
-        RestAssured.requestSpecification = new RequestSpecBuilder()
-            .setContentType(ContentType.JSON)
-            .setAccept(ContentType.JSON)
-            .setConfig(newConfig().encoderConfig(encoderConfig().defaultContentCharset(StandardCharsets.UTF_8)))
-            .setPort(server.getPort().getValue())
-            .setBasePath("/")
-            .setBasePath("/smtpBehaviors")
-            .build();
-    }
-
-    @AfterEach
-    void tearDown() throws Exception {
-        server.stop();
-    }
-
-    @Test
-    void getShouldReturnEmptyByDefault() {
-        when()
-            .get()
-        .then()
-            .body(".", hasSize(0));
-    }
-
-    @Test
-    void getShouldReturnPreviouslyStoredData() {
-        with().body(JSON_BEHAVIORS).put();
-
-        String response = when()
-            .get()
+    @Nested
+    class SMTPBehaviorsTest {
+        @BeforeEach
+        void setUp() throws Exception {
+            server = HTTPConfigurationServer.onRandomPort(new SMTPBehaviorRepository(), new
ReceivedMailRepository());
+            server.start();
+
+            RestAssured.requestSpecification = new RequestSpecBuilder()
+                .setContentType(ContentType.JSON)
+                .setAccept(ContentType.JSON)
+                .setConfig(newConfig().encoderConfig(encoderConfig().defaultContentCharset(StandardCharsets.UTF_8)))
+                .setPort(server.getPort().getValue())
+                .setBasePath("/smtpBehaviors")
+                .build();
+        }
+
+        @AfterEach
+        void tearDown() throws Exception {
+            server.stop();
+        }
+
+        @Test
+        void getShouldReturnEmptyByDefault() {
+            when()
+                .get()
             .then()
-            .extract().asString();
-
-        assertThatJson(response)
-            .withOptions(new Options(Option.TREATING_NULL_AS_ABSENT, Option.IGNORING_ARRAY_ORDER))
-            .isEqualTo(JSON_BEHAVIORS);
-    }
-
-    @Test
-    void getShouldReturnEmptyAfterDelete() {
-        with().body(JSON_BEHAVIORS).put();
-
-        with().delete();
-
-        when()
-            .get()
-        .then()
-            .body(".", hasSize(0));
-    }
+                .contentType(ContentType.JSON)
+                .body(".", hasSize(0));
+        }
+
+        @Test
+        void getShouldReturnPreviouslyStoredData() {
+            with().body(JSON_BEHAVIORS).put();
+
+            String response = when()
+                    .get()
+                .then()
+                    .contentType(ContentType.JSON)
+                    .extract()
+                    .asString();
+
+            assertThatJson(response)
+                .withOptions(new Options(Option.TREATING_NULL_AS_ABSENT, Option.IGNORING_ARRAY_ORDER))
+                .isEqualTo(JSON_BEHAVIORS);
+        }
+
+        @Test
+        void getShouldReturnEmptyAfterDelete() {
+            with().body(JSON_BEHAVIORS).put();
+
+            with().delete();
+
+            when()
+                .get()
+            .then()
+                .contentType(ContentType.JSON)
+                .body(".", hasSize(0));
+        }
+
+        @Test
+        void putShouldReturnNoContent() {
+            given()
+                .body(JSON_BEHAVIORS)
+            .when()
+                .put()
+            .then()
+                .statusCode(HttpStatus.NO_CONTENT_204);
+        }
 
-    @Test
-    void putShouldReturnNoContent() {
-        given()
-            .body(JSON_BEHAVIORS)
-        .when()
-            .put()
-        .then()
-            .statusCode(HttpStatus.NO_CONTENT_204);
-    }
+        @Test
+        void putShouldBeIdempotent() {
+            with().body(JSON_BEHAVIORS).put();
 
-    @Test
-    void putShouldBeIdempotent() {
-        with().body(JSON_BEHAVIORS).put();
+            given()
+                .body(JSON_BEHAVIORS)
+            .when()
+                .put()
+            .then()
+                .statusCode(HttpStatus.NO_CONTENT_204);
+        }
 
-        given()
-            .body(JSON_BEHAVIORS)
-        .when()
-            .put()
-        .then()
-            .statusCode(HttpStatus.NO_CONTENT_204);
+        @Test
+        void deleteShouldReturnNoContent() {
+            when()
+                .delete()
+            .then()
+                .statusCode(HttpStatus.NO_CONTENT_204);
+        }
     }
 
-    @Test
-    void deleteShouldReturnNoContent() {
-        when()
-            .delete()
-        .then()
-            .statusCode(HttpStatus.NO_CONTENT_204);
+    @Nested
+    class SMTPMailsTest {
+        private ReceivedMailRepository mailRepository;
+        private Mail mail1;
+        private Mail mail2;
+
+        @BeforeEach
+        void setUp() throws Exception {
+            mailRepository = new ReceivedMailRepository();
+
+            server = HTTPConfigurationServer.onRandomPort(new SMTPBehaviorRepository(), mailRepository);
+            server.start();
+
+            RestAssured.requestSpecification = new RequestSpecBuilder()
+                .setContentType(ContentType.JSON)
+                .setAccept(ContentType.JSON)
+                .setConfig(newConfig().encoderConfig(encoderConfig().defaultContentCharset(StandardCharsets.UTF_8)))
+                .setPort(server.getPort().getValue())
+                .setBasePath("/smtpMails")
+                .build();
+
+            mail1 = new Mail(
+                new Mail.Envelope(
+                    new MailAddress(BOB),
+                    ImmutableList.of(new MailAddress(ALICE), new MailAddress(JACK))),
+                "bob to alice and jack");
+
+            mail2 = new Mail(
+                new Mail.Envelope(
+                    new MailAddress(ALICE),
+                    ImmutableList.of(new MailAddress(BOB))),
+                "alice to bob");
+        }
+
+        @AfterEach
+        void tearDown() throws Exception {
+            server.stop();
+        }
+
+        @Test
+        void getShouldReturnEmptyByDefault() {
+            when()
+                .get()
+            .then()
+                .contentType(ContentType.JSON)
+                .body(".", hasSize(0));
+        }
+
+        @Test
+        void getShouldReturnPreviouslyStoredData() throws Exception {
+            mailRepository.store(mail1);
+
+            String response = when()
+                    .get()
+                .then()
+                    .contentType(ContentType.JSON)
+                    .extract()
+                    .asString();
+
+            assertThatJson(response)
+                .withOptions(new Options(Option.TREATING_NULL_AS_ABSENT, Option.IGNORING_ARRAY_ORDER))
+                .isEqualTo(JSON_MAIL);
+        }
+
+        @Test
+        void getShouldReturnMultipleEmails() throws Exception {
+            mailRepository.store(mail1);
+            mailRepository.store(mail2);
+
+            String response = when()
+                    .get()
+                .then()
+                    .contentType(ContentType.JSON)
+                    .extract()
+                    .asString();
+
+            assertThatJson(response)
+                .withOptions(new Options(Option.TREATING_NULL_AS_ABSENT, Option.IGNORING_ARRAY_ORDER))
+                .isEqualTo(JSON_MAILS_LIST);
+        }
+
+        @Test
+        void getShouldReturnEmptyAfterClear() {
+            mailRepository.store(mail1);
+
+            mailRepository.clear();
+
+            when()
+                .get()
+            .then()
+                .contentType(ContentType.JSON)
+                .body(".", hasSize(0));
+        }
     }
 }
\ No newline at end of file
diff --git a/server/mailet/mock-smtp-server/src/test/java/org/apache/james/mock/smtp/server/jackson/MailAddressModuleTest.java
b/server/mailet/mock-smtp-server/src/test/java/org/apache/james/mock/smtp/server/jackson/MailAddressModuleTest.java
index 8c44504..fee1952 100644
--- a/server/mailet/mock-smtp-server/src/test/java/org/apache/james/mock/smtp/server/jackson/MailAddressModuleTest.java
+++ b/server/mailet/mock-smtp-server/src/test/java/org/apache/james/mock/smtp/server/jackson/MailAddressModuleTest.java
@@ -36,7 +36,7 @@ class MailAddressModuleTest {
     @BeforeEach
     void setup() {
         mapper = new ObjectMapper()
-            .registerModule(new MailAddressModule().asJacksonModule());
+            .registerModule(MailAddressModule.MODULE);
     }
 
     @Test


---------------------------------------------------------------------
To unsubscribe, e-mail: server-dev-unsubscribe@james.apache.org
For additional commands, e-mail: server-dev-help@james.apache.org


Mime
View raw message