View Javadoc

1   /*
2    * $Id: RemoteDispatcherTestCase.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.test.construct;
12  
13  import org.mule.api.ExceptionPayload;
14  import org.mule.api.MuleMessage;
15  import org.mule.module.client.MuleClient;
16  import org.mule.module.client.RemoteDispatcher;
17  import org.mule.tck.AbstractServiceAndFlowTestCase;
18  import org.mule.tck.junit4.rule.DynamicPort;
19  
20  import java.util.Arrays;
21  import java.util.Collection;
22  
23  import org.junit.Rule;
24  import org.junit.Test;
25  import org.junit.runners.Parameterized.Parameters;
26  
27  import static org.junit.Assert.assertEquals;
28  import static org.junit.Assert.assertNotNull;
29  import static org.junit.Assert.assertTrue;
30  
31  /**
32   * Test remote dispatcher using serialization wire format
33   */
34  public class RemoteDispatcherTestCase extends AbstractServiceAndFlowTestCase
35  {
36      @Rule
37      public DynamicPort port1 = new DynamicPort("port1");
38  
39      @Parameters
40      public static Collection<Object[]> parameters()
41      {
42          return Arrays.asList(new Object[][]{
43              {ConfigVariant.SERVICE, "org/mule/test/construct/remote-dispatcher.xml"}
44  
45          });
46      }
47  
48      public RemoteDispatcherTestCase(ConfigVariant variant, String configResources)
49      {
50          super(variant, configResources);
51      }
52  
53      @Test
54      public void testRemoting() throws Exception
55      {
56          String[] targets = {"service1", "nosuch", "vmConnector", "flow1"};
57          String expectedResponses[] = {"Hellogoodbye", null, null, "Helloaloha"};
58  
59          MuleClient client = new MuleClient(muleContext);
60          RemoteDispatcher dispatcher = client.getRemoteDispatcher("http://localhost:" + port1.getNumber());
61          for (int i = 0; i < targets.length; i++)
62          {
63              String construct = targets[i];
64              String expected = expectedResponses[i];
65  
66              MuleMessage result = dispatcher.sendToRemoteComponent(construct, "Hello", null);
67  
68              assertNotNull(result);
69              if (expected != null)
70              {
71                  assertEquals(expected, result.getPayload());
72              }
73              else
74              {
75                  ExceptionPayload payload = result.getExceptionPayload();
76                  assertNotNull(payload);
77                  assertTrue(payload.getException().getMessage().contains(construct));
78              }
79          }
80      }
81  }