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/05 02:56:48 UTC

[29/44] james-project git commit: JAMES-2267 Factorize FakeSmtp concerns

JAMES-2267 Factorize FakeSmtp concerns


Project: http://git-wip-us.apache.org/repos/asf/james-project/repo
Commit: http://git-wip-us.apache.org/repos/asf/james-project/commit/95a3266c
Tree: http://git-wip-us.apache.org/repos/asf/james-project/tree/95a3266c
Diff: http://git-wip-us.apache.org/repos/asf/james-project/diff/95a3266c

Branch: refs/heads/improve-mailet-testing-experience-v1
Commit: 95a3266c77828b1ce7fa947c29efa35e156aaacf
Parents: d6fade9
Author: benwa <bt...@linagora.com>
Authored: Thu Dec 21 10:33:46 2017 +0700
Committer: benwa <bt...@linagora.com>
Committed: Fri Jan 5 09:33:52 2018 +0700

----------------------------------------------------------------------
 mpt/impl/smtp/core/pom.xml                      |  4 ++
 .../apache/james/mpt/smtp/ForwardSmtpTest.java  | 36 +++++--------
 .../GatewayRemoteDeliveryIntegrationTest.java   | 30 +++--------
 .../james/smtp/SmtpAuthorizedAddressesTest.java | 44 +++++----------
 .../jmap/VacationRelayIntegrationTest.java      | 35 +++---------
 server/testing/pom.xml                          |  4 ++
 .../org/apache/james/utils/FakeSmtpHelper.java  | 56 ++++++++++++++++++++
 7 files changed, 102 insertions(+), 107 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/james-project/blob/95a3266c/mpt/impl/smtp/core/pom.xml
----------------------------------------------------------------------
diff --git a/mpt/impl/smtp/core/pom.xml b/mpt/impl/smtp/core/pom.xml
index d88ca7e..9db5462 100644
--- a/mpt/impl/smtp/core/pom.xml
+++ b/mpt/impl/smtp/core/pom.xml
@@ -50,6 +50,10 @@
         </dependency>
         <dependency>
             <groupId>${project.groupId}</groupId>
+            <artifactId>james-server-testing</artifactId>
+        </dependency>
+        <dependency>
+            <groupId>${project.groupId}</groupId>
             <artifactId>james-server-util-java8</artifactId>
             <type>test-jar</type>
         </dependency>

http://git-wip-us.apache.org/repos/asf/james-project/blob/95a3266c/mpt/impl/smtp/core/src/main/java/org/apache/james/mpt/smtp/ForwardSmtpTest.java
----------------------------------------------------------------------
diff --git a/mpt/impl/smtp/core/src/main/java/org/apache/james/mpt/smtp/ForwardSmtpTest.java b/mpt/impl/smtp/core/src/main/java/org/apache/james/mpt/smtp/ForwardSmtpTest.java
index d3ce040..12e41fb 100644
--- a/mpt/impl/smtp/core/src/main/java/org/apache/james/mpt/smtp/ForwardSmtpTest.java
+++ b/mpt/impl/smtp/core/src/main/java/org/apache/james/mpt/smtp/ForwardSmtpTest.java
@@ -18,18 +18,17 @@
  ****************************************************************/
 package org.apache.james.mpt.smtp;
 
-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 com.jayway.awaitility.Duration.FIVE_HUNDRED_MILLISECONDS;
+import static com.jayway.awaitility.Duration.ONE_MINUTE;
 import static org.hamcrest.Matchers.equalTo;
 
 import java.net.InetAddress;
-import java.nio.charset.StandardCharsets;
 import java.util.Locale;
 
 import org.apache.james.mpt.script.SimpleScriptedTestProtocol;
 import org.apache.james.util.docker.Images;
 import org.apache.james.util.docker.SwarmGenericContainer;
+import org.apache.james.utils.FakeSmtpHelper;
 import org.junit.Before;
 import org.junit.Rule;
 import org.junit.Test;
@@ -41,8 +40,6 @@ import com.jayway.awaitility.Awaitility;
 import com.jayway.awaitility.Duration;
 import com.jayway.awaitility.core.ConditionFactory;
 import com.jayway.restassured.RestAssured;
-import com.jayway.restassured.builder.RequestSpecBuilder;
-import com.jayway.restassured.http.ContentType;
 
 public abstract class ForwardSmtpTest {
 
@@ -59,6 +56,7 @@ public abstract class ForwardSmtpTest {
     
     @Rule
     public final RuleChain chain = RuleChain.outerRule(folder).around(fakeSmtp);
+    private ConditionFactory calmlyAwait;
 
     protected abstract SmtpHostSystem createSmtpHostSystem();
     
@@ -79,35 +77,27 @@ public abstract class ForwardSmtpTest {
             .registerRecord("yopmail.com", containerIp, "yopmail.com");
         hostSystem.addAddressMapping(USER, DOMAIN, "ray@yopmail.com");
 
-        RestAssured.requestSpecification = new RequestSpecBuilder()
-        		.setContentType(ContentType.JSON)
-        		.setAccept(ContentType.JSON)
-        		.setConfig(newConfig().encoderConfig(encoderConfig().defaultContentCharset(StandardCharsets.UTF_8)))
-        		.setPort(80)
-        		.setBaseUri("http://" + containerIp.getHostAddress())
-        		.build();
+        RestAssured.requestSpecification = FakeSmtpHelper.requestSpecification(fakeSmtp.getContainerIp());
 
-        Duration slowPacedPollInterval = Duration.FIVE_HUNDRED_MILLISECONDS;
-        ConditionFactory calmlyAwait = Awaitility.with()
+        Duration slowPacedPollInterval = FIVE_HUNDRED_MILLISECONDS;
+        calmlyAwait = Awaitility.with()
             .pollInterval(slowPacedPollInterval)
             .and()
             .with()
             .pollDelay(slowPacedPollInterval)
             .await();
 
-        calmlyAwait.atMost(Duration.ONE_MINUTE).until(() -> fakeSmtp.tryConnect(25));
+        calmlyAwait.atMost(ONE_MINUTE).until(() -> fakeSmtp.tryConnect(25));
     }
 
     @Test
     public void forwardingAnEmailShouldWork() throws Exception {
         scriptedTest.run("helo");
 
-        when()
-            .get("/api/email")
-        .then()
-            .statusCode(200)
-            .body("[0].from", equalTo("matthieu@yopmail.com"))
-            .body("[0].subject", equalTo("test"))
-            .body("[0].text", equalTo("content"));
+        calmlyAwait.atMost(ONE_MINUTE).until(() ->
+            FakeSmtpHelper.isReceived(response -> response
+                .body("[0].from", equalTo("matthieu@yopmail.com"))
+                .body("[0].subject", equalTo("test"))
+                .body("[0].text", equalTo("content"))));
     }
 }

http://git-wip-us.apache.org/repos/asf/james-project/blob/95a3266c/server/mailet/integration-testing/src/test/java/org/apache/james/mailets/GatewayRemoteDeliveryIntegrationTest.java
----------------------------------------------------------------------
diff --git a/server/mailet/integration-testing/src/test/java/org/apache/james/mailets/GatewayRemoteDeliveryIntegrationTest.java b/server/mailet/integration-testing/src/test/java/org/apache/james/mailets/GatewayRemoteDeliveryIntegrationTest.java
index 714264b..bb95936 100644
--- a/server/mailet/integration-testing/src/test/java/org/apache/james/mailets/GatewayRemoteDeliveryIntegrationTest.java
+++ b/server/mailet/integration-testing/src/test/java/org/apache/james/mailets/GatewayRemoteDeliveryIntegrationTest.java
@@ -20,8 +20,6 @@
 package org.apache.james.mailets;
 
 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.MemoryJamesServerMain.SMTP_AND_IMAP_MODULE;
 import static org.apache.james.MemoryJamesServerMain.SMTP_ONLY_MODULE;
 import static org.apache.james.mailets.configuration.Constants.DEFAULT_DOMAIN;
@@ -34,7 +32,6 @@ import static org.hamcrest.Matchers.equalTo;
 import static org.hamcrest.Matchers.hasSize;
 
 import java.net.InetAddress;
-import java.nio.charset.StandardCharsets;
 import java.util.concurrent.TimeUnit;
 
 import org.apache.james.dnsservice.api.DNSService;
@@ -52,6 +49,7 @@ import org.apache.james.transport.matchers.RecipientIsLocal;
 import org.apache.james.util.docker.Images;
 import org.apache.james.util.docker.SwarmGenericContainer;
 import org.apache.james.utils.DataProbeImpl;
+import org.apache.james.utils.FakeSmtpHelper;
 import org.apache.james.utils.IMAPMessageReader;
 import org.apache.james.utils.SMTPMessageSender;
 import org.junit.After;
@@ -63,8 +61,6 @@ import org.junit.rules.TemporaryFolder;
 import org.testcontainers.containers.wait.HostPortWaitStrategy;
 
 import com.jayway.restassured.RestAssured;
-import com.jayway.restassured.builder.RequestSpecBuilder;
-import com.jayway.restassured.http.ContentType;
 
 public class GatewayRemoteDeliveryIntegrationTest {
     private static final String JAMES_ANOTHER_DOMAIN = "james.com";
@@ -95,13 +91,7 @@ public class GatewayRemoteDeliveryIntegrationTest {
     public void setup() throws Exception {
         awaitOneMinute.until(() -> fakeSmtp.tryConnect(25));
 
-        RestAssured.requestSpecification = new RequestSpecBuilder()
-            .setContentType(ContentType.JSON)
-            .setAccept(ContentType.JSON)
-            .setConfig(newConfig().encoderConfig(encoderConfig().defaultContentCharset(StandardCharsets.UTF_8)))
-            .setPort(80)
-            .setBaseUri("http://" + fakeSmtp.getContainerIp())
-            .build();
+        RestAssured.requestSpecification = FakeSmtpHelper.requestSpecification(fakeSmtp.getContainerIp());
     }
 
     @After
@@ -318,18 +308,10 @@ public class GatewayRemoteDeliveryIntegrationTest {
     }
 
     private boolean messageIsReceivedByTheSmtpServer() {
-        try {
-            when()
-                .get("/api/email")
-            .then()
-                .statusCode(200)
-                .body("", hasSize(1))
-                .body("[0].from", equalTo(FROM))
-                .body("[0].subject", equalTo("test"));
-            return true;
-        } catch (Exception e) {
-            return false;
-        }
+        return FakeSmtpHelper.isReceived(response -> response
+            .body("", hasSize(1))
+            .body("[0].from", equalTo(FROM))
+            .body("[0].subject", equalTo("test")));
     }
 
     private MailetContainer generateMailetContainerConfiguration(String gatewayProperty) {

http://git-wip-us.apache.org/repos/asf/james-project/blob/95a3266c/server/mailet/integration-testing/src/test/java/org/apache/james/smtp/SmtpAuthorizedAddressesTest.java
----------------------------------------------------------------------
diff --git a/server/mailet/integration-testing/src/test/java/org/apache/james/smtp/SmtpAuthorizedAddressesTest.java b/server/mailet/integration-testing/src/test/java/org/apache/james/smtp/SmtpAuthorizedAddressesTest.java
index 1e8ba38..571e3c3 100644
--- a/server/mailet/integration-testing/src/test/java/org/apache/james/smtp/SmtpAuthorizedAddressesTest.java
+++ b/server/mailet/integration-testing/src/test/java/org/apache/james/smtp/SmtpAuthorizedAddressesTest.java
@@ -19,9 +19,6 @@
 
 package org.apache.james.smtp;
 
-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.mailets.configuration.Constants.DEFAULT_DOMAIN;
 import static org.apache.james.mailets.configuration.Constants.IMAP_PORT;
 import static org.apache.james.mailets.configuration.Constants.LOCALHOST_IP;
@@ -31,8 +28,6 @@ import static org.apache.james.mailets.configuration.Constants.awaitOneMinute;
 import static org.hamcrest.Matchers.equalTo;
 import static org.hamcrest.Matchers.hasSize;
 
-import java.nio.charset.StandardCharsets;
-
 import org.apache.james.MemoryJamesServerMain;
 import org.apache.james.mailets.TemporaryJamesServer;
 import org.apache.james.mailets.configuration.CommonProcessors;
@@ -47,8 +42,10 @@ import org.apache.james.transport.mailets.ToProcessor;
 import org.apache.james.transport.matchers.All;
 import org.apache.james.transport.matchers.RecipientIsLocal;
 import org.apache.james.transport.matchers.SMTPIsAuthNetwork;
+import org.apache.james.util.docker.Images;
 import org.apache.james.util.docker.SwarmGenericContainer;
 import org.apache.james.utils.DataProbeImpl;
+import org.apache.james.utils.FakeSmtpHelper;
 import org.apache.james.utils.IMAPMessageReader;
 import org.apache.james.utils.SMTPMessageSender;
 import org.junit.After;
@@ -60,15 +57,13 @@ import org.junit.rules.TemporaryFolder;
 import org.testcontainers.containers.wait.HostPortWaitStrategy;
 
 import com.jayway.restassured.RestAssured;
-import com.jayway.restassured.builder.RequestSpecBuilder;
-import com.jayway.restassured.http.ContentType;
 
 public class SmtpAuthorizedAddressesTest {
     private static final String FROM = "fromuser@" + DEFAULT_DOMAIN;
     private static final String TO = "to@any.com";
 
     private final TemporaryFolder smtpFolder = new TemporaryFolder();
-    private final SwarmGenericContainer fakeSmtp = new SwarmGenericContainer("weave/rest-smtp-sink:latest")
+    private final SwarmGenericContainer fakeSmtp = new SwarmGenericContainer(Images.FAKE_SMTP)
         .withExposedPorts(25)
         .withAffinityToContainer()
         .waitingFor(new HostPortWaitStrategy());
@@ -89,13 +84,7 @@ public class SmtpAuthorizedAddressesTest {
     public void setup() throws Exception {
         awaitOneMinute.until(() -> fakeSmtp.tryConnect(25));
 
-        RestAssured.requestSpecification = new RequestSpecBuilder()
-            .setContentType(ContentType.JSON)
-            .setAccept(ContentType.JSON)
-            .setConfig(newConfig().encoderConfig(encoderConfig().defaultContentCharset(StandardCharsets.UTF_8)))
-            .setPort(80)
-            .setBaseUri("http://" + fakeSmtp.getContainerIp())
-            .build();
+        RestAssured.requestSpecification = FakeSmtpHelper.requestSpecification(fakeSmtp.getContainerIp());
     }
 
     private void createJamesServer(SmtpConfiguration.Builder smtpConfiguration) throws Exception {
@@ -158,7 +147,10 @@ public class SmtpAuthorizedAddressesTest {
             .sendMessage(FROM, TO)
             .awaitSent(awaitOneMinute);
 
-        awaitOneMinute.until(this::messageIsReceivedByTheSmtpServer);
+        awaitOneMinute.until(() -> FakeSmtpHelper.isReceived(response -> response
+            .body("", hasSize(1))
+            .body("[0].from", equalTo(FROM))
+            .body("[0].subject", equalTo("test"))));
     }
 
     @Test
@@ -184,7 +176,10 @@ public class SmtpAuthorizedAddressesTest {
             .sendMessage(FROM, TO)
             .awaitSent(awaitOneMinute);
 
-        awaitOneMinute.until(this::messageIsReceivedByTheSmtpServer);
+        awaitOneMinute.until(() -> FakeSmtpHelper.isReceived(response -> response
+            .body("", hasSize(1))
+            .body("[0].from", equalTo(FROM))
+            .body("[0].subject", equalTo("test"))));
     }
 
     @Test
@@ -203,19 +198,4 @@ public class SmtpAuthorizedAddressesTest {
             .awaitMessage(awaitOneMinute);
     }
 
-    private boolean messageIsReceivedByTheSmtpServer() {
-        try {
-            when()
-                .get("/api/email")
-            .then()
-                .statusCode(200)
-                .body("", hasSize(1))
-                .body("[0].from", equalTo(FROM))
-                .body("[0].subject", equalTo("test"));
-            return true;
-        } catch (Exception e) {
-            return false;
-        }
-    }
-
 }

http://git-wip-us.apache.org/repos/asf/james-project/blob/95a3266c/server/protocols/jmap-integration-testing/jmap-integration-testing-common/src/test/java/org/apache/james/jmap/VacationRelayIntegrationTest.java
----------------------------------------------------------------------
diff --git a/server/protocols/jmap-integration-testing/jmap-integration-testing-common/src/test/java/org/apache/james/jmap/VacationRelayIntegrationTest.java b/server/protocols/jmap-integration-testing/jmap-integration-testing-common/src/test/java/org/apache/james/jmap/VacationRelayIntegrationTest.java
index da9baa9..3ef66d6 100644
--- a/server/protocols/jmap-integration-testing/jmap-integration-testing-common/src/test/java/org/apache/james/jmap/VacationRelayIntegrationTest.java
+++ b/server/protocols/jmap-integration-testing/jmap-integration-testing-common/src/test/java/org/apache/james/jmap/VacationRelayIntegrationTest.java
@@ -19,13 +19,9 @@
 
 package org.apache.james.jmap;
 
-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.hamcrest.Matchers.equalTo;
 
 import java.net.InetAddress;
-import java.nio.charset.StandardCharsets;
 import java.util.concurrent.TimeUnit;
 
 import org.apache.commons.net.smtp.SMTPClient;
@@ -40,6 +36,7 @@ import org.apache.james.probe.DataProbe;
 import org.apache.james.util.docker.Images;
 import org.apache.james.util.docker.SwarmGenericContainer;
 import org.apache.james.utils.DataProbeImpl;
+import org.apache.james.utils.FakeSmtpHelper;
 import org.apache.james.utils.JmapGuiceProbe;
 import org.junit.After;
 import org.junit.Before;
@@ -51,8 +48,6 @@ import com.jayway.awaitility.Awaitility;
 import com.jayway.awaitility.Duration;
 import com.jayway.awaitility.core.ConditionFactory;
 import com.jayway.restassured.RestAssured;
-import com.jayway.restassured.builder.RequestSpecBuilder;
-import com.jayway.restassured.http.ContentType;
 
 public abstract class VacationRelayIntegrationTest {
 
@@ -102,13 +97,7 @@ public abstract class VacationRelayIntegrationTest {
 
         jmapGuiceProbe = guiceJamesServer.getProbe(JmapGuiceProbe.class);
 
-        RestAssured.requestSpecification = new RequestSpecBuilder()
-            .setContentType(ContentType.JSON)
-            .setAccept(ContentType.JSON)
-            .setConfig(newConfig().encoderConfig(encoderConfig().defaultContentCharset(StandardCharsets.UTF_8)))
-            .setPort(80)
-            .setBaseUri("http://" + containerIp.getHostAddress())
-            .build();
+        RestAssured.requestSpecification = FakeSmtpHelper.requestSpecification(fakeSmtp.getContainerIp());
 
         Duration slowPacedPollInterval = Duration.FIVE_HUNDRED_MILLISECONDS;
         calmlyAwait = Awaitility
@@ -143,20 +132,10 @@ public abstract class VacationRelayIntegrationTest {
         smtpClient.sendShortMessageData("content");
 
         calmlyAwait.atMost(1, TimeUnit.MINUTES)
-            .until(() -> {
-                try {
-                    when()
-                        .get("/api/email")
-                    .then()
-                        .statusCode(200)
-                        .body("[0].from", equalTo(USER_WITH_DOMAIN))
-                        .body("[0].to[0]", equalTo(externalMail))
-                        .body("[0].text", equalTo(REASON));
-
-                    return true;
-                } catch(AssertionError e) {
-                    return false;
-                }
-            });
+            .until(() ->
+                FakeSmtpHelper.isReceived(response -> response
+                    .body("[0].from", equalTo(USER_WITH_DOMAIN))
+                    .body("[0].to[0]", equalTo(externalMail))
+                    .body("[0].text", equalTo(REASON))));
     }
 }

http://git-wip-us.apache.org/repos/asf/james-project/blob/95a3266c/server/testing/pom.xml
----------------------------------------------------------------------
diff --git a/server/testing/pom.xml b/server/testing/pom.xml
index 1a1b9fc..f989249 100644
--- a/server/testing/pom.xml
+++ b/server/testing/pom.xml
@@ -45,6 +45,10 @@
             <artifactId>awaitility</artifactId>
         </dependency>
         <dependency>
+            <groupId>com.jayway.restassured</groupId>
+            <artifactId>rest-assured</artifactId>
+        </dependency>
+        <dependency>
             <groupId>com.google.guava</groupId>
             <artifactId>guava</artifactId>
         </dependency>

http://git-wip-us.apache.org/repos/asf/james-project/blob/95a3266c/server/testing/src/main/java/org/apache/james/utils/FakeSmtpHelper.java
----------------------------------------------------------------------
diff --git a/server/testing/src/main/java/org/apache/james/utils/FakeSmtpHelper.java b/server/testing/src/main/java/org/apache/james/utils/FakeSmtpHelper.java
new file mode 100644
index 0000000..7129f60
--- /dev/null
+++ b/server/testing/src/main/java/org/apache/james/utils/FakeSmtpHelper.java
@@ -0,0 +1,56 @@
+/****************************************************************
+ * 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.utils;
+
+import static com.jayway.restassured.RestAssured.when;
+import static com.jayway.restassured.config.EncoderConfig.encoderConfig;
+import static com.jayway.restassured.config.RestAssuredConfig.newConfig;
+
+import java.util.function.Function;
+
+import com.google.common.base.Charsets;
+import com.jayway.restassured.builder.RequestSpecBuilder;
+import com.jayway.restassured.http.ContentType;
+import com.jayway.restassured.response.ValidatableResponse;
+import com.jayway.restassured.specification.RequestSpecification;
+
+public class FakeSmtpHelper {
+    public static RequestSpecification requestSpecification(String ip) {
+        return new RequestSpecBuilder()
+            .setContentType(ContentType.JSON)
+            .setAccept(ContentType.JSON)
+            .setConfig(newConfig().encoderConfig(encoderConfig().defaultContentCharset(Charsets.UTF_8)))
+            .setPort(80)
+            .setBaseUri("http://" + ip)
+            .build();
+    }
+
+    public static boolean isReceived(Function<ValidatableResponse, ValidatableResponse> expectations) {
+        try {
+            expectations.apply(when()
+                .get("/api/email")
+            .then()
+                .statusCode(200));
+            return true;
+        } catch (Exception e) {
+            return false;
+        }
+    }
+}


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