james-server-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From btell...@apache.org
Subject [42/44] james-project git commit: JAMES-2262 Upgrade SpamAssassin tests code quality
Date Fri, 05 Jan 2018 02:57:01 GMT
JAMES-2262 Upgrade SpamAssassin tests code quality


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

Branch: refs/heads/improve-mailet-testing-experience-v1
Commit: 1c529101b6eb28166ccc89dbb562ab95e43c9824
Parents: 1d9f9df
Author: benwa <btellier@linagora.com>
Authored: Mon Dec 25 11:59:20 2017 +0700
Committer: benwa <btellier@linagora.com>
Committed: Fri Jan 5 09:34:39 2018 +0700

----------------------------------------------------------------------
 .../transport/mailets/SpamAssassinTest.java     | 198 +++++++------------
 1 file changed, 74 insertions(+), 124 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/james-project/blob/1c529101/server/mailet/integration-testing/src/test/java/org/apache/james/transport/mailets/SpamAssassinTest.java
----------------------------------------------------------------------
diff --git a/server/mailet/integration-testing/src/test/java/org/apache/james/transport/mailets/SpamAssassinTest.java
b/server/mailet/integration-testing/src/test/java/org/apache/james/transport/mailets/SpamAssassinTest.java
index bf1541e..f20a7e2 100644
--- a/server/mailet/integration-testing/src/test/java/org/apache/james/transport/mailets/SpamAssassinTest.java
+++ b/server/mailet/integration-testing/src/test/java/org/apache/james/transport/mailets/SpamAssassinTest.java
@@ -19,18 +19,24 @@
 package org.apache.james.transport.mailets;
 
 import static org.apache.james.MemoryJamesServerMain.SMTP_AND_IMAP_MODULE;
+import static org.apache.james.mailets.configuration.Constants.DEFAULT_DOMAIN;
+import static org.apache.james.mailets.configuration.Constants.FROM;
+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.RECIPIENT;
+import static org.apache.james.mailets.configuration.Constants.SMTP_PORT;
+import static org.apache.james.mailets.configuration.Constants.awaitOneMinute;
 import static org.assertj.core.api.Assertions.assertThat;
 
-import javax.mail.internet.MimeMessage;
+import javax.mail.MessagingException;
 
-import org.apache.james.core.MailAddress;
 import org.apache.james.mailets.TemporaryJamesServer;
 import org.apache.james.mailets.configuration.CommonProcessors;
 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.transport.matchers.All;
-import org.apache.james.transport.matchers.RecipientIsLocal;
 import org.apache.james.util.docker.Images;
 import org.apache.james.util.docker.SwarmGenericContainer;
 import org.apache.james.util.scanner.SpamAssassinInvoker;
@@ -43,81 +49,52 @@ import org.junit.After;
 import org.junit.Before;
 import org.junit.Rule;
 import org.junit.Test;
-import org.junit.rules.RuleChain;
 import org.junit.rules.TemporaryFolder;
 import org.testcontainers.containers.wait.HostPortWaitStrategy;
 
-import com.jayway.awaitility.Awaitility;
-import com.jayway.awaitility.Duration;
-import com.jayway.awaitility.core.ConditionFactory;
-
 public class SpamAssassinTest {
-    private static final String LOCALHOST_IP = "127.0.0.1";
-    private static final int IMAP_PORT = 1143;
-    private static final int SMTP_PORT = 1025;
-
-    public static final String JAMES_ORG = "james.org";
-    public static final String SENDER = "sender@" + JAMES_ORG;
-    public static final String TO = "to@" + JAMES_ORG;
-    public static final String PASSWORD = "secret";
+    private static final String SPAM_CONTENT = "XJS*C4JDBQADN1.NSBN3*2IDNEN*GTUBE-STANDARD-ANTI-UBE-TEST-EMAIL*C.34X";
 
+    @Rule
     public SwarmGenericContainer spamAssassinContainer = new SwarmGenericContainer(Images.SPAMASSASSIN)
         .withExposedPorts(783)
         .withAffinityToContainer()
         .waitingFor(new HostPortWaitStrategy());
-
+    @Rule
     public TemporaryFolder temporaryFolder = new TemporaryFolder();
-
     @Rule
-    public RuleChain chain = RuleChain.outerRule(temporaryFolder).around(spamAssassinContainer);
+    public IMAPMessageReader messageReader = new IMAPMessageReader();
+    @Rule
+    public SMTPMessageSender messageSender = new SMTPMessageSender(DEFAULT_DOMAIN);
 
     private TemporaryJamesServer jamesServer;
-    private ConditionFactory calmlyAwait;
 
     @Before
     public void setup() throws Exception {
-        MailetContainer mailets = MailetContainer
-            .builder()
-            .threads(5)
-            .postmaster(SENDER)
-            .addProcessor(CommonProcessors.root())
-            .addProcessor(CommonProcessors.error())
+        MailetContainer.Builder mailets = TemporaryJamesServer.DEFAUL_MAILET_CONTAINER_CONFIGURATION
             .addProcessor(
-                ProcessorConfiguration.builder()
-                    .state("transport")
+                ProcessorConfiguration.transport()
                     .addMailet(MailetConfiguration.builder()
                         .matcher(All.class)
                         .mailet(SpamAssassin.class)
-                        .addProperty(SpamAssassin.SPAMD_HOST, spamAssassinContainer.getContainerIp())
-                        .build())
+                        .addProperty(SpamAssassin.SPAMD_HOST, spamAssassinContainer.getContainerIp()))
                     .addMailet(MailetConfiguration.builder()
                         .matcher(All.class)
                         .mailet(MailAttributesToMimeHeaders.class)
                         .addProperty("simplemapping",
                             SpamAssassinInvoker.FLAG_MAIL_ATTRIBUTE_NAME + ";" + SpamAssassinInvoker.FLAG_MAIL_ATTRIBUTE_NAME
+ "," +
-                            SpamAssassinInvoker.STATUS_MAIL_ATTRIBUTE_NAME + ";" + SpamAssassinInvoker.STATUS_MAIL_ATTRIBUTE_NAME)
-                        .build())
-                    .addMailet(MailetConfiguration.builder()
-                        .matcher(All.class)
-                        .mailet(RemoveMimeHeader.class)
-                        .addProperty("name", "bcc")
-                        .build())
-                    .addMailet(MailetConfiguration.builder()
-                        .matcher(RecipientIsLocal.class)
-                        .mailet(LocalDelivery.class)
-                        .build())
-                    .build())
-            .build();
+                            SpamAssassinInvoker.STATUS_MAIL_ATTRIBUTE_NAME + ";" + SpamAssassinInvoker.STATUS_MAIL_ATTRIBUTE_NAME))
+                    .addMailetsFrom(CommonProcessors.deliverOnlyTransport()));
+
         jamesServer = TemporaryJamesServer.builder()
             .withBase(SMTP_AND_IMAP_MODULE)
-            .build(temporaryFolder, mailets);
-        Duration slowPacedPollInterval = Duration.FIVE_HUNDRED_MILLISECONDS;
-        calmlyAwait = Awaitility.with().pollInterval(slowPacedPollInterval).and().with().pollDelay(slowPacedPollInterval).await();
+            .withMailetContainer(mailets)
+            .build(temporaryFolder);
 
         DataProbeImpl probe = jamesServer.getProbe(DataProbeImpl.class);
-        probe.addDomain(JAMES_ORG);
-        probe.addUser(SENDER, PASSWORD);
-        probe.addUser(TO, PASSWORD);
+        probe.addDomain(DEFAULT_DOMAIN);
+        probe.addUser(FROM, PASSWORD);
+        probe.addUser(RECIPIENT, PASSWORD);
     }
 
     @After
@@ -127,88 +104,61 @@ public class SpamAssassinTest {
 
     @Test
     public void spamAssassinShouldAppendNewHeaderOnMessage() throws Exception {
-        MimeMessage message = MimeMessageBuilder.mimeMessageBuilder()
-            .setSender(SENDER)
-            .addToRecipient(TO)
-            .setSubject("This is the subject")
-            .setText("This is -SPAM- my email")
-            .build();
-        FakeMail mail = FakeMail.builder()
-            .mimeMessage(message)
-            .sender(new MailAddress(SENDER))
-            .recipients(new MailAddress(TO))
-            .build();
-
-        try (SMTPMessageSender messageSender = SMTPMessageSender.noAuthentication(LOCALHOST_IP,
SMTP_PORT, JAMES_ORG);
-             IMAPMessageReader imapMessageReader = new IMAPMessageReader(LOCALHOST_IP, IMAP_PORT))
{
-
-            messageSender.sendMessage(mail);
-
-            calmlyAwait.atMost(Duration.ONE_MINUTE).until(messageSender::messageHasBeenSent);
-            calmlyAwait.atMost(Duration.ONE_MINUTE).until(() -> imapMessageReader.userReceivedMessage(TO,
PASSWORD));
-
-            String receivedHeaders = imapMessageReader.readFirstMessageHeadersInInbox(TO,
PASSWORD);
-
-            assertThat(receivedHeaders).contains(SpamAssassinInvoker.FLAG_MAIL_ATTRIBUTE_NAME,
SpamAssassinInvoker.STATUS_MAIL_ATTRIBUTE_NAME);
-        }
+        messageSender.connect(LOCALHOST_IP, SMTP_PORT)
+            .sendMessage(mailWithContent("This is the content"))
+            .awaitSent(awaitOneMinute);
+
+        messageReader.connect(LOCALHOST_IP, IMAP_PORT)
+            .login(RECIPIENT, PASSWORD)
+            .select(IMAPMessageReader.INBOX)
+            .awaitMessage(awaitOneMinute);
+
+        assertThat(messageReader.readFirstMessageHeaders())
+            .contains(
+                SpamAssassinInvoker.FLAG_MAIL_ATTRIBUTE_NAME,
+                SpamAssassinInvoker.STATUS_MAIL_ATTRIBUTE_NAME);
     }
 
     @Test
     public void spamAssassinShouldAppendNewHeaderWhichDetectIsSpamWhenSpamMessage() throws
Exception {
-        String spamContent = "XJS*C4JDBQADN1.NSBN3*2IDNEN*GTUBE-STANDARD-ANTI-UBE-TEST-EMAIL*C.34X";
-        MimeMessage message = MimeMessageBuilder.mimeMessageBuilder()
-            .setSender(SENDER)
-            .addToRecipient(TO)
-            .setSubject("This is the subject")
-            .setText(spamContent)
-            .build();
-        FakeMail mail = FakeMail.builder()
-            .mimeMessage(message)
-            .sender(new MailAddress(SENDER))
-            .recipients(new MailAddress(TO))
-            .build();
-
-        try (SMTPMessageSender messageSender = SMTPMessageSender.noAuthentication(LOCALHOST_IP,
SMTP_PORT, JAMES_ORG);
-             IMAPMessageReader imapMessageReader = new IMAPMessageReader(LOCALHOST_IP, IMAP_PORT))
{
-
-            messageSender.sendMessage(mail);
-
-            calmlyAwait.atMost(Duration.ONE_MINUTE).until(messageSender::messageHasBeenSent);
-            calmlyAwait.atMost(Duration.ONE_MINUTE).until(() -> imapMessageReader.userReceivedMessage(TO,
PASSWORD));
-
-            String receivedHeaders = imapMessageReader.readFirstMessageInInbox(TO, PASSWORD);
-
-            assertThat(receivedHeaders).contains(SpamAssassinInvoker.FLAG_MAIL_ATTRIBUTE_NAME
+ ": YES");
-            assertThat(receivedHeaders).contains(SpamAssassinInvoker.STATUS_MAIL_ATTRIBUTE_NAME
+ ": Yes");
-        }
+        messageSender.connect(LOCALHOST_IP, SMTP_PORT)
+            .sendMessage(mailWithContent(SPAM_CONTENT))
+            .awaitSent(awaitOneMinute);
+
+        messageReader.connect(LOCALHOST_IP, IMAP_PORT)
+            .login(RECIPIENT, PASSWORD)
+            .select(IMAPMessageReader.INBOX)
+            .awaitMessage(awaitOneMinute);
+
+        String receivedHeaders = messageReader.readFirstMessageHeaders();
+        assertThat(receivedHeaders).contains(SpamAssassinInvoker.FLAG_MAIL_ATTRIBUTE_NAME
+ ": YES");
+        assertThat(receivedHeaders).contains(SpamAssassinInvoker.STATUS_MAIL_ATTRIBUTE_NAME
+ ": Yes");
     }
 
     @Test
     public void spamAssassinShouldAppendNewHeaderWhichNoWhenNonSpamMessage() throws Exception
{
-        MimeMessage message = MimeMessageBuilder.mimeMessageBuilder()
-            .setSender(SENDER)
-            .addToRecipient(TO)
-            .setSubject("This is the subject")
-            .setText("This is the content")
-            .build();
-        FakeMail mail = FakeMail.builder()
-            .mimeMessage(message)
-            .sender(new MailAddress(SENDER))
-            .recipients(new MailAddress(TO))
-            .build();
-
-        try (SMTPMessageSender messageSender = SMTPMessageSender.noAuthentication(LOCALHOST_IP,
SMTP_PORT, JAMES_ORG);
-             IMAPMessageReader imapMessageReader = new IMAPMessageReader(LOCALHOST_IP, IMAP_PORT))
{
-
-            messageSender.sendMessage(mail);
-
-            calmlyAwait.atMost(Duration.ONE_MINUTE).until(messageSender::messageHasBeenSent);
-            calmlyAwait.atMost(Duration.ONE_MINUTE).until(() -> imapMessageReader.userReceivedMessage(TO,
PASSWORD));
-
-            String receivedHeaders = imapMessageReader.readFirstMessageInInbox(TO, PASSWORD);
-
-            assertThat(receivedHeaders).contains(SpamAssassinInvoker.FLAG_MAIL_ATTRIBUTE_NAME
+ ": NO");
-            assertThat(receivedHeaders).contains(SpamAssassinInvoker.STATUS_MAIL_ATTRIBUTE_NAME
+ ": No");
-        }
+        messageSender.connect(LOCALHOST_IP, SMTP_PORT)
+            .sendMessage(mailWithContent("This is the content"))
+            .awaitSent(awaitOneMinute);
+
+        messageReader.connect(LOCALHOST_IP, IMAP_PORT)
+            .login(RECIPIENT, PASSWORD)
+            .select(IMAPMessageReader.INBOX)
+            .awaitMessage(awaitOneMinute);
+
+        String receivedHeaders = messageReader.readFirstMessageHeaders();
+        assertThat(receivedHeaders).contains(SpamAssassinInvoker.FLAG_MAIL_ATTRIBUTE_NAME
+ ": NO");
+        assertThat(receivedHeaders).contains(SpamAssassinInvoker.STATUS_MAIL_ATTRIBUTE_NAME
+ ": No");
+    }
+
+    private FakeMail.Builder mailWithContent(String textContent) throws MessagingException
{
+        return FakeMail.builder()
+            .mimeMessage(MimeMessageBuilder.mimeMessageBuilder()
+                .setSender(FROM)
+                .addToRecipient(RECIPIENT)
+                .setSubject("This is the subject")
+                .setText(textContent))
+            .sender(FROM)
+            .recipient(RECIPIENT);
     }
 }


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