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.http.functional;
8   
9   import org.mule.api.MuleException;
10  import org.mule.api.MuleMessage;
11  import org.mule.api.endpoint.InboundEndpoint;
12  import org.mule.api.transformer.DataType;
13  import org.mule.module.client.MuleClient;
14  import org.mule.tck.junit4.FunctionalTestCase;
15  import org.mule.tck.junit4.rule.DynamicPort;
16  
17  import java.util.ArrayList;
18  import java.util.List;
19  
20  import org.junit.Rule;
21  import org.junit.Test;
22  
23  import static org.junit.Assert.assertEquals;
24  import static org.junit.Assert.assertNotNull;
25  
26  public class TwoEndpointsSinglePortTestCase extends FunctionalTestCase
27  {
28      
29      @Rule
30      public DynamicPort dynamicPort = new DynamicPort("port1");
31  
32      @Override
33      protected String getConfigResources()
34      {
35          return "two-endpoints-single-port.xml";
36      }
37  
38      @Test
39      public void testSendToEach() throws Exception
40      {
41          sendWithResponse("inMyComponent1", "test", "mycomponent1", 10);
42          sendWithResponse("inMyComponent2", "test", "mycomponent2", 10);
43      }
44  
45      @Test
46      public void testSendToEachWithBadEndpoint() throws Exception
47      {
48  
49          MuleClient client = new MuleClient(muleContext);
50  
51          sendWithResponse("inMyComponent1", "test", "mycomponent1", 5);
52          sendWithResponse("inMyComponent2", "test", "mycomponent2", 5);
53  
54          MuleMessage result = client.send("http://localhost:" + dynamicPort.getNumber() + "/mycomponent-notfound", "test", null);
55          assertNotNull(result);
56          assertNotNull(result.getExceptionPayload());
57          final int status = result.getInboundProperty("http.status", 0);
58          assertEquals(404, status);
59  
60          // Test that after the exception the endpoints still receive events
61          sendWithResponse("inMyComponent1", "test", "mycomponent1", 5);
62          sendWithResponse("inMyComponent2", "test", "mycomponent2", 5);
63      }
64  
65      protected void sendWithResponse(String endPointName, String message, String response, int noOfMessages)
66          throws MuleException
67      {
68          MuleClient client = new MuleClient(muleContext);
69  
70          List results = new ArrayList();
71          for (int i = 0; i < noOfMessages; i++)
72          {
73              results.add(client.send(((InboundEndpoint) client.getMuleContext().getRegistry().lookupObject(endPointName)).getAddress(), message, null).getPayload(DataType.BYTE_ARRAY_DATA_TYPE));
74          }
75  
76          assertEquals(noOfMessages, results.size());
77          for (int i = 0; i < noOfMessages; i++)
78          {
79              assertEquals(response, new String((byte[])results.get(i)));
80          }
81      }
82  
83  }