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/06/26 02:43:44 UTC

[2/2] james-project git commit: JAMES-2279 Add WithPriority mailet

JAMES-2279 Add WithPriority mailet


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

Branch: refs/heads/master
Commit: 5a4c0ded9f68565afa1c357c1a680389bc75a231
Parents: e21b78b
Author: clitetailor <du...@gmail.com>
Authored: Wed Jun 20 19:41:20 2018 +0700
Committer: benwa <bt...@linagora.com>
Committed: Tue Jun 26 09:42:48 2018 +0700

----------------------------------------------------------------------
 .../james/transport/mailets/WithPriority.java   |  63 +++++++++
 .../transport/mailets/WithPriorityTest.java     | 133 +++++++++++++++++++
 2 files changed, 196 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/james-project/blob/5a4c0ded/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/WithPriority.java
----------------------------------------------------------------------
diff --git a/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/WithPriority.java b/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/WithPriority.java
new file mode 100644
index 0000000..5e5218c
--- /dev/null
+++ b/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/WithPriority.java
@@ -0,0 +1,63 @@
+/****************************************************************
+ * 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 java.util.Optional;
+
+import javax.mail.MessagingException;
+
+import org.apache.james.queue.api.MailPrioritySupport;
+import org.apache.mailet.Mail;
+import org.apache.mailet.base.GenericMailet;
+
+/**
+ * This mailet sets the priority of the incoming mail.
+ *
+ * Example configuration:
+ *
+ * <mailet match="All" class="WithPriority">
+ *     <priority>7</priority>
+ * </mailet>
+ */
+public class WithPriority extends GenericMailet {
+
+    private int priority;
+
+    @Override
+    public String getMailetInfo() {
+        return "With Priority Mailet";
+    }
+
+    @Override
+    public void init() throws MessagingException {
+        priority = Optional.ofNullable(getInitParameter("priority", null))
+                .map(Integer::valueOf)
+                .orElseThrow(() -> new IllegalArgumentException("'priority' init parameter is compulsory"));
+
+        if (priority < 0 || priority > 9) {
+            throw new IllegalArgumentException("Invalid priority: Priority should be from 0 to 9");
+        }
+    }
+
+    @Override
+    public void service(Mail mail) throws MessagingException {
+        mail.setAttribute(MailPrioritySupport.MAIL_PRIORITY, priority);
+    }
+}

http://git-wip-us.apache.org/repos/asf/james-project/blob/5a4c0ded/server/mailet/mailets/src/test/java/org/apache/james/transport/mailets/WithPriorityTest.java
----------------------------------------------------------------------
diff --git a/server/mailet/mailets/src/test/java/org/apache/james/transport/mailets/WithPriorityTest.java b/server/mailet/mailets/src/test/java/org/apache/james/transport/mailets/WithPriorityTest.java
new file mode 100644
index 0000000..620964a
--- /dev/null
+++ b/server/mailet/mailets/src/test/java/org/apache/james/transport/mailets/WithPriorityTest.java
@@ -0,0 +1,133 @@
+/****************************************************************
+ * 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 org.apache.james.queue.api.MailPrioritySupport;
+import org.apache.mailet.Mail;
+import org.apache.mailet.MailetConfig;
+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;
+import org.junit.Test;
+
+import static org.assertj.core.api.Assertions.*;
+
+public class WithPriorityTest {
+
+    private WithPriority mailet;
+
+    @Before
+    public void setup() throws Exception {
+        mailet = new WithPriority();
+    }
+
+    @Test
+    public void getMailetInfoShouldReturnExpectedContent() {
+        String expected = "With Priority Mailet";
+
+        String actual = mailet.getMailetInfo();
+
+        assertThat(actual).isEqualTo(expected);
+    }
+
+    @Test
+    public void initShouldNotThrowWhenValidPriority() {
+        MailetConfig mockedMailetConfig = FakeMailetConfig.builder()
+            .mailetContext(FakeMailContext.defaultContext())
+            .setProperty("priority", "7")
+            .build();
+
+        assertThatCode(() -> mailet.init(mockedMailetConfig))
+            .doesNotThrowAnyException();
+    }
+
+    @Test
+    public void initShouldThrowWhenInvalidPriority() {
+        MailetConfig mockedMailetConfig = FakeMailetConfig.builder()
+            .mailetContext(FakeMailContext.defaultContext())
+            .setProperty("priority", "-1")
+            .build();
+
+        assertThatThrownBy(() -> mailet.init(mockedMailetConfig))
+            .isInstanceOf(IllegalArgumentException.class);
+    }
+
+    @Test
+    public void initShouldThrowWhenPriorityIsNotANumber() {
+        MailetConfig mockedMailetConfig = FakeMailetConfig.builder()
+                .mailetContext(FakeMailContext.defaultContext())
+                .setProperty("priority", "k")
+                .build();
+
+        assertThatThrownBy(() -> mailet.init(mockedMailetConfig))
+                .isInstanceOf(NumberFormatException.class);
+    }
+
+    @Test
+    public void initShouldThrowWhenPriorityIsEmpty() {
+        MailetConfig mockedMailetConfig = FakeMailetConfig.builder()
+                .mailetContext(FakeMailContext.defaultContext())
+                .setProperty("priority", "")
+                .build();
+
+        assertThatThrownBy(() -> mailet.init(mockedMailetConfig))
+                .isInstanceOf(NumberFormatException.class);
+    }
+
+    @Test
+    public void initShouldThrowWhenNoPriority() {
+        MailetConfig mockedMailetConfig = FakeMailetConfig.builder()
+                .mailetContext(FakeMailContext.defaultContext())
+                .build();
+
+        assertThatThrownBy(() -> mailet.init(mockedMailetConfig))
+                .isInstanceOf(IllegalArgumentException.class);
+    }
+
+    @Test
+    public void serviceShouldSetMailPriorityWhenNone() throws Exception {
+        MailetConfig mockedMailetConfig = FakeMailetConfig.builder()
+            .mailetContext(FakeMailContext.defaultContext())
+            .setProperty("priority", "7")
+            .build();
+
+        mailet.init(mockedMailetConfig);
+        Mail mail = FakeMail.builder().build();
+        mailet.service(mail);
+
+        assertThat(mail.getAttribute(MailPrioritySupport.MAIL_PRIORITY)).isEqualTo(7);
+    }
+
+    @Test
+    public void serviceShouldSetMailPriorityWhenPriorityExists() throws Exception {
+        MailetConfig mockedMailetConfig = FakeMailetConfig.builder()
+            .mailetContext(FakeMailContext.defaultContext())
+            .setProperty("priority", "7")
+            .build();
+
+        mailet.init(mockedMailetConfig);
+        Mail mail = FakeMail.builder()
+                .attribute(MailPrioritySupport.MAIL_PRIORITY, 5)
+                .build();
+        mailet.service(mail);
+
+        assertThat(mail.getAttribute(MailPrioritySupport.MAIL_PRIORITY)).isEqualTo(7);
+    }
+}


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