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.fail;
32
33 public class AjaxFunctionalJsonBindingsTestCase extends FunctionalTestCase
34 {
35
36 public static int SERVER_PORT = -1;
37
38 private BayeuxClient client;
39
40 @Rule
41 public DynamicPort dynamicPort = new DynamicPort("port1");
42
43 @Override
44 protected String getConfigResources()
45 {
46 return "ajax-embedded-functional-json-bindings-test.xml";
47 }
48
49 @Override
50 protected void doSetUp() throws Exception
51 {
52 SERVER_PORT = dynamicPort.getNumber();
53 HttpClient http = new HttpClient();
54 http.setConnectorType(HttpClient.CONNECTOR_SELECT_CHANNEL);
55
56 client = new BayeuxClient(http, new Address("localhost", SERVER_PORT), "/ajax/cometd");
57 http.start();
58
59 client.start();
60 }
61
62 @Override
63 protected void doTearDown() throws Exception
64 {
65
66
67
68 try
69 {
70 client.stop();
71 }
72 catch (IllegalStateException e)
73 {
74 logger.info("caught an IllegalStateException during tearDown", e);
75 }
76 catch(Exception e1)
77 {
78 fail("unexpected exception during tearDown :" + e1.getMessage());
79 }
80 }
81
82 @Test
83 public void testClientSubscribeWithJsonObjectResponse() throws Exception
84 {
85 final Latch latch = new Latch();
86
87 final AtomicReference<String> data = new AtomicReference<String>();
88 client.addListener(new MessageListener()
89 {
90 public void deliver(Client fromClient, Client toClient, Message message)
91 {
92 if (message.getData() != null)
93 {
94
95 data.set(message.toString());
96 latch.release();
97 }
98 }
99 });
100 client.subscribe("/test1");
101
102 MuleClient muleClient = new MuleClient(muleContext);
103 muleClient.dispatch("vm://in1", "Ross", null);
104 latch.await(10, TimeUnit.SECONDS);
105
106 assertNotNull(data.get());
107
108
109
110 ObjectMapper mapper = new ObjectMapper();
111 Map<?, ?> result = mapper.readValue(data.get(), Map.class);
112 assertEquals("/test1", result.get("channel"));
113 assertEquals("Ross", ((Map<?, ?>)result.get("data")).get("name"));
114 }
115
116 @Test
117 public void testClientPublishWithJsonObject() throws Exception
118 {
119 client.publish("/test2", "{\"name\":\"Ross\"}", null);
120 MuleClient muleClient = new MuleClient(muleContext);
121 MuleMessage msg = muleClient.request("vm://in2", 5000L);
122
123 assertNotNull(msg);
124 assertEquals("Received: DummyJsonBean{name='Ross'}", msg.getPayloadAsString());
125 }
126 }