james-server-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From btell...@apache.org
Subject [james-project] 16/23: JAMES-2442 Propose a fluent builder for behaviors
Date Fri, 06 Sep 2019 02:02:07 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 6862555452f35de6664dbf0600e33be21210163b
Author: Benoit Tellier <btellier@linagora.com>
AuthorDate: Wed Sep 4 16:49:19 2019 +0700

    JAMES-2442 Propose a fluent builder for behaviors
---
 .../james/mailets/RemoteDeliveryErrorTest.java     | 95 ++++++++++++++-------
 .../mock/smtp/server/model/MockSmtpBehaviors.java  | 99 ++++++++++++++++++++++
 2 files changed, 162 insertions(+), 32 deletions(-)

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 443a5ad..ce4e2ef 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,8 +41,6 @@ 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;
@@ -74,16 +72,69 @@ public class RemoteDeliveryErrorTest {
     private static final String FROM = "from@" + DEFAULT_DOMAIN;
     private static final String RECIPIENT = "touser@" + ANOTHER_DOMAIN;
 
-    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 MockSmtpBehaviors ALWAYS_421_RCPT_BEHAVIOR = MockSmtpBehaviors.builder()
+        .addNewBehavior()
+        .onCommand(SMTPCommand.RCPT_TO)
+        .respond(Response.SMTPStatusCode.SERVICE_NOT_AVAILABLE_421, "mock response")
+        .forAnyInput()
+        .unlimitedNumberOfAnswer()
+        .build();
+    private static final MockSmtpBehaviors ALWAYS_421_FROM_BEHAVIOR = MockSmtpBehaviors.builder()
+        .addNewBehavior()
+        .onCommand(SMTPCommand.MAIL_FROM)
+        .respond(Response.SMTPStatusCode.SERVICE_NOT_AVAILABLE_421, "mock response")
+        .forAnyInput()
+        .unlimitedNumberOfAnswer()
+        .build();
+    private static final MockSmtpBehaviors ALWAYS_421_DATA_BEHAVIOR = MockSmtpBehaviors.builder()
+        .addNewBehavior()
+        .onCommand(SMTPCommand.DATA)
+        .respond(Response.SMTPStatusCode.SERVICE_NOT_AVAILABLE_421, "mock response")
+        .forAnyInput()
+        .unlimitedNumberOfAnswer()
+        .build();
+    private static final MockSmtpBehaviors TWICE_421_RCPT_BEHAVIOR = MockSmtpBehaviors.builder()
+        .addNewBehavior()
+        .onCommand(SMTPCommand.RCPT_TO)
+        .respond(Response.SMTPStatusCode.SERVICE_NOT_AVAILABLE_421, "mock response")
+        .forAnyInput()
+        .onlySomeAnswers(2)
+        .build();
+    private static final MockSmtpBehaviors TWICE_421_FROM_BEHAVIOR = MockSmtpBehaviors.builder()
+        .addNewBehavior()
+        .onCommand(SMTPCommand.MAIL_FROM)
+        .respond(Response.SMTPStatusCode.SERVICE_NOT_AVAILABLE_421, "mock response")
+        .forAnyInput()
+        .onlySomeAnswers(2)
+        .build();
+    private static final MockSmtpBehaviors TWICE_421_DATA_BEHAVIOR = MockSmtpBehaviors.builder()
+        .addNewBehavior()
+        .onCommand(SMTPCommand.DATA)
+        .respond(Response.SMTPStatusCode.SERVICE_NOT_AVAILABLE_421, "mock response")
+        .forAnyInput()
+        .onlySomeAnswers(2)
+        .build();
+    private static final MockSmtpBehaviors SINGLE_500_RCPT_BEHAVIOR = MockSmtpBehaviors.builder()
+        .addNewBehavior()
+        .onCommand(SMTPCommand.RCPT_TO)
+        .respond(Response.SMTPStatusCode.DOES_NOT_ACCEPT_MAIL_521, "mock response")
+        .forAnyInput()
+        .onlySomeAnswers(1)
+        .build();
+    private static final MockSmtpBehaviors SINGLE_500_FROM_BEHAVIOR = MockSmtpBehaviors.builder()
+        .addNewBehavior()
+        .onCommand(SMTPCommand.MAIL_FROM)
+        .respond(Response.SMTPStatusCode.DOES_NOT_ACCEPT_MAIL_521, "mock response")
+        .forAnyInput()
+        .onlySomeAnswers(1)
+        .build();
+    private static final MockSmtpBehaviors SINGLE_500_DATA_BEHAVIOR = MockSmtpBehaviors.builder()
+        .addNewBehavior()
+        .onCommand(SMTPCommand.DATA)
+        .respond(Response.SMTPStatusCode.DOES_NOT_ACCEPT_MAIL_521, "mock response")
+        .forAnyInput()
+        .onlySomeAnswers(1)
+        .build();
     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" +
@@ -91,26 +142,6 @@ 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 ad9f834..16a7bc7 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
@@ -25,9 +25,108 @@ import java.util.Objects;
 
 import com.fasterxml.jackson.annotation.JsonCreator;
 import com.fasterxml.jackson.annotation.JsonValue;
+import com.google.common.base.Preconditions;
 import com.google.common.collect.ImmutableList;
 
 public class MockSmtpBehaviors {
+    public static class Builder {
+        public static class CommandStep {
+            private final Builder backReference;
+
+            CommandStep(Builder backReference) {
+                this.backReference = backReference;
+            }
+
+            public ResponseStep onCommand(SMTPCommand command) {
+                Preconditions.checkNotNull(command, "'command' should not be null");
+                return new ResponseStep(backReference, command);
+            }
+        }
+
+        public static class ResponseStep {
+            private final Builder backReference;
+            private final SMTPCommand command;
+
+            ResponseStep(Builder backReference, SMTPCommand command) {
+                this.backReference = backReference;
+                this.command = command;
+            }
+
+            public ConditionStep respond(Response.SMTPStatusCode statusCode, String message)
{
+                return new ConditionStep(backReference, command, new Response(statusCode,
message));
+            }
+        }
+
+        public static class ConditionStep {
+            private final Builder backReference;
+            private final SMTPCommand command;
+            private final Response response;
+
+            ConditionStep(Builder backReference, SMTPCommand command, Response response)
{
+                this.backReference = backReference;
+                this.command = command;
+                this.response = response;
+            }
+
+            public NumberOfAnswerStep forAnyInput() {
+                return new NumberOfAnswerStep(backReference, command, response, Condition.MATCH_ALL);
+            }
+
+            public NumberOfAnswerStep forInputContaining(String value) {
+                return new NumberOfAnswerStep(backReference, command, response, new Condition.OperatorCondition(Operator.CONTAINS,
value));
+            }
+        }
+
+        public static class NumberOfAnswerStep {
+            private final Builder backReference;
+            private final SMTPCommand command;
+            private final Response response;
+            private final Condition condition;
+
+            NumberOfAnswerStep(Builder backReference, SMTPCommand command, Response response,
Condition condition) {
+                this.backReference = backReference;
+                this.command = command;
+                this.response = response;
+                this.condition = condition;
+            }
+
+            public Builder unlimitedNumberOfAnswer() {
+                return backReference.add(toBehavior(MockSMTPBehavior.NumberOfAnswersPolicy.anytime()));
+            }
+
+            public Builder onlySomeAnswers(int count) {
+                return backReference.add(toBehavior(MockSMTPBehavior.NumberOfAnswersPolicy.times(count)));
+            }
+
+            MockSMTPBehavior toBehavior(MockSMTPBehavior.NumberOfAnswersPolicy numberOfAnswersPolicy)
{
+                return new MockSMTPBehavior(command, condition, response, numberOfAnswersPolicy);
+            }
+        }
+
+        private final ImmutableList.Builder<MockSMTPBehavior> behaviors;
+
+        public Builder() {
+            this.behaviors = ImmutableList.builder();
+        }
+
+        public CommandStep addNewBehavior() {
+            return new CommandStep(this);
+        }
+
+        Builder add(MockSMTPBehavior behavior) {
+            this.behaviors.add(behavior);
+            return this;
+        }
+
+        public MockSmtpBehaviors build() {
+            return new MockSmtpBehaviors(behaviors.build());
+        }
+    }
+
+    public static Builder builder() {
+        return new Builder();
+    }
+
     private final List<MockSMTPBehavior> behaviorList;
 
     @JsonCreator


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