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