View Javadoc
1   /*
2    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
3    * The software in this package is published under the terms of the CPAL v1.0
4    * license, a copy of which has been included with this distribution in the
5    * LICENSE.txt file.
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          //need to start the client before you can add subscriptions
59          client.start();
60      }
61  
62      @Override
63      protected void doTearDown() throws Exception
64      {
65          //9 times out of 10 this throws a "ava.lang.IllegalStateException: Not running" exception, it can be ignored
66          //client.stop();
67          // TODO DZ: it seems like you would want to do this, maybe causing port locking issues?
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                      // This simulate what the browser would receive
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         // parse the result string into java objects. different jvms return it in
109         // different order, so we can't do a straight string comparison
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 }