1
2
3
4
5
6
7 package org.mule.module.json.transformers;
8
9 import org.mule.api.transformer.Transformer;
10 import org.mule.tck.testmodels.fruit.Apple;
11 import org.mule.tck.testmodels.fruit.Orange;
12 import org.mule.transformer.AbstractTransformerTestCase;
13 import org.mule.transformer.types.DataTypeFactory;
14
15 import static org.junit.Assert.fail;
16
17
18
19
20
21
22
23
24
25 public class JsonBeanRoundTripTestCase extends AbstractTransformerTestCase
26 {
27
28 public static final String JSON_STRING = "{\"apple\":{\"bitten\":true,\"washed\":false},\"orange\":{\"brand\":\"JuicyFruit\",\"segments\":8,\"radius\":3.45,\"listProperties\":null,\"mapProperties\":null,\"arrayProperties\":null}}";
29
30
31 public static final FruitCollection JSON_OBJECT = new FruitCollection(new Apple(true), null, new Orange(8, 3.45, "JuicyFruit"));
32
33 @Override
34 public Transformer getTransformer() throws Exception
35 {
36 ObjectToJson trans = new ObjectToJson();
37 trans.getSerializationMixins().put(FruitCollection.class, FruitCollectionMixin.class);
38 trans.getSerializationMixins().put(Apple.class, AppleMixin.class);
39 trans.getSerializationMixins().put(Orange.class, OrangeMixin.class);
40 trans.setSourceClass(FruitCollection.class);
41 initialiseObject(trans);
42 return trans;
43 }
44
45 @Override
46 public Transformer getRoundTripTransformer() throws Exception
47 {
48 JsonToObject trans = new JsonToObject();
49 trans.setReturnDataType(DataTypeFactory.create(getTestData().getClass()));
50 trans.getDeserializationMixins().put(FruitCollection.class, FruitCollectionMixin.class);
51 trans.getDeserializationMixins().put(Apple.class, AppleMixin.class);
52 trans.getDeserializationMixins().put(Orange.class, OrangeMixin.class);
53 initialiseObject(trans);
54 return trans;
55 }
56
57 @Override
58 public Object getTestData()
59 {
60
61 return JSON_OBJECT;
62 }
63
64 @Override
65 public Object getResultData()
66 {
67
68 return JSON_STRING;
69 }
70
71 @Override
72 public boolean compareResults(Object expected, Object result)
73 {
74
75 if(expected instanceof String)
76 {
77 try
78 {
79 Transformer toObject = getRoundTripTransformer();
80 expected = toObject.transform(expected);
81 result = toObject.transform(result);
82 }
83 catch (Exception e)
84 {
85 fail(e.getMessage());
86 return false;
87 }
88 }
89
90 return super.compareResults(expected, result);
91 }
92 }