View Javadoc

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