View Javadoc

1   /*
2    * $Id: JsonAutoTransformerTestCase.java 22391 2011-07-12 12:00:48Z dirk.olmes $
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.api.transformer.DataType;
15  import org.mule.json.model.Item;
16  import org.mule.module.json.JsonData;
17  import org.mule.tck.junit4.AbstractMuleContextTestCase;
18  import org.mule.transformer.types.DataTypeFactory;
19  
20  import org.junit.Test;
21  
22  import static org.junit.Assert.assertEquals;
23  import static org.junit.Assert.assertNotNull;
24  import static org.junit.Assert.assertTrue;
25  
26  public class JsonAutoTransformerTestCase extends AbstractMuleContextTestCase
27  {
28      public static final String ITEM_JSON = "{\"code\":\"1234\",\"description\":\"Vacuum Cleaner\",\"in-stock\":true}";
29  
30      @Test
31      public void testCustomTransform() throws Exception
32      {
33          MuleMessage message = new DefaultMuleMessage(ITEM_JSON, muleContext);
34  
35          Item item = message.getPayload(DataTypeFactory.create(Item.class));
36          assertNotNull(item);
37          assertEquals("1234", item.getCode());
38          assertEquals("Vacuum Cleaner", item.getDescription());
39          assertTrue(item.isInStock());
40  
41          //and back again
42          message = new DefaultMuleMessage(item, muleContext);
43          String json = message.getPayload(DataType.STRING_DATA_TYPE);
44          assertNotNull(json);
45          assertEquals(ITEM_JSON, json);
46          JsonData data = new JsonData(json);
47          assertEquals("1234", data.getAsString("code"));
48          assertEquals("Vacuum Cleaner", data.getAsString("description"));
49          assertEquals("true", data.getAsString("in-stock"));
50      }
51  }