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.functional;
8   
9   import org.mule.tck.probe.PollingProber;
10  import org.mule.tck.probe.Probe;
11  import org.mule.tck.probe.Prober;
12  import org.mule.util.FileUtils;
13  import org.mule.util.IOUtils;
14  import org.mule.util.SystemUtils;
15  
16  import com.icegreen.greenmail.util.GreenMailUtil;
17  
18  import java.io.File;
19  import java.io.InputStream;
20  
21  import javax.mail.Message;
22  
23  import org.junit.Test;
24  
25  import static org.junit.Assert.assertEquals;
26  import static org.junit.Assert.assertNotNull;
27  
28  public class ImapFunctionalWithAttachmentsTestCase extends AbstractEmailFunctionalTestCase
29  {
30      private static final String CONFIG_FILE = "email-attachment-save.xml";
31      private File saveDir;
32  
33      public ImapFunctionalWithAttachmentsTestCase()
34      {
35          super(true, "imap", CONFIG_FILE, true);
36          setStartContext(false);
37          
38          // set up email properties for mule config
39          System.setProperty("mail.user", DEFAULT_USER);
40          System.setProperty("mail.password", DEFAULT_PASSWORD);
41          System.setProperty("mail.host", "localhost");
42          
43          saveDir = new File(SystemUtils.JAVA_IO_TMPDIR, getClass().getName());
44          System.setProperty("mail.save.dir", saveDir.getAbsolutePath());
45      }
46  
47      @Override
48      protected void doSetUp() throws Exception
49      {
50          super.doSetUp();
51          
52          // clear out destination directory
53          FileUtils.deleteTree(new File(System.getProperty("mail.save.dir")));
54      }
55          
56      @Test
57      public void testRequest() throws Exception
58      {
59          // we do this since we need to set system mail properties before starting
60          // mule since they are referenced in there
61          muleContext.start();
62          
63          // a test email gets sent by default
64          assertEquals(1, server.getReceivedMessages().length);
65  
66          InputStream inputstream = IOUtils.getResourceAsStream(CONFIG_FILE, getClass(), true, false);
67          assertNotNull(inputstream);
68          byte[] byteArray = IOUtils.toByteArray(inputstream);
69  
70          // send a file attachment email
71          GreenMailUtil.sendAttachmentEmail(DEFAULT_EMAIL, "joe", "email subject with attachments", "",
72              byteArray, "text/xml", CONFIG_FILE, "description", smtpSetup);
73          Message[] messages = server.getReceivedMessages();
74          assertEquals(2, messages.length);
75  
76          assertAttachmentWasSaved();
77      }
78      
79      private void assertAttachmentWasSaved()
80      {
81          Prober prober = new PollingProber(10000, 100);
82          prober.check(new Probe()
83          {
84              public boolean isSatisfied()
85              {
86                  // FIXME DZ: don't know why these are empty, so just compare the saved email
87                  // file to the expected content
88                  // assertEquals(messages[1].ATTACHMENT, messages[1].getDisposition());
89                  // assertEquals("email-attachment-save.xml", messages[1].getFileName());
90  
91                  File savedFile = new File(saveDir, CONFIG_FILE);
92                  return savedFile.exists();
93  
94                  // TODO DZ: compare the source and target files and makes sure they are the
95                  // same
96                  // FIXME DF: when you send an attachment email with text in the body, this
97                  // mule config saves the body in it's own file; how do you just save the file
98                  // attachment?
99              }
100 
101             public String describeFailure()
102             {
103                 return "No attachments were saved";
104             }
105         });
106     }
107 }