View Javadoc

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