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.module.json.JsonData;
12  import org.mule.tck.junit4.AbstractMuleContextTestCase;
13  import org.mule.tck.testmodels.fruit.Apple;
14  import org.mule.transformer.types.DataTypeFactory;
15  
16  import org.junit.Test;
17  
18  import static org.junit.Assert.assertEquals;
19  import static org.junit.Assert.assertFalse;
20  import static org.junit.Assert.assertNotNull;
21  import static org.junit.Assert.assertTrue;
22  
23  public class JsonCustomTransformerWithMixinsTestCase extends AbstractMuleContextTestCase
24  {
25      public static final String APPLE_JSON = "{\"washed\":false,\"bitten\":true}";
26  
27      @Override
28      protected void doSetUp() throws Exception
29      {
30          muleContext.getRegistry().registerObject("trans", new JsonCustomTransformerWithMixins());
31      }
32  
33      @Test
34      public void testCustomTransform() throws Exception
35      {
36          //THough the data is simple we are testing two things -
37          //1) Mixins are recognised by the Transformer resolver
38          //2) that we successfully marshal and marshal an object that is not annotated directly
39          MuleMessage message=  new DefaultMuleMessage(APPLE_JSON, muleContext);
40  
41          Apple apple = message.getPayload(DataTypeFactory.create(Apple.class));
42          assertNotNull(apple);
43          assertFalse(apple.isWashed());
44          assertTrue(apple.isBitten());
45  
46          message=  new DefaultMuleMessage(apple, muleContext);
47          String json = message.getPayload(DataTypeFactory.STRING);
48          assertNotNull(json);
49          JsonData data = new JsonData(json);
50          assertEquals("true", data.getAsString("bitten"));
51          assertEquals("false", data.getAsString("washed"));
52      }
53  }