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 ro...@apache.org on 2016/09/05 13:10:09 UTC

[1/2] james-project git commit: MAILET-114 Refactor ToProcessor

Repository: james-project
Updated Branches:
  refs/heads/master 40644067f -> 526f8b5e1


MAILET-114 Refactor ToProcessor


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

Branch: refs/heads/master
Commit: 813ba84c36f054c82cc1f728b820e3be62dbe2e5
Parents: 4a25501
Author: Antoine Duprat <ad...@linagora.com>
Authored: Tue Aug 30 15:34:00 2016 +0200
Committer: Antoine Duprat <ad...@linagora.com>
Committed: Mon Sep 5 13:50:02 2016 +0200

----------------------------------------------------------------------
 .../james/transport/mailets/ToProcessor.java    |  73 ++++------
 .../transport/mailets/ToProcessorTest.java      | 136 +++++++++++--------
 2 files changed, 109 insertions(+), 100 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/james-project/blob/813ba84c/mailet/standard/src/main/java/org/apache/james/transport/mailets/ToProcessor.java
----------------------------------------------------------------------
diff --git a/mailet/standard/src/main/java/org/apache/james/transport/mailets/ToProcessor.java b/mailet/standard/src/main/java/org/apache/james/transport/mailets/ToProcessor.java
index 780e9b7..5a54925 100644
--- a/mailet/standard/src/main/java/org/apache/james/transport/mailets/ToProcessor.java
+++ b/mailet/standard/src/main/java/org/apache/james/transport/mailets/ToProcessor.java
@@ -21,11 +21,13 @@
 
 package org.apache.james.transport.mailets;
 
-import org.apache.mailet.base.GenericMailet;
+import javax.mail.MessagingException;
+
 import org.apache.mailet.Mail;
 import org.apache.mailet.MailetException;
+import org.apache.mailet.base.GenericMailet;
 
-import javax.mail.MessagingException;
+import com.google.common.base.Optional;
 
 /**
  * <p>This mailet redirects the mail to the named processor</p>
@@ -42,62 +44,45 @@ import javax.mail.MessagingException;
  */
 public class ToProcessor extends GenericMailet {
 
-    /**
-     * Controls certain log messages
-     */
-    private boolean isDebug = false;
-
-    /**
-     * The name of the processor to which this mailet forwards mail
-     */
-    String processor;
+    private boolean debug;
+    private String processor;
+    private Optional<String> noticeText;
 
-    /**
-     * The error message to attach to the forwarded message
-     */
-    String noticeText = null;
-
-    /**
-     * Initialize the mailet
-     *
-     * @throws MailetException if the processor parameter is missing
-     */
+    @Override
     public void init() throws MailetException {
-        isDebug = (getInitParameter("debug") == null) ? false : Boolean.valueOf(getInitParameter("debug"));
+        debug = isDebug();
         processor = getInitParameter("processor");
         if (processor == null) {
             throw new MailetException("processor parameter is required");
         }
-        noticeText = getInitParameter("notice");
+        noticeText = Optional.fromNullable(getInitParameter("notice"));
+    }
+
+    private boolean isDebug() {
+        return getInitParameter("debug", false);
+    }
+
+    @Override
+    public String getMailetInfo() {
+        return "ToProcessor Mailet";
     }
 
-    /**
-     * Deliver a mail to the processor.
-     *
-     * @param mail the mail to process
-     *
-     * @throws MessagingException in all cases
-     */
+    @Override
     public void service(Mail mail) throws MessagingException {
-        if (isDebug) {
+        if (debug) {
             log(String.format("Sending mail %s to %s", mail, processor));
         }
         mail.setState(processor);
-        if (noticeText != null) {
-            if (mail.getErrorMessage() == null) {
-                mail.setErrorMessage(noticeText);
-            } else {
-                mail.setErrorMessage(String.format("%s\r\n%s", mail.getErrorMessage(), noticeText));
-            }
+        if (noticeText.isPresent()) {
+            setNoticeInErrorMessage(mail);
         }
     }
 
-    /**
-     * Return a string describing this mailet.
-     *
-     * @return a string describing this mailet
-     */
-    public String getMailetInfo() {
-        return "ToProcessor Mailet";
+    private void setNoticeInErrorMessage(Mail mail) {
+        if (mail.getErrorMessage() == null) {
+            mail.setErrorMessage(noticeText.get());
+        } else {
+            mail.setErrorMessage(String.format("%s\r\n%s", mail.getErrorMessage(), noticeText.get()));
+        }
     }
 }

http://git-wip-us.apache.org/repos/asf/james-project/blob/813ba84c/mailet/standard/src/test/java/org/apache/james/transport/mailets/ToProcessorTest.java
----------------------------------------------------------------------
diff --git a/mailet/standard/src/test/java/org/apache/james/transport/mailets/ToProcessorTest.java b/mailet/standard/src/test/java/org/apache/james/transport/mailets/ToProcessorTest.java
index de395cb..e380c13 100644
--- a/mailet/standard/src/test/java/org/apache/james/transport/mailets/ToProcessorTest.java
+++ b/mailet/standard/src/test/java/org/apache/james/transport/mailets/ToProcessorTest.java
@@ -20,92 +20,116 @@
 
 package org.apache.james.transport.mailets;
 
-import java.util.Arrays;
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.verify;
 
 import javax.mail.MessagingException;
-import javax.mail.internet.MimeMessage;
-import javax.mail.internet.ParseException;
 
 import org.apache.mailet.Mail;
 import org.apache.mailet.MailAddress;
 import org.apache.mailet.Mailet;
+import org.apache.mailet.MailetException;
 import org.apache.mailet.base.test.FakeMail;
 import org.apache.mailet.base.test.FakeMailContext;
 import org.apache.mailet.base.test.FakeMailetConfig;
-import org.junit.Assert;
+import org.junit.Before;
+import org.junit.Rule;
 import org.junit.Test;
+import org.junit.rules.ExpectedException;
+import org.slf4j.Logger;
 
 public class ToProcessorTest {
 
-    private MimeMessage mockedMimeMessage;
-
-    private Mail mockedMail;
+    @Rule
+    public ExpectedException expectedException = ExpectedException.none();
 
     private Mailet mailet;
+    private FakeMailetConfig mailetConfig;
+    private Logger logger;
 
-    private String processor = null;
-
-    private String notice = null;
-
-    private void setProcessor(String processor) {
-        this.processor = processor;
+    @Before
+    public void setup() {
+        mailet = new ToProcessor();
+        logger = mock(Logger.class);
+        mailetConfig = new FakeMailetConfig("Test", FakeMailContext.builder().logger(logger).build());
     }
 
-    private void setNotice(String notice) {
-        this.notice = notice;
+    @Test
+    public void getMailetInfoShouldReturnValue() {
+        assertThat(mailet.getMailetInfo()).isEqualTo("ToProcessor Mailet");
     }
 
-    private void setupMockedMail(MimeMessage m) throws ParseException {
-        mockedMail = new FakeMail();
-        mockedMail.setMessage(m);
-        mockedMail.setRecipients(Arrays.asList(new MailAddress("test@james.apache.org"),
-                new MailAddress("test2@james.apache.org")));
-
-    }
+    @Test
+    public void initShouldThrowWhenProcessorIsNotGiven() throws MessagingException {
+        mailetConfig.setProperty("notice", "error in message");
+        expectedException.expect(MailetException.class);
 
-    private void setupMailet() throws MessagingException {
-        mailet = new ToProcessor();
-        FakeMailetConfig mci = new FakeMailetConfig("Test",
-                FakeMailContext.defaultContext());
-        if (processor != null) {
-            mci.setProperty("processor", processor);
-        }
-        if (notice != null) {
-            mci.setProperty("notice", notice);
-        }
-        mailet.init(mci);
+        mailet.init(mailetConfig);
     }
 
-    // test if ToProcessor works
     @Test
-    public void testValidToProcessor() throws MessagingException {
-        setProcessor("error");
-        setNotice("error in message");
-        setupMockedMail(mockedMimeMessage);
-        setupMailet();
+    public void serviceShouldSetTheStateOfTheMail() throws MessagingException {
+        String processor = "error";
+        mailetConfig.setProperty("processor", processor);
+        mailet.init(mailetConfig);
 
-        mailet.service(mockedMail);
+        Mail mail = FakeMail.builder()
+                .recipients(new MailAddress("test@james.apache.org"), new MailAddress("test2@james.apache.org"))
+                .build();
+        mailet.service(mail);
 
-        Assert.assertEquals(processor, mockedMail.getState());
-        Assert.assertEquals(notice, mockedMail.getErrorMessage());
+        assertThat(mail.getState()).isEqualTo(processor);
+    }
 
+    @Test
+    public void serviceShouldSetTheErrorMessageOfTheMailWhenNotAlreadySet() throws MessagingException {
+        String processor = "error";
+        mailetConfig.setProperty("processor", processor);
+        String notice = "error in message";
+        mailetConfig.setProperty("notice", notice);
+        mailet.init(mailetConfig);
+
+        Mail mail = FakeMail.builder()
+                .recipients(new MailAddress("test@james.apache.org"), new MailAddress("test2@james.apache.org"))
+                .build();
+        mailet.service(mail);
+
+        assertThat(mail.getErrorMessage()).isEqualTo(notice);
     }
 
-    // test if exception was thrown
     @Test
-    public void testExceptionThrown() throws MessagingException {
-        boolean exceptionThrown = false;
-        setProcessor(null);
-        setNotice("error in message");
-        setupMockedMail(mockedMimeMessage);
-
-        try {
-            setupMailet();
-            mailet.service(mockedMail);
-        } catch (MessagingException m) {
-            exceptionThrown = true;
-        }
-        Assert.assertTrue(exceptionThrown);
+    public void serviceShouldAppendTheErrorMessageOfTheMailWhenSomeErrorMessageOnMail() throws MessagingException {
+        String processor = "error";
+        mailetConfig.setProperty("processor", processor);
+        String notice = "error in message";
+        mailetConfig.setProperty("notice", notice);
+        mailet.init(mailetConfig);
+
+        Mail mail = FakeMail.builder()
+                .recipients(new MailAddress("test@james.apache.org"), new MailAddress("test2@james.apache.org"))
+                .build();
+        String initialErrorMessage = "first";
+        mail.setErrorMessage(initialErrorMessage);
+        mailet.service(mail);
+
+        assertThat(mail.getErrorMessage()).isEqualTo(initialErrorMessage + "\r\n" + notice);
     }
 
+    @Test
+    public void serviceShouldLogWhenDebug() throws MessagingException {
+        mailetConfig.setProperty("processor", "error");
+        mailetConfig.setProperty("notice", "error in message");
+        mailetConfig.setProperty("debug", "true");
+        mailet.init(mailetConfig);
+
+        Mail mail = FakeMail.builder()
+                .recipients(new MailAddress("test@james.apache.org"), new MailAddress("test2@james.apache.org"))
+                .build();
+        String initialErrorMessage = "first";
+        mail.setErrorMessage(initialErrorMessage);
+        mailet.service(mail);
+
+        verify(logger).info("Sending mail " + mail +" to error");
+    }
 }


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


[2/2] james-project git commit: Merge remote-tracking branch 'aduprat/MAILET-114'

Posted by ro...@apache.org.
Merge remote-tracking branch 'aduprat/MAILET-114'


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

Branch: refs/heads/master
Commit: 526f8b5e19741d7f351bb97147f85c6ecebc6a85
Parents: 4064406 813ba84
Author: Raphael Ouazana <ra...@linagora.com>
Authored: Mon Sep 5 15:09:28 2016 +0200
Committer: Raphael Ouazana <ra...@linagora.com>
Committed: Mon Sep 5 15:09:28 2016 +0200

----------------------------------------------------------------------
 .../james/transport/mailets/ToProcessor.java    |  73 ++++------
 .../transport/mailets/ToProcessorTest.java      | 136 +++++++++++--------
 2 files changed, 109 insertions(+), 100 deletions(-)
----------------------------------------------------------------------



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