View Javadoc

1   /*
2    * $Id: EmailRoundTripTestCase.java 22471 2011-07-20 10:36:17Z claude.mamo $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
5    *
6    * The software in this package is published under the terms of the CPAL v1.0
7    * license, a copy of which has been included with this distribution in the
8    * LICENSE.txt file.
9    */
10  
11  package org.mule.transport.email;
12  
13  import static org.junit.Assert.assertEquals;
14  import static org.junit.Assert.assertNotNull;
15  import static org.junit.Assert.assertTrue;
16  
17  import org.mule.api.MuleContext;
18  import org.mule.api.MuleException;
19  import org.mule.api.MuleMessage;
20  import org.mule.module.client.MuleClient;
21  import org.mule.tck.AbstractServiceAndFlowTestCase;
22  import org.mule.tck.AbstractServiceAndFlowTestCase.ConfigVariant;
23  import org.mule.tck.junit4.rule.DynamicPort;
24  import org.mule.transport.email.functional.AbstractEmailFunctionalTestCase;
25  
26  import java.util.ArrayList;
27  import java.util.Arrays;
28  import java.util.Collection;
29  import java.util.List;
30  
31  import javax.mail.internet.MimeMessage;
32  
33  import org.junit.Rule;
34  import org.junit.Test;
35  import org.junit.runners.Parameterized.Parameters;
36  
37  /**
38   * This demonstrates "round trip" processing of email - an email is pulled from a POP
39   * server and then sent to an SMTP server.  While within Mule the message is serialized
40   * as RFC822 encoded bytes (this would let the message be transmitted over JMS etc).
41   *
42   * <p>The email servers for the test are managed by the greenMailSupport instance.
43   * The Mule services (defined in email-round-trip-test.xml) are started by the test framework.
44   * So all we need to do here is test that the message is handled correctly.</p>
45   */
46  public class EmailRoundTripTestCase extends AbstractServiceAndFlowTestCase
47  {
48  
49      public EmailRoundTripTestCase(ConfigVariant variant, String configResources)
50      {
51          super(variant, configResources);
52      }
53  
54      private AbstractGreenMailSupport greenMailSupport = null;
55  
56      @Rule
57      public DynamicPort dynamicPort1 = new DynamicPort("port1");
58  
59      @Rule
60      public DynamicPort dynamicPort2 = new DynamicPort("port2");
61  
62      @Rule
63      public DynamicPort dynamicPort3 = new DynamicPort("port3");
64  
65      @Rule
66      public DynamicPort dynamicPort4 = new DynamicPort("port4");
67  
68      @Rule
69      public DynamicPort dynamicPort5 = new DynamicPort("port5");
70  
71      @Rule
72      public DynamicPort dynamicPort6 = new DynamicPort("port6");
73      
74      @Parameters
75      public static Collection<Object[]> parameters()
76      {
77          return Arrays.asList(new Object[][]{
78              {ConfigVariant.SERVICE, "email-round-trip-test-service.xml"},
79              {ConfigVariant.FLOW, "email-round-trip-test-flow.xml"}
80          });
81      }      
82  
83      @Override
84      protected MuleContext createMuleContext() throws Exception
85      {
86          startServer();
87          return super.createMuleContext();
88      }
89  
90      /**
91       * Start the servers when the test starts
92       * @throws Exception
93       */
94      public void startServer() throws Exception
95      {
96          // see AbstractGreenMailSupport for all the ports this test uses and their order
97          //portsForClass = PortUtil.findFreePorts(6);
98          greenMailSupport = new FixedPortGreenMailSupport(dynamicPort2.getNumber());
99  
100         List<Integer> ports = new ArrayList<Integer>(6);
101         ports.add(dynamicPort1.getNumber());
102         ports.add(dynamicPort2.getNumber());
103         ports.add(dynamicPort3.getNumber());
104         ports.add(dynamicPort4.getNumber());
105         ports.add(dynamicPort5.getNumber());
106         ports.add(dynamicPort6.getNumber());
107 
108         greenMailSupport.startServers(ports);
109         greenMailSupport.createBobAndStoreEmail(greenMailSupport.getValidMessage(AbstractGreenMailSupport.ALICE_EMAIL));
110     }
111 
112     /**
113      * Stop the servers when the test ends
114      * @throws Exception
115      */
116     protected void doTearDown() throws Exception
117     {
118         greenMailSupport.stopServers();
119         super.doTearDown();
120     }
121 
122     @Test
123     public void testRoundTrip() throws MuleException, InterruptedException
124     {
125         // first, check that the conversion happened - we should have a copy of
126         // the message as rfc822 encoded bytes on vm://rfc822
127         MuleClient client = new MuleClient(muleContext);
128         MuleMessage message = client.request("vm://rfc822", RECEIVE_TIMEOUT);
129         assertTrue(message.getPayload() instanceof byte[]);
130 
131         // next, check that the email is received in the server
132         greenMailSupport.getServers().waitForIncomingEmail(AbstractEmailFunctionalTestCase.DELIVERY_DELAY_MS, 1);
133         MimeMessage[] messages = greenMailSupport.getServers().getReceivedMessages();
134         assertNotNull("did not receive any messages", messages);
135         assertEquals("did not receive 1 mail", 1, messages.length);
136     }
137 
138 }