View Javadoc
1   /*
2    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
3    * The software in this package is published under the terms of the CPAL v1.0
4    * license, a copy of which has been included with this distribution in the
5    * LICENSE.txt file.
6    */
7   package org.mule.transport.email;
8   
9   import org.mule.api.MuleMessage;
10  import org.mule.api.transport.MuleMessageFactory;
11  import org.mule.transport.AbstractMuleMessageFactoryTestCase;
12  
13  import javax.activation.DataHandler;
14  import javax.activation.DataSource;
15  import javax.mail.BodyPart;
16  import javax.mail.Message;
17  import javax.mail.Message.RecipientType;
18  import javax.mail.MessagingException;
19  import javax.mail.Multipart;
20  import javax.mail.Session;
21  import javax.mail.internet.MimeBodyPart;
22  import javax.mail.internet.MimeMessage;
23  import javax.mail.internet.MimeMultipart;
24  import javax.mail.util.ByteArrayDataSource;
25  
26  import org.junit.Test;
27  
28  import static org.junit.Assert.assertEquals;
29  
30  public class MailMuleMessageFactoryTestCase extends AbstractMuleMessageFactoryTestCase
31  {
32      @Override
33      protected MuleMessageFactory doCreateMuleMessageFactory()
34      {
35          return new MailMuleMessageFactory(muleContext);
36      }
37  
38      @Override
39      protected MimeMessage getValidTransportMessage() throws Exception
40      {
41          MimeMessage message = new MimeMessage((Session) null);
42          message.setContent(TEST_MESSAGE, "text/plain; charset=ISO-8859-1");
43          return message;
44      }
45  
46      @Override
47      protected Object getUnsupportedTransportMessage()
48      {
49          return "this is not a valid transport message for MailMuleMessageFactory";
50      }
51  
52      @Test
53      public void testAttachments() throws Exception
54      {
55          Message payload = createMimeMessageWithAttachment();
56  
57          MuleMessageFactory factory = createMuleMessageFactory();
58          MuleMessage muleMessage = factory.create(payload, encoding);
59          assertEquals(2, muleMessage.getInboundAttachmentNames().size());
60      }
61  
62      @Test
63      public void testAddRecipientProperties() throws Exception
64      {
65          String to = "Information <info@domain.com>";
66          String cc = "\"info@\" <domain.com info@domain.com>";
67          String bcc = "'invalid@domain.com', info <info@domain.com>";
68  
69          MimeMessage payload = getValidTransportMessage();
70          payload.setHeader(RecipientType.TO.toString(), to);
71          payload.setHeader(RecipientType.CC.toString(), cc);
72          payload.setHeader(RecipientType.BCC.toString(), bcc);
73  
74          MuleMessageFactory factory = createMuleMessageFactory();
75          MuleMessage muleMessage = factory.create(payload, encoding);
76  
77          assertEquals(to, muleMessage.getOutboundProperty(MailProperties.INBOUND_TO_ADDRESSES_PROPERTY));
78          assertEquals(cc, muleMessage.getOutboundProperty(MailProperties.INBOUND_CC_ADDRESSES_PROPERTY));
79          assertEquals(bcc, muleMessage.getOutboundProperty(MailProperties.INBOUND_BCC_ADDRESSES_PROPERTY));
80      }
81  
82      @Test
83      public void testAttachmentsWithSameName() throws Exception
84      {
85          Message payload = createMimeMessageWithSameAttachmentNames();
86  
87          MuleMessageFactory factory = createMuleMessageFactory();
88          MuleMessage muleMessage = factory.create(payload, encoding);
89          assertEquals(3, muleMessage.getInboundAttachmentNames().size());
90      }
91  
92      private Message createMimeMessageWithAttachment() throws Exception
93      {
94          MimeBodyPart mainBody = new MimeBodyPart();
95          mainBody.setText("This is the main message text");
96  
97          MimeBodyPart attachment = createBodyPart(TEST_MESSAGE, "message.txt");
98  
99          Multipart multipart = createMultipart(mainBody, attachment);
100 
101         MimeMessage message = getValidTransportMessage();
102         message.setContent(multipart);
103         return message;
104     }
105 
106     private Message createMimeMessageWithSameAttachmentNames() throws Exception
107     {
108         MimeBodyPart mainBody = new MimeBodyPart();
109         mainBody.setText("This is the main message text");
110 
111         MimeBodyPart firstAttachment = createBodyPart("The first attachment content", "message.txt");
112         MimeBodyPart secondAttachment = createBodyPart("The second attachment content", "message.txt");
113 
114         Multipart multipart = createMultipart(mainBody, firstAttachment, secondAttachment);
115 
116         MimeMessage message = getValidTransportMessage();
117         message.setContent(multipart);
118         return message;
119     }
120 
121     private MimeBodyPart createBodyPart(String content, String fileName) throws MessagingException
122     {
123         MimeBodyPart attachment = new MimeBodyPart();
124         DataSource source = new ByteArrayDataSource(content.getBytes(), "text/plain");
125         attachment.setDataHandler(new DataHandler(source));
126         attachment.setFileName(fileName);
127         return attachment;
128     }
129 
130     private Multipart createMultipart(BodyPart... parts) throws MessagingException
131     {
132         Multipart multipart = new MimeMultipart();
133 
134         for (BodyPart part : parts)
135         {
136             multipart.addBodyPart(part);
137         }
138 
139         return multipart;
140     }
141 }