View Javadoc

1   /*
2    * $Id: AjaxMuleMessageFactoryTestCase.java 19191 2010-08-25 21:05:23Z tcarlson $
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 org.mule.api.MuleMessage;
14  import org.mule.api.transport.MuleMessageFactory;
15  import org.mule.tck.testmodels.fruit.Apple;
16  import org.mule.tck.testmodels.fruit.Banana;
17  import org.mule.tck.testmodels.fruit.FruitBowl;
18  import org.mule.transport.AbstractMuleMessageFactoryTestCase;
19  import org.mule.transport.ajax.embedded.AjaxConnector;
20  
21  import java.util.HashMap;
22  import java.util.Map;
23  
24  import org.cometd.Bayeux;
25  
26  public class AjaxMuleMessageFactoryTestCase extends AbstractMuleMessageFactoryTestCase
27  {
28      private static final String JSON_STRING = "{\"value1\":\"foo\",\"value2\":\"bar\"}";
29  
30      public AjaxMuleMessageFactoryTestCase()
31      {
32          super();
33          runUnsuppoprtedTransportMessageTest = false;
34      }
35      
36      @Override
37      protected MuleMessageFactory doCreateMuleMessageFactory()
38      {
39          return new AjaxMuleMessageFactory(muleContext);
40      }
41  
42      @Override
43      protected Object getValidTransportMessage() throws Exception
44      {
45          Map<String, Object> map = new HashMap<String, Object>();
46          map.put(Bayeux.DATA_FIELD, JSON_STRING);
47          map.put(AjaxConnector.REPLYTO_PARAM, "/reply");
48          map.put("message-property", "mp-value");
49          
50          return map;
51      }
52  
53      @Override
54      public void testValidPayload() throws Exception
55      {
56          MuleMessageFactory factory = createMuleMessageFactory();
57          
58          Object payload = getValidTransportMessage();
59          MuleMessage message = factory.create(payload, encoding);
60          assertNotNull(message);
61          assertEquals(JSON_STRING, message.getPayload());
62          assertEquals("/reply", message.getReplyTo());
63          assertEquals("mp-value", message.getInvocationProperty("message-property"));
64      }
65      
66      public void testMapPayloadWithoutData() throws Exception
67      {
68          Map<?, ?> payload = (Map<?, ?>) getValidTransportMessage();
69          payload.remove(Bayeux.DATA_FIELD);
70          
71          MuleMessageFactory factory = createMuleMessageFactory();
72          try
73          {
74              factory.create(payload, encoding);
75              fail("Creating a MuleMessage from a map without " + Bayeux.DATA_FIELD + " key must fail");
76          }
77          catch (IllegalArgumentException iae)
78          {
79              // this one was expected
80          }
81      }
82      
83      public void testJsonStringPayloadWithoutData() throws Exception
84      {
85          String payload = "{\"value1\" : \"foo\", \"value2\" : \"bar\"}";
86          MuleMessageFactory factory = createMuleMessageFactory();
87          MuleMessage message = factory.create(payload, encoding);
88          assertNotNull(message);
89          assertEquals(payload, message.getPayload());
90          assertTrue(message.getPayload() instanceof String);
91      }
92      
93      public void testJsonStringWithData() throws Exception
94      {
95          String data = JSON_STRING;
96          String payload = String.format("{ \"data\" : %1s, \"%2s\" : \"/replyEndpoint\"}",
97              data, AjaxConnector.REPLYTO_PARAM);
98          
99          MuleMessageFactory factory = createMuleMessageFactory();
100         MuleMessage message = factory.create(payload, encoding);
101         assertNotNull(message);
102         assertEquals(data, message.getPayload());
103         assertEquals("/replyEndpoint", message.getReplyTo());
104     }
105     
106     public void testNonMapNonJsonPayload() throws Exception
107     {
108         FruitBowl payload = new FruitBowl(new Apple(), new Banana());
109         MuleMessageFactory factory = createMuleMessageFactory();
110         MuleMessage message = factory.create(payload, encoding);
111         assertNotNull(message);
112         assertEquals(payload, message.getPayload());
113     }
114 }