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.connectors;
8   
9   import org.mule.tck.junit4.rule.DynamicPort;
10  import org.mule.tck.transformer.NoActionTransformer;
11  import org.mule.transport.AbstractConnectorTestCase;
12  import org.mule.transport.email.GreenMailUtilities;
13  
14  import com.icegreen.greenmail.util.GreenMail;
15  import com.icegreen.greenmail.util.ServerSetup;
16  
17  import javax.mail.Address;
18  import javax.mail.Message;
19  import javax.mail.internet.MimeMessage;
20  
21  import edu.emory.mathcs.backport.java.util.concurrent.atomic.AtomicInteger;
22  import org.junit.Rule;
23  
24  import static org.junit.Assert.assertEquals;
25  import static org.junit.Assert.assertNotNull;
26  import static org.junit.Assert.assertTrue;
27  
28  /**
29   * Start a (greenmail) mail server with a known message, for use in subclasses.
30   */
31  public abstract class AbstractMailConnectorFunctionalTestCase extends AbstractConnectorTestCase
32  {
33  
34      public static final String LOCALHOST = "127.0.0.1";
35      public static final String USER = "bob";
36      public static final String PROVIDER = "example.com";
37      public static final String EMAIL = USER + "@" + PROVIDER;
38      public static final String PASSWORD = "secret";
39      public static final String MESSAGE = "Test Email Message";
40  
41      public static final int START_ATTEMPTS = 3;
42      public static final int TEST_ATTEMPTS = 5;
43      public static final long STARTUP_PERIOD_MS = 1000;
44  
45      // for constructor
46      public static final boolean SEND_INITIAL_EMAIL = true;
47      public static final boolean NO_INITIAL_EMAIL = false;
48  
49      private static final AtomicInteger nameCount = new AtomicInteger(0);
50  
51      private MimeMessage message;
52      private GreenMail servers;
53      private final boolean initialEmail;
54      private String protocol;
55      private int port;
56  
57      @Rule
58      public DynamicPort dynamicPort = new DynamicPort("port1");
59  
60      protected AbstractMailConnectorFunctionalTestCase(boolean initialEmail, String protocol)
61      {
62          super();
63          this.initialEmail = initialEmail;
64          this.protocol = protocol;
65      }
66  
67      @Override
68      protected synchronized void doSetUp() throws Exception
69      {
70          super.doSetUp();
71          //TODO(pablo.kraan): looks like port is redundant. Remove it
72          this.port = dynamicPort.getNumber();
73          startServers();
74          muleContext.getRegistry().registerObject("noActionTransformer", new NoActionTransformer());
75      }
76  
77      @Override
78      protected synchronized void doTearDown() throws Exception
79      {
80          stopServers();
81          super.doTearDown();
82      }
83  
84      private synchronized void storeEmail() throws Exception
85      {
86          GreenMailUtilities.storeEmail(servers.getManagers().getUserManager(), EMAIL, USER,
87                                        PASSWORD, (MimeMessage) getValidMessage());
88          assertEquals(1, servers.getReceivedMessages().length);
89      }
90  
91      private synchronized void startServers() throws Exception
92      {
93          servers = new GreenMail(getSetups());
94          GreenMailUtilities.robustStartup(servers, LOCALHOST, port, START_ATTEMPTS, TEST_ATTEMPTS, STARTUP_PERIOD_MS);
95          if (initialEmail)
96          {
97              storeEmail();
98          }
99      }
100 
101     private ServerSetup[] getSetups()
102     {
103         return new ServerSetup[] {new ServerSetup(port, null, protocol)};
104     }
105 
106     private synchronized void stopServers() throws Exception
107     {
108         if (null != servers)
109         {
110             try
111             {
112                 servers.stop();
113             }
114             catch (RuntimeException e)
115             {
116                 e.printStackTrace();
117             }
118         }
119     }
120 
121     protected synchronized GreenMail getServers()
122     {
123         return servers;
124     }
125 
126     @Override
127     public Object getValidMessage() throws Exception
128     {
129         if (null == message)
130         {
131             message = GreenMailUtilities.toMessage(MESSAGE, EMAIL, null);
132         }
133         return message;
134     }
135 
136     @Override
137     public String getTestEndpointURI()
138     {
139         String uri = protocol + "://" + USER + ":" + PASSWORD + "@" + LOCALHOST + ":" + port + "?connector="
140                      + connectorName;
141         if (!transformInboundMessage())
142         {
143             uri = uri + "&transformers=noActionTransformer";
144         }
145         return uri;
146     }
147 
148     protected boolean transformInboundMessage()
149     {
150         return false;
151     }
152 
153     protected void assertMessageOk(Object mailMessage) throws Exception
154     {
155         assertTrue("Did not receive a MimeMessage", mailMessage instanceof MimeMessage);
156 
157         MimeMessage received = (MimeMessage) mailMessage;
158 
159         // for some reason, something is adding a newline at the end of messages
160         // so we need to strip that out for comparison
161         assertTrue("Did not receive a message with String contents",
162                    received.getContent() instanceof String);
163 
164         String receivedText = ((String) received.getContent()).trim();
165         assertEquals(MESSAGE, receivedText);
166 
167         Address[] recipients = received.getRecipients(Message.RecipientType.TO);
168         assertNotNull(recipients);
169         assertEquals("recipients", 1, recipients.length);
170         assertEquals("recipient", EMAIL, recipients[0].toString());
171     }
172 
173     protected String uniqueName(String root)
174     {
175         return root + nameCount.incrementAndGet();
176     }
177 
178 }