View Javadoc
1   /*
2    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
3    * The software in this package is published under the terms of the CPAL v1.0
4    * license, a copy of which has been included with this distribution in the
5    * LICENSE.txt file.
6    */
7   package org.mule.module.json.transformers;
8   
9   import org.mule.DefaultMuleMessage;
10  import org.mule.api.MuleMessage;
11  import org.mule.api.transformer.DataType;
12  import org.mule.json.model.Item;
13  import org.mule.module.json.JsonData;
14  import org.mule.tck.junit4.AbstractMuleContextTestCase;
15  import org.mule.transformer.types.DataTypeFactory;
16  
17  import org.junit.Test;
18  
19  import static org.junit.Assert.assertEquals;
20  import static org.junit.Assert.assertNotNull;
21  import static org.junit.Assert.assertTrue;
22  
23  public class JsonAutoTransformerTestCase extends AbstractMuleContextTestCase
24  {
25      public static final String ITEM_JSON = "{\"code\":\"1234\",\"description\":\"Vacuum Cleaner\",\"in-stock\":true}";
26  
27      @Test
28      public void testCustomTransform() throws Exception
29      {
30          MuleMessage message = new DefaultMuleMessage(ITEM_JSON, muleContext);
31  
32          Item item = (Item) message.getPayload(DataTypeFactory.create(Item.class));
33          assertNotNull(item);
34          assertEquals("1234", item.getCode());
35          assertEquals("Vacuum Cleaner", item.getDescription());
36          assertTrue(item.isInStock());
37  
38          //and back again
39          message = new DefaultMuleMessage(item, muleContext);
40          String json = message.getPayload(DataType.STRING_DATA_TYPE);
41          assertNotNull(json);
42          assertEquals(ITEM_JSON, json);
43          JsonData data = new JsonData(json);
44          assertEquals("1234", data.getAsString("code"));
45          assertEquals("Vacuum Cleaner", data.getAsString("description"));
46          assertEquals("true", data.getAsString("in-stock"));
47      }
48  }