View Javadoc

1   /*
2    * $Id: AjaxFunctionalJsonBindingsTestCase.java 22460 2011-07-20 07:44:33Z claude.mamo $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
5    *
6    * The software in this package is published under the terms of the CPAL v1.0
7    * license, a copy of which has been included with this distribution in the
8    * LICENSE.txt file.
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  import static org.junit.Assert.fail;
17  
18  import org.mule.api.MuleMessage;
19  import org.mule.module.client.MuleClient;
20  import org.mule.tck.AbstractServiceAndFlowTestCase;
21  import org.mule.tck.junit4.rule.DynamicPort;
22  import org.mule.util.concurrent.Latch;
23  
24  import java.util.Arrays;
25  import java.util.Collection;
26  import java.util.Map;
27  import java.util.concurrent.TimeUnit;
28  import java.util.concurrent.atomic.AtomicReference;
29  
30  import org.codehaus.jackson.map.ObjectMapper;
31  import org.cometd.Client;
32  import org.cometd.Message;
33  import org.cometd.MessageListener;
34  import org.junit.Rule;
35  import org.junit.Test;
36  import org.junit.runners.Parameterized.Parameters;
37  import org.mortbay.cometd.client.BayeuxClient;
38  import org.mortbay.jetty.client.Address;
39  import org.mortbay.jetty.client.HttpClient;
40  
41  public class AjaxFunctionalJsonBindingsTestCase extends AbstractServiceAndFlowTestCase
42  {
43      
44      public AjaxFunctionalJsonBindingsTestCase(ConfigVariant variant, String configResources)
45      {
46          super(variant, configResources);
47      }
48  
49      public static int SERVER_PORT = -1;
50  
51      private BayeuxClient client;
52  
53      @Rule
54      public DynamicPort dynamicPort = new DynamicPort("port1");
55     
56      @Parameters
57      public static Collection<Object[]> parameters()
58      {
59          return Arrays.asList(new Object[][]{
60              {ConfigVariant.SERVICE, "ajax-embedded-functional-json-bindings-test-service.xml"},
61              {ConfigVariant.FLOW, "ajax-embedded-functional-json-bindings-test-flow.xml"}
62          });
63      }      
64  
65      @Override
66      protected void doSetUp() throws Exception
67      {
68          SERVER_PORT = dynamicPort.getNumber();
69          HttpClient http = new HttpClient();
70          http.setConnectorType(HttpClient.CONNECTOR_SELECT_CHANNEL);
71  
72          client = new BayeuxClient(http, new Address("localhost", SERVER_PORT), "/ajax/cometd");
73          http.start();
74          //need to start the client before you can add subscriptions
75          client.start();
76      }
77  
78      @Override
79      protected void doTearDown() throws Exception
80      {
81          //9 times out of 10 this throws a "ava.lang.IllegalStateException: Not running" exception, it can be ignored
82          //client.stop();
83          // TODO DZ: it seems like you would want to do this, maybe causing port locking issues?
84          try
85          {
86              client.stop();
87          }
88          catch (IllegalStateException e)
89          {
90              logger.info("caught an IllegalStateException during tearDown", e);
91          }
92          catch(Exception e1)
93          {
94              fail("unexpected exception during tearDown :" + e1.getMessage());
95          }
96      }
97  
98      @Test
99      public void testClientSubscribeWithJsonObjectResponse() throws Exception
100     {
101         final Latch latch = new Latch();
102 
103         final AtomicReference<String> data = new AtomicReference<String>();
104         client.addListener(new MessageListener()
105         {
106             @Override
107             public void deliver(Client fromClient, Client toClient, Message message)
108             {
109                 if (message.getData() != null)
110                 {
111                     // This simulate what the browser would receive
112                     data.set(message.toString());
113                     latch.release();
114                 }
115             }
116         });
117         client.subscribe("/test1");
118 
119         MuleClient muleClient = new MuleClient(muleContext);
120         muleClient.dispatch("vm://in1", "Ross", null);
121         assertTrue("data did not arrive in 10 seconds", latch.await(10, TimeUnit.SECONDS));
122 
123         assertNotNull(data.get());
124 
125         // parse the result string into java objects. different jvms return it in
126         // different order, so we can't do a straight string comparison
127         ObjectMapper mapper = new ObjectMapper();
128         Map<?, ?> result = mapper.readValue(data.get(), Map.class);
129         assertEquals("/test1", result.get("channel"));
130         assertEquals("Ross", ((Map<?, ?>)result.get("data")).get("name"));
131     }
132 
133     @Test
134     public void testClientPublishWithJsonObject() throws Exception
135     {
136         client.publish("/test2", "{\"name\":\"Ross\"}", null);
137         MuleClient muleClient = new MuleClient(muleContext);
138         MuleMessage msg = muleClient.request("vm://in2", 5000L);
139 
140         assertNotNull(msg);
141         assertEquals("Received: DummyJsonBean{name='Ross'}", msg.getPayloadAsString());
142     }
143 }