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.polling;
8   
9   import static org.junit.Assert.assertEquals;
10  import static org.junit.Assert.assertNotNull;
11  import static org.junit.Assert.assertNull;
12  
13  import org.mule.DefaultMuleEvent;
14  import org.mule.DefaultMuleMessage;
15  import org.mule.api.MuleEvent;
16  import org.mule.api.MuleException;
17  import org.mule.api.construct.FlowConstruct;
18  import org.mule.api.endpoint.InboundEndpoint;
19  import org.mule.api.lifecycle.InitialisationException;
20  import org.mule.api.processor.MessageProcessor;
21  import org.mule.endpoint.EndpointURIEndpointBuilder;
22  import org.mule.tck.SensingNullMessageProcessor;
23  import org.mule.tck.junit4.AbstractMuleContextTestCase;
24  import org.mule.transport.NullPayload;
25  
26  import org.junit.Test;
27  import org.mockito.Mockito;
28  
29  public class MessageProcessorPollingMessageReceiverTestCase extends AbstractMuleContextTestCase
30  {
31  
32      @Test
33      public void testNullResponseFromNestedMP() throws Exception
34      {
35  
36          MessageProcessorPollingMessageReceiver receiver = createReceiver(new MessageProcessor()
37          {
38              public MuleEvent process(MuleEvent event) throws MuleException
39              {
40                  return null;
41              }
42          });
43  
44          SensingNullMessageProcessor flow = getSensingNullMessageProcessor();
45          receiver.setListener(flow);
46  
47          receiver.poll();
48  
49          assertNull(flow.event);
50      }
51  
52      @Test
53      public void testNullPayloadResponseFromNestedMP() throws Exception
54      {
55  
56          MessageProcessorPollingMessageReceiver receiver = createReceiver(new MessageProcessor()
57          {
58              public MuleEvent process(MuleEvent event) throws MuleException
59              {
60                  return new DefaultMuleEvent(new DefaultMuleMessage(NullPayload.getInstance(), muleContext),
61                      event);
62              }
63          });
64  
65          SensingNullMessageProcessor flow = getSensingNullMessageProcessor();
66          receiver.setListener(flow);
67  
68          receiver.poll();
69  
70          assertNull(flow.event);
71      }
72  
73      @Test
74      public void testEmptyStringResponseFromNestedMP() throws Exception
75      {
76  
77          MessageProcessorPollingMessageReceiver receiver = createReceiver(new MessageProcessor()
78          {
79              public MuleEvent process(MuleEvent event) throws MuleException
80              {
81                  return new DefaultMuleEvent(new DefaultMuleMessage("", muleContext), event);
82              }
83          });
84  
85          SensingNullMessageProcessor flow = getSensingNullMessageProcessor();
86          receiver.setListener(flow);
87  
88          receiver.poll();
89  
90          assertNotNull(flow.event);
91      }
92  
93      @Test
94      public void testNestedOneWayEndpoint() throws Exception
95      {
96  
97          try
98          {
99              createReceiver(muleContext.getEndpointFactory().getOutboundEndpoint("test://test2"));
100 
101             org.junit.Assert.fail("Exception expected");
102         }
103         catch (Exception e)
104         {
105 
106             assertEquals(InitialisationException.class, e.getClass());
107         }
108 
109     }
110 
111     private MessageProcessorPollingMessageReceiver createReceiver(MessageProcessor processor)
112         throws MuleException
113     {
114         EndpointURIEndpointBuilder builder = new EndpointURIEndpointBuilder("test://test", muleContext);
115         builder.setProperty(MessageProcessorPollingMessageReceiver.SOURCE_MESSAGE_PROCESSOR_PROPERTY_NAME,
116             processor);
117 
118         InboundEndpoint inboundEndpoint = muleContext.getEndpointFactory().getInboundEndpoint(builder);
119 
120         MessageProcessorPollingMessageReceiver receiver = new MessageProcessorPollingMessageReceiver(
121             inboundEndpoint.getConnector(), Mockito.mock(FlowConstruct.class), inboundEndpoint);
122 
123         receiver.initialise();
124         return receiver;
125     }
126 
127 }