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