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