View Javadoc

1   /*
2    * $Id: JsonAutoTransformerWithMixinsTestCase.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.api.transformer.DataType;
15  import org.mule.module.json.JsonData;
16  import org.mule.tck.AbstractMuleTestCase;
17  import org.mule.tck.testmodels.fruit.Apple;
18  
19  import org.codehaus.jackson.map.ObjectMapper;
20  import org.mule.transformer.types.DataTypeFactory;
21  
22  //TODO: IBEANS-141. No support for Mixin resolution yet
23  public class JsonAutoTransformerWithMixinsTestCase extends AbstractMuleTestCase
24  {
25      public static final String APPLE_JSON = "{\"washed\":false,\"bitten\":true}";
26  
27  
28      @Override
29      protected void doSetUp() throws Exception
30      {
31          //We don't register a custom transformer, instead we register a 'global' mapper that will
32          //be used for Json transforms
33          ObjectMapper mapper = new ObjectMapper();
34          mapper.getSerializationConfig().addMixInAnnotations(Apple.class, AppleMixin.class);
35          mapper.getDeserializationConfig().addMixInAnnotations(Apple.class, AppleMixin.class);
36          muleContext.getRegistry().registerObject("mapper", mapper);
37      }
38  
39      public void testCustomTransform() throws Exception
40      {
41          //Though the data is simple we are testing two things -
42          //1) Mixins are recognised by the Transformer resolver
43          //2) that we successfully marshal and marshal an object that is not annotated directly
44          MuleMessage message = new DefaultMuleMessage(APPLE_JSON, muleContext);
45          Apple apple = (Apple) message.getPayload(DataTypeFactory.create(Apple.class));
46          assertNotNull(apple);
47          assertFalse(apple.isWashed());
48          assertTrue(apple.isBitten());
49  
50          message = new DefaultMuleMessage(apple, muleContext);
51          String json = message.getPayload(DataType.STRING_DATA_TYPE);
52          assertNotNull(json);
53          JsonData data = new JsonData(json);
54          assertEquals("true", data.get("bitten"));
55          assertEquals("false", data.get("washed"));
56      }
57  }