View Javadoc

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