2020import static com .google .cloud .firestore .LocalFirestoreHelper .mapAnyType ;
2121import static org .junit .Assert .assertEquals ;
2222import static org .junit .Assert .assertNull ;
23+ import static org .junit .Assert .assertThrows ;
2324import static org .junit .Assert .assertTrue ;
2425import static org .junit .Assert .fail ;
2526
27+ import com .google .cloud .Timestamp ;
2628import com .google .cloud .firestore .annotation .DocumentId ;
2729import com .google .cloud .firestore .annotation .Exclude ;
2830import com .google .cloud .firestore .annotation .PropertyName ;
4345import java .util .Objects ;
4446import java .util .Set ;
4547import org .junit .Test ;
48+ import org .junit .function .ThrowingRunnable ;
4649import org .junit .runner .RunWith ;
4750import org .mockito .Mockito ;
4851import org .mockito .Spy ;
@@ -698,11 +701,11 @@ public String getValue() {
698701 }
699702
700703 private static class PackageConstructorBean {
701- private String value ;
704+ private Timestamp value ;
702705
703706 PackageConstructorBean () {}
704707
705- public String getValue () {
708+ public Timestamp getValue () {
706709 return value ;
707710 }
708711 }
@@ -950,11 +953,20 @@ private static <T> T deserialize(String jsonString, Class<T> clazz) {
950953 return deserialize (jsonString , clazz , /*docRef=*/ null );
951954 }
952955
956+ private static <T > T deserialize (Map <String , Object > json , Class <T > clazz ) {
957+ return deserialize (json , clazz , /*docRef=*/ null );
958+ }
959+
953960 private static <T > T deserialize (String jsonString , Class <T > clazz , DocumentReference docRef ) {
954961 Map <String , Object > json = fromSingleQuotedString (jsonString );
955962 return CustomClassMapper .convertToCustomClass (json , clazz , docRef );
956963 }
957964
965+ private static <T > T deserialize (
966+ Map <String , Object > json , Class <T > clazz , DocumentReference docRef ) {
967+ return CustomClassMapper .convertToCustomClass (json , clazz , docRef );
968+ }
969+
958970 private static Object serialize (Object object ) {
959971 return CustomClassMapper .convertToPlainJavaTypes (object );
960972 }
@@ -1079,23 +1091,35 @@ public void primitiveDeserializeDouble() {
10791091 @ Test
10801092 public void primitiveDeserializeBigDecimal () {
10811093 BigDecimalBean beanBigdecimal = deserialize ("{'value': 123}" , BigDecimalBean .class );
1082- assertEquals (BigDecimal .valueOf (123 ), beanBigdecimal .value );
1094+ assertEquals (BigDecimal .valueOf (123.0 ), beanBigdecimal .value );
10831095
10841096 beanBigdecimal = deserialize ("{'value': '123'}" , BigDecimalBean .class );
10851097 assertEquals (BigDecimal .valueOf (123 ), beanBigdecimal .value );
10861098
10871099 // Int
1088- BigDecimalBean beanInt = deserialize ("{'value': 1}" , BigDecimalBean .class );
1100+ BigDecimalBean beanInt =
1101+ deserialize (Collections .<String , Object >singletonMap ("value" , 1 ), BigDecimalBean .class );
10891102 assertEquals (BigDecimal .valueOf (1 ), beanInt .value );
10901103
10911104 // Long
1092- BigDecimalBean beanLong = deserialize ("{'value': 1234567890123}" , BigDecimalBean .class );
1105+ BigDecimalBean beanLong =
1106+ deserialize (
1107+ Collections .<String , Object >singletonMap ("value" , 1234567890123L ),
1108+ BigDecimalBean .class );
10931109 assertEquals (BigDecimal .valueOf (1234567890123L ), beanLong .value );
10941110
10951111 // Double
1096- BigDecimalBean beanDouble = deserialize ("{'value': 1.1}" , BigDecimalBean .class );
1112+ BigDecimalBean beanDouble =
1113+ deserialize (Collections .<String , Object >singletonMap ("value" , 1.1 ), BigDecimalBean .class );
10971114 assertEquals (BigDecimal .valueOf (1.1 ), beanDouble .value );
10981115
1116+ // BigDecimal
1117+ BigDecimalBean beanBigDecimal =
1118+ deserialize (
1119+ Collections .<String , Object >singletonMap ("value" , BigDecimal .valueOf (1.2 )),
1120+ BigDecimalBean .class );
1121+ assertEquals (BigDecimal .valueOf (1.2 ), beanBigDecimal .value );
1122+
10991123 // Boolean
11001124 try {
11011125 deserialize ("{'value': true}" , BigDecimalBean .class );
@@ -1117,10 +1141,13 @@ public void primitiveDeserializeFloat() {
11171141 assertEquals (1.1 , beanFloat .value , EPSILON );
11181142
11191143 // Int
1120- FloatBean beanInt = deserialize ("{'value': 1}" , FloatBean .class );
1144+ FloatBean beanInt =
1145+ deserialize (Collections .<String , Object >singletonMap ("value" , 1 ), FloatBean .class );
11211146 assertEquals (1 , beanInt .value , EPSILON );
11221147 // Long
1123- FloatBean beanLong = deserialize ("{'value': 1234567890123}" , FloatBean .class );
1148+ FloatBean beanLong =
1149+ deserialize (
1150+ Collections .<String , Object >singletonMap ("value" , 1234567890123L ), FloatBean .class );
11241151 assertEquals ((float ) 1234567890123L , beanLong .value , EPSILON );
11251152
11261153 // Boolean
@@ -1213,15 +1240,18 @@ public void primitiveDeserializeLong() {
12131240
12141241 @ Test
12151242 public void primitiveDeserializeWrongTypeMap () {
1216- assertExceptionContains (
1217- "Failed to convert value of type java.util.LinkedHashMap to String "
1218- + "(found in field 'value')" ,
1219- new Runnable () {
1220- @ Override
1221- public void run () {
1222- deserialize ("{'value': {'foo': 'bar'}}" , StringBean .class );
1223- }
1224- });
1243+ String expectedExceptionMessage =
1244+ ".* Failed to convert value of type .*Map to String \\ (found in field 'value'\\ ).*" ;
1245+ Throwable exception =
1246+ assertThrows (
1247+ RuntimeException .class ,
1248+ new ThrowingRunnable () {
1249+ @ Override
1250+ public void run () throws Throwable {
1251+ deserialize ("{'value': {'foo': 'bar'}}" , StringBean .class );
1252+ }
1253+ });
1254+ assertTrue (exception .getMessage ().matches (expectedExceptionMessage ));
12251255 }
12261256
12271257 @ Test
@@ -1565,14 +1595,16 @@ public void serializeDoubleBean() {
15651595 public void serializeIntBean () {
15661596 IntBean bean = new IntBean ();
15671597 bean .value = 1 ;
1568- assertJson ("{'value': 1}" , serialize (bean ));
1598+ assertJson ("{'value': 1}" , serialize (Collections . singletonMap ( "value" , 1.0 ) ));
15691599 }
15701600
15711601 @ Test
15721602 public void serializeLongBean () {
15731603 LongBean bean = new LongBean ();
15741604 bean .value = 1234567890123L ;
1575- assertJson ("{'value': 1234567890123}" , serialize (bean ));
1605+ assertJson (
1606+ "{'value': 1.234567890123E12}" ,
1607+ serialize (Collections .singletonMap ("value" , 1.234567890123E12 )));
15761608 }
15771609
15781610 @ Test
@@ -2061,8 +2093,12 @@ public void run() {
20612093
20622094 @ Test
20632095 public void packageConstructorCanBeDeserialized () {
2064- PackageConstructorBean bean = deserialize ("{'value': 'foo'}" , PackageConstructorBean .class );
2065- assertEquals ("foo" , bean .value );
2096+ Timestamp timestamp = Timestamp .now ();
2097+ PackageConstructorBean bean =
2098+ deserialize (
2099+ Collections .<String , Object >singletonMap ("value" , timestamp ),
2100+ PackageConstructorBean .class );
2101+ assertEquals (timestamp , bean .value );
20662102 }
20672103
20682104 @ Test
@@ -2245,10 +2281,10 @@ public void serializingGenericBeansSupported() {
22452281 recursiveBean .value .value = "foo" ;
22462282 assertJson ("{'value': {'value': 'foo'}}" , serialize (recursiveBean ));
22472283
2248- DoubleGenericBean <String , Integer > doubleBean = new DoubleGenericBean <>();
2284+ DoubleGenericBean <String , Double > doubleBean = new DoubleGenericBean <>();
22492285 doubleBean .valueA = "foo" ;
2250- doubleBean .valueB = 1 ;
2251- assertJson ("{'valueA ': 'foo' , 'valueB ': 1 }" , serialize (doubleBean ));
2286+ doubleBean .valueB = 1.0 ;
2287+ assertJson ("{'valueB ': 1 , 'valueA ': 'foo' }" , serialize (doubleBean ));
22522288 }
22532289
22542290 @ Test
@@ -2486,7 +2522,7 @@ public void run() {
24862522 public void settersCanOverridePrimitiveSettersSerializing () {
24872523 NonConflictingSetterSubBean bean = new NonConflictingSetterSubBean ();
24882524 bean .value = 1 ;
2489- assertJson ("{'value': 1}" , serialize (bean ));
2525+ assertJson ("{'value': 1}" , serialize (Collections . singletonMap ( "value" , 1.0 ) ));
24902526 }
24912527
24922528 @ Test
@@ -2741,6 +2777,14 @@ public void documentIdsDeserialize() {
27412777
27422778 assertEquals ("doc123" , deserialize ("{}" , DocumentIdOnStringField .class , ref ).docId );
27432779
2780+ assertEquals (
2781+ "doc123" ,
2782+ deserialize (
2783+ Collections .<String , Object >singletonMap ("property" , 100 ),
2784+ DocumentIdOnStringField .class ,
2785+ ref )
2786+ .docId );
2787+
27442788 DocumentIdOnStringFieldAsProperty target =
27452789 deserialize ("{'anotherProperty': 100}" , DocumentIdOnStringFieldAsProperty .class , ref );
27462790 assertEquals ("doc123" , target .docId );
0 commit comments