View Javadoc

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