james-server-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From btell...@apache.org
Subject [james-project] 14/23: JAMES-2442 Use Java API of Mock SMTP server
Date Fri, 06 Sep 2019 02:02:05 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 2a9e756b8621a0cdc601adf572cbf09cbae750ef
Author: Benoit Tellier <btellier@linagora.com>
AuthorDate: Tue Sep 3 10:45:56 2019 +0700

    JAMES-2442 Use Java API of Mock SMTP server
---
 pom.xml                                            |  5 ++
 server/mailet/integration-testing/pom.xml          |  5 ++
 .../james/mailets/RemoteDeliveryErrorTest.java     | 87 +++++++++-------------
 .../mock/smtp/server/model/MockSmtpBehaviors.java  |  5 ++
 4 files changed, 51 insertions(+), 51 deletions(-)

diff --git a/pom.xml b/pom.xml
index 80ed9c8..2a660ba 100644
--- a/pom.xml
+++ b/pom.xml
@@ -1824,6 +1824,11 @@
             </dependency>
             <dependency>
                 <groupId>${james.groupId}</groupId>
+                <artifactId>mock-smtp-server</artifactId>
+                <version>${project.version}</version>
+            </dependency>
+            <dependency>
+                <groupId>${james.groupId}</groupId>
                 <artifactId>quota-mailing-cassandra</artifactId>
                 <version>${project.version}</version>
             </dependency>
diff --git a/server/mailet/integration-testing/pom.xml b/server/mailet/integration-testing/pom.xml
index 510bff6..506e8fe 100644
--- a/server/mailet/integration-testing/pom.xml
+++ b/server/mailet/integration-testing/pom.xml
@@ -114,6 +114,11 @@
             <scope>test</scope>
         </dependency>
         <dependency>
+            <groupId>${james.groupId}</groupId>
+            <artifactId>mock-smtp-server</artifactId>
+            <scope>test</scope>
+        </dependency>
+        <dependency>
             <groupId>ch.qos.logback</groupId>
             <artifactId>logback-classic</artifactId>
             <scope>test</scope>
diff --git a/server/mailet/integration-testing/src/test/java/org/apache/james/mailets/RemoteDeliveryErrorTest.java
b/server/mailet/integration-testing/src/test/java/org/apache/james/mailets/RemoteDeliveryErrorTest.java
index ab839d2..210d00f 100644
--- a/server/mailet/integration-testing/src/test/java/org/apache/james/mailets/RemoteDeliveryErrorTest.java
+++ b/server/mailet/integration-testing/src/test/java/org/apache/james/mailets/RemoteDeliveryErrorTest.java
@@ -41,6 +41,11 @@ import org.apache.james.mailets.configuration.CommonProcessors;
 import org.apache.james.mailets.configuration.MailetConfiguration;
 import org.apache.james.mailets.configuration.MailetContainer;
 import org.apache.james.mailets.configuration.ProcessorConfiguration;
+import org.apache.james.mock.smtp.server.model.Condition;
+import org.apache.james.mock.smtp.server.model.MockSMTPBehavior;
+import org.apache.james.mock.smtp.server.model.MockSmtpBehaviors;
+import org.apache.james.mock.smtp.server.model.Response;
+import org.apache.james.mock.smtp.server.model.SMTPCommand;
 import org.apache.james.modules.protocols.ImapGuiceProbe;
 import org.apache.james.modules.protocols.SmtpGuiceProbe;
 import org.apache.james.transport.mailets.RemoteDelivery;
@@ -68,57 +73,17 @@ public class RemoteDeliveryErrorTest {
 
     private static final String FROM = "from@" + DEFAULT_DOMAIN;
     private static final String RECIPIENT = "touser@" + ANOTHER_DOMAIN;
-    private static final String ALWAYS_421_RCPT_BEHAVIOR = "[{" +
-        "  \"condition\": {\"operator\":\"matchAll\"}," +
-        "  \"response\": {\"code\":421, \"message\":\"mock response\", \"rejected\":true},"
+
-        "  \"command\": \"RCPT TO\"" +
-        "}]";
-    private static final String ALWAYS_421_FROM_BEHAVIOR = "[{" +
-        "  \"condition\": {\"operator\":\"matchAll\"}," +
-        "  \"response\": {\"code\":421, \"message\":\"mock response\", \"rejected\":true},"
+
-        "  \"command\": \"MAIL FROM\"" +
-        "}]";
-    private static final String ALWAYS_421_DATA_BEHAVIOR = "[{" +
-        "  \"condition\": {\"operator\":\"matchAll\"}," +
-        "  \"response\": {\"code\":421, \"message\":\"mock response\", \"rejected\":true},"
+
-        "  \"command\": \"DATA\"" +
-        "}]";
-    private static final String TWICE_421_RCPT_BEHAVIOR = "[{" +
-        "  \"condition\": {\"operator\":\"matchAll\"}," +
-        "  \"response\": {\"code\":421, \"message\":\"mock response\", \"rejected\":true},"
+
-        "  \"command\": \"RCPT TO\"," +
-        "  \"numberOfAnswer\": 2" +
-        "}]";
-    private static final String TWICE_421_FROM_BEHAVIOR = "[{" +
-        "  \"condition\": {\"operator\":\"matchAll\"}," +
-        "  \"response\": {\"code\":421, \"message\":\"mock response\", \"rejected\":true},"
+
-        "  \"command\": \"MAIL FROM\"," +
-        "  \"numberOfAnswer\": 2" +
-        "}]";
-    private static final String TWICE_421_DATA_BEHAVIOR = "[{" +
-        "  \"condition\": {\"operator\":\"matchAll\"}," +
-        "  \"response\": {\"code\":421, \"message\":\"mock response\", \"rejected\":true},"
+
-        "  \"command\": \"DATA\"," +
-        "  \"numberOfAnswer\": 2" +
-        "}]";
-    private static final String SINGLE_500_RCPT_BEHAVIOR = "[{" +
-        "  \"condition\": {\"operator\":\"matchAll\"}," +
-        "  \"response\": {\"code\":521, \"message\":\"mock response\", \"rejected\":true},"
+
-        "  \"command\": \"RCPT TO\"," +
-        "  \"numberOfAnswer\": 1" +
-        "}]";
-    private static final String SINGLE_500_FROM_BEHAVIOR = "[{" +
-        "  \"condition\": {\"operator\":\"matchAll\"}," +
-        "  \"response\": {\"code\":521, \"message\":\"mock response\", \"rejected\":true},"
+
-        "  \"command\": \"MAIL FROM\"," +
-        "  \"numberOfAnswer\": 1" +
-        "}]";
-    private static final String SINGLE_500_DATA_BEHAVIOR = "[{" +
-        "  \"condition\": {\"operator\":\"matchAll\"}," +
-        "  \"response\": {\"code\":521, \"message\":\"mock response\", \"rejected\":true},"
+
-        "  \"command\": \"DATA\"," +
-        "  \"numberOfAnswer\": 1" +
-        "}]";
+
+    private static final MockSMTPBehavior.NumberOfAnswersPolicy TWO_TIMES = MockSMTPBehavior.NumberOfAnswersPolicy.times(2);
+    private static final MockSmtpBehaviors ALWAYS_421_RCPT_BEHAVIOR = reject421Behavior(SMTPCommand.RCPT_TO);
+    private static final MockSmtpBehaviors ALWAYS_421_FROM_BEHAVIOR = reject421Behavior(SMTPCommand.MAIL_FROM);
+    private static final MockSmtpBehaviors ALWAYS_421_DATA_BEHAVIOR = reject421Behavior(SMTPCommand.DATA);
+    private static final MockSmtpBehaviors TWICE_421_RCPT_BEHAVIOR = reject421Behavior(SMTPCommand.RCPT_TO,
TWO_TIMES);
+    private static final MockSmtpBehaviors TWICE_421_FROM_BEHAVIOR = reject421Behavior(SMTPCommand.RCPT_TO,
TWO_TIMES);
+    private static final MockSmtpBehaviors TWICE_421_DATA_BEHAVIOR = reject421Behavior(SMTPCommand.RCPT_TO,
TWO_TIMES);
+    private static final MockSmtpBehaviors SINGLE_500_RCPT_BEHAVIOR = reject521Behavior(SMTPCommand.RCPT_TO);
+    private static final MockSmtpBehaviors SINGLE_500_FROM_BEHAVIOR = reject521Behavior(SMTPCommand.MAIL_FROM);
+    private static final MockSmtpBehaviors SINGLE_500_DATA_BEHAVIOR = reject521Behavior(SMTPCommand.DATA);
     private static final String BOUNCE_MESSAGE = "Hi. This is the James mail server at localhost.\n"
+
         "I'm afraid I wasn't able to deliver your message to the following addresses.\n"
+
         "This is a permanent error; I've given up. Sorry it didn't work out. Below\n" +
@@ -126,6 +91,26 @@ public class RemoteDeliveryErrorTest {
         "your message.";
     private static final ResponseSpecification RESPONSE_SPECIFICATION = new ResponseSpecBuilder().build();
 
+    private static MockSmtpBehaviors reject421Behavior(SMTPCommand smtpCommand) {
+        return reject421Behavior(smtpCommand, MockSMTPBehavior.NumberOfAnswersPolicy.anytime());
+    }
+
+    private static MockSmtpBehaviors reject421Behavior(SMTPCommand smtpCommand, MockSMTPBehavior.NumberOfAnswersPolicy
numberOfAnswer) {
+        return new MockSmtpBehaviors(
+            new MockSMTPBehavior(smtpCommand,
+                Condition.MATCH_ALL,
+                new Response(Response.SMTPStatusCode.SERVICE_NOT_AVAILABLE_421, "mock response"),
+                numberOfAnswer));
+    }
+
+    private static MockSmtpBehaviors reject521Behavior(SMTPCommand smtpCommand) {
+        return new MockSmtpBehaviors(
+            new MockSMTPBehavior(smtpCommand,
+                Condition.MATCH_ALL,
+                new Response(Response.SMTPStatusCode.DOES_NOT_ACCEPT_MAIL_521, "mock response"),
+                MockSMTPBehavior.NumberOfAnswersPolicy.times(1)));
+    }
+
     @Rule
     public TemporaryFolder temporaryFolder = new TemporaryFolder();
     @Rule
diff --git a/server/mailet/mock-smtp-server/src/main/java/org/apache/james/mock/smtp/server/model/MockSmtpBehaviors.java
b/server/mailet/mock-smtp-server/src/main/java/org/apache/james/mock/smtp/server/model/MockSmtpBehaviors.java
index 5efd902..ad9f834 100644
--- a/server/mailet/mock-smtp-server/src/main/java/org/apache/james/mock/smtp/server/model/MockSmtpBehaviors.java
+++ b/server/mailet/mock-smtp-server/src/main/java/org/apache/james/mock/smtp/server/model/MockSmtpBehaviors.java
@@ -19,6 +19,7 @@
 
 package org.apache.james.mock.smtp.server.model;
 
+import java.util.Arrays;
 import java.util.List;
 import java.util.Objects;
 
@@ -34,6 +35,10 @@ public class MockSmtpBehaviors {
         this.behaviorList = ImmutableList.copyOf(behaviorList);
     }
 
+    public MockSmtpBehaviors(MockSMTPBehavior... behaviorList) {
+        this(Arrays.asList(behaviorList));
+    }
+
     @JsonValue
     public List<MockSMTPBehavior> getBehaviorList() {
         return behaviorList;


---------------------------------------------------------------------
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