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.DefaultMuleEvent;
10  import org.mule.DefaultMuleMessage;
11  import org.mule.ResponseOutputStream;
12  import org.mule.api.MuleException;
13  import org.mule.api.MuleMessage;
14  import org.mule.api.MuleSession;
15  import org.mule.api.endpoint.EndpointBuilder;
16  import org.mule.api.endpoint.EndpointException;
17  import org.mule.api.endpoint.OutboundEndpoint;
18  import org.mule.api.routing.OutboundRouterCollection;
19  import org.mule.api.service.Service;
20  import org.mule.api.transport.Connector;
21  import org.mule.endpoint.EndpointURIEndpointBuilder;
22  import org.mule.routing.outbound.OutboundPassThroughRouter;
23  import org.mule.tck.functional.FunctionalTestComponent;
24  import org.mule.tck.testmodels.fruit.Apple;
25  import org.mule.transport.email.AbstractMailConnector;
26  import org.mule.transport.email.MailProperties;
27  import org.mule.transport.email.SmtpConnector;
28  import org.mule.transport.email.functional.AbstractEmailFunctionalTestCase;
29  
30  import com.icegreen.greenmail.util.ServerSetup;
31  
32  import javax.mail.URLName;
33  import javax.mail.internet.MimeMessage;
34  
35  import org.junit.Test;
36  
37  import static org.junit.Assert.assertEquals;
38  import static org.junit.Assert.assertFalse;
39  import static org.junit.Assert.assertNotNull;
40  import static org.junit.Assert.assertNull;
41  import static org.junit.Assert.assertTrue;
42  import static org.junit.Assert.fail;
43  
44  /**
45   * Send a message via SMTP to a (greenmail) server.
46   */
47  public class SmtpConnectorTestCase extends AbstractMailConnectorFunctionalTestCase
48  {    
49      public SmtpConnectorTestCase()
50      {
51          this(NO_INITIAL_EMAIL, ServerSetup.PROTOCOL_SMTP);
52          setStartContext(true);
53      } 
54      
55      protected SmtpConnectorTestCase(boolean initialEmail, String protocol)
56      {
57          super(initialEmail, protocol);
58      }
59      
60      @Override
61      public Connector createConnector() throws Exception
62      {
63          SmtpConnector c = new SmtpConnector(muleContext);
64          c.setName("SmtpConnector");
65          return c;
66      }
67      
68      @Override
69      protected boolean transformInboundMessage()
70      {
71          return true;
72      }
73  
74      /**
75       * The SmtpConnector does not accept listeners, so the test in the superclass makes no sense 
76       * here. The SMTP transport does not even support inbound endpoints, as SMTP is an outbound
77       * transport only so you cannot even create an inbound endpoint to register as listener.
78       */
79      @Override
80      public void testConnectorListenerSupport() throws Exception
81      {
82          // do nothing
83      }
84  
85      @Test
86      public void testSmtpDoesNotSupportOutboundEndpoints() throws MuleException
87      {
88          EndpointBuilder builder = new EndpointURIEndpointBuilder(getTestEndpointURI(), muleContext);
89          builder.setName("test");
90  
91          try
92          {
93              muleContext.getEndpointFactory().getInboundEndpoint(builder);
94              fail("Inbound SMTP endpoints are not supported");
95          }
96          catch (EndpointException ex)
97          {
98              // expected
99          }
100     }
101 
102     @Test
103     public void testSend() throws Exception
104     {
105         //muleContext.getRegistry().registerConnector(createConnector(false));
106         OutboundEndpoint endpoint = muleContext.getEndpointFactory().getOutboundEndpoint(
107             getTestEndpointURI());
108         
109         Service service = getTestService(uniqueName("testComponent"), FunctionalTestComponent.class);
110         // TODO Simplify this API for adding an outbound endpoint.
111         OutboundPassThroughRouter passThroughRouter = new OutboundPassThroughRouter();
112         passThroughRouter.addRoute(endpoint);
113         ((OutboundRouterCollection) service.getOutboundMessageProcessor()).addRoute(passThroughRouter);
114         //muleContext.getRegistry().registerComponent(service);
115 
116         MuleMessage message = new DefaultMuleMessage(MESSAGE, muleContext);
117         message.setOutboundProperty(MailProperties.TO_ADDRESSES_PROPERTY, EMAIL);
118         MuleSession session = getTestSession(getTestService("apple", Apple.class), muleContext);
119         DefaultMuleEvent event = new DefaultMuleEvent(message, endpoint, session, new ResponseOutputStream(System.out));
120         endpoint.process(event);
121 
122         getServers().waitForIncomingEmail(AbstractEmailFunctionalTestCase.DELIVERY_DELAY_MS, 1);
123         MimeMessage[] messages = getServers().getReceivedMessages();
124         assertNotNull("did not receive any messages", messages);
125         assertEquals("did not receive 1 mail", 1, messages.length);
126         assertMessageOk(messages[0]);
127     }
128 
129 
130     // MULE-2130 (Impossible to re-initialise SMTP connector)
131     @Test
132     public void testConnectorRestart() throws Exception
133     {
134         Connector c = getConnector();
135         assertTrue(c.isStarted());
136 
137         c.stop();
138         assertFalse(c.isStarted());
139 
140         assertFalse(c.isStarted());
141 
142         c.start();
143         assertFalse(c.isDisposed());
144         assertTrue(c.isStarted());
145     }
146 
147     @Test
148     public void testNullUsernameAndPassword() throws Exception
149     {
150         OutboundEndpoint endpoint = muleContext.getEndpointFactory().getOutboundEndpoint("smtp://localhost:23");
151         URLName name = ((AbstractMailConnector)getConnector()).urlFromEndpoint(endpoint);
152         assertNull(name.getUsername());
153         assertNull(name.getPassword());
154 
155         endpoint = muleContext.getEndpointFactory().getOutboundEndpoint("smtp://george@localhost:23");
156         name = ((AbstractMailConnector)getConnector()).urlFromEndpoint(endpoint);
157         assertEquals("george", name.getUsername());
158         assertNull(name.getPassword());
159     }
160 }