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.api.MuleMessage;
10  import org.mule.module.client.MuleClient;
11  import org.mule.tck.junit4.FunctionalTestCase;
12  import org.mule.tck.junit4.rule.DynamicPort;
13  import org.mule.transport.email.GreenMailUtilities;
14  import org.mule.transport.email.ImapConnector;
15  
16  import com.icegreen.greenmail.util.GreenMail;
17  import com.icegreen.greenmail.util.ServerSetup;
18  
19  import org.junit.Rule;
20  import org.junit.Test;
21  
22  import static org.junit.Assert.assertEquals;
23  import static org.junit.Assert.assertNotNull;
24  
25  public class ImapMessageRequesterTestCase extends FunctionalTestCase
26  {
27      private static final String EMAIL = "bob@example.com";
28      private static final String MESSAGE = "Test email message";
29      private static final String PASSWORD = "password";
30      private static int PORT = -1;
31      private static final String USER = "bob";
32  
33      private GreenMail server;
34  
35      @Rule
36      public DynamicPort dynamicPort = new DynamicPort("port1");
37  
38  
39      @Override
40      protected String getConfigResources()
41      {
42          return "imap-message-requester.xml";
43      }
44  
45      @Override
46      protected void doSetUp() throws Exception
47      {
48          super.doSetUp();
49          PORT = dynamicPort.getNumber();
50          startGreenmailServer();
51      }
52  
53      private void startGreenmailServer() throws Exception
54      {
55          ServerSetup setup = new ServerSetup(PORT, "localhost", ImapConnector.IMAP);
56          server = new GreenMail(setup);
57          server.start();
58          GreenMailUtilities.storeEmail(server.getManagers().getUserManager(), EMAIL, USER, PASSWORD,
59              GreenMailUtilities.toMessage(MESSAGE, EMAIL, null));
60      }
61  
62      @Override
63      protected void doTearDown() throws Exception
64      {
65          server.stop();
66          super.doTearDown();
67      }
68  
69      @Test
70      public void testMessageRequester() throws Exception
71      {
72          String imapUri = String.format("imap://%1s:%2s@localhost:%3d/INBOX", USER, PASSWORD, PORT);
73          
74          MuleClient client = new MuleClient(muleContext);
75          MuleMessage message = client.request(imapUri, RECEIVE_TIMEOUT);
76          
77          assertNotNull(message);
78          assertEquals(MESSAGE, message.getPayload());
79      }
80  
81  }
82  
83