2121import com .google .gson .reflect .TypeToken ;
2222import com .softlayer .api .service .Entity ;
2323import com .softlayer .api .service .TestEntity ;
24+ import com .softlayer .api .service .TestThing ;
2425
2526public class GsonJsonMarshallerFactoryTest {
2627
@@ -51,21 +52,22 @@ private String toJson(Object obj) throws Exception {
5152 public void testRead () throws Exception {
5253 Entity entity = fromJson (Entity .class ,
5354 "{"
54- + "\" complexType\" : \" SoftLayer_TestEntity\" ,"
55- + "\" bar\" : \" some string\" ,"
56- + "\" foo\" : \" another string\" ,"
57- + "\" baz\" : null,"
58- + "\" date\" : \" 1984-02-25T20:15:25-06:00\" ,"
59- + "\" notApiProperty\" : \" bad value\" ,"
60- + "\" child\" : {"
61- + " \" complexType\" : \" SoftLayer_TestEntity\" ,"
62- + " \" bar\" : \" child string\" "
63- + "},"
64- + "\" moreChildren\" : ["
65- + " { \" complexType\" : \" SoftLayer_TestEntity\" , \" bar\" : \" child 1\" },"
66- + " { \" complexType\" : \" SoftLayer_TestEntity\" , \" bar\" : \" child 2\" }"
67- + "]"
68- + "}" );
55+ + "\" complexType\" : \" SoftLayer_TestEntity\" ,"
56+ + "\" bar\" : \" some string\" ,"
57+ + "\" foo\" : \" another string\" ,"
58+ + "\" baz\" : null,"
59+ + "\" date\" : \" 1984-02-25T20:15:25-06:00\" ,"
60+ + "\" notApiProperty\" : \" bad value\" ,"
61+ + "\" child\" : {"
62+ + " \" complexType\" : \" SoftLayer_TestEntity\" ,"
63+ + " \" bar\" : \" child string\" "
64+ + "},"
65+ + "\" moreChildren\" : ["
66+ + " { \" complexType\" : \" SoftLayer_TestEntity\" , \" bar\" : \" child 1\" },"
67+ + " { \" complexType\" : \" SoftLayer_TestEntity\" , \" bar\" : \" child 2\" }"
68+ + "],"
69+ + "\" testThing\" : {\" complexType\" : \" SoftLayer_TestThing\" , \" id\" : 123}"
70+ + "}" );
6971 assertEquals (TestEntity .class , entity .getClass ());
7072 TestEntity obj = (TestEntity ) entity ;
7173 assertEquals ("some string" , obj .getFoo ());
@@ -85,6 +87,69 @@ public void testRead() throws Exception {
8587 assertEquals (2 , obj .getMoreChildren ().size ());
8688 assertEquals ("child 1" , obj .getMoreChildren ().get (0 ).getFoo ());
8789 assertEquals ("child 2" , obj .getMoreChildren ().get (1 ).getFoo ());
90+ assertEquals (TestThing .class , obj .getTestThing ().getClass ());
91+ assertEquals (123 , obj .getTestThing ().getId ().intValue ());
92+ }
93+
94+ @ Test
95+ public void testReadPropertyWithIncorrectComplexTypeCoercesTheType () throws Exception {
96+ Entity entity = fromJson (Entity .class ,
97+ "{"
98+ + "\" complexType\" : \" SoftLayer_TestEntity\" ,"
99+ + "\" testThing\" : {\" complexType\" : \" SoftLayer_TestEntity\" , \" id\" : 123, \" foo\" : \" unknown!\" }"
100+ + "}" );
101+ assertEquals (TestEntity .class , entity .getClass ());
102+ TestEntity obj = (TestEntity ) entity ;
103+ assertEquals (0 , obj .getUnknownProperties ().size ());
104+ assertEquals (TestThing .class , obj .getTestThing ().getClass ());
105+ assertEquals (123 , obj .getTestThing ().getId ().intValue ());
106+ assertEquals (1 , obj .getTestThing ().getUnknownProperties ().size ());
107+ assertEquals ("unknown!" , obj .getTestThing ().getUnknownProperties ().get ("foo" ));
108+ }
109+
110+
111+ @ Test
112+ public void testReadPropertyWithUnknownComplexTypeCoercesTheType () throws Exception {
113+ Entity entity = fromJson (Entity .class ,
114+ "{"
115+ + "\" complexType\" : \" SoftLayer_TestEntity\" ,"
116+ + "\" testThing\" : {\" complexType\" : \" WhoKnows\" , \" id\" : 123, \" foo\" : \" unknown!\" }"
117+ + "}" );
118+ assertEquals (TestEntity .class , entity .getClass ());
119+ TestEntity obj = (TestEntity ) entity ;
120+ assertEquals (0 , obj .getUnknownProperties ().size ());
121+ assertEquals (TestThing .class , obj .getTestThing ().getClass ());
122+ assertEquals (123 , obj .getTestThing ().getId ().intValue ());
123+ assertEquals (1 , obj .getTestThing ().getUnknownProperties ().size ());
124+ assertEquals ("unknown!" , obj .getTestThing ().getUnknownProperties ().get ("foo" ));
125+ }
126+
127+ @ Test
128+ public void testReadPropertyThrowsExceptionWithoutComplexType () {
129+
130+ Exception e = assertThrows (RuntimeException .class , () -> {
131+ Entity entity = fromJson (Entity .class ,
132+ "{"
133+ + "\" complexType\" : \" SoftLayer_TestEntity\" ,"
134+ + "\" testThing\" : {\" id\" : 123}"
135+ + "}" );
136+ });
137+
138+ assertEquals ("Expected 'complexType' as first property" , e .getMessage ());
139+ }
140+
141+ @ Test
142+ public void testReadPropertyThrowsExceptioWithComplexTypeNotFirst () {
143+
144+ Exception e = assertThrows (RuntimeException .class , () -> {
145+ Entity entity = fromJson (Entity .class ,
146+ "{"
147+ + "\" testThing\" : {\" id\" : 123},"
148+ + "\" complexType\" : \" SoftLayer_TestEntity\" "
149+ + "}" );
150+ });
151+
152+ assertEquals ("Expected 'complexType' as first property" , e .getMessage ());
88153 }
89154
90155 @ Test
0 commit comments