You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@james.apache.org by bt...@apache.org on 2022/11/03 08:08:34 UTC

[james-project] 05/05: JAMES-3843 Rewrite VacationRelayIntegrationTest with 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 415e1896e1eb469a9617c8191a0b01084d692a6e
Author: Benoit Tellier <bt...@linagora.com>
AuthorDate: Tue Nov 1 22:52:14 2022 +0700

    JAMES-3843 Rewrite VacationRelayIntegrationTest with Mock SMTP server
---
 .../jmap-draft-integration-testing-common/pom.xml  |  4 ++
 .../org/apache/james/jmap/MockSmtpTestRule.java    | 74 ++++++++++++++++++++++
 .../james/jmap/VacationRelayIntegrationTest.java   | 40 ++++++++----
 3 files changed, 104 insertions(+), 14 deletions(-)

diff --git a/server/protocols/jmap-draft-integration-testing/jmap-draft-integration-testing-common/pom.xml b/server/protocols/jmap-draft-integration-testing/jmap-draft-integration-testing-common/pom.xml
index 96fcb9264d..62d10065b1 100644
--- a/server/protocols/jmap-draft-integration-testing/jmap-draft-integration-testing-common/pom.xml
+++ b/server/protocols/jmap-draft-integration-testing/jmap-draft-integration-testing-common/pom.xml
@@ -101,6 +101,10 @@
             <artifactId>james-server-testing</artifactId>
             <scope>test</scope>
         </dependency>
+        <dependency>
+            <groupId>${james.groupId}</groupId>
+            <artifactId>mock-smtp-server</artifactId>
+        </dependency>
         <dependency>
             <groupId>${james.groupId}</groupId>
             <artifactId>testing-base</artifactId>
diff --git a/server/protocols/jmap-draft-integration-testing/jmap-draft-integration-testing-common/src/test/java/org/apache/james/jmap/MockSmtpTestRule.java b/server/protocols/jmap-draft-integration-testing/jmap-draft-integration-testing-common/src/test/java/org/apache/james/jmap/MockSmtpTestRule.java
new file mode 100644
index 0000000000..c8e9d8c071
--- /dev/null
+++ b/server/protocols/jmap-draft-integration-testing/jmap-draft-integration-testing-common/src/test/java/org/apache/james/jmap/MockSmtpTestRule.java
@@ -0,0 +1,74 @@
+/****************************************************************
+ * 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.jmap;
+
+import java.util.UUID;
+
+import org.apache.james.mock.smtp.server.ConfigurationClient;
+import org.apache.james.util.Host;
+import org.apache.james.util.docker.DockerContainer;
+import org.apache.james.util.docker.Images;
+import org.junit.rules.TestRule;
+import org.junit.runner.Description;
+import org.junit.runners.model.Statement;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.testcontainers.containers.wait.strategy.Wait;
+
+public class MockSmtpTestRule implements TestRule {
+    public static class DockerMockSmtp {
+        private static final Logger LOGGER = LoggerFactory.getLogger(DockerMockSmtp.class);
+
+        private final DockerContainer mockSmtpServer;
+
+        DockerMockSmtp() {
+            mockSmtpServer = DockerContainer.fromName(Images.MOCK_SMTP_SERVER)
+                .withLogConsumer(outputFrame -> LOGGER.debug("MockSMTP: " + outputFrame.getUtf8String()))
+                .withExposedPorts(25, 8000)
+                .waitingFor(Wait.forLogMessage(".*Mock SMTP server started.*", 1))
+                .withName("james-testing-mock-smtp-server-" + UUID.randomUUID());
+        }
+
+        public ConfigurationClient getConfigurationClient() {
+            return ConfigurationClient.from(Host.from(
+                mockSmtpServer.getHostIp(),
+                mockSmtpServer.getMappedPort(8000)));
+        }
+
+        public String getIPAddress() {
+            return mockSmtpServer.getContainerIp();
+        }
+    }
+
+    private final DockerMockSmtp dockerMockSmtp;
+
+    public MockSmtpTestRule() {
+        this.dockerMockSmtp = new DockerMockSmtp();
+    }
+
+    @Override
+    public Statement apply(Statement statement, Description description) {
+        return dockerMockSmtp.mockSmtpServer.apply(statement, description);
+    }
+
+    public DockerMockSmtp getDockerMockSmtp() {
+        return dockerMockSmtp;
+    }
+}
diff --git a/server/protocols/jmap-draft-integration-testing/jmap-draft-integration-testing-common/src/test/java/org/apache/james/jmap/VacationRelayIntegrationTest.java b/server/protocols/jmap-draft-integration-testing/jmap-draft-integration-testing-common/src/test/java/org/apache/james/jmap/VacationRelayIntegrationTest.java
index 6e022bbed5..96501f725e 100644
--- a/server/protocols/jmap-draft-integration-testing/jmap-draft-integration-testing-common/src/test/java/org/apache/james/jmap/VacationRelayIntegrationTest.java
+++ b/server/protocols/jmap-draft-integration-testing/jmap-draft-integration-testing-common/src/test/java/org/apache/james/jmap/VacationRelayIntegrationTest.java
@@ -22,33 +22,37 @@ package org.apache.james.jmap;
 import static org.apache.james.jmap.JMAPTestingConstants.DOMAIN;
 import static org.apache.james.jmap.JMAPTestingConstants.LOCALHOST_IP;
 import static org.apache.james.jmap.JMAPTestingConstants.calmlyAwait;
-import static org.hamcrest.Matchers.equalTo;
+import static org.assertj.core.api.Assertions.assertThat;
 
 import java.io.IOException;
+import java.util.List;
 import java.util.concurrent.TimeUnit;
 
 import org.apache.commons.net.smtp.SMTPClient;
 import org.apache.james.GuiceJamesServer;
+import org.apache.james.core.MailAddress;
 import org.apache.james.dnsservice.api.InMemoryDNSService;
 import org.apache.james.jmap.api.model.AccountId;
-import org.apache.james.transport.mailets.VacationMailet;
-import org.apache.james.vacation.api.VacationPatch;
 import org.apache.james.jmap.draft.JmapGuiceProbe;
 import org.apache.james.junit.categories.BasicFeature;
 import org.apache.james.mailbox.DefaultMailboxes;
 import org.apache.james.mailbox.model.MailboxConstants;
 import org.apache.james.mailbox.probe.MailboxProbe;
+import org.apache.james.mock.smtp.server.model.Mail;
 import org.apache.james.modules.MailboxProbeImpl;
 import org.apache.james.modules.protocols.SmtpGuiceProbe;
 import org.apache.james.probe.DataProbe;
 import org.apache.james.utils.DataProbeImpl;
-import org.apache.james.utils.FakeSmtp;
+import org.apache.james.vacation.api.VacationPatch;
+import org.assertj.core.api.SoftAssertions;
 import org.junit.After;
 import org.junit.Before;
 import org.junit.ClassRule;
 import org.junit.Test;
 import org.junit.experimental.categories.Category;
 
+import com.github.fge.lambdas.Throwing;
+
 public abstract class VacationRelayIntegrationTest {
 
     private static final String USER = "benwa";
@@ -57,7 +61,7 @@ public abstract class VacationRelayIntegrationTest {
     private static final String REASON = "Message explaining my wonderful vacations";
 
     @ClassRule
-    public static FakeSmtp fakeSmtp = FakeSmtp.withDefaultPort();
+    public static MockSmtpTestRule fakeSmtp = new MockSmtpTestRule();
 
     private GuiceJamesServer guiceJamesServer;
     private JmapGuiceProbe jmapGuiceProbe;
@@ -69,9 +73,8 @@ public abstract class VacationRelayIntegrationTest {
     @Before
     public void setUp() throws Exception {
         getInMemoryDns()
-            .registerMxRecord("yopmail.com", fakeSmtp.getContainer().getContainerIp());
+            .registerMxRecord("yopmail.com", fakeSmtp.getDockerMockSmtp().getIPAddress());
 
-        System.setProperty(VacationMailet.EXPLICIT_SENDER_PROPERTY, "true");
         guiceJamesServer = getJmapServer();
         guiceJamesServer.start();
 
@@ -87,9 +90,9 @@ public abstract class VacationRelayIntegrationTest {
 
     @After
     public void teardown() {
-        fakeSmtp.clean();
+        fakeSmtp.getDockerMockSmtp().getConfigurationClient().clearMails();
+        fakeSmtp.getDockerMockSmtp().getConfigurationClient().clearBehaviors();
         guiceJamesServer.stop();
-        System.clearProperty(VacationMailet.EXPLICIT_SENDER_PROPERTY);
     }
 
     @Category(BasicFeature.class)
@@ -111,10 +114,19 @@ public abstract class VacationRelayIntegrationTest {
         smtpClient.sendShortMessageData("Reply-To: <" + externalMail + ">\r\n\r\ncontent");
 
         calmlyAwait.atMost(1, TimeUnit.MINUTES)
-            .untilAsserted(() ->
-                fakeSmtp.assertEmailReceived(response -> response
-                    .body("[0].from", equalTo(USER_WITH_DOMAIN))
-                    .body("[0].to[0]", equalTo(externalMail))
-                    .body("[0].text", equalTo(REASON))));
+            .untilAsserted(() -> {
+                List<Mail> mails = fakeSmtp.getDockerMockSmtp().getConfigurationClient()
+                    .listMails();
+
+                assertThat(mails).hasSize(1);
+                SoftAssertions.assertSoftly(Throwing.consumer(softly -> {
+                    softly.assertThat(mails.get(0).getEnvelope().getFrom()).isEqualTo(MailAddress.nullSender());
+                    softly.assertThat(mails.get(0).getEnvelope().getRecipients())
+                        .containsOnly(Mail.Recipient.builder()
+                            .address(new MailAddress(externalMail))
+                            .build());
+                    softly.assertThat(mails.get(0).getMessage()).contains(REASON);
+                }));
+            });
     }
 }


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