james-server-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rouaz...@apache.org
Subject [23/50] [abbrv] james-project git commit: MAILET-115 Add tests on subject modification/creation
Date Wed, 11 Jan 2017 09:26:11 GMT
MAILET-115 Add tests on subject modification/creation


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

Branch: refs/heads/master
Commit: 8b1a8c8b2bf1eedd4430d21275bb4b85394ecb04
Parents: fffe057
Author: Antoine Duprat <aduprat@apache.org>
Authored: Tue Nov 15 14:04:07 2016 +0100
Committer: Benoit Tellier <btellier@linagora.com>
Committed: Wed Jan 11 10:03:30 2017 +0700

----------------------------------------------------------------------
 .../mailet/base/test/FakeMailContext.java       | 14 +++++
 .../james/transport/mailets/DSNBounceTest.java  | 20 ++++++++
 .../james/transport/mailets/ForwardTest.java    | 24 +++++++++
 .../transport/mailets/NotifyPostmasterTest.java | 24 +++++++++
 .../transport/mailets/NotifySenderTest.java     | 24 +++++++++
 .../james/transport/mailets/RedirectTest.java   | 46 +++++++++++++++++
 .../james/transport/mailets/ResendTest.java     | 54 +++++++++++++++++++-
 7 files changed, 205 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/james-project/blob/8b1a8c8b/mailet/base/src/test/java/org/apache/mailet/base/test/FakeMailContext.java
----------------------------------------------------------------------
diff --git a/mailet/base/src/test/java/org/apache/mailet/base/test/FakeMailContext.java b/mailet/base/src/test/java/org/apache/mailet/base/test/FakeMailContext.java
index d509a24..4617a94 100644
--- a/mailet/base/src/test/java/org/apache/mailet/base/test/FakeMailContext.java
+++ b/mailet/base/src/test/java/org/apache/mailet/base/test/FakeMailContext.java
@@ -156,6 +156,7 @@ public class FakeMailContext implements MailetContext {
         private final MailAddress sender;
         private final Collection<MailAddress> recipients;
         private final MimeMessage msg;
+        private final Optional<String> subject;
         private final Map<String, Serializable> attributes;
         private final String state;
 
@@ -163,10 +164,19 @@ public class FakeMailContext implements MailetContext {
             this.sender = sender;
             this.recipients = ImmutableList.copyOf(recipients);
             this.msg = msg;
+            this.subject = getSubject(msg);
             this.attributes = ImmutableMap.copyOf(attributes);
             this.state = state;
         }
 
+        private Optional<String> getSubject(MimeMessage msg) {
+            try {
+                return Optional.fromNullable(msg.getSubject());
+            } catch (Exception e) {
+                return Optional.absent();
+            }
+        }
+
         public MailAddress getSender() {
             return sender;
         }
@@ -183,6 +193,10 @@ public class FakeMailContext implements MailetContext {
             return state;
         }
 
+        public Optional<String> getSubject() {
+            return subject;
+        }
+
         @Override
         public boolean equals(Object o) {
             if (!(o instanceof SentMail)) {

http://git-wip-us.apache.org/repos/asf/james-project/blob/8b1a8c8b/server/mailet/mailets/src/test/java/org/apache/james/transport/mailets/DSNBounceTest.java
----------------------------------------------------------------------
diff --git a/server/mailet/mailets/src/test/java/org/apache/james/transport/mailets/DSNBounceTest.java
b/server/mailet/mailets/src/test/java/org/apache/james/transport/mailets/DSNBounceTest.java
index 53994e9..a9a9dcb 100644
--- a/server/mailet/mailets/src/test/java/org/apache/james/transport/mailets/DSNBounceTest.java
+++ b/server/mailet/mailets/src/test/java/org/apache/james/transport/mailets/DSNBounceTest.java
@@ -38,6 +38,7 @@ import org.apache.james.dnsservice.api.DNSService;
 import org.apache.james.transport.mailets.redirect.SpecialAddress;
 import org.apache.mailet.Mail;
 import org.apache.mailet.MailAddress;
+import org.apache.mailet.base.MailAddressFixture;
 import org.apache.mailet.base.RFC2822Headers;
 import org.apache.mailet.base.mail.MimeMultipartReport;
 import org.apache.mailet.base.test.FakeMail;
@@ -472,4 +473,23 @@ public class DSNBounceTest {
         MimeMessage sentMessage = sentMail.getMsg();
         assertThat(sentMessage.getHeader(RFC2822Headers.DATE)[0]).isEqualTo(expectedDate);
     }
+
+    @Test
+    public void dsnBounceShouldAddPrefixToSubjectWhenPrefixIsConfigured() throws Exception
{
+        FakeMailetConfig mailetConfig = new FakeMailetConfig(MAILET_NAME, fakeMailContext);
+        mailetConfig.setProperty("prefix", "pre");
+        dsnBounce.init(mailetConfig);
+
+        MimeMessage mimeMessage = new MimeMessage(Session.getDefaultInstance(new Properties()));
+        mimeMessage.setSubject("My subject");
+        FakeMail mail = FakeMail.builder()
+                .name(MAILET_NAME)
+                .sender(MailAddressFixture.ANY_AT_JAMES)
+                .mimeMessage(mimeMessage)
+                .build();
+
+        dsnBounce.service(mail);
+
+        assertThat(mail.getMessage().getSubject()).isEqualTo("pre My subject");
+    }
 }

http://git-wip-us.apache.org/repos/asf/james-project/blob/8b1a8c8b/server/mailet/mailets/src/test/java/org/apache/james/transport/mailets/ForwardTest.java
----------------------------------------------------------------------
diff --git a/server/mailet/mailets/src/test/java/org/apache/james/transport/mailets/ForwardTest.java
b/server/mailet/mailets/src/test/java/org/apache/james/transport/mailets/ForwardTest.java
index 9037db2..25d8013 100644
--- a/server/mailet/mailets/src/test/java/org/apache/james/transport/mailets/ForwardTest.java
+++ b/server/mailet/mailets/src/test/java/org/apache/james/transport/mailets/ForwardTest.java
@@ -25,12 +25,16 @@ import static org.mockito.Mockito.when;
 
 import java.net.UnknownHostException;
 import java.util.Collection;
+import java.util.Properties;
 
 import javax.mail.MessagingException;
+import javax.mail.Session;
+import javax.mail.internet.MimeMessage;
 
 import org.apache.james.dnsservice.api.DNSService;
 import org.apache.mailet.MailAddress;
 import org.apache.mailet.base.MailAddressFixture;
+import org.apache.mailet.base.test.FakeMail;
 import org.apache.mailet.base.test.FakeMailContext;
 import org.apache.mailet.base.test.FakeMailetConfig;
 import org.junit.Before;
@@ -171,4 +175,24 @@ public class ForwardTest {
 
         assertThat(forward.getRecipients()).containsOnly(postmaster);
     }
+
+    @Test
+    public void forwardShouldNotModifySubject() throws Exception {
+        FakeMailetConfig mailetConfig = new FakeMailetConfig(MAILET_NAME, fakeMailContext);
+        mailetConfig.setProperty("forwardTo", "other@james.org");
+        forward.init(mailetConfig);
+
+        MimeMessage mimeMessage = new MimeMessage(Session.getDefaultInstance(new Properties()));
+        String expectedSubject = "My subject";
+        mimeMessage.setSubject(expectedSubject);
+        FakeMail mail = FakeMail.builder()
+                .name(MAILET_NAME)
+                .sender(MailAddressFixture.ANY_AT_JAMES)
+                .mimeMessage(mimeMessage)
+                .build();
+
+        forward.service(mail);
+
+        assertThat(mail.getMessage().getSubject()).isEqualTo(expectedSubject);
+    }
 }

http://git-wip-us.apache.org/repos/asf/james-project/blob/8b1a8c8b/server/mailet/mailets/src/test/java/org/apache/james/transport/mailets/NotifyPostmasterTest.java
----------------------------------------------------------------------
diff --git a/server/mailet/mailets/src/test/java/org/apache/james/transport/mailets/NotifyPostmasterTest.java
b/server/mailet/mailets/src/test/java/org/apache/james/transport/mailets/NotifyPostmasterTest.java
index e0db8d8..e70786a 100644
--- a/server/mailet/mailets/src/test/java/org/apache/james/transport/mailets/NotifyPostmasterTest.java
+++ b/server/mailet/mailets/src/test/java/org/apache/james/transport/mailets/NotifyPostmasterTest.java
@@ -24,12 +24,17 @@ import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.when;
 
 import java.net.UnknownHostException;
+import java.util.Properties;
 
 import javax.mail.MessagingException;
+import javax.mail.Session;
+import javax.mail.internet.MimeMessage;
 
 import org.apache.james.dnsservice.api.DNSService;
 import org.apache.james.transport.mailets.redirect.SpecialAddress;
 import org.apache.mailet.MailAddress;
+import org.apache.mailet.base.MailAddressFixture;
+import org.apache.mailet.base.test.FakeMail;
 import org.apache.mailet.base.test.FakeMailContext;
 import org.apache.mailet.base.test.FakeMailetConfig;
 import org.junit.Before;
@@ -122,4 +127,23 @@ public class NotifyPostmasterTest {
 
         assertThat(notifyPostmaster.getTo()).containsOnly(postmaster.toInternetAddress());
     }
+
+    @Test
+    public void notifyPostmasterShouldAddPrefixToSubjectWhenPrefixIsConfigured() throws Exception
{
+        FakeMailetConfig mailetConfig = new FakeMailetConfig(MAILET_NAME, fakeMailContext);
+        mailetConfig.setProperty("prefix", "pre");
+        notifyPostmaster.init(mailetConfig);
+
+        MimeMessage mimeMessage = new MimeMessage(Session.getDefaultInstance(new Properties()));
+        mimeMessage.setSubject("My subject");
+        FakeMail mail = FakeMail.builder()
+                .name(MAILET_NAME)
+                .sender(MailAddressFixture.ANY_AT_JAMES)
+                .mimeMessage(mimeMessage)
+                .build();
+
+        notifyPostmaster.service(mail);
+
+        assertThat(mail.getMessage().getSubject()).isEqualTo("pre My subject");
+    }
 }

http://git-wip-us.apache.org/repos/asf/james-project/blob/8b1a8c8b/server/mailet/mailets/src/test/java/org/apache/james/transport/mailets/NotifySenderTest.java
----------------------------------------------------------------------
diff --git a/server/mailet/mailets/src/test/java/org/apache/james/transport/mailets/NotifySenderTest.java
b/server/mailet/mailets/src/test/java/org/apache/james/transport/mailets/NotifySenderTest.java
index a59196e..5c19686 100644
--- a/server/mailet/mailets/src/test/java/org/apache/james/transport/mailets/NotifySenderTest.java
+++ b/server/mailet/mailets/src/test/java/org/apache/james/transport/mailets/NotifySenderTest.java
@@ -24,12 +24,17 @@ import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.when;
 
 import java.net.UnknownHostException;
+import java.util.Properties;
 
 import javax.mail.MessagingException;
+import javax.mail.Session;
+import javax.mail.internet.MimeMessage;
 
 import org.apache.james.dnsservice.api.DNSService;
 import org.apache.james.transport.mailets.redirect.SpecialAddress;
 import org.apache.mailet.MailAddress;
+import org.apache.mailet.base.MailAddressFixture;
+import org.apache.mailet.base.test.FakeMail;
 import org.apache.mailet.base.test.FakeMailContext;
 import org.apache.mailet.base.test.FakeMailetConfig;
 import org.junit.Before;
@@ -131,4 +136,23 @@ public class NotifySenderTest {
 
         assertThat(notifySender.getTo()).containsOnly(SpecialAddress.SENDER.toInternetAddress());
     }
+
+    @Test
+    public void notifySenderShouldAddPrefixToSubjectWhenPrefixIsConfigured() throws Exception
{
+        FakeMailetConfig mailetConfig = new FakeMailetConfig(MAILET_NAME, fakeMailContext);
+        mailetConfig.setProperty("prefix", "pre");
+        notifySender.init(mailetConfig);
+
+        MimeMessage mimeMessage = new MimeMessage(Session.getDefaultInstance(new Properties()));
+        mimeMessage.setSubject("My subject");
+        FakeMail mail = FakeMail.builder()
+                .name(MAILET_NAME)
+                .sender(MailAddressFixture.ANY_AT_JAMES)
+                .mimeMessage(mimeMessage)
+                .build();
+
+        notifySender.service(mail);
+
+        assertThat(mail.getMessage().getSubject()).isEqualTo("pre My subject");
+    }
 }
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/james-project/blob/8b1a8c8b/server/mailet/mailets/src/test/java/org/apache/james/transport/mailets/RedirectTest.java
----------------------------------------------------------------------
diff --git a/server/mailet/mailets/src/test/java/org/apache/james/transport/mailets/RedirectTest.java
b/server/mailet/mailets/src/test/java/org/apache/james/transport/mailets/RedirectTest.java
index 152e59e..eab7081 100644
--- a/server/mailet/mailets/src/test/java/org/apache/james/transport/mailets/RedirectTest.java
+++ b/server/mailet/mailets/src/test/java/org/apache/james/transport/mailets/RedirectTest.java
@@ -20,6 +20,7 @@
 package org.apache.james.transport.mailets;
 
 import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.guava.api.Assertions.assertThat;
 import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.when;
 
@@ -32,8 +33,10 @@ import javax.mail.internet.MimeMessage;
 
 import org.apache.james.dnsservice.api.DNSService;
 import org.apache.mailet.MailAddress;
+import org.apache.mailet.base.MailAddressFixture;
 import org.apache.mailet.base.test.FakeMail;
 import org.apache.mailet.base.test.FakeMailContext;
+import org.apache.mailet.base.test.FakeMailContext.SentMail;
 import org.apache.mailet.base.test.FakeMailetConfig;
 import org.junit.Before;
 import org.junit.Rule;
@@ -295,4 +298,47 @@ public class RedirectTest {
 
         assertThat(redirect.getReversePath(mail)).isEqualTo(new MailAddress("sender@james.org"));
     }
+
+    @Test
+    public void redirectShouldNotModifyOriginalSubject() throws Exception {
+        FakeMailetConfig mailetConfig = new FakeMailetConfig(MAILET_NAME, fakeMailContext);
+        mailetConfig.setProperty("subject", "subj");
+        mailetConfig.setProperty("prefix", "pref");
+        redirect.init(mailetConfig);
+
+        MimeMessage mimeMessage = new MimeMessage(Session.getDefaultInstance(new Properties()));
+        mimeMessage.setSubject("My subject");
+        mimeMessage.setText("content");
+        FakeMail mail = FakeMail.builder()
+                .name(MAILET_NAME)
+                .sender(MailAddressFixture.ANY_AT_JAMES)
+                .mimeMessage(mimeMessage)
+                .build();
+
+        redirect.service(mail);
+
+        assertThat(mail.getMessage().getSubject()).isEqualTo("My subject");
+    }
+
+    @Test
+    public void redirectShouldAddPrefixAndSubjectToSentMail() throws Exception {
+        FakeMailetConfig mailetConfig = new FakeMailetConfig(MAILET_NAME, fakeMailContext);
+        mailetConfig.setProperty("subject", "subj");
+        mailetConfig.setProperty("prefix", "pre");
+        redirect.init(mailetConfig);
+
+        MimeMessage mimeMessage = new MimeMessage(Session.getDefaultInstance(new Properties()));
+        mimeMessage.setSubject("My subject");
+        mimeMessage.setText("content");
+        FakeMail mail = FakeMail.builder()
+                .name(MAILET_NAME)
+                .sender(MailAddressFixture.ANY_AT_JAMES)
+                .mimeMessage(mimeMessage)
+                .build();
+
+        redirect.service(mail);
+
+        SentMail newMail = fakeMailContext.getSentMails().get(0);
+        assertThat(newMail.getSubject()).contains("pre subj");
+    }
 }

http://git-wip-us.apache.org/repos/asf/james-project/blob/8b1a8c8b/server/mailet/mailets/src/test/java/org/apache/james/transport/mailets/ResendTest.java
----------------------------------------------------------------------
diff --git a/server/mailet/mailets/src/test/java/org/apache/james/transport/mailets/ResendTest.java
b/server/mailet/mailets/src/test/java/org/apache/james/transport/mailets/ResendTest.java
index b40878b..1aae78b 100644
--- a/server/mailet/mailets/src/test/java/org/apache/james/transport/mailets/ResendTest.java
+++ b/server/mailet/mailets/src/test/java/org/apache/james/transport/mailets/ResendTest.java
@@ -20,17 +20,23 @@
 package org.apache.james.transport.mailets;
 
 import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.guava.api.Assertions.assertThat;
 import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.when;
 
 import java.net.UnknownHostException;
+import java.util.Properties;
 
 import javax.mail.MessagingException;
+import javax.mail.Session;
+import javax.mail.internet.MimeMessage;
 
 import org.apache.james.dnsservice.api.DNSService;
-import org.apache.james.transport.mailets.Resend;
 import org.apache.mailet.MailAddress;
+import org.apache.mailet.base.MailAddressFixture;
+import org.apache.mailet.base.test.FakeMail;
 import org.apache.mailet.base.test.FakeMailContext;
+import org.apache.mailet.base.test.FakeMailContext.SentMail;
 import org.apache.mailet.base.test.FakeMailetConfig;
 import org.junit.Before;
 import org.junit.Rule;
@@ -98,4 +104,50 @@ public class ResendTest {
         resend.init(mailetConfig);
     }
 
+    @Test
+    public void resendShouldNotModifyOriginalSubject() throws Exception {
+        FakeMailetConfig mailetConfig = new FakeMailetConfig(MAILET_NAME, fakeMailContext);
+        mailetConfig.setProperty("subject", "subj");
+        mailetConfig.setProperty("prefix", "pref");
+        mailetConfig.setProperty("recipients", "user@james.org, user2@james.org");
+        mailetConfig.setProperty("to", "to@james.org");
+        resend.init(mailetConfig);
+
+        MimeMessage mimeMessage = new MimeMessage(Session.getDefaultInstance(new Properties()));
+        mimeMessage.setSubject("My subject");
+        mimeMessage.setText("content");
+        FakeMail mail = FakeMail.builder()
+                .name(MAILET_NAME)
+                .sender(MailAddressFixture.ANY_AT_JAMES)
+                .mimeMessage(mimeMessage)
+                .build();
+
+        resend.service(mail);
+
+        assertThat(mail.getMessage().getSubject()).isEqualTo("My subject");
+    }
+
+    @Test
+    public void resendShouldAddPrefixAndSubjectToSentMail() throws Exception {
+        FakeMailetConfig mailetConfig = new FakeMailetConfig(MAILET_NAME, fakeMailContext);
+        mailetConfig.setProperty("subject", "subj");
+        mailetConfig.setProperty("prefix", "pre");
+        mailetConfig.setProperty("recipients", "user@james.org, user2@james.org");
+        mailetConfig.setProperty("to", "to@james.org");
+        resend.init(mailetConfig);
+
+        MimeMessage mimeMessage = new MimeMessage(Session.getDefaultInstance(new Properties()));
+        mimeMessage.setSubject("My subject");
+        mimeMessage.setText("content");
+        FakeMail mail = FakeMail.builder()
+                .name(MAILET_NAME)
+                .sender(MailAddressFixture.ANY_AT_JAMES)
+                .mimeMessage(mimeMessage)
+                .build();
+
+        resend.service(mail);
+
+        SentMail newMail = fakeMailContext.getSentMails().get(0);
+        assertThat(newMail.getSubject()).contains("pre subj");
+    }
 }


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