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 2019/08/26 11:38:29 UTC

[james-project] 06/11: JAMES-2864 MockSMTPBehavior POJO for Mock SMTP server

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 d48ece8d451988f3ed503f30f02f5adde423e6cd
Author: Tran Tien Duc <dt...@linagora.com>
AuthorDate: Wed Aug 21 11:28:28 2019 +0700

    JAMES-2864 MockSMTPBehavior POJO for Mock SMTP server
---
 .../james/mock/smtp/server/MockSMTPBehavior.java   | 81 +++++++++++++++++++++
 .../apache/james/mock/smtp/server/SMTPCommand.java | 37 ++++++++++
 .../mock/smtp/server/MockSMTPBehaviorTest.java     | 83 ++++++++++++++++++++++
 3 files changed, 201 insertions(+)

diff --git a/server/mailet/mock-smtp-server/src/main/java/org/apache/james/mock/smtp/server/MockSMTPBehavior.java b/server/mailet/mock-smtp-server/src/main/java/org/apache/james/mock/smtp/server/MockSMTPBehavior.java
new file mode 100644
index 0000000..5d1d7b4
--- /dev/null
+++ b/server/mailet/mock-smtp-server/src/main/java/org/apache/james/mock/smtp/server/MockSMTPBehavior.java
@@ -0,0 +1,81 @@
+/****************************************************************
+ * 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;
+
+import java.util.Optional;
+
+import com.google.common.base.Preconditions;
+
+public class MockSMTPBehavior {
+    public static final class NumberOfAnswersPolicy {
+
+        public static NumberOfAnswersPolicy anytime() {
+            return new NumberOfAnswersPolicy(Optional.empty());
+        }
+
+        public static NumberOfAnswersPolicy times(int times) {
+            Preconditions.checkArgument(times > 0, "times should be positive");
+            return new NumberOfAnswersPolicy(Optional.of(times));
+        }
+
+        private final Optional<Integer> numberOfAnswers;
+
+        private NumberOfAnswersPolicy(Optional<Integer> numberOfAnswers) {
+            this.numberOfAnswers = numberOfAnswers;
+        }
+
+        public Optional<Integer> getNumberOfAnswers() {
+            return numberOfAnswers;
+        }
+    }
+
+    private final SMTPCommand smtpCommand;
+    private final Optional<Condition> condition;
+    private final Response response;
+    private final NumberOfAnswersPolicy numberOfAnswers;
+
+    public MockSMTPBehavior(SMTPCommand smtpCommand, Optional<Condition> condition, Response response, NumberOfAnswersPolicy numberOfAnswers) {
+        Preconditions.checkNotNull(smtpCommand);
+        Preconditions.checkNotNull(condition);
+        Preconditions.checkNotNull(response);
+        Preconditions.checkNotNull(numberOfAnswers);
+
+        this.smtpCommand = smtpCommand;
+        this.condition = condition;
+        this.response = response;
+        this.numberOfAnswers = numberOfAnswers;
+    }
+
+    public SMTPCommand getSmtpCommand() {
+        return smtpCommand;
+    }
+
+    public Optional<Condition> getCondition() {
+        return condition;
+    }
+
+    public Response getResponse() {
+        return response;
+    }
+
+    public NumberOfAnswersPolicy getNumberOfAnswers() {
+        return numberOfAnswers;
+    }
+}
diff --git a/server/mailet/mock-smtp-server/src/main/java/org/apache/james/mock/smtp/server/SMTPCommand.java b/server/mailet/mock-smtp-server/src/main/java/org/apache/james/mock/smtp/server/SMTPCommand.java
new file mode 100644
index 0000000..024b662
--- /dev/null
+++ b/server/mailet/mock-smtp-server/src/main/java/org/apache/james/mock/smtp/server/SMTPCommand.java
@@ -0,0 +1,37 @@
+/****************************************************************
+ * 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;
+
+enum SMTPCommand {
+    RCPT_TO("RCPT TO"),
+    EHLO("EHLO"),
+    MAIL_FROM("MAIL FROM"),
+    DATA("DATA"),
+    RSET("RSET"),
+    VRFY("VRFY"),
+    NOOP("NOOP"),
+    QUIT("QUIT");
+
+    private final String commandName;
+
+    SMTPCommand(String commandName) {
+        this.commandName = commandName;
+    }
+}
diff --git a/server/mailet/mock-smtp-server/src/test/java/org/apache/james/mock/smtp/server/MockSMTPBehaviorTest.java b/server/mailet/mock-smtp-server/src/test/java/org/apache/james/mock/smtp/server/MockSMTPBehaviorTest.java
new file mode 100644
index 0000000..5fbeb51
--- /dev/null
+++ b/server/mailet/mock-smtp-server/src/test/java/org/apache/james/mock/smtp/server/MockSMTPBehaviorTest.java
@@ -0,0 +1,83 @@
+/****************************************************************
+ * 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;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
+
+import java.util.Optional;
+
+import org.junit.jupiter.api.Nested;
+import org.junit.jupiter.api.Test;
+
+class MockSMTPBehaviorTest {
+    private static final Response RESPONSE = Response.serverAccept(Response.SMTPStatusCode.of(250), "message");
+
+    @Nested
+    class NumberOfAnswersPolicyTest {
+        @Test
+        void timesShouldThrowWhenNegativeValue() {
+            assertThatThrownBy(() -> MockSMTPBehavior.NumberOfAnswersPolicy.times(-1))
+                .isInstanceOf(IllegalArgumentException.class);
+        }
+
+        @Test
+        void timesShouldThrowWhenZero() {
+            assertThatThrownBy(() -> MockSMTPBehavior.NumberOfAnswersPolicy.times(0))
+                .isInstanceOf(IllegalArgumentException.class);
+        }
+
+        @Test
+        void getNumberOfAnswersShouldReturnEmptyWhenAlways() {
+            assertThat(MockSMTPBehavior.NumberOfAnswersPolicy.anytime().getNumberOfAnswers())
+                .isEmpty();
+        }
+
+        @Test
+        void getNumberOfAnswersShouldReturnSpecifiedNumberWhenTimes() {
+            assertThat(MockSMTPBehavior.NumberOfAnswersPolicy.times(5).getNumberOfAnswers())
+                .contains(5);
+        }
+    }
+
+    @Test
+    void constructorShouldThrowWhenCommandIsNull() {
+        assertThatThrownBy(() -> new MockSMTPBehavior(null, Optional.empty(), RESPONSE, MockSMTPBehavior.NumberOfAnswersPolicy.anytime()))
+            .isInstanceOf(NullPointerException.class);
+    }
+
+    @Test
+    void constructorShouldThrowWhenConditionIsNull() {
+        assertThatThrownBy(() -> new MockSMTPBehavior(SMTPCommand.NOOP, null, RESPONSE, MockSMTPBehavior.NumberOfAnswersPolicy.anytime()))
+            .isInstanceOf(NullPointerException.class);
+    }
+
+    @Test
+    void constructorShouldThrowWhenResponseIsNull() {
+        assertThatThrownBy(() -> new MockSMTPBehavior(SMTPCommand.NOOP, Optional.empty(), null, MockSMTPBehavior.NumberOfAnswersPolicy.anytime()))
+            .isInstanceOf(NullPointerException.class);
+    }
+
+    @Test
+    void constructorShouldThrowWhenNumberOfAnswersIsNull() {
+        assertThatThrownBy(() -> new MockSMTPBehavior(SMTPCommand.NOOP, Optional.empty(), RESPONSE, null))
+            .isInstanceOf(NullPointerException.class);
+    }
+}


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