JAMES-1773: New integration testing for RRT mailet

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

Branch: refs/heads/master
Commit: cd2b4b153a256f12cb91ded209e15e7b489ea8d3
Parents: e7e5476
Author: Quynh Nguyen <[email protected]>
Authored: Tue Nov 29 16:34:37 2016 +0700
Committer: Quynh Nguyen <[email protected]>
Committed: Wed Nov 30 09:31:10 2016 +0700

----------------------------------------------------------------------
 .../RecipientRewriteTableIntegrationTest.java   | 155 +++++++++++++++++++
 .../james/mailets/utils/IMAPMessageReader.java  |   8 +
 2 files changed, 163 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/james-project/blob/cd2b4b15/server/mailet/integration-testing/src/test/java/org/apache/james/mailets/RecipientRewriteTableIntegrationTest.java
----------------------------------------------------------------------
diff --git 
a/server/mailet/integration-testing/src/test/java/org/apache/james/mailets/RecipientRewriteTableIntegrationTest.java
 
b/server/mailet/integration-testing/src/test/java/org/apache/james/mailets/RecipientRewriteTableIntegrationTest.java
new file mode 100644
index 0000000..3c42411
--- /dev/null
+++ 
b/server/mailet/integration-testing/src/test/java/org/apache/james/mailets/RecipientRewriteTableIntegrationTest.java
@@ -0,0 +1,155 @@
+/****************************************************************
+ * 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.mailets;
+
+import org.apache.james.mailbox.model.MailboxConstants;
+import org.apache.james.mailets.configuration.CommonProcessors;
+import org.apache.james.mailets.configuration.MailetContainer;
+import org.apache.james.mailets.utils.IMAPMessageReader;
+import org.apache.james.mailets.utils.SMTPMessageSender;
+import org.apache.james.utils.GuiceServerProbe;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.TemporaryFolder;
+
+import com.jayway.awaitility.Awaitility;
+import com.jayway.awaitility.Duration;
+import com.jayway.awaitility.core.ConditionFactory;
+
+public class RecipientRewriteTableIntegrationTest {
+    private static final String LOCALHOST_IP = "127.0.0.1";
+    private static final int IMAP_PORT = 1143;
+    private static final int SMTP_PORT = 1025;
+    private static final String PASSWORD = "secret";
+
+    private static final String JAMES_APACHE_ORG = "james.org";
+
+    private static final String FROM = "fromUser@" + JAMES_APACHE_ORG;
+    private static final String RECIPIENT = "touser@" + JAMES_APACHE_ORG;
+
+    private static final String ANY_AT_JAMES = "any@" + JAMES_APACHE_ORG;
+    private static final String OTHER_AT_JAMES = "other@" + JAMES_APACHE_ORG;
+
+    @Rule
+    public TemporaryFolder temporaryFolder = new TemporaryFolder();
+
+    private TemporaryJamesServer jamesServer;
+    private ConditionFactory calmlyAwait;
+    private GuiceServerProbe serverProbe;
+
+
+    @Before
+    public void setup() throws Exception {
+        MailetContainer mailetContainer = MailetContainer.builder()
+            .postmaster("postmaster@" + JAMES_APACHE_ORG)
+            .threads(5)
+            .addProcessor(CommonProcessors.root())
+            .addProcessor(CommonProcessors.error())
+            .addProcessor(CommonProcessors.transport())
+            .addProcessor(CommonProcessors.spam())
+            .addProcessor(CommonProcessors.localAddressError())
+            .addProcessor(CommonProcessors.relayDenied())
+            .addProcessor(CommonProcessors.bounces())
+            .addProcessor(CommonProcessors.sieveManagerCheck())
+            .build();
+
+        jamesServer = new TemporaryJamesServer(temporaryFolder, 
mailetContainer);
+        Duration slowPacedPollInterval = Duration.FIVE_HUNDRED_MILLISECONDS;
+        calmlyAwait = 
Awaitility.with().pollInterval(slowPacedPollInterval).and().with().pollDelay(slowPacedPollInterval).await();
+        serverProbe = jamesServer.getServerProbe();
+
+        serverProbe.addDomain(JAMES_APACHE_ORG);
+    }
+
+    @After
+    public void tearDown() {
+        jamesServer.shutdown();
+    }
+
+    @Test
+    public void rrtServiceShouldDeliverEmailToMappingRecipients() throws 
Exception {
+        serverProbe.addUser(FROM, PASSWORD);
+
+        createUserInbox(ANY_AT_JAMES);
+        createUserInbox(OTHER_AT_JAMES);
+
+        serverProbe.addAddressMapping("touser", JAMES_APACHE_ORG, 
ANY_AT_JAMES);
+        serverProbe.addAddressMapping("touser", JAMES_APACHE_ORG, 
OTHER_AT_JAMES);
+
+        try (SMTPMessageSender messageSender = 
SMTPMessageSender.noAuthentication(LOCALHOST_IP, SMTP_PORT, JAMES_APACHE_ORG);
+             IMAPMessageReader imapMessageReader = new 
IMAPMessageReader(LOCALHOST_IP, IMAP_PORT)) {
+            messageSender.sendMessage(FROM, RECIPIENT);
+            
calmlyAwait.atMost(Duration.ONE_MINUTE).until(messageSender::messageHasBeenSent);
+            calmlyAwait.atMost(Duration.ONE_MINUTE).until(() -> 
imapMessageReader.userReceivedMessage(ANY_AT_JAMES, PASSWORD));
+            calmlyAwait.atMost(Duration.ONE_MINUTE).until(() -> 
imapMessageReader.userReceivedMessage(OTHER_AT_JAMES, PASSWORD));
+        }
+    }
+
+    @Test
+    public void 
rrtServiceShouldNotDeliverEmailToRecipientWhenHaveMappingRecipients() throws 
Exception {
+        serverProbe.addUser(FROM, PASSWORD);
+
+        createUserInbox(RECIPIENT);
+        createUserInbox(ANY_AT_JAMES);
+        createUserInbox(OTHER_AT_JAMES);
+
+        serverProbe.addAddressMapping("touser", JAMES_APACHE_ORG, 
ANY_AT_JAMES);
+        serverProbe.addAddressMapping("touser", JAMES_APACHE_ORG, 
OTHER_AT_JAMES);
+
+        try (SMTPMessageSender messageSender = 
SMTPMessageSender.noAuthentication(LOCALHOST_IP, SMTP_PORT, JAMES_APACHE_ORG);
+             IMAPMessageReader imapMessageReader = new 
IMAPMessageReader(LOCALHOST_IP, IMAP_PORT)) {
+            messageSender.sendMessage(FROM, RECIPIENT);
+            
calmlyAwait.atMost(Duration.ONE_MINUTE).until(messageSender::messageHasBeenSent);
+
+            calmlyAwait.atMost(Duration.ONE_MINUTE).until(() -> 
imapMessageReader.userDoesNotReceiveMessage(RECIPIENT, PASSWORD));
+        }
+    }
+
+    @Test
+    public void 
rrtServiceShouldDeliverEmailToRecipientOnLocalWhenMappingContainsNonDomain() 
throws Exception {
+        String nonDomainUser = "nondomain";
+        String localUser = nonDomainUser + "@" + 
serverProbe.getDefaultDomain();
+
+        serverProbe.addUser(FROM, PASSWORD);
+
+        createUserInbox(localUser);
+        createUserInbox(OTHER_AT_JAMES);
+
+        serverProbe.addAddressMapping("touser", JAMES_APACHE_ORG, 
nonDomainUser);
+        serverProbe.addAddressMapping("touser", JAMES_APACHE_ORG, 
OTHER_AT_JAMES);
+
+        try (SMTPMessageSender messageSender = 
SMTPMessageSender.noAuthentication(LOCALHOST_IP, SMTP_PORT, JAMES_APACHE_ORG);
+             IMAPMessageReader imapMessageReader = new 
IMAPMessageReader(LOCALHOST_IP, IMAP_PORT)) {
+            messageSender.sendMessage(FROM, RECIPIENT);
+            
calmlyAwait.atMost(Duration.ONE_MINUTE).until(messageSender::messageHasBeenSent);
+
+            calmlyAwait.atMost(Duration.ONE_MINUTE).until(() -> 
imapMessageReader.userReceivedMessage(OTHER_AT_JAMES, PASSWORD));
+            calmlyAwait.atMost(Duration.ONE_MINUTE).until(() -> 
imapMessageReader.userReceivedMessage(localUser, PASSWORD));
+        }
+    }
+
+    private void createUserInbox(String username) throws Exception {
+        serverProbe.addUser(username, PASSWORD);
+        serverProbe.createMailbox(MailboxConstants.USER_NAMESPACE, username, 
"INBOX");
+    }
+
+}

http://git-wip-us.apache.org/repos/asf/james-project/blob/cd2b4b15/server/mailet/integration-testing/src/test/java/org/apache/james/mailets/utils/IMAPMessageReader.java
----------------------------------------------------------------------
diff --git 
a/server/mailet/integration-testing/src/test/java/org/apache/james/mailets/utils/IMAPMessageReader.java
 
b/server/mailet/integration-testing/src/test/java/org/apache/james/mailets/utils/IMAPMessageReader.java
index cfeda6c..17c7afd 100644
--- 
a/server/mailet/integration-testing/src/test/java/org/apache/james/mailets/utils/IMAPMessageReader.java
+++ 
b/server/mailet/integration-testing/src/test/java/org/apache/james/mailets/utils/IMAPMessageReader.java
@@ -45,6 +45,14 @@ public class IMAPMessageReader implements Closeable {
             .contains("OK FETCH completed");
     }
 
+    public boolean userDoesNotReceiveMessage(String user, String password) 
throws IOException {
+        imapClient.login(user, password);
+        imapClient.select("INBOX");
+        imapClient.fetch("1:1", "ALL");
+        return imapClient.getReplyString()
+             .contains("BAD FETCH failed. Invalid messageset");
+    }
+
     public String readFirstMessageInInbox(String user, String password) throws 
IOException {
         imapClient.login(user, password);
         imapClient.select("INBOX");


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to