1
2
3
4
5
6
7
8
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
63
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
71 client.start();
72 }
73
74 @Override
75 protected void doTearDown() throws Exception
76 {
77
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
95 data.set((message.getData()));
96 latch.release();
97 }
98 }
99 });
100
101 client.subscribe("/response");
102
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 }