View Javadoc

1   /*
2    * $Id: TwoEndpointsSinglePortTestCase.java 22450 2011-07-19 08:20:41Z dirk.olmes $
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.http.functional;
12  
13  import org.mule.api.MuleException;
14  import org.mule.api.MuleMessage;
15  import org.mule.api.endpoint.InboundEndpoint;
16  import org.mule.api.transformer.DataType;
17  import org.mule.module.client.MuleClient;
18  import org.mule.tck.AbstractServiceAndFlowTestCase;
19  import org.mule.tck.junit4.rule.DynamicPort;
20  
21  import java.util.ArrayList;
22  import java.util.Arrays;
23  import java.util.Collection;
24  import java.util.List;
25  
26  import org.junit.Rule;
27  import org.junit.Test;
28  import org.junit.runners.Parameterized.Parameters;
29  
30  import static org.junit.Assert.assertEquals;
31  import static org.junit.Assert.assertNotNull;
32  
33  public class TwoEndpointsSinglePortTestCase extends AbstractServiceAndFlowTestCase
34  {
35      @Rule
36      public DynamicPort port1 = new DynamicPort("port1");
37  
38      @Parameters
39      public static Collection<Object[]> parameters()
40      {
41          return Arrays.asList(new Object[][]{
42              {ConfigVariant.SERVICE, "two-endpoints-single-port-service.xml"},
43              {ConfigVariant.FLOW, "two-endpoints-single-port-flow.xml"}
44          });
45      }
46  
47      public TwoEndpointsSinglePortTestCase(ConfigVariant variant, String configResources)
48      {
49          super(variant, configResources);
50      }
51      
52      @Test
53      public void testSendToEach() throws Exception
54      {
55          sendWithResponse("inMyComponent1", "test", "mycomponent1", 10);
56          sendWithResponse("inMyComponent2", "test", "mycomponent2", 10);
57      }
58  
59      @Test
60      public void testSendToEachWithBadEndpoint() throws Exception
61      {
62  
63          MuleClient client = new MuleClient(muleContext);
64  
65          sendWithResponse("inMyComponent1", "test", "mycomponent1", 5);
66          sendWithResponse("inMyComponent2", "test", "mycomponent2", 5);
67  
68          String url = String.format("http://localhost:%d/mycomponent-notfound", port1.getNumber());
69          MuleMessage result = client.send(url, "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("inMyComponent1", "test", "mycomponent1", 5);
77          sendWithResponse("inMyComponent2", "test", "mycomponent2", 5);
78      }
79  
80      protected void sendWithResponse(String endPointName, String message, String response, int noOfMessages)
81          throws MuleException
82      {
83          MuleClient client = new MuleClient(muleContext);
84  
85          List<Object> results = new ArrayList<Object>();
86          for (int i = 0; i < noOfMessages; i++)
87          {
88              results.add(client.send(
89                  ((InboundEndpoint) client.getMuleContext().getRegistry().lookupObject(endPointName)).getAddress(),
90                  message, null)
91                  .getPayload(DataType.BYTE_ARRAY_DATA_TYPE));
92          }
93  
94          assertEquals(noOfMessages, results.size());
95          for (int i = 0; i < noOfMessages; i++)
96          {
97              assertEquals(response, new String((byte[]) results.get(i)));
98          }
99      }
100 }