1
2
3
4
5
6
7
8
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
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
37
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
48
49
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 }