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.transport.ajax;
8   
9   import org.mule.tck.junit4.FunctionalTestCase;
10  import org.mule.tck.junit4.rule.DynamicPort;
11  import org.mule.util.concurrent.Latch;
12  
13  import java.util.concurrent.atomic.AtomicReference;
14  
15  import edu.emory.mathcs.backport.java.util.concurrent.TimeUnit;
16  import org.cometd.Client;
17  import org.cometd.Message;
18  import org.cometd.MessageListener;
19  import org.junit.Rule;
20  import org.junit.Test;
21  import org.mortbay.cometd.client.BayeuxClient;
22  import org.mortbay.jetty.client.Address;
23  import org.mortbay.jetty.client.HttpClient;
24  
25  import static org.junit.Assert.assertEquals;
26  import static org.junit.Assert.assertNotNull;
27  
28  public class AjaxRPCFunctionalTestCase extends FunctionalTestCase
29  {
30  
31      public static final String TEST_JSON_MESSAGE = "{\"data\" : {\"value1\" : \"foo\", \"value2\" : \"bar\"}, \"replyTo\" : \"/response\"}";
32  
33      public static int SERVER_PORT = -1;
34  
35      private BayeuxClient client;
36  
37      @Rule
38      public DynamicPort dynamicPort = new DynamicPort("port1");
39  
40      @Override
41      protected String getConfigResources()
42      {
43          return "ajax-rpc-test.xml";
44      }
45  
46      @Override
47      protected void doSetUp() throws Exception
48      {
49          // FIXME DZ: we don't use the inherited SERVER_PORT here because it's not set
50          // at this point and we can't move super.doSetUp() above this
51          SERVER_PORT = dynamicPort.getNumber();
52          HttpClient http = new HttpClient();
53          http.setConnectorType(HttpClient.CONNECTOR_SELECT_CHANNEL);
54  
55          client = new BayeuxClient(http, new Address("localhost", SERVER_PORT), "/ajax/cometd");
56          http.start();
57          //need to start the client before you can add subscriptions
58          client.start();
59      }
60  
61      @Override
62      protected void doTearDown() throws Exception
63      {
64          //9 times out of 10 this throws a "ava.lang.IllegalStateException: Not running" exception, it can be ignored
65          client.stop();
66      }
67  
68      @Test
69      public void testDispatchReceiveSimple() throws Exception
70      {
71          final Latch latch = new Latch();
72  
73          final AtomicReference<Object> data = new AtomicReference<Object>();
74          client.addListener(new MessageListener()
75          {
76              public void deliver(Client client, Client client1, Message message)
77              {
78                  if (message.getData() != null)
79                  {
80                      //This simulate what the browser would receive
81                      data.set((message.getData()));
82                      latch.release();
83                  }
84              }
85          });
86          //The '/response' channel is set on the request message
87          client.subscribe("/response");
88          //Simulates dispatching from the browser
89          client.publish("/request", TEST_JSON_MESSAGE, null);
90          latch.await(10, TimeUnit.SECONDS);
91  
92          assertNotNull(data.get());
93          assertEquals("{\"value1\":\"foo\",\"value2\":\"bar\"}", data.get());
94      }
95  }