/
MapperTest.java
2881 lines (2418 loc) · 78.5 KB
/
MapperTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright 2019 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.cloud.firestore;
import static com.google.cloud.firestore.LocalFirestoreHelper.fromSingleQuotedString;
import static com.google.cloud.firestore.LocalFirestoreHelper.mapAnyType;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertThrows;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import com.google.cloud.Timestamp;
import com.google.cloud.firestore.annotation.DocumentId;
import com.google.cloud.firestore.annotation.Exclude;
import com.google.cloud.firestore.annotation.PropertyName;
import com.google.cloud.firestore.annotation.ThrowOnExtraProperties;
import com.google.cloud.firestore.spi.v1.FirestoreRpc;
import com.google.common.collect.ImmutableList;
import com.google.firestore.v1.DatabaseRootName;
import java.io.Serializable;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import org.junit.Test;
import org.junit.function.ThrowingRunnable;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
import org.mockito.Spy;
import org.mockito.runners.MockitoJUnitRunner;
@SuppressWarnings({"unused", "WeakerAccess", "SpellCheckingInspection"})
@RunWith(MockitoJUnitRunner.class)
public class MapperTest {
@Spy
private FirestoreImpl firestoreMock =
new FirestoreImpl(
FirestoreOptions.newBuilder().setProjectId("test-project").build(),
Mockito.mock(FirestoreRpc.class));
private static final double EPSILON = 0.0003;
private static class StringBean {
private String value;
public String getValue() {
return value;
}
}
private static class DoubleBean {
private double value;
public double getValue() {
return value;
}
}
private static class BigDecimalBean {
private BigDecimal value;
public BigDecimal getValue() {
return value;
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
BigDecimalBean bean = (BigDecimalBean) o;
return Objects.equals(value, bean.value);
}
@Override
public int hashCode() {
return Objects.hash(value);
}
}
private static class FloatBean {
private float value;
public float getValue() {
return value;
}
}
private static class LongBean {
private long value;
public long getValue() {
return value;
}
}
private static class IntBean {
private int value;
public int getValue() {
return value;
}
}
private static class BooleanBean {
private boolean value;
public boolean isValue() {
return value;
}
}
private static class ShortBean {
private short value;
public short getValue() {
return value;
}
}
private static class ByteBean {
private byte value;
public byte getValue() {
return value;
}
}
private static class CharBean {
private char value;
public char getValue() {
return value;
}
}
private static class IntArrayBean {
private int[] values;
public int[] getValues() {
return values;
}
}
private static class StringArrayBean {
private String[] values;
public String[] getValues() {
return values;
}
}
private static class XMLAndURLBean {
private String XMLAndURL1;
public String XMLAndURL2;
public String getXMLAndURL1() {
return XMLAndURL1;
}
public void setXMLAndURL1(String value) {
XMLAndURL1 = value;
}
}
private static class SetterBean {
private String value;
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = "setter:" + value;
}
}
private static class PrivateSetterBean {
public String value;
private void setValue(String value) {
this.value = "setter:" + value;
}
}
private static class GetterBean {
private String value;
public String getValue() {
return "getter:" + this.value;
}
}
@ThrowOnExtraProperties
private static class GetterBeanNoField {
public String getValue() {
return "getter:value";
}
}
private static class GetterPublicFieldBean {
public String value;
public String getValue() {
return "getter:" + value;
}
}
private static class GetterPublicFieldBeanCaseSensitive {
public String valueCase;
public String getValueCASE() {
return "getter:" + valueCase;
}
}
private static class CaseSensitiveGetterBean1 {
private String value;
public String getVALUE() {
return value;
}
}
private static class CaseSensitiveGetterBean2 {
private String value;
public String getvalue() {
return value;
}
}
private static class CaseSensitiveGetterBean3 {
private String value;
public String getVAlue() {
return value;
}
}
private static class CaseSensitiveGetterBean4 {
private String value;
public String getvaLUE() {
return value;
}
}
private static class CaseSensitiveSetterBean1 {
private String value;
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = "setter:" + value;
}
public void setVAlue(String value) {
this.value = "wrong setter!";
}
}
private static class CaseSensitiveSetterBean2 {
private String value;
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = "setter:" + value;
}
public void setvalue(String value) {
this.value = "wrong setter!";
}
}
private static class CaseSensitiveSetterBean3 {
private String value;
public String getValue() {
return value;
}
public void setvalue(String value) {
this.value = "setter:" + value;
}
}
private static class CaseSensitiveSetterBean4 {
private String value;
public String getValue() {
return value;
}
public void setVALUE(String value) {
this.value = "setter:" + value;
}
}
private static class CaseSensitiveSetterBean5 {
private String value;
public String getValue() {
return value;
}
public void SETVALUE(String value) {
this.value = "wrong setter!";
}
}
private static class CaseSensitiveSetterBean6 {
private String value;
public String getValue() {
return value;
}
public void setVaLUE(String value) {
this.value = "setter:" + value;
}
}
@SuppressWarnings("ConstantField")
private static class CaseSensitiveFieldBean1 {
public String VALUE;
}
private static class CaseSensitiveFieldBean2 {
public String value;
}
private static class CaseSensitiveFieldBean3 {
public String Value;
}
private static class CaseSensitiveFieldBean4 {
public String valUE;
}
private static class WrongSetterBean {
private String value;
public String getValue() {
return value;
}
public void setValue() {
this.value = "wrong setter!";
}
public void setValue(String one, String two) {
this.value = "wrong setter!";
}
}
private static class WrongTypeBean {
private Integer value;
public String getValue() {
return "" + value;
}
}
private static class NestedBean {
private StringBean bean;
public StringBean getBean() {
return bean;
}
}
private static class ObjectBean {
private Object value;
public Object getValue() {
return value;
}
}
private static class GenericBean<B> {
private B value;
public B getValue() {
return value;
}
}
private static class DoubleGenericBean<A, B> {
private A valueA;
private B valueB;
public A getValueA() {
return valueA;
}
public B getValueB() {
return valueB;
}
}
private static class ListBean {
private List<String> values;
public List<String> getValues() {
return values;
}
}
private static class SetBean {
private Set<String> values;
public Set<String> getValues() {
return values;
}
}
private static class CollectionBean {
private Collection<String> values;
public Collection<String> getValues() {
return values;
}
}
private static class MapBean {
private Map<String, String> values;
public Map<String, String> getValues() {
return values;
}
}
/**
* This form is not terribly useful in Java, but Kotlin Maps are immutable and are rewritten into
* this form (b/67470108 has more details).
*/
private static class UpperBoundedMapBean {
private Map<String, ? extends Date> values;
public Map<String, ? extends Date> getValues() {
return values;
}
}
private static class MultiBoundedMapBean<T extends Date & Serializable> {
private Map<String, T> values;
public Map<String, T> getValues() {
return values;
}
}
private static class MultiBoundedMapHolderBean {
private MultiBoundedMapBean<Date> map;
public MultiBoundedMapBean<Date> getMap() {
return map;
}
}
private static class UnboundedMapBean {
private Map<String, ?> values;
public Map<String, ?> getValues() {
return values;
}
}
private static class UnboundedTypeVariableMapBean<T> {
private Map<String, T> values;
public Map<String, T> getValues() {
return values;
}
}
private static class UnboundedTypeVariableMapHolderBean {
private UnboundedTypeVariableMapBean<String> map;
public UnboundedTypeVariableMapBean<String> getMap() {
return map;
}
}
private static class NestedListBean {
private List<StringBean> values;
public List<StringBean> getValues() {
return values;
}
}
private static class NestedMapBean {
private Map<String, StringBean> values;
public Map<String, StringBean> getValues() {
return values;
}
}
private static class IllegalKeyMapBean {
private Map<Integer, StringBean> values;
public Map<Integer, StringBean> getValues() {
return values;
}
}
private static class PublicFieldBean {
public String value;
}
@ThrowOnExtraProperties
private static class ThrowOnUnknownPropertiesBean {
public String value;
}
@ThrowOnExtraProperties
private static class PackageFieldBean {
String value;
}
@ThrowOnExtraProperties
@SuppressWarnings("unused") // Unused, but required for the test
private static class PrivateFieldBean {
private String value;
}
private static class PackageGetterBean {
private String packageValue;
private String publicValue;
String getPackageValue() {
return packageValue;
}
public String getPublicValue() {
return publicValue;
}
}
private static class ExcludedBean {
@Exclude public String excludedField = "no-value";
private String excludedGetter = "no-value";
private String includedGetter = "no-value";
@Exclude
public String getExcludedGetter() {
return excludedGetter;
}
public String getIncludedGetter() {
return includedGetter;
}
}
private static class ExcludedSetterBean {
private String value;
public String getValue() {
return value;
}
@Exclude
public void setValue(String value) {
this.value = "wrong setter";
}
}
private static class PropertyNameBean {
@PropertyName("my_key")
public String key;
private String value;
@PropertyName("my_value")
public String getValue() {
return value;
}
@PropertyName("my_value")
public void setValue(String value) {
this.value = value;
}
}
private static class PublicPrivateFieldBean {
public String value1;
String value2;
private String value3;
}
private static class TwoSetterBean {
private String value;
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = "string:" + value;
}
public void setValue(Integer value) {
this.value = "int:" + value;
}
}
private static class TwoGetterBean {
private String value;
public String getValue() {
return value;
}
public String getVALUE() {
return value;
}
}
private static class GetterArgumentsBean {
private String value;
public String getValue1() {
return value + "1";
}
public void getValue2() {}
public String getValue3(boolean flag) {
return value + "3";
}
public String getValue4() {
return value + "4";
}
}
@SuppressWarnings({"ConstantField", "NonAsciiCharacters"})
private static class UnicodeBean {
private String 漢字;
public String get漢字() {
return 漢字;
}
}
private static class PublicConstructorBean {
private String value;
public PublicConstructorBean() {}
public String getValue() {
return value;
}
}
private static class PrivateConstructorBean {
private String value;
private PrivateConstructorBean() {}
public String getValue() {
return value;
}
}
private static class PackageConstructorBean {
private Timestamp value;
PackageConstructorBean() {}
public Timestamp getValue() {
return value;
}
}
private static class ArgConstructorBean {
private String value;
public ArgConstructorBean(String value) {
this.value = value;
}
public String getValue() {
return value;
}
}
private static class MultipleConstructorBean {
private String value;
public MultipleConstructorBean(String value) {
this.value = "wrong-value";
}
public MultipleConstructorBean() {}
public String getValue() {
return value;
}
}
private static class StaticFieldBean {
public static String value1 = "static-value";
public String value2;
}
private static class StaticMethodBean {
private static String value1 = "static-value";
public String value2;
public static String getValue1() {
return StaticMethodBean.value1;
}
public static void setValue1(String value1) {
StaticMethodBean.value1 = value1;
}
}
private enum SimpleEnum {
Foo,
Bar
}
private enum ComplexEnum {
One("one"),
Two("two"),
@PropertyName("Three")
THREE("three");
private final String value;
ComplexEnum(String value) {
this.value = value;
}
public String getValue() {
return value;
}
}
private enum PathologicalEnum {
@PropertyName("Two")
One,
@PropertyName("One")
Two
}
private static class EnumBean {
public SimpleEnum enumField;
private SimpleEnum enumValue;
public ComplexEnum complexEnum;
public ComplexEnum enumUsingPropertyName;
public PathologicalEnum pathologicalEnum;
public SimpleEnum getEnumValue() {
return enumValue;
}
public void setEnumValue(SimpleEnum enumValue) {
this.enumValue = enumValue;
}
}
private static class BaseBean {
// Public field on base class
public String baseValue;
// Value that is accessed through overridden methods in subclasses
public String overrideValue;
// Field that is package private in base class
String packageBaseValue;
// Private field that is used in getter/setter in base class
private String baseMethodValue;
// Private field that has field with same name in subclasses
private String classPrivateValue;
public String getClassPrivateValue() {
return classPrivateValue;
}
public String getBaseMethod() {
return baseMethodValue;
}
public String getPackageBaseValue() {
return packageBaseValue;
}
public void setBaseMethod(String value) {
baseMethodValue = value;
}
}
private static class InheritedBean extends BaseBean {
public String inheritedValue;
private String inheritedMethodValue;
private String classPrivateValue;
@Override
public String getClassPrivateValue() {
return classPrivateValue;
}
public String getInheritedMethod() {
return inheritedMethodValue;
}
public void setInheritedMethod(String value) {
inheritedMethodValue = value;
}
public String getOverrideValue() {
return overrideValue + "-inherited";
}
public void setOverrideValue(String value) {
overrideValue = value + "-inherited";
}
}
private static final class FinalBean extends InheritedBean {
public String finalValue;
private String finalMethodValue;
private String classPrivateValue;
@Override
public String getClassPrivateValue() {
return classPrivateValue;
}
public String getFinalMethod() {
return finalMethodValue;
}
public void setFinalMethod(String value) {
finalMethodValue = value;
}
@Override
public String getOverrideValue() {
return overrideValue + "-final";
}
@Override
public void setOverrideValue(String value) {
overrideValue = value + "-final";
}
}
// Conflicting setters are not supported. When inheriting from a base class we require all
// setters be an override of a base class
private static class ConflictingSetterBean {
public int value;
// package private so override can be public
void setValue(int value) {
this.value = value;
}
}
private static class ConflictingSetterSubBean extends ConflictingSetterBean {
public void setValue(String value) {
this.value = -1;
}
}
private static class ConflictingSetterSubBean2 extends ConflictingSetterBean {
public void setValue(Integer value) {
this.value = -1;
}
}
private static class NonConflictingSetterSubBean extends ConflictingSetterBean {
@Override
public void setValue(int value) {
this.value = value * -1;
}
}
private static class GenericSetterBaseBean<T> {
public T value;
void setValue(T value) {
this.value = value;
}
}
private static class ConflictingGenericSetterSubBean<T> extends GenericSetterBaseBean<T> {
public void setValue(String value) {
// wrong setter
}
}
private static class NonConflictingGenericSetterSubBean extends GenericSetterBaseBean<String> {
@Override
public void setValue(String value) {
this.value = "subsetter:" + value;
}
}
private static <T> T deserialize(String jsonString, Class<T> clazz) {
return deserialize(jsonString, clazz, /*docRef=*/ null);
}
private static <T> T deserialize(Map<String, Object> json, Class<T> clazz) {
return deserialize(json, clazz, /*docRef=*/ null);
}
private static <T> T deserialize(String jsonString, Class<T> clazz, DocumentReference docRef) {
Map<String, Object> json = fromSingleQuotedString(jsonString);
return CustomClassMapper.convertToCustomClass(json, clazz, docRef);
}
private static <T> T deserialize(
Map<String, Object> json, Class<T> clazz, DocumentReference docRef) {
return CustomClassMapper.convertToCustomClass(json, clazz, docRef);
}
private static Object serialize(Object object) {
return CustomClassMapper.convertToPlainJavaTypes(object);
}
private static void assertJson(String expected, Object actual) {
assertEquals(fromSingleQuotedString(expected), actual);
}
private static void assertExceptionContains(String partialMessage, Runnable run) {
try {
run.run();
fail("Expected exception not thrown");
} catch (RuntimeException e) {
assertTrue(e.getMessage().contains(partialMessage));
}
}
private static <T> T convertToCustomClass(
Object object, Class<T> clazz, DocumentReference docRef) {
return CustomClassMapper.convertToCustomClass(object, clazz, docRef);
}
private static <T> T convertToCustomClass(Object object, Class<T> clazz) {
return CustomClassMapper.convertToCustomClass(object, clazz, null);
}
@Test
public void primitiveDeserializeString() {
StringBean bean = deserialize("{'value': 'foo'}", StringBean.class);
assertEquals("foo", bean.value);