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.MuleContext;
10  import org.mule.api.MuleException;
11  import org.mule.api.MuleMessage;
12  import org.mule.module.client.MuleClient;
13  import org.mule.tck.junit4.FunctionalTestCase;
14  import org.mule.tck.junit4.rule.DynamicPort;
15  import org.mule.transport.email.functional.AbstractEmailFunctionalTestCase;
16  
17  import java.util.ArrayList;
18  import java.util.List;
19  
20  import javax.mail.internet.MimeMessage;
21  
22  import org.junit.Rule;
23  import org.junit.Test;
24  
25  import static org.junit.Assert.assertEquals;
26  import static org.junit.Assert.assertNotNull;
27  import static org.junit.Assert.assertTrue;
28  
29  /**
30   * This demonstrates "round trip" processing of email - an email is pulled from a POP
31   * server and then sent to an SMTP server.  While within Mule the message is serialized
32   * as RFC822 encoded bytes (this would let the message be transmitted over JMS etc).
33   *
34   * <p>The email servers for the test are managed by the greenMailSupport instance.
35   * The Mule services (defined in email-round-trip-test.xml) are started by the test framework.
36   * So all we need to do here is test that the message is handled correctly.</p>
37   */
38  public class EmailRoundTripTestCase extends FunctionalTestCase
39  {
40  
41      private AbstractGreenMailSupport greenMailSupport = null;
42  
43      @Rule
44      public DynamicPort dynamicPort1 = new DynamicPort("port1");
45  
46      @Rule
47      public DynamicPort dynamicPort2 = new DynamicPort("port2");
48  
49      @Rule
50      public DynamicPort dynamicPort3 = new DynamicPort("port3");
51  
52      @Rule
53      public DynamicPort dynamicPort4 = new DynamicPort("port4");
54  
55      @Rule
56      public DynamicPort dynamicPort5 = new DynamicPort("port5");
57  
58      @Rule
59      public DynamicPort dynamicPort6 = new DynamicPort("port6");
60  
61      @Override
62      protected String getConfigResources()
63      {
64          return "email-round-trip-test.xml";
65      }
66  
67      @Override
68      protected MuleContext createMuleContext() throws Exception
69      {
70          startServer();
71          return super.createMuleContext();
72      }
73  
74      /**
75       * Start the servers when the test starts
76       * @throws Exception
77       */
78      public void startServer() throws Exception
79      {
80          // see AbstractGreenMailSupport for all the ports this test uses and their order
81          //portsForClass = PortUtil.findFreePorts(6);
82          greenMailSupport = new FixedPortGreenMailSupport(dynamicPort2.getNumber());
83  
84          List<Integer> ports = new ArrayList<Integer>(6);
85          ports.add(dynamicPort1.getNumber());
86          ports.add(dynamicPort2.getNumber());
87          ports.add(dynamicPort3.getNumber());
88          ports.add(dynamicPort4.getNumber());
89          ports.add(dynamicPort5.getNumber());
90          ports.add(dynamicPort6.getNumber());
91  
92          greenMailSupport.startServers(ports);
93          greenMailSupport.createBobAndStoreEmail(greenMailSupport.getValidMessage(AbstractGreenMailSupport.ALICE_EMAIL));
94      }
95  
96      /**
97       * Stop the servers when the test ends
98       * @throws Exception
99       */
100     protected void doTearDown() throws Exception
101     {
102         greenMailSupport.stopServers();
103         super.doTearDown();
104     }
105 
106     @Test
107     public void testRoundTrip() throws MuleException, InterruptedException
108     {
109         // first, check that the conversion happened - we should have a copy of
110         // the message as rfc822 encoded bytes on vm://rfc822
111         MuleClient client = new MuleClient(muleContext);
112         MuleMessage message = client.request("vm://rfc822", RECEIVE_TIMEOUT);
113         assertTrue(message.getPayload() instanceof byte[]);
114 
115         // next, check that the email is received in the server
116         greenMailSupport.getServers().waitForIncomingEmail(AbstractEmailFunctionalTestCase.DELIVERY_DELAY_MS, 1);
117         MimeMessage[] messages = greenMailSupport.getServers().getReceivedMessages();
118         assertNotNull("did not receive any messages", messages);
119         assertEquals("did not receive 1 mail", 1, messages.length);
120     }
121 
122 }