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.servlet.jetty;
8   
9   import org.mule.api.MuleException;
10  import org.mule.api.MuleMessage;
11  import org.mule.api.transformer.DataType;
12  import org.mule.module.client.MuleClient;
13  import org.mule.tck.junit4.FunctionalTestCase;
14  import org.mule.tck.junit4.rule.DynamicPort;
15  
16  import java.util.ArrayList;
17  import java.util.List;
18  
19  import org.junit.Rule;
20  import org.junit.Test;
21  
22  import static org.junit.Assert.assertEquals;
23  import static org.junit.Assert.assertNotNull;
24  
25  public class JettyTwoEndpointsSinglePortTestCase extends FunctionalTestCase
26  {
27  
28      @Rule
29      public DynamicPort dynamicPort = new DynamicPort("port1");
30  
31      @Override
32      protected String getConfigResources()
33      {
34          return "jetty-two-endpoints-single-port.xml";
35      }
36  
37      @Test
38      public void testSendToEach() throws Exception
39      {
40  
41          sendWithResponse("http://localhost:" + dynamicPort.getNumber() + "/mycomponent1", "test", "mycomponent1", 10);
42          sendWithResponse("http://localhost:" + dynamicPort.getNumber() + "/mycomponent2", "test", "mycomponent2", 10);
43      }
44  
45      @Test
46      public void testSendToEachWithBadEndpoint() throws Exception
47      {
48  
49          MuleClient client = new MuleClient(muleContext);
50  
51          sendWithResponse("http://localhost:" + dynamicPort.getNumber() + "/mycomponent1", "test", "mycomponent1", 5);
52          sendWithResponse("http://localhost:" + dynamicPort.getNumber() + "/mycomponent2", "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("http://localhost:" + dynamicPort.getNumber() + "/mycomponent1", "test", "mycomponent1", 5);
62          sendWithResponse("http://localhost:" + dynamicPort.getNumber() + "/mycomponent2", "test", "mycomponent2", 5);
63      }
64  
65      protected void sendWithResponse(String endpoint, 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              MuleMessage result = client.send(endpoint, message, null);
74              results.add(result.getPayload(DataType.BYTE_ARRAY_DATA_TYPE));
75          }
76  
77          assertEquals(noOfMessages, results.size());
78          for (int i = 0; i < noOfMessages; i++)
79          {
80              assertEquals(response, new String((byte[])results.get(i)));
81          }
82      }
83  }