View Javadoc

1   /*
2    * $Id: JsonBeanRoundTripTestCase.java 22377 2011-07-11 12:41:42Z 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  
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   * For this test I picked difficult beans in that they are not real beans, so I could test how to use
23   * mixins to decorate the objects
24   * <p/>
25   * FruitCleaner is ignored since there is no concrete implementation to construct
26   * bitten - is ignored because the Orange because there is no setter method.  On the apple I tested using a
27   * constructor
28   */
29  public class JsonBeanRoundTripTestCase extends AbstractTransformerTestCase
30  {
31      //Note that Banana has been excluded
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      //Note that Banana is null
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          //Banana is null
65          return JSON_OBJECT;
66      }
67  
68      @Override
69      public Object getResultData()
70      {
71          //Note that Banana has been excluded
72          return JSON_STRING;
73      }
74  
75      @Override
76      public boolean compareResults(Object expected, Object result)
77      {
78          //MULE-4879 field ordering is not guaranteed by the JVM so we cannot compare result strings
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  }