View Javadoc

1   /*
2    * $Id: JsonCustomTransformerTestCase.java 343 2010-05-05 05:43:44Z ross $
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  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.AbstractMuleTestCase;
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  public class JsonCustomTransformerTestCase extends AbstractMuleTestCase
27  {
28      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\"}";
29      public static final String EMAIL_JSON = "{\"type\":\"home\",\"address\":\"john.doe@gmail.com\"}";
30      public static final String ITEMS_JSON = "[{\"code\":\"1234\",\"description\":\"Vacuum Cleaner\",\"in-stock\":true},{\"code\":\"1234-1\",\"description\":\"Cleaner Bag\",\"in-stock\":false}]";
31  
32      @Override
33      protected void doSetUp() throws Exception
34      {
35          muleContext.getRegistry().registerObject("trans", new JsonCustomTransformer());
36      }
37  
38      public void testCustomTransform() throws Exception
39      {
40          MuleMessage message = new DefaultMuleMessage(PERSON_JSON, muleContext);
41  
42          Person person = (Person) message.getPayload(DataTypeFactory.create(Person.class));
43          assertNotNull(person);
44          assertEquals("John Doe", person.getName());
45          assertEquals("01/01/1970", person.getDob());
46          assertEquals(2, person.getEmailAddresses().size());
47          assertEquals("home", person.getEmailAddresses().get(0).getType());
48          assertEquals("john.doe@gmail.com", person.getEmailAddresses().get(0).getAddress());
49          assertEquals("work", person.getEmailAddresses().get(1).getType());
50          assertEquals("jdoe@bigco.com", person.getEmailAddresses().get(1).getAddress());
51      }
52  
53      public void testCustomTransformWithMuleMessage() throws Exception
54      {
55          ByteArrayInputStream in = new ByteArrayInputStream(EMAIL_JSON.getBytes());
56          DefaultMuleMessage message = new DefaultMuleMessage(in, muleContext);
57          message.setInboundProperty("foo", "fooValue");
58          EmailAddress emailAddress = message.getPayload(new SimpleDataType<EmailAddress>(EmailAddress.class));
59          assertNotNull(emailAddress);
60          assertEquals("home", emailAddress.getType());
61          assertEquals("john.doe@gmail.com", emailAddress.getAddress());
62      }
63  
64      public void testCustomListTransform() throws Exception
65      {
66          MuleMessage message = new DefaultMuleMessage(ITEMS_JSON, muleContext);
67          List<Item> items = message.getPayload(new CollectionDataType<List<Item>>(List.class, Item.class));
68          assertNotNull(items);
69          assertEquals("1234", items.get(0).getCode());
70          assertEquals("Vacuum Cleaner", items.get(0).getDescription());
71          assertEquals("1234-1", items.get(1).getCode());
72          assertEquals("Cleaner Bag", items.get(1).getDescription());
73  
74          //Call this transformer here to test that the cached transformer from the previous invocation does not interfer with
75          //Finding the List<Person> transformer
76          String people_json = "[" + PERSON_JSON + "," + PERSON_JSON + "]";
77          message = new DefaultMuleMessage(people_json, muleContext);
78  
79          List<Person> people = message.getPayload(new CollectionDataType<List<Person>>(List.class, Person.class));
80          assertNotNull(people);
81          assertEquals(2, people.size());
82      }
83  
84      public void testDifferentListTransformer() throws Exception
85      {
86          //Test that we can resolve other collections
87  
88          String people_json = "[" + PERSON_JSON + "," + PERSON_JSON + "]";
89          MuleMessage message = new DefaultMuleMessage(people_json, muleContext);
90  
91          List<Person> people = message.getPayload(new ListDataType<List<Person>>(Person.class));
92          assertNotNull(people);
93          assertEquals(2, people.size());
94      }
95  }