james-server-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rouaz...@apache.org
Subject [02/10] james-project git commit: MAILET-117 cover SetMimeHeader with tests
Date Mon, 05 Sep 2016 13:42:01 GMT
MAILET-117 cover SetMimeHeader with tests


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

Branch: refs/heads/master
Commit: 9ff634d306f264e9fa5a7faeaa1aee1918e08bcb
Parents: 1c89c7c
Author: Matthieu Baechler <matthieu.baechler@linagora.com>
Authored: Mon Aug 29 17:58:12 2016 +0200
Committer: Matthieu Baechler <matthieu.baechler@linagora.com>
Committed: Mon Sep 5 14:17:50 2016 +0200

----------------------------------------------------------------------
 .../transport/mailets/SetMimeHeaderTest.java    | 132 +++++++++++++------
 1 file changed, 89 insertions(+), 43 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/james-project/blob/9ff634d3/mailet/standard/src/test/java/org/apache/james/transport/mailets/SetMimeHeaderTest.java
----------------------------------------------------------------------
diff --git a/mailet/standard/src/test/java/org/apache/james/transport/mailets/SetMimeHeaderTest.java
b/mailet/standard/src/test/java/org/apache/james/transport/mailets/SetMimeHeaderTest.java
index 8475334..d1d9921 100644
--- a/mailet/standard/src/test/java/org/apache/james/transport/mailets/SetMimeHeaderTest.java
+++ b/mailet/standard/src/test/java/org/apache/james/transport/mailets/SetMimeHeaderTest.java
@@ -20,72 +20,118 @@
 
 package org.apache.james.transport.mailets;
 
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
 import javax.mail.MessagingException;
 import javax.mail.internet.MimeMessage;
 
+import org.apache.mailet.Mail;
 import org.apache.mailet.Mailet;
-import org.apache.mailet.base.test.FakeMail;
 import org.apache.mailet.base.test.FakeMailContext;
 import org.apache.mailet.base.test.FakeMailetConfig;
 import org.apache.mailet.base.test.MailUtil;
-import org.junit.Assert;
 import org.junit.Before;
+import org.junit.Ignore;
+import org.junit.Rule;
 import org.junit.Test;
+import org.junit.rules.ExpectedException;
 
 public class SetMimeHeaderTest {
 
+    @Rule public ExpectedException expectedException = ExpectedException.none();
+    
     private Mailet mailet;
 
-    private final String HEADER_NAME = "JUNIT";
-
-    private final String HEADER_VALUE = "test-value";
-
-    private String headerName = "defaultHeaderName";
-
-    private String headerValue = "defaultHeaderValue";
-
-    private void setHeaderName(String headerName) {
-        this.headerName = headerName;
-    }
-
-    private void setHeaderValue(String headerValue) {
-        this.headerValue = headerValue;
-    }
-
     @Before
     public void setUp() throws Exception {
         mailet = new SetMimeHeader();
-        FakeMailetConfig mci = new FakeMailetConfig("Test",
-                FakeMailContext.defaultContext());
-        mci.setProperty("name", HEADER_NAME);
-        mci.setProperty("value", HEADER_VALUE);
-
-        mailet.init(mci);
     }
 
-    // test if the Header was add
     @Test
-    public void testHeaderIsPresent() throws MessagingException {
-        MimeMessage mockedMimeMessage = MailUtil.createMimeMessage(headerName, headerValue);
-        FakeMail mockedMail = MailUtil.createMockMail2Recipients(mockedMimeMessage);
-
-        mailet.service(mockedMail);
-
-        Assert.assertEquals(HEADER_VALUE, mockedMail.getMessage().getHeader(HEADER_NAME)[0]);
-
+    public void shouldAddHeaderToMime() throws MessagingException {
+        FakeMailetConfig mailetConfig = new FakeMailetConfig("Test", FakeMailContext.defaultContext());
+        mailetConfig.setProperty("name", "header-name");
+        mailetConfig.setProperty("value", "test-value");
+        mailet.init(mailetConfig);
+        
+        MimeMessage mimeMessage = MailUtil.createMimeMessage();
+        Mail mail = MailUtil.createMockMail2Recipients(mimeMessage);
+
+        mailet.service(mail);
+
+        assertThat(mail.getMessage().getHeader("header-name")).containsExactly("test-value");
     }
 
-    // test if the Header was replaced
+    @Ignore("need multivaluated headers in FakeMail")
     @Test
-    public void testHeaderIsReplaced() throws MessagingException {
-        setHeaderName(HEADER_NAME);
-        setHeaderValue(headerValue);
-
-        MimeMessage mockedMimeMessage = MailUtil.createMimeMessage(headerName, headerValue);
-        FakeMail mockedMail = MailUtil.createMockMail2Recipients(mockedMimeMessage);
-
-        mailet.service(mockedMail);
+    public void shouldAddHeaderWhenAlreadyPresent() throws MessagingException {
+        FakeMailetConfig mailetConfig = new FakeMailetConfig("Test", FakeMailContext.defaultContext());
+        mailetConfig.setProperty("name", "header-name");
+        mailetConfig.setProperty("value", "test-value");
+        mailet.init(mailetConfig);
+        
+        MimeMessage mimeMessage = MailUtil.createMimeMessage();
+        mimeMessage.addHeader("header-name", "first-value");
+        Mail mail = MailUtil.createMockMail2Recipients(mimeMessage);
+
+        mailet.service(mail);
+
+        assertThat(mail.getMessage().getHeader("header-name")).containsOnly("test-value",
"first-value");
+    }
 
-        Assert.assertEquals(HEADER_VALUE, mockedMail.getMessage().getHeader(HEADER_NAME)[0]);
+    @Test
+    public void shouldNotThrowOnMessagingException() throws MessagingException {
+        FakeMailetConfig mailetConfig = new FakeMailetConfig("Test", FakeMailContext.defaultContext());
+        mailetConfig.setProperty("name", "header-name");
+        mailetConfig.setProperty("value", "test-value");
+        mailet.init(mailetConfig);
+        
+        Mail mail = mock(Mail.class);
+        when(mail.getMessage()).thenThrow(new MessagingException());
+        mailet.service(mail);
+    }
+    
+    @Test
+    public void shouldThrowWhenNoConfiguration() throws MessagingException {
+        FakeMailetConfig mailetConfig = new FakeMailetConfig("Test", FakeMailContext.defaultContext());
+        expectedException.expect(MessagingException.class);
+        mailet.init(mailetConfig);
+    }
+    
+    @Test
+    public void shouldThrowWhenNoValue() throws MessagingException {
+        FakeMailetConfig mailetConfig = new FakeMailetConfig("Test", FakeMailContext.defaultContext());
+        mailetConfig.setProperty("name", "correct");
+        expectedException.expect(MessagingException.class);
+        mailet.init(mailetConfig);
+    }
+    
+    @Test
+    public void shouldThrowWhenNoHeader() throws MessagingException {
+        FakeMailetConfig mailetConfig = new FakeMailetConfig("Test", FakeMailContext.defaultContext());
+        mailetConfig.setProperty("value", "correct");
+        expectedException.expect(MessagingException.class);
+        mailet.init(mailetConfig);
+    }
+    
+    @Test
+    public void shouldThrowWhenEmptyValue() throws MessagingException {
+        FakeMailetConfig mailetConfig = new FakeMailetConfig("Test", FakeMailContext.defaultContext());
+        mailetConfig.setProperty("value", "");
+        mailetConfig.setProperty("name", "correct");
+        expectedException.expect(MessagingException.class);
+        mailet.init(mailetConfig);
+    }
+    
+    @Test
+    public void shouldThrowWhenEmptyHeader() throws MessagingException {
+        FakeMailetConfig mailetConfig = new FakeMailetConfig("Test", FakeMailContext.defaultContext());
+        mailetConfig.setProperty("name", "");
+        mailetConfig.setProperty("value", "correct");
+        expectedException.expect(MessagingException.class);
+        mailet.init(mailetConfig);
     }
 }


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