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 2018/01/26 01:13:49 UTC

[05/10] james-project git commit: JAMES-2294 Integration test for mail reprocessing

JAMES-2294 Integration test for mail reprocessing


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

Branch: refs/heads/master
Commit: d5594ae7d0e8369528c197887b38616138b02f42
Parents: c73d5de
Author: benwa <bt...@linagora.com>
Authored: Thu Jan 25 11:35:16 2018 +0700
Committer: benwa <bt...@linagora.com>
Committed: Fri Jan 26 08:12:21 2018 +0700

----------------------------------------------------------------------
 .../james/utils/MailRepositoryProbeImpl.java    |  15 +-
 server/mailet/integration-testing/pom.xml       |   5 +
 .../transport/mailets/ToRepositoryTest.java     | 165 +++++++++++++++++++
 .../apache/james/utils/IMAPMessageReader.java   |   6 +-
 .../apache/james/utils/SMTPMessageSender.java   |   3 +-
 5 files changed, 187 insertions(+), 7 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/james-project/blob/d5594ae7/server/container/guice/guice-common/src/main/java/org/apache/james/utils/MailRepositoryProbeImpl.java
----------------------------------------------------------------------
diff --git a/server/container/guice/guice-common/src/main/java/org/apache/james/utils/MailRepositoryProbeImpl.java b/server/container/guice/guice-common/src/main/java/org/apache/james/utils/MailRepositoryProbeImpl.java
index baac629..e3143f2 100644
--- a/server/container/guice/guice-common/src/main/java/org/apache/james/utils/MailRepositoryProbeImpl.java
+++ b/server/container/guice/guice-common/src/main/java/org/apache/james/utils/MailRepositoryProbeImpl.java
@@ -19,11 +19,13 @@
 
 package org.apache.james.utils;
 
+import java.util.List;
+
 import javax.inject.Inject;
 
 import org.apache.james.mailrepository.api.MailRepositoryStore;
 
-import com.google.common.collect.Iterators;
+import com.google.common.collect.ImmutableList;
 
 public class MailRepositoryProbeImpl implements GuiceProbe {
 
@@ -37,9 +39,14 @@ public class MailRepositoryProbeImpl implements GuiceProbe {
     /**
      * Get the count of email currently stored in a given repository
      */
-    public int getRepositoryMailCount(String url) throws Exception {
-        return Iterators.size(repositoryStore.select(url)
-            .list());
+    public long getRepositoryMailCount(String url) throws Exception {
+        return repositoryStore.select(url).size();
+    }
+
+    public List<String> listMailKeys(String url) throws Exception {
+        return ImmutableList.copyOf(
+            repositoryStore.select(url)
+                .list());
     }
 
 }
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/james-project/blob/d5594ae7/server/mailet/integration-testing/pom.xml
----------------------------------------------------------------------
diff --git a/server/mailet/integration-testing/pom.xml b/server/mailet/integration-testing/pom.xml
index 30e083a..58b2f3f 100644
--- a/server/mailet/integration-testing/pom.xml
+++ b/server/mailet/integration-testing/pom.xml
@@ -145,6 +145,11 @@
             <scope>test</scope>
         </dependency>
         <dependency>
+            <groupId>org.junit.platform</groupId>
+            <artifactId>junit-platform-launcher</artifactId>
+            <scope>test</scope>
+        </dependency>
+        <dependency>
             <groupId>org.slf4j</groupId>
             <artifactId>slf4j-api</artifactId>
         </dependency>

http://git-wip-us.apache.org/repos/asf/james-project/blob/d5594ae7/server/mailet/integration-testing/src/test/java/org/apache/james/transport/mailets/ToRepositoryTest.java
----------------------------------------------------------------------
diff --git a/server/mailet/integration-testing/src/test/java/org/apache/james/transport/mailets/ToRepositoryTest.java b/server/mailet/integration-testing/src/test/java/org/apache/james/transport/mailets/ToRepositoryTest.java
new file mode 100644
index 0000000..3f821fa
--- /dev/null
+++ b/server/mailet/integration-testing/src/test/java/org/apache/james/transport/mailets/ToRepositoryTest.java
@@ -0,0 +1,165 @@
+/****************************************************************
+ * 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.transport.mailets;
+
+import static com.jayway.restassured.RestAssured.with;
+import static org.apache.james.mailets.configuration.Constants.DEFAULT_DOMAIN;
+import static org.apache.james.mailets.configuration.Constants.IMAP_PORT;
+import static org.apache.james.mailets.configuration.Constants.LOCALHOST_IP;
+import static org.apache.james.mailets.configuration.Constants.PASSWORD;
+import static org.apache.james.mailets.configuration.Constants.SMTP_PORT;
+import static org.apache.james.mailets.configuration.Constants.awaitOneMinute;
+
+import java.net.URLEncoder;
+import java.nio.charset.StandardCharsets;
+
+import org.apache.james.mailets.TemporaryJamesServer;
+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.probe.DataProbe;
+import org.apache.james.transport.matchers.All;
+import org.apache.james.utils.DataProbeImpl;
+import org.apache.james.utils.IMAPMessageReader;
+import org.apache.james.utils.MailRepositoryProbeImpl;
+import org.apache.james.utils.SMTPMessageSender;
+import org.apache.james.utils.WebAdminGuiceProbe;
+import org.apache.james.webadmin.WebAdminUtils;
+import org.apache.james.webadmin.routes.MailRepositoriesRoutes;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.TemporaryFolder;
+
+import com.jayway.restassured.specification.RequestSpecification;
+
+public class ToRepositoryTest {
+    private static final String RECIPIENT = "touser@" + DEFAULT_DOMAIN;
+    public static final String CUSTOM_REPOSITORY = "file://var/mail/custom/";
+
+    @Rule
+    public TemporaryFolder temporaryFolder = new TemporaryFolder();
+    @Rule
+    public IMAPMessageReader imapMessageReader = new IMAPMessageReader();
+    @Rule
+    public SMTPMessageSender messageSender = new SMTPMessageSender(DEFAULT_DOMAIN);
+
+    private TemporaryJamesServer jamesServer;
+    private MailRepositoryProbeImpl probe;
+    private RequestSpecification webAdminAPI;
+
+    @Before
+    public void setup() throws Exception {
+        MailetContainer.Builder mailetContainer = TemporaryJamesServer.SIMPLE_MAILET_CONTAINER_CONFIGURATION
+            .putProcessor(ProcessorConfiguration.root()
+                .addMailet(MailetConfiguration.builder()
+                    .matcher(All.class)
+                    .mailet(ToRepository.class)
+                    .addProperty("repositoryPath", CUSTOM_REPOSITORY)));
+
+        jamesServer = TemporaryJamesServer.builder()
+            .withMailetContainer(mailetContainer)
+            .build(temporaryFolder);
+
+        DataProbe dataProbe = jamesServer.getProbe(DataProbeImpl.class);
+        dataProbe.addDomain(DEFAULT_DOMAIN);
+        dataProbe.addUser(RECIPIENT, PASSWORD);
+
+        probe = jamesServer.getProbe(MailRepositoryProbeImpl.class);
+        webAdminAPI = WebAdminUtils.buildRequestSpecification(
+            jamesServer.getProbe(WebAdminGuiceProbe.class)
+                .getWebAdminPort())
+            .build();
+    }
+
+    @After
+    public void tearDown() {
+        jamesServer.shutdown();
+    }
+
+    @Test
+    public void incomingShouldBeStoredInProcessorByDefault() throws Exception {
+        messageSender.connect(LOCALHOST_IP, SMTP_PORT)
+            .sendMessage(RECIPIENT, RECIPIENT)
+            .awaitSent(awaitOneMinute)
+            .sendMessage(RECIPIENT, RECIPIENT)
+            .awaitSent(awaitOneMinute);
+
+        awaitOneMinute.until(() -> probe.getRepositoryMailCount(CUSTOM_REPOSITORY) == 2);
+    }
+
+    @Test
+    public void userShouldBeAbleToAccessReprocessedMails() throws Exception {
+        messageSender.connect(LOCALHOST_IP, SMTP_PORT)
+            .sendMessage(RECIPIENT, RECIPIENT)
+            .awaitSent(awaitOneMinute)
+            .sendMessage(RECIPIENT, RECIPIENT)
+            .awaitSent(awaitOneMinute);
+
+        awaitOneMinute.until(() -> probe.getRepositoryMailCount(CUSTOM_REPOSITORY) == 2);
+
+        with()
+            .spec(webAdminAPI)
+            .queryParam("processor", ProcessorConfiguration.STATE_TRANSPORT)
+            .queryParam("action", "reprocess")
+            .patch(MailRepositoriesRoutes.MAIL_REPOSITORIES
+                + "/" + URLEncoder.encode(CUSTOM_REPOSITORY, StandardCharsets.UTF_8.displayName())
+                + "/mails")
+            .jsonPath()
+            .get("taskId");
+
+        imapMessageReader.connect(LOCALHOST_IP, IMAP_PORT)
+            .login(RECIPIENT, PASSWORD)
+            .select(IMAPMessageReader.INBOX)
+            .awaitMessage(awaitOneMinute)
+            .hasMessageCount(2);
+        awaitOneMinute.until(() -> probe.getRepositoryMailCount(CUSTOM_REPOSITORY) == 0);
+    }
+
+    @Test
+    public void userShouldBeAbleToAccessReprocessedMail() throws Exception {
+        messageSender.connect(LOCALHOST_IP, SMTP_PORT)
+            .sendMessage(RECIPIENT, RECIPIENT)
+            .awaitSent(awaitOneMinute)
+            .sendMessage(RECIPIENT, RECIPIENT)
+            .awaitSent(awaitOneMinute);
+
+        awaitOneMinute.until(() -> probe.getRepositoryMailCount(CUSTOM_REPOSITORY) == 2);
+        String key = probe.listMailKeys(CUSTOM_REPOSITORY).get(0);
+
+        with()
+            .spec(webAdminAPI)
+            .queryParam("processor", ProcessorConfiguration.STATE_TRANSPORT)
+            .queryParam("action", "reprocess")
+            .patch(MailRepositoriesRoutes.MAIL_REPOSITORIES
+                + "/" + URLEncoder.encode(CUSTOM_REPOSITORY, StandardCharsets.UTF_8.displayName())
+                + "/mails/" + key)
+            .jsonPath()
+            .get("taskId");
+
+        imapMessageReader.connect(LOCALHOST_IP, IMAP_PORT)
+            .login(RECIPIENT, PASSWORD)
+            .select(IMAPMessageReader.INBOX)
+            .awaitMessage(awaitOneMinute)
+            .hasMessageCount(1);
+        awaitOneMinute.until(() -> probe.getRepositoryMailCount(CUSTOM_REPOSITORY) == 1);
+    }
+}

http://git-wip-us.apache.org/repos/asf/james-project/blob/d5594ae7/server/testing/src/main/java/org/apache/james/utils/IMAPMessageReader.java
----------------------------------------------------------------------
diff --git a/server/testing/src/main/java/org/apache/james/utils/IMAPMessageReader.java b/server/testing/src/main/java/org/apache/james/utils/IMAPMessageReader.java
index 14a1fad..277fe6d 100644
--- a/server/testing/src/main/java/org/apache/james/utils/IMAPMessageReader.java
+++ b/server/testing/src/main/java/org/apache/james/utils/IMAPMessageReader.java
@@ -72,12 +72,14 @@ public class IMAPMessageReader extends ExternalResource implements Closeable {
             .contains("OK FETCH completed");
     }
 
-    public void awaitMessage(ConditionFactory conditionFactory) throws IOException {
+    public IMAPMessageReader awaitMessage(ConditionFactory conditionFactory) throws IOException {
         conditionFactory.until(this::hasAMessage);
+        return this;
     }
 
-    public void awaitNoMessage(ConditionFactory conditionFactory) throws IOException {
+    public IMAPMessageReader awaitNoMessage(ConditionFactory conditionFactory) throws IOException {
         conditionFactory.until(this::userDoesNotReceiveMessage);
+        return this;
     }
 
     public boolean hasAMessageWithFlags(String flags) throws IOException {

http://git-wip-us.apache.org/repos/asf/james-project/blob/d5594ae7/server/testing/src/main/java/org/apache/james/utils/SMTPMessageSender.java
----------------------------------------------------------------------
diff --git a/server/testing/src/main/java/org/apache/james/utils/SMTPMessageSender.java b/server/testing/src/main/java/org/apache/james/utils/SMTPMessageSender.java
index bb8d982..cc35a0a 100644
--- a/server/testing/src/main/java/org/apache/james/utils/SMTPMessageSender.java
+++ b/server/testing/src/main/java/org/apache/james/utils/SMTPMessageSender.java
@@ -164,8 +164,9 @@ public class SMTPMessageSender extends ExternalResource implements Closeable {
             .contains("250 2.6.0 Message received");
     }
 
-    public void awaitSent(ConditionFactory conditionFactory) {
+    public SMTPMessageSender awaitSent(ConditionFactory conditionFactory) {
         conditionFactory.until(this::messageHasBeenSent);
+        return this;
     }
 
     public void awaitSentFail(ConditionFactory conditionFactory) {


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