View Javadoc

1   /*
2    * $Id: AjaxMuleMessageFactoryTestCase.java 22447 2011-07-19 06:52:00Z dirk.olmes $
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  import org.junit.Test;
26  
27  import static org.junit.Assert.assertEquals;
28  import static org.junit.Assert.assertNotNull;
29  import static org.junit.Assert.assertTrue;
30  
31  public class AjaxMuleMessageFactoryTestCase extends AbstractMuleMessageFactoryTestCase
32  {
33      private static final String JSON_STRING = "{\"value1\":\"foo\",\"value2\":\"bar\"}";
34  
35      public AjaxMuleMessageFactoryTestCase()
36      {
37          super();
38          runUnsuppoprtedTransportMessageTest = false;
39      }
40      
41      @Override
42      protected MuleMessageFactory doCreateMuleMessageFactory()
43      {
44          return new AjaxMuleMessageFactory(muleContext);
45      }
46  
47      @Override
48      protected Object getValidTransportMessage() throws Exception
49      {
50          Map<String, Object> map = new HashMap<String, Object>();
51          map.put(Bayeux.DATA_FIELD, JSON_STRING);
52          map.put(AjaxConnector.REPLYTO_PARAM, "/reply");
53          map.put("message-property", "mp-value");
54          
55          return map;
56      }
57  
58      @Override
59      public void testValidPayload() throws Exception
60      {
61          MuleMessageFactory factory = createMuleMessageFactory();
62          
63          Object payload = getValidTransportMessage();
64          MuleMessage message = factory.create(payload, encoding);
65          assertNotNull(message);
66          assertEquals(JSON_STRING, message.getPayload());
67          assertEquals("/reply", message.getReplyTo());
68          assertEquals("mp-value", message.getInvocationProperty("message-property"));
69      }
70      
71      @Test(expected=IllegalArgumentException.class)
72      public void testMapPayloadWithoutData() throws Exception
73      {
74          Map<?, ?> payload = (Map<?, ?>) getValidTransportMessage();
75          payload.remove(Bayeux.DATA_FIELD);
76          
77          MuleMessageFactory factory = createMuleMessageFactory();
78          factory.create(payload, encoding);
79      }
80      
81      @Test
82      public void testJsonStringPayloadWithoutData() throws Exception
83      {
84          String payload = "{\"value1\" : \"foo\", \"value2\" : \"bar\"}";
85          MuleMessageFactory factory = createMuleMessageFactory();
86          MuleMessage message = factory.create(payload, encoding);
87          assertNotNull(message);
88          assertEquals(payload, message.getPayload());
89          assertTrue(message.getPayload() instanceof String);
90      }
91      
92      @Test
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     @Test
107     public void testNonMapNonJsonPayload() throws Exception
108     {
109         FruitBowl payload = new FruitBowl(new Apple(), new Banana());
110         MuleMessageFactory factory = createMuleMessageFactory();
111         MuleMessage message = factory.create(payload, encoding);
112         assertNotNull(message);
113         assertEquals(payload, message.getPayload());
114     }
115 }