View Javadoc
1   /*
2    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
3    * The software in this package is published under the terms of the CPAL v1.0
4    * license, a copy of which has been included with this distribution in the
5    * LICENSE.txt file.
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   * For this test I picked difficult beans in that they are not real beans, so I could test how to use
19   * mixins to decorate the objects
20   * <p/>
21   * FruitCleaner is ignored since there is no concrete implementation to construct
22   * bitten - is ignored because the Orange because there is no setter method.  On the apple I tested using a
23   * constructor
24   */
25  public class JsonBeanRoundTripTestCase extends AbstractTransformerTestCase
26  {
27      //Note that Banana has been excluded
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      //Note that Banana is null
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          //Banana is null
61          return JSON_OBJECT;
62      }
63  
64      @Override
65      public Object getResultData()
66      {
67          //Note that Banana has been excluded
68          return JSON_STRING;
69      }
70  
71      @Override
72      public boolean compareResults(Object expected, Object result)
73      {
74          //MULE-4879 field ordering is not guaranteed by the JVM so we cannot compare result strings
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  }