james-server-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From adup...@apache.org
Subject [4/4] james-project git commit: MAILET-135 Add a simple test on sending an email
Date Tue, 04 Oct 2016 17:27:23 GMT
MAILET-135 Add a simple test on sending an email


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

Branch: refs/heads/master
Commit: cd6fe9cbd4ce369378e8b450e8e8adf005872997
Parents: 4dd77f9
Author: Antoine Duprat <aduprat@linagora.com>
Authored: Wed Sep 21 15:49:15 2016 +0200
Committer: Antoine Duprat <aduprat@linagora.com>
Committed: Tue Oct 4 18:41:44 2016 +0200

----------------------------------------------------------------------
 .../modules/server/MemoryMailQueueFactory.java  | 11 ++-
 .../mailets/CommonMailetConfigurationTest.java  | 80 +++++++++++++++++++-
 .../james/mailets/TemporaryJamesServer.java     |  5 ++
 .../src/test/resources/logback.xml              | 21 +++++
 .../src/test/resources/usersrepository.xml      | 25 ++++++
 5 files changed, 140 insertions(+), 2 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/james-project/blob/cd6fe9cb/server/container/guice/memory-guice/src/main/java/org/apache/james/modules/server/MemoryMailQueueFactory.java
----------------------------------------------------------------------
diff --git a/server/container/guice/memory-guice/src/main/java/org/apache/james/modules/server/MemoryMailQueueFactory.java
b/server/container/guice/memory-guice/src/main/java/org/apache/james/modules/server/MemoryMailQueueFactory.java
index c1f92b1..eebc896 100644
--- a/server/container/guice/memory-guice/src/main/java/org/apache/james/modules/server/MemoryMailQueueFactory.java
+++ b/server/container/guice/memory-guice/src/main/java/org/apache/james/modules/server/MemoryMailQueueFactory.java
@@ -25,6 +25,7 @@ import java.util.concurrent.LinkedBlockingDeque;
 import java.util.concurrent.TimeUnit;
 
 import javax.mail.MessagingException;
+import javax.mail.internet.MimeMessage;
 
 import org.apache.james.core.MailImpl;
 import org.apache.james.queue.api.MailQueue;
@@ -32,6 +33,7 @@ import org.apache.james.queue.api.MailQueueFactory;
 import org.apache.james.queue.api.MailQueueItemDecoratorFactory;
 import org.apache.mailet.Mail;
 
+import com.github.fge.lambdas.Throwing;
 import com.google.common.base.Objects;
 import com.google.inject.Inject;
 
@@ -78,12 +80,19 @@ public class MemoryMailQueueFactory implements MailQueueFactory {
         @Override
         public void enQueue(Mail mail) throws MailQueueException {
             try {
-                mailItems.addFirst(new MemoryMailQueueItem(new MailImpl(mail)));
+                mailItems.addFirst(new MemoryMailQueueItem(cloneMail(mail)));
             } catch (MessagingException e) {
                 throw new MailQueueException("Error while copying mail " + mail.getName(),
e);
             }
         }
 
+        private Mail cloneMail(Mail mail) throws MessagingException {
+            MailImpl mailImpl = new MailImpl(mail);
+            Optional.ofNullable(mail.getMessage())
+                    .ifPresent(Throwing.consumer(message -> mailImpl.setMessage(new MimeMessage(message))));
+            return mailImpl;
+        }
+
         @Override
         public MailQueueItem deQueue() throws MailQueueException, InterruptedException {
             while (true) {

http://git-wip-us.apache.org/repos/asf/james-project/blob/cd6fe9cb/server/mailet/integration-testing/src/test/java/org/apache/james/mailets/CommonMailetConfigurationTest.java
----------------------------------------------------------------------
diff --git a/server/mailet/integration-testing/src/test/java/org/apache/james/mailets/CommonMailetConfigurationTest.java
b/server/mailet/integration-testing/src/test/java/org/apache/james/mailets/CommonMailetConfigurationTest.java
index 3d785aa..d8a8cad 100644
--- a/server/mailet/integration-testing/src/test/java/org/apache/james/mailets/CommonMailetConfigurationTest.java
+++ b/server/mailet/integration-testing/src/test/java/org/apache/james/mailets/CommonMailetConfigurationTest.java
@@ -19,6 +19,12 @@
 
 package org.apache.james.mailets;
 
+import java.io.IOException;
+import java.nio.channels.SocketChannel;
+
+import org.apache.commons.net.imap.IMAPClient;
+import org.apache.commons.net.smtp.SMTPClient;
+import org.apache.james.mailbox.model.MailboxConstants;
 import org.apache.james.mailets.configuration.CommonProcessors;
 import org.apache.james.mailets.configuration.MailetContainer;
 import org.junit.After;
@@ -27,14 +33,24 @@ import org.junit.Rule;
 import org.junit.Test;
 import org.junit.rules.TemporaryFolder;
 
+import com.google.common.base.Throwables;
+import com.jayway.awaitility.Awaitility;
+import com.jayway.awaitility.Duration;
+import com.jayway.awaitility.core.ConditionFactory;
+
 public class CommonMailetConfigurationTest {
 
     private static final String DEFAULT_DOMAIN = "james.org";
+    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";
 
     @Rule
     public TemporaryFolder temporaryFolder = new TemporaryFolder();
 
     private TemporaryJamesServer jamesServer;
+    private ConditionFactory calmlyAwait;
 
     @Before
     public void setup() throws Exception {
@@ -52,6 +68,8 @@ public class CommonMailetConfigurationTest {
             .build();
 
         jamesServer = new TemporaryJamesServer(temporaryFolder, mailetContainer);
+        Duration slowPacedPollInterval = Duration.FIVE_HUNDRED_MILLISECONDS;
+        calmlyAwait = Awaitility.with().pollInterval(slowPacedPollInterval).and().with().pollDelay(slowPacedPollInterval).await();
     }
 
     @After
@@ -60,6 +78,66 @@ public class CommonMailetConfigurationTest {
     }
 
     @Test
-    public void startingJamesWithCommonMailetConfigurationShouldWork() {
+    public void startingJamesWithCommonMailetConfigurationShouldWork() throws Exception {
+    }
+
+    @Test
+    public void simpleMailShouldBeSent() throws Exception {
+        jamesServer.getServerProbe().addDomain(DEFAULT_DOMAIN);
+        String from = "user@" + DEFAULT_DOMAIN;
+        jamesServer.getServerProbe().addUser(from, PASSWORD);
+        String recipient = "user2@" + DEFAULT_DOMAIN;
+        jamesServer.getServerProbe().addUser(recipient, PASSWORD);
+        jamesServer.getServerProbe().createMailbox(MailboxConstants.USER_NAMESPACE, recipient,
"INBOX");
+
+        SMTPClient smtpClient = new SMTPClient();
+        try (SocketChannel socketChannel = SocketChannel.open()) {
+            smtpClient.connect(LOCALHOST_IP, SMTP_PORT);
+            sendMessage(smtpClient, from, recipient);
+
+            calmlyAwait.atMost(Duration.ONE_MINUTE).until(() -> messageHasBeenSent(smtpClient));
+            calmlyAwait.atMost(Duration.ONE_MINUTE).until(() -> userReceivedMessage(recipient));
+        } finally {
+            smtpClient.disconnect();
+        }
+    }
+
+    private void sendMessage(SMTPClient smtpClient, String from, String recipient) {
+        try {
+            smtpClient.helo(DEFAULT_DOMAIN);
+            smtpClient.setSender(from);
+            smtpClient.rcpt("<" + recipient + ">");
+            smtpClient.sendShortMessageData("subject: test\r\n" +
+                    "\r\n" +
+                    "content\r\n" +
+                    ".\r\n");
+        } catch (IOException e) {
+            throw Throwables.propagate(e);
+        }
+    }
+
+    private boolean messageHasBeenSent(SMTPClient smtpClient) throws IOException {
+        return smtpClient.getReplyString()
+            .contains("250 2.6.0 Message received");
+    }
+
+    private boolean userReceivedMessage(String user) {
+        IMAPClient imapClient = new IMAPClient();
+        try {
+            imapClient.connect(LOCALHOST_IP, IMAP_PORT);
+            imapClient.login(user, PASSWORD);
+            imapClient.select("INBOX");
+            imapClient.fetch("1:1", "ALL");
+            String replyString = imapClient.getReplyString();
+            return replyString.contains("OK FETCH completed");
+        } catch (IOException e) {
+            throw Throwables.propagate(e);
+        } finally {
+            try {
+                imapClient.close();
+            } catch (IOException e) {
+                throw Throwables.propagate(e);
+            }
+        }
     }
 }

http://git-wip-us.apache.org/repos/asf/james-project/blob/cd6fe9cb/server/mailet/integration-testing/src/test/java/org/apache/james/mailets/TemporaryJamesServer.java
----------------------------------------------------------------------
diff --git a/server/mailet/integration-testing/src/test/java/org/apache/james/mailets/TemporaryJamesServer.java
b/server/mailet/integration-testing/src/test/java/org/apache/james/mailets/TemporaryJamesServer.java
index 265df8b..71cedb1 100644
--- a/server/mailet/integration-testing/src/test/java/org/apache/james/mailets/TemporaryJamesServer.java
+++ b/server/mailet/integration-testing/src/test/java/org/apache/james/mailets/TemporaryJamesServer.java
@@ -31,6 +31,7 @@ import org.apache.james.GuiceJamesServer;
 import org.apache.james.MemoryJamesServerMain;
 import org.apache.james.mailets.configuration.MailetContainer;
 import org.apache.james.modules.TestJMAPServerModule;
+import org.apache.james.utils.ExtendedServerProbe;
 import org.junit.rules.TemporaryFolder;
 
 public class TemporaryJamesServer {
@@ -67,4 +68,8 @@ public class TemporaryJamesServer {
     public void shutdown() {
         jamesServer.stop();
     }
+
+    public ExtendedServerProbe getServerProbe() {
+        return jamesServer.serverProbe();
+    }
 }

http://git-wip-us.apache.org/repos/asf/james-project/blob/cd6fe9cb/server/mailet/integration-testing/src/test/resources/logback.xml
----------------------------------------------------------------------
diff --git a/server/mailet/integration-testing/src/test/resources/logback.xml b/server/mailet/integration-testing/src/test/resources/logback.xml
new file mode 100644
index 0000000..52c0153
--- /dev/null
+++ b/server/mailet/integration-testing/src/test/resources/logback.xml
@@ -0,0 +1,21 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<configuration>
+
+        <contextListener class="ch.qos.logback.classic.jul.LevelChangePropagator">
+                <resetJUL>true</resetJUL>
+        </contextListener>
+
+        <appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
+                <encoder>
+                        <pattern>%d{HH:mm:ss.SSS} [%-5level] %logger{15} - %msg%n%rEx</pattern>
+                        <immediateFlush>false</immediateFlush>
+                </encoder>
+        </appender>
+
+        <root level="WARN">
+                <appender-ref ref="CONSOLE" />
+        </root>
+
+        <logger name="org.apache.james.mailetcontainer.impl.JamesMailetContext" level="DEBUG"/>
+
+</configuration>

http://git-wip-us.apache.org/repos/asf/james-project/blob/cd6fe9cb/server/mailet/integration-testing/src/test/resources/usersrepository.xml
----------------------------------------------------------------------
diff --git a/server/mailet/integration-testing/src/test/resources/usersrepository.xml b/server/mailet/integration-testing/src/test/resources/usersrepository.xml
new file mode 100644
index 0000000..85046b3
--- /dev/null
+++ b/server/mailet/integration-testing/src/test/resources/usersrepository.xml
@@ -0,0 +1,25 @@
+<?xml version="1.0"?>
+<!--
+  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.                                           
+ -->
+
+<usersrepository name="LocalUsers">
+    <algorithm>MD5</algorithm>
+    <enableVirtualHosting>true</enableVirtualHosting>    
+</usersrepository>
+


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