View Javadoc

1   /*
2    * $Id: JettyTwoEndpointsSinglePortTestCase.java 22554 2011-07-25 09:43:43Z claude.mamo $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
5    *
6    * The software in this package is published under the terms of the CPAL v1.0
7    * license, a copy of which has been included with this distribution in the
8    * LICENSE.txt file.
9    */
10  
11  package org.mule.transport.servlet.jetty;
12  
13  import static org.junit.Assert.assertEquals;
14  import static org.junit.Assert.assertNotNull;
15  
16  import org.mule.api.MuleException;
17  import org.mule.api.MuleMessage;
18  import org.mule.api.transformer.DataType;
19  import org.mule.module.client.MuleClient;
20  import org.mule.tck.AbstractServiceAndFlowTestCase;
21  import org.mule.tck.junit4.rule.DynamicPort;
22  
23  import java.util.ArrayList;
24  import java.util.Arrays;
25  import java.util.Collection;
26  import java.util.List;
27  
28  import org.junit.Rule;
29  import org.junit.Test;
30  import org.junit.runners.Parameterized.Parameters;
31  
32  public class JettyTwoEndpointsSinglePortTestCase extends AbstractServiceAndFlowTestCase
33  {
34  
35      @Rule
36      public DynamicPort dynamicPort = new DynamicPort("port1");
37  
38      public JettyTwoEndpointsSinglePortTestCase(ConfigVariant variant, String configResources)
39      {
40          super(variant, configResources);
41      }
42  
43      @Parameters
44      public static Collection<Object[]> parameters()
45      {
46          return Arrays.asList(new Object[][]{
47              {ConfigVariant.SERVICE, "jetty-two-endpoints-single-port-service.xml"},
48              {ConfigVariant.FLOW, "jetty-two-endpoints-single-port-flow.xml"}
49          });
50      }      
51      
52      @Test
53      public void testSendToEach() throws Exception
54      {
55  
56          sendWithResponse("http://localhost:" + dynamicPort.getNumber() + "/mycomponent1", "test", "mycomponent1", 10);
57          sendWithResponse("http://localhost:" + dynamicPort.getNumber() + "/mycomponent2", "test", "mycomponent2", 10);
58      }
59  
60      @Test
61      public void testSendToEachWithBadEndpoint() throws Exception
62      {
63  
64          MuleClient client = new MuleClient(muleContext);
65  
66          sendWithResponse("http://localhost:" + dynamicPort.getNumber() + "/mycomponent1", "test", "mycomponent1", 5);
67          sendWithResponse("http://localhost:" + dynamicPort.getNumber() + "/mycomponent2", "test", "mycomponent2", 5);
68  
69          MuleMessage result = client.send("http://localhost:" + dynamicPort.getNumber() + "/mycomponent-notfound", "test", null);
70          assertNotNull(result);
71          assertNotNull(result.getExceptionPayload());
72          final int status = result.getInboundProperty("http.status", 0);
73          assertEquals(404, status);
74  
75          // Test that after the exception the endpoints still receive events
76          sendWithResponse("http://localhost:" + dynamicPort.getNumber() + "/mycomponent1", "test", "mycomponent1", 5);
77          sendWithResponse("http://localhost:" + dynamicPort.getNumber() + "/mycomponent2", "test", "mycomponent2", 5);
78      }
79  
80      protected void sendWithResponse(String endpoint, String message, String response, int noOfMessages)
81          throws MuleException
82      {
83          MuleClient client = new MuleClient(muleContext);
84  
85          List results = new ArrayList();
86          for (int i = 0; i < noOfMessages; i++)
87          {
88              MuleMessage result = client.send(endpoint, message, null);
89              results.add(result.getPayload(DataType.BYTE_ARRAY_DATA_TYPE));
90          }
91  
92          assertEquals(noOfMessages, results.size());
93          for (int i = 0; i < noOfMessages; i++)
94          {
95              assertEquals(response, new String((byte[])results.get(i)));
96          }
97      }
98  }