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.integration.client;
8   
9   
10  import org.mule.api.MuleEvent;
11  import org.mule.api.MuleMessage;
12  import org.mule.module.client.MuleClient;
13  import org.mule.module.client.RemoteDispatcher;
14  import org.mule.tck.junit4.FunctionalTestCase;
15  
16  import org.junit.Test;
17  
18  import static org.junit.Assert.assertEquals;
19  import static org.junit.Assert.assertNotNull;
20  import static org.junit.Assert.assertNull;
21  
22  public abstract class AbstractClientRemotingTestCase extends FunctionalTestCase
23  {
24  
25      public abstract String getRemoteEndpointUri();
26  
27      @Test
28      public void testClientSendToRemoteComponent() throws Exception
29      {
30          // Will connect to the server using remote endpoint
31          MuleClient client = new MuleClient(muleContext);
32   
33          RemoteDispatcher dispatcher = client.getRemoteDispatcher(getRemoteEndpointUri());
34          MuleMessage message = dispatcher.sendToRemoteComponent("TestReceiverUMO", "Test Client Send message", null);
35          assertNotNull(message);
36          assertEquals("Test Client Send message Received", message.getPayload());
37      }
38  
39      @Test
40      public void testClientRequestResponseOnEndpoint() throws Exception
41      {
42          // Will connect to the server using tcp://localhost:60504
43          MuleClient client = new MuleClient(muleContext);
44   
45          RemoteDispatcher dispatcher = client.getRemoteDispatcher(getRemoteEndpointUri());
46          MuleMessage message = dispatcher.sendRemote("vm://remote.endpoint?connector=vmRemoteConnector", "foo",
47                  null);
48          assertNotNull(message);
49          assertEquals("received from remote component", message.getPayloadAsString());
50      }
51  
52      /**
53       * A test that writes a message to a remote queue
54       *
55       * @throws Exception
56       */
57      @Test
58      public void testClientSendAndReceiveRemote() throws Exception
59      {
60          String remoteEndpoint = "vm://remote.queue?connector=vmRemoteQueueConnector";
61          // Will connect to the server using The Server endpoint
62          MuleClient client = new MuleClient(muleContext);
63  
64          RemoteDispatcher dispatcher = client.getRemoteDispatcher(getRemoteEndpointUri());
65          // Doubling timeout see MULE-3000
66          // Use TIMEOUT_NOT_SET_VALUE as we need respective remoteDispatcherEndpoint to you timeout as defined on the endpoint.  
67          MuleMessage message = dispatcher.receiveRemote(remoteEndpoint,MuleEvent.TIMEOUT_NOT_SET_VALUE);
68          assertNull(message);
69          // We do a send instead of a dispatch here so the operation is
70          // synchronous thus eaiser to test
71          dispatcher.sendRemote(remoteEndpoint, "Test Remote Message 2", null);
72  
73          // Doubling timeout see MULE-3000
74          message = dispatcher.receiveRemote(remoteEndpoint, RECEIVE_TIMEOUT * 2);
75          assertNotNull(message);
76          assertEquals("Test Remote Message 2", message.getPayload());
77      }
78      
79  }