1
2
3
4
5
6
7
8
9
10
11 package org.mule.transport.ajax;
12
13 import static org.junit.Assert.assertEquals;
14 import static org.junit.Assert.assertNotNull;
15 import static org.junit.Assert.assertTrue;
16
17 import org.mule.api.MuleMessage;
18 import org.mule.module.client.MuleClient;
19 import org.mule.tck.AbstractServiceAndFlowTestCase;
20 import org.mule.tck.junit4.rule.DynamicPort;
21 import org.mule.util.concurrent.Latch;
22
23 import java.util.Arrays;
24 import java.util.Collection;
25 import java.util.Map;
26 import java.util.concurrent.TimeUnit;
27 import java.util.concurrent.atomic.AtomicReference;
28
29 import org.codehaus.jackson.map.ObjectMapper;
30 import org.cometd.Client;
31 import org.cometd.Message;
32 import org.cometd.MessageListener;
33 import org.junit.Rule;
34 import org.junit.Test;
35 import org.junit.runners.Parameterized.Parameters;
36 import org.mortbay.cometd.client.BayeuxClient;
37 import org.mortbay.jetty.client.Address;
38 import org.mortbay.jetty.client.HttpClient;
39
40 public class AjaxFunctionalTestCase extends AbstractServiceAndFlowTestCase
41 {
42 public static int SERVER_PORT = -1;
43
44 private HttpClient httpClient;
45 private BayeuxClient bayeuxClient;
46
47 @Rule
48 public DynamicPort dynamicPort = new DynamicPort("port1");
49
50 public AjaxFunctionalTestCase(ConfigVariant variant, String configResources)
51 {
52
53
54 super(variant, configResources);
55 setStartContext(false);
56 }
57
58 @Parameters
59 public static Collection<Object[]> parameters()
60 {
61 return Arrays.asList(new Object[][]{
62 {ConfigVariant.SERVICE, "ajax-embedded-functional-test-service.xml"},
63 {ConfigVariant.FLOW, "ajax-embedded-functional-test-flow.xml"}
64 });
65 }
66
67 @Override
68 protected void doSetUp() throws Exception
69 {
70 SERVER_PORT = dynamicPort.getNumber();
71 httpClient = new HttpClient();
72 httpClient.setConnectorType(HttpClient.CONNECTOR_SELECT_CHANNEL);
73 httpClient.start();
74
75 bayeuxClient = new BayeuxClient(httpClient, new Address("localhost", SERVER_PORT), "/ajax/cometd");
76
77 bayeuxClient.start();
78
79 assertTrue("httpClient is not running", httpClient.isRunning());
80 assertTrue("bayeuxClient is not running", bayeuxClient.isRunning());
81 muleContext.start();
82 }
83
84 @Override
85 protected void doTearDown() throws Exception
86 {
87 if(muleContext.isStarted())
88 {
89 muleContext.stop();
90 }
91
92 if (httpClient.isRunning())
93 {
94 httpClient.stop();
95 }
96
97 try
98 {
99
100
101
102
103
104 bayeuxClient.stop();
105 }
106 catch (Exception e)
107 {
108
109 }
110 }
111
112 @Test
113 public void testClientSubscribeWithString() throws Exception
114 {
115
116
117
118
119 Thread.sleep(5000);
120 final Latch latch = new Latch();
121
122 final AtomicReference<Object> data = new AtomicReference<Object>();
123 bayeuxClient.addListener(new MessageListener()
124 {
125 @Override
126 public void deliver(Client fromClient, Client toClient, Message message)
127 {
128
129 if (message.getData() != null)
130 {
131
132 data.set(message.toString());
133 latch.release();
134 }
135 }
136 });
137 bayeuxClient.subscribe("/test1");
138
139 MuleClient muleClient = new MuleClient(muleContext);
140 muleClient.dispatch("vm://in1", "Ross", null);
141
142 latch.await(10, TimeUnit.SECONDS);
143 assertNotNull(data.get());
144
145
146 ObjectMapper mapper = new ObjectMapper();
147 Map<?, ?> result = mapper.readValue((String) data.get(), Map.class);
148 assertEquals("/test1", result.get("channel"));
149 assertEquals("Ross Received", result.get("data"));
150 }
151
152 @Test
153 public void testClientPublishWithString() throws Exception
154 {
155 MuleClient muleClient = new MuleClient(muleContext);
156
157 bayeuxClient.publish("/test2", "Ross", null);
158 MuleMessage msg = muleClient.request("vm://in2", RECEIVE_TIMEOUT * 2);
159
160 assertNotNull(msg);
161 assertEquals("Ross Received", msg.getPayloadAsString());
162 }
163
164 }