View Javadoc

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