1
2
3
4
5
6
7 package org.mule.module.json.transformers;
8
9 import org.mule.DefaultMuleMessage;
10 import org.mule.api.MuleMessage;
11 import org.mule.json.model.EmailAddress;
12 import org.mule.json.model.Item;
13 import org.mule.json.model.Person;
14 import org.mule.tck.junit4.AbstractMuleContextTestCase;
15 import org.mule.transformer.types.CollectionDataType;
16 import org.mule.transformer.types.DataTypeFactory;
17 import org.mule.transformer.types.ListDataType;
18 import org.mule.transformer.types.SimpleDataType;
19
20 import java.io.ByteArrayInputStream;
21 import java.util.List;
22
23 import org.junit.Test;
24
25 import static org.junit.Assert.assertEquals;
26 import static org.junit.Assert.assertNotNull;
27
28 public class JsonCustomTransformerTestCase extends AbstractMuleContextTestCase
29 {
30 public static final String PERSON_JSON = "{\"emailAddresses\":[{\"type\":\"home\",\"address\":\"john.doe@gmail.com\"},{\"type\":\"work\",\"address\":\"jdoe@bigco.com\"}],\"name\":\"John Doe\",\"dob\":\"01/01/1970\"}";
31 public static final String EMAIL_JSON = "{\"type\":\"home\",\"address\":\"john.doe@gmail.com\"}";
32 public static final String ITEMS_JSON = "[{\"code\":\"1234\",\"description\":\"Vacuum Cleaner\",\"in-stock\":true},{\"code\":\"1234-1\",\"description\":\"Cleaner Bag\",\"in-stock\":false}]";
33
34 @Override
35 protected void doSetUp() throws Exception
36 {
37 muleContext.getRegistry().registerObject("trans", new JsonCustomTransformer());
38 }
39
40 @Test
41 public void testCustomTransform() throws Exception
42 {
43 MuleMessage message = new DefaultMuleMessage(PERSON_JSON, muleContext);
44
45 Person person = (Person) message.getPayload(DataTypeFactory.create(Person.class));
46 assertNotNull(person);
47 assertEquals("John Doe", person.getName());
48 assertEquals("01/01/1970", person.getDob());
49 assertEquals(2, person.getEmailAddresses().size());
50 assertEquals("home", person.getEmailAddresses().get(0).getType());
51 assertEquals("john.doe@gmail.com", person.getEmailAddresses().get(0).getAddress());
52 assertEquals("work", person.getEmailAddresses().get(1).getType());
53 assertEquals("jdoe@bigco.com", person.getEmailAddresses().get(1).getAddress());
54 }
55
56 @Test
57 public void testCustomTransformWithMuleMessage() throws Exception
58 {
59 ByteArrayInputStream in = new ByteArrayInputStream(EMAIL_JSON.getBytes());
60 DefaultMuleMessage message = new DefaultMuleMessage(in, muleContext);
61 message.setInboundProperty("foo", "fooValue");
62 EmailAddress emailAddress = message.getPayload(new SimpleDataType<EmailAddress>(EmailAddress.class));
63 assertNotNull(emailAddress);
64 assertEquals("home", emailAddress.getType());
65 assertEquals("john.doe@gmail.com", emailAddress.getAddress());
66 }
67
68 @Test
69 public void testCustomListTransform() throws Exception
70 {
71 MuleMessage message = new DefaultMuleMessage(ITEMS_JSON, muleContext);
72 List<Item> items = message.getPayload(new CollectionDataType<List<Item>>(List.class, Item.class));
73 assertNotNull(items);
74 assertEquals("1234", items.get(0).getCode());
75 assertEquals("Vacuum Cleaner", items.get(0).getDescription());
76 assertEquals("1234-1", items.get(1).getCode());
77 assertEquals("Cleaner Bag", items.get(1).getDescription());
78
79
80
81 String people_json = "[" + PERSON_JSON + "," + PERSON_JSON + "]";
82 message = new DefaultMuleMessage(people_json, muleContext);
83
84 List<Person> people = message.getPayload(new CollectionDataType<List<Person>>(List.class, Person.class));
85 assertNotNull(people);
86 assertEquals(2, people.size());
87 }
88
89 @Test
90 public void testDifferentListTransformer() throws Exception
91 {
92
93
94 String people_json = "[" + PERSON_JSON + "," + PERSON_JSON + "]";
95 MuleMessage message = new DefaultMuleMessage(people_json, muleContext);
96
97 List<Person> people = message.getPayload(new ListDataType<List<Person>>(Person.class));
98 assertNotNull(people);
99 assertEquals(2, people.size());
100 }
101 }