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