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.test.construct;
8   
9   import org.mule.api.ExceptionPayload;
10  import org.mule.api.MuleMessage;
11  import org.mule.module.client.MuleClient;
12  import org.mule.module.client.RemoteDispatcher;
13  import org.mule.tck.junit4.FunctionalTestCase;
14  import org.mule.tck.junit4.rule.DynamicPort;
15  
16  import org.junit.Rule;
17  import org.junit.Test;
18  
19  import static org.junit.Assert.assertEquals;
20  import static org.junit.Assert.assertNotNull;
21  import static org.junit.Assert.assertTrue;
22  
23  /**
24   * Test remote dispatcher using serialization wire format
25   */
26  public class RemoteDispatcherTestCase extends FunctionalTestCase
27  {
28  
29      @Rule
30      public DynamicPort dynamicPort = new DynamicPort("port1");
31  
32      @Override
33      protected String getConfigResources()
34      {
35          return "org/mule/test/construct/remote-dispatcher.xml";
36      }
37  
38      @Test
39      public void testRemoting() throws Exception
40      {
41          String[] targets = {"service1", "nosuch", "vmConnector", "flow1"};
42          String expectedResponses[] = { "Hellogoodbye", null, null, "Helloaloha"};
43  
44          MuleClient client = new MuleClient(muleContext);
45          RemoteDispatcher dispatcher = client.getRemoteDispatcher("http://localhost:" + dynamicPort.getNumber());
46          for (int i = 0; i < targets.length; i++)
47          {
48              String construct = targets[i];
49              String expected = expectedResponses[i];
50  
51              MuleMessage result = dispatcher.sendToRemoteComponent(construct, "Hello", null);
52  
53              assertNotNull(result);
54              if (expected != null)
55              {
56                  assertEquals(expected, result.getPayload());
57              }
58              else
59              {
60                  ExceptionPayload payload = result.getExceptionPayload();
61                  assertNotNull(payload);
62                  assertTrue(payload.getException().getMessage().contains(construct));
63              }
64          }
65      }
66  }