-
Notifications
You must be signed in to change notification settings - Fork 79
/
JsonToProtoMessageTest.java
1390 lines (1319 loc) · 54.2 KB
/
JsonToProtoMessageTest.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 2020 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
*
* https://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.bigquery.storage.v1;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import com.google.cloud.bigquery.storage.test.JsonTest.*;
import com.google.cloud.bigquery.storage.test.SchemaTest.*;
import com.google.common.collect.ImmutableMap;
import com.google.protobuf.ByteString;
import com.google.protobuf.Descriptors.Descriptor;
import com.google.protobuf.DynamicMessage;
import com.google.protobuf.Message;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Map;
import java.util.logging.Logger;
import org.json.JSONArray;
import org.json.JSONObject;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
import org.threeten.bp.LocalTime;
@RunWith(JUnit4.class)
public class JsonToProtoMessageTest {
private static final Logger LOG = Logger.getLogger(JsonToProtoMessageTest.class.getName());
private static ImmutableMap<Descriptor, String> AllTypesToDebugMessageTest =
new ImmutableMap.Builder<Descriptor, String>()
.put(BoolType.getDescriptor(), "boolean")
.put(BytesType.getDescriptor(), "bytes")
.put(Int64Type.getDescriptor(), "int64")
.put(Int32Type.getDescriptor(), "int32")
.put(DoubleType.getDescriptor(), "double")
.put(StringType.getDescriptor(), "string")
.put(RepeatedType.getDescriptor(), "array")
.put(ObjectType.getDescriptor(), "object")
.build();
private static ImmutableMap<Descriptor, Message[]> AllTypesToCorrectProto =
new ImmutableMap.Builder<Descriptor, Message[]>()
.put(
BoolType.getDescriptor(),
new Message[] {BoolType.newBuilder().setTestFieldType(true).build()})
.put(
BytesType.getDescriptor(),
new Message[] {
BytesType.newBuilder().setTestFieldType(ByteString.copyFromUtf8("test")).build(),
BytesType.newBuilder()
.setTestFieldType(ByteString.copyFrom(new byte[] {1, 2, 3}))
.build()
})
.put(
Int64Type.getDescriptor(),
new Message[] {
Int64Type.newBuilder().setTestFieldType(Long.MAX_VALUE).build(),
Int64Type.newBuilder().setTestFieldType(new Long(Integer.MAX_VALUE)).build()
})
.put(
Int32Type.getDescriptor(),
new Message[] {Int32Type.newBuilder().setTestFieldType(Integer.MAX_VALUE).build()})
.put(
DoubleType.getDescriptor(),
new Message[] {
DoubleType.newBuilder().setTestFieldType(Long.MAX_VALUE).build(),
DoubleType.newBuilder().setTestFieldType(Integer.MAX_VALUE).build(),
DoubleType.newBuilder().setTestFieldType(1.23).build()
})
.put(
StringType.getDescriptor(),
new Message[] {StringType.newBuilder().setTestFieldType("test").build()})
.put(
RepeatedType.getDescriptor(),
new Message[] {
RepeatedType.newBuilder()
.addAllTestFieldType(
new ArrayList<Long>() {
{
add(1L);
add(2L);
add(3L);
}
})
.build()
})
.put(
ObjectType.getDescriptor(),
new Message[] {
ObjectType.newBuilder()
.setTestFieldType(ComplexLvl2.newBuilder().setTestInt(1).build())
.build()
})
.build();
private static ImmutableMap<Descriptor, String> AllRepeatedTypesToDebugMessageTest =
new ImmutableMap.Builder<Descriptor, String>()
.put(RepeatedBool.getDescriptor(), "boolean")
.put(RepeatedBytes.getDescriptor(), "bytes")
.put(RepeatedInt64.getDescriptor(), "int64")
.put(RepeatedInt32.getDescriptor(), "int32")
.put(RepeatedDouble.getDescriptor(), "double")
.put(RepeatedString.getDescriptor(), "string")
.put(RepeatedObject.getDescriptor(), "object")
.build();
private static ImmutableMap<Descriptor, Message[]> AllRepeatedTypesToCorrectProto =
new ImmutableMap.Builder<Descriptor, Message[]>()
.put(
RepeatedBool.getDescriptor(),
new Message[] {
RepeatedBool.newBuilder().addTestRepeated(true).addTestRepeated(false).build()
})
.put(
RepeatedBytes.getDescriptor(),
new Message[] {
RepeatedBytes.newBuilder()
.addTestRepeated(ByteString.copyFrom(new byte[] {0}))
.addTestRepeated(ByteString.copyFrom(new byte[] {0, -116, -122, 71}))
.build(),
RepeatedBytes.newBuilder()
.addTestRepeated(
BigDecimalByteStringEncoder.encodeToNumericByteString(new BigDecimal("0")))
.addTestRepeated(
BigDecimalByteStringEncoder.encodeToNumericByteString(
new BigDecimal("1.2")))
.build()
})
.put(
RepeatedString.getDescriptor(),
new Message[] {
RepeatedString.newBuilder().addTestRepeated("hello").addTestRepeated("test").build()
})
.put(
RepeatedInt64.getDescriptor(),
new Message[] {
RepeatedInt64.newBuilder()
.addTestRepeated(Long.MAX_VALUE)
.addTestRepeated(Long.MIN_VALUE)
.addTestRepeated(Integer.MAX_VALUE)
.addTestRepeated(Integer.MIN_VALUE)
.addTestRepeated(Short.MAX_VALUE)
.addTestRepeated(Short.MIN_VALUE)
.addTestRepeated(Byte.MAX_VALUE)
.addTestRepeated(Byte.MIN_VALUE)
.addTestRepeated(0)
.build(),
RepeatedInt64.newBuilder()
.addTestRepeated(Integer.MAX_VALUE)
.addTestRepeated(Integer.MIN_VALUE)
.addTestRepeated(Short.MAX_VALUE)
.addTestRepeated(Short.MIN_VALUE)
.addTestRepeated(Byte.MAX_VALUE)
.addTestRepeated(Byte.MIN_VALUE)
.addTestRepeated(0)
.build()
})
.put(
RepeatedInt32.getDescriptor(),
new Message[] {
RepeatedInt32.newBuilder()
.addTestRepeated(Integer.MAX_VALUE)
.addTestRepeated(Integer.MIN_VALUE)
.addTestRepeated(Short.MAX_VALUE)
.addTestRepeated(Short.MIN_VALUE)
.addTestRepeated(Byte.MAX_VALUE)
.addTestRepeated(Byte.MIN_VALUE)
.addTestRepeated(0)
.build()
})
.put(
RepeatedDouble.getDescriptor(),
new Message[] {
RepeatedDouble.newBuilder()
.addTestRepeated(Long.MAX_VALUE)
.addTestRepeated(Long.MIN_VALUE)
.addTestRepeated(Integer.MAX_VALUE)
.addTestRepeated(Integer.MIN_VALUE)
.addTestRepeated(Short.MAX_VALUE)
.addTestRepeated(Short.MIN_VALUE)
.addTestRepeated(Byte.MAX_VALUE)
.addTestRepeated(Byte.MIN_VALUE)
.addTestRepeated(0)
.build(),
RepeatedDouble.newBuilder()
.addTestRepeated(Integer.MAX_VALUE)
.addTestRepeated(Integer.MIN_VALUE)
.addTestRepeated(Short.MAX_VALUE)
.addTestRepeated(Short.MIN_VALUE)
.addTestRepeated(Byte.MAX_VALUE)
.addTestRepeated(Byte.MIN_VALUE)
.addTestRepeated(0)
.build(),
RepeatedDouble.newBuilder()
.addTestRepeated(Double.MAX_VALUE)
.addTestRepeated(Double.MIN_VALUE)
.addTestRepeated(Float.MAX_VALUE)
.addTestRepeated(Float.MIN_VALUE)
.build(),
RepeatedDouble.newBuilder()
.addTestRepeated(Float.MAX_VALUE)
.addTestRepeated(Float.MIN_VALUE)
.build()
})
.put(
RepeatedObject.getDescriptor(),
new Message[] {
RepeatedObject.newBuilder()
.addTestRepeated(ComplexLvl2.newBuilder().setTestInt(1).build())
.addTestRepeated(ComplexLvl2.newBuilder().setTestInt(2).build())
.addTestRepeated(ComplexLvl2.newBuilder().setTestInt(3).build())
.build()
})
.build();
private static JSONObject[] simpleJSONObjects = {
new JSONObject().put("test_field_type", Long.MAX_VALUE),
new JSONObject().put("test_field_type", Integer.MAX_VALUE),
new JSONObject().put("test_field_type", 1.23),
new JSONObject().put("test_field_type", true),
new JSONObject().put("test_field_type", ByteString.copyFromUtf8("test")),
new JSONObject().put("test_field_type", new JSONArray("[1, 2, 3]")),
new JSONObject().put("test_field_type", new JSONObject().put("test_int", 1)),
new JSONObject().put("test_field_type", "test")
};
private static JSONObject[] simpleJSONArrays = {
new JSONObject()
.put(
"test_repeated",
new JSONArray(
new Long[] {
Long.MAX_VALUE,
Long.MIN_VALUE,
(long) Integer.MAX_VALUE,
(long) Integer.MIN_VALUE,
(long) Short.MAX_VALUE,
(long) Short.MIN_VALUE,
(long) Byte.MAX_VALUE,
(long) Byte.MIN_VALUE,
0L
})),
new JSONObject()
.put(
"test_repeated",
new JSONArray(
new Integer[] {
Integer.MAX_VALUE,
Integer.MIN_VALUE,
(int) Short.MAX_VALUE,
(int) Short.MIN_VALUE,
(int) Byte.MAX_VALUE,
(int) Byte.MIN_VALUE,
0
})),
new JSONObject()
.put(
"test_repeated",
new JSONArray(
new Double[] {
Double.MAX_VALUE,
Double.MIN_VALUE,
(double) Float.MAX_VALUE,
(double) Float.MIN_VALUE
})),
new JSONObject()
.put("test_repeated", new JSONArray(new Float[] {Float.MAX_VALUE, Float.MIN_VALUE})),
new JSONObject().put("test_repeated", new JSONArray(new Boolean[] {true, false})),
new JSONObject().put("test_repeated", new JSONArray(new String[] {"hello", "test"})),
new JSONObject()
.put(
"test_repeated",
new JSONArray(
new byte[][] {
BigDecimalByteStringEncoder.encodeToNumericByteString(new BigDecimal("0"))
.toByteArray(),
BigDecimalByteStringEncoder.encodeToNumericByteString(new BigDecimal("1.2"))
.toByteArray()
})),
new JSONObject().put("test_repeated", new JSONArray(new int[][] {{11111, 22222}})),
new JSONObject().put("test_repeated", new JSONArray(new char[][] {{'a', 'b'}, {'c'}})),
new JSONObject().put("test_repeated", new JSONArray(new String[][] {{"hello"}, {"test"}})),
new JSONObject()
.put(
"test_repeated",
new JSONArray(
new JSONObject[] {
new JSONObject().put("test_int", 1),
new JSONObject().put("test_int", 2),
new JSONObject().put("test_int", 3)
}))
};
private final TableFieldSchema TEST_INT =
TableFieldSchema.newBuilder()
.setType(TableFieldSchema.Type.INT64)
.setMode(TableFieldSchema.Mode.NULLABLE)
.setName("test_int")
.build();
private final TableFieldSchema TEST_STRING =
TableFieldSchema.newBuilder()
.setType(TableFieldSchema.Type.STRING)
.setMode(TableFieldSchema.Mode.REPEATED)
.setName("test_string")
.build();
private final TableFieldSchema TEST_BYTES =
TableFieldSchema.newBuilder()
.setType(TableFieldSchema.Type.BYTES)
.setMode(TableFieldSchema.Mode.REQUIRED)
.setName("test_bytes")
.build();
private final TableFieldSchema TEST_BOOL =
TableFieldSchema.newBuilder()
.setType(TableFieldSchema.Type.BOOL)
.setMode(TableFieldSchema.Mode.NULLABLE)
.setName("test_bool")
.build();
private final TableFieldSchema TEST_DOUBLE =
TableFieldSchema.newBuilder()
.setType(TableFieldSchema.Type.DOUBLE)
.setMode(TableFieldSchema.Mode.REPEATED)
.setName("test_double")
.build();
private final TableFieldSchema TEST_DATE =
TableFieldSchema.newBuilder()
.setType(TableFieldSchema.Type.DATE)
.setMode(TableFieldSchema.Mode.REQUIRED)
.setName("test_date")
.build();
private final TableFieldSchema TEST_DATETIME =
TableFieldSchema.newBuilder()
.setType(TableFieldSchema.Type.DATETIME)
.setMode(TableFieldSchema.Mode.NULLABLE)
.setName("test_datetime")
.build();
private final TableFieldSchema TEST_DATETIME_STR =
TableFieldSchema.newBuilder()
.setType(TableFieldSchema.Type.DATETIME)
.setMode(TableFieldSchema.Mode.REPEATED)
.setName("test_datetime_str")
.build();
private final TableFieldSchema COMPLEXLVL2 =
TableFieldSchema.newBuilder()
.setType(TableFieldSchema.Type.STRUCT)
.setMode(TableFieldSchema.Mode.REQUIRED)
.addFields(0, TEST_INT)
.setName("complex_lvl2")
.build();
private final TableFieldSchema COMPLEXLVL1 =
TableFieldSchema.newBuilder()
.setType(TableFieldSchema.Type.STRUCT)
.setMode(TableFieldSchema.Mode.REQUIRED)
.addFields(0, TEST_INT)
.addFields(1, COMPLEXLVL2)
.setName("complex_lvl1")
.build();
private final TableFieldSchema TEST_NUMERIC =
TableFieldSchema.newBuilder()
.setType(TableFieldSchema.Type.NUMERIC)
.setMode(TableFieldSchema.Mode.NULLABLE)
.setName("test_numeric")
.build();
private final TableFieldSchema TEST_NUMERIC_REPEATED =
TableFieldSchema.newBuilder()
.setType(TableFieldSchema.Type.NUMERIC)
.setMode(TableFieldSchema.Mode.REPEATED)
.setName("test_numeric_repeated")
.build();
private final TableFieldSchema TEST_GEO =
TableFieldSchema.newBuilder()
.setType(TableFieldSchema.Type.GEOGRAPHY)
.setMode(TableFieldSchema.Mode.NULLABLE)
.setName("test_geo")
.build();
private final TableFieldSchema TEST_TIMESTAMP =
TableFieldSchema.newBuilder()
.setType(TableFieldSchema.Type.TIMESTAMP)
.setMode(TableFieldSchema.Mode.NULLABLE)
.setName("test_timestamp")
.build();
private final TableFieldSchema TEST_TIME =
TableFieldSchema.newBuilder()
.setType(TableFieldSchema.Type.TIME)
.setMode(TableFieldSchema.Mode.NULLABLE)
.setName("test_time")
.build();
private final TableFieldSchema TEST_TIME_STR =
TableFieldSchema.newBuilder()
.setType(TableFieldSchema.Type.TIME)
.setMode(TableFieldSchema.Mode.NULLABLE)
.setName("test_time_str")
.build();
private final TableFieldSchema TEST_NUMERIC_STR =
TableFieldSchema.newBuilder()
.setType(TableFieldSchema.Type.NUMERIC)
.setMode(TableFieldSchema.Mode.NULLABLE)
.setName("test_numeric_str")
.build();
private final TableFieldSchema TEST_NUMERIC_SHORT =
TableFieldSchema.newBuilder()
.setType(TableFieldSchema.Type.NUMERIC)
.setMode(TableFieldSchema.Mode.NULLABLE)
.setName("test_numeric_short")
.build();
private final TableFieldSchema TEST_NUMERIC_INT =
TableFieldSchema.newBuilder()
.setType(TableFieldSchema.Type.NUMERIC)
.setMode(TableFieldSchema.Mode.NULLABLE)
.setName("test_numeric_int")
.build();
private final TableFieldSchema TEST_NUMERIC_LONG =
TableFieldSchema.newBuilder()
.setType(TableFieldSchema.Type.NUMERIC)
.setMode(TableFieldSchema.Mode.NULLABLE)
.setName("test_numeric_long")
.build();
private final TableFieldSchema TEST_NUMERIC_FLOAT =
TableFieldSchema.newBuilder()
.setType(TableFieldSchema.Type.NUMERIC)
.setMode(TableFieldSchema.Mode.NULLABLE)
.setName("test_numeric_float")
.build();
private final TableFieldSchema TEST_NUMERIC_DOUBLE =
TableFieldSchema.newBuilder()
.setType(TableFieldSchema.Type.NUMERIC)
.setMode(TableFieldSchema.Mode.NULLABLE)
.setName("test_numeric_double")
.build();
private final TableFieldSchema TEST_BIGNUMERIC =
TableFieldSchema.newBuilder()
.setType(TableFieldSchema.Type.BIGNUMERIC)
.setMode(TableFieldSchema.Mode.NULLABLE)
.setName("test_bignumeric")
.build();
private final TableFieldSchema TEST_BIGNUMERIC_STR =
TableFieldSchema.newBuilder()
.setType(TableFieldSchema.Type.BIGNUMERIC)
.setMode(TableFieldSchema.Mode.REPEATED)
.setName("test_bignumeric_str")
.build();
private final TableFieldSchema TEST_BIGNUMERIC_SHORT =
TableFieldSchema.newBuilder()
.setType(TableFieldSchema.Type.BIGNUMERIC)
.setMode(TableFieldSchema.Mode.NULLABLE)
.setName("test_bignumeric_short")
.build();
private final TableFieldSchema TEST_BIGNUMERIC_INT =
TableFieldSchema.newBuilder()
.setType(TableFieldSchema.Type.BIGNUMERIC)
.setMode(TableFieldSchema.Mode.NULLABLE)
.setName("test_bignumeric_int")
.build();
private final TableFieldSchema TEST_BIGNUMERIC_LONG =
TableFieldSchema.newBuilder()
.setType(TableFieldSchema.Type.BIGNUMERIC)
.setMode(TableFieldSchema.Mode.NULLABLE)
.setName("test_bignumeric_long")
.build();
private final TableFieldSchema TEST_BIGNUMERIC_FLOAT =
TableFieldSchema.newBuilder()
.setType(TableFieldSchema.Type.BIGNUMERIC)
.setMode(TableFieldSchema.Mode.NULLABLE)
.setName("test_bignumeric_float")
.build();
private final TableFieldSchema TEST_BIGNUMERIC_DOUBLE =
TableFieldSchema.newBuilder()
.setType(TableFieldSchema.Type.BIGNUMERIC)
.setMode(TableFieldSchema.Mode.NULLABLE)
.setName("test_bignumeric_double")
.build();
final TableFieldSchema TEST_INTERVAL =
TableFieldSchema.newBuilder()
.setType(TableFieldSchema.Type.INTERVAL)
.setMode(TableFieldSchema.Mode.NULLABLE)
.setName("test_interval")
.build();
final TableFieldSchema TEST_JSON =
TableFieldSchema.newBuilder()
.setType(TableFieldSchema.Type.JSON)
.setMode(TableFieldSchema.Mode.REPEATED)
.setName("test_json")
.build();
private final TableSchema COMPLEX_TABLE_SCHEMA =
TableSchema.newBuilder()
.addFields(0, TEST_INT)
.addFields(1, TEST_STRING)
.addFields(2, TEST_BYTES)
.addFields(3, TEST_BOOL)
.addFields(4, TEST_DOUBLE)
.addFields(5, TEST_DATE)
.addFields(6, TEST_DATETIME)
.addFields(7, TEST_DATETIME_STR)
.addFields(8, COMPLEXLVL1)
.addFields(9, COMPLEXLVL2)
.addFields(10, TEST_NUMERIC)
.addFields(11, TEST_GEO)
.addFields(12, TEST_TIMESTAMP)
.addFields(13, TEST_TIME)
.addFields(14, TEST_TIME_STR)
.addFields(15, TEST_NUMERIC_REPEATED)
.addFields(16, TEST_NUMERIC_STR)
.addFields(17, TEST_NUMERIC_SHORT)
.addFields(18, TEST_NUMERIC_INT)
.addFields(19, TEST_NUMERIC_LONG)
.addFields(20, TEST_NUMERIC_FLOAT)
.addFields(21, TEST_NUMERIC_DOUBLE)
.addFields(22, TEST_BIGNUMERIC)
.addFields(23, TEST_BIGNUMERIC_STR)
.addFields(24, TEST_BIGNUMERIC_SHORT)
.addFields(25, TEST_BIGNUMERIC_INT)
.addFields(26, TEST_BIGNUMERIC_LONG)
.addFields(27, TEST_BIGNUMERIC_FLOAT)
.addFields(28, TEST_BIGNUMERIC_DOUBLE)
.addFields(29, TEST_INTERVAL)
.addFields(30, TEST_JSON)
.build();
@Test
public void testDifferentNameCasing() throws Exception {
TestInt64 expectedProto =
TestInt64.newBuilder().setByte(1).setShort(1).setInt(1).setLong(1).build();
JSONObject json = new JSONObject();
json.put("bYtE", (byte) 1);
json.put("SHORT", (short) 1);
json.put("inT", 1);
json.put("lONg", 1L);
DynamicMessage protoMsg =
JsonToProtoMessage.convertJsonToProtoMessage(TestInt64.getDescriptor(), json);
assertEquals(expectedProto, protoMsg);
}
@Test
public void testBool() throws Exception {
TestBool expectedProto =
TestBool.newBuilder().setBool(true).setUppercase(true).setLowercase(false).build();
JSONObject json = new JSONObject();
json.put("bool", true);
json.put("uppercase", "TRUE");
json.put("lowercase", "false");
DynamicMessage protoMsg =
JsonToProtoMessage.convertJsonToProtoMessage(TestBool.getDescriptor(), json);
assertEquals(expectedProto, protoMsg);
}
@Test
public void testInt64() throws Exception {
TestInt64 expectedProto =
TestInt64.newBuilder().setByte(1).setShort(1).setInt(1).setLong(1).setString(1).build();
JSONObject json = new JSONObject();
json.put("byte", (byte) 1);
json.put("short", (short) 1);
json.put("int", 1);
json.put("long", 1L);
json.put("string", "1");
DynamicMessage protoMsg =
JsonToProtoMessage.convertJsonToProtoMessage(TestInt64.getDescriptor(), json);
assertEquals(expectedProto, protoMsg);
}
@Test
public void testInt32() throws Exception {
TestInt32 expectedProto =
TestInt32.newBuilder().setByte(1).setShort(1).setInt(1).setString(1).build();
JSONObject json = new JSONObject();
json.put("byte", (byte) 1);
json.put("short", (short) 1);
json.put("int", 1);
json.put("string", 1);
DynamicMessage protoMsg =
JsonToProtoMessage.convertJsonToProtoMessage(TestInt32.getDescriptor(), json);
assertEquals(expectedProto, protoMsg);
}
@Test
public void testInt32NotMatchInt64() throws Exception {
JSONObject json = new JSONObject();
json.put("byte", (byte) 1);
json.put("short", (short) 1);
json.put("int", 1L);
try {
DynamicMessage protoMsg =
JsonToProtoMessage.convertJsonToProtoMessage(TestInt32.getDescriptor(), json);
Assert.fail("should fail");
} catch (IllegalArgumentException e) {
assertEquals("JSONObject does not have a int32 field at root.int.", e.getMessage());
}
}
@Test
public void testDateTimeMismatch() throws Exception {
TableFieldSchema field =
TableFieldSchema.newBuilder()
.setName("datetime")
.setType(TableFieldSchema.Type.DATETIME)
.setMode(TableFieldSchema.Mode.REPEATED)
.build();
TableSchema tableSchema = TableSchema.newBuilder().addFields(field).build();
JSONObject json = new JSONObject();
json.put("datetime", 1.0);
try {
DynamicMessage protoMsg =
JsonToProtoMessage.convertJsonToProtoMessage(
TestDatetime.getDescriptor(), tableSchema, json);
Assert.fail("should fail");
} catch (IllegalArgumentException e) {
assertEquals("JSONObject does not have a int64 field at root.datetime.", e.getMessage());
}
}
@Test
public void testTimeMismatch() throws Exception {
TableFieldSchema field =
TableFieldSchema.newBuilder()
.setName("time")
.setType(TableFieldSchema.Type.TIME)
.setMode(TableFieldSchema.Mode.REPEATED)
.build();
TableSchema tableSchema = TableSchema.newBuilder().addFields(field).build();
JSONObject json = new JSONObject();
json.put("time", new JSONArray(new Double[] {1.0}));
try {
DynamicMessage protoMsg =
JsonToProtoMessage.convertJsonToProtoMessage(TestTime.getDescriptor(), tableSchema, json);
Assert.fail("should fail");
} catch (IllegalArgumentException e) {
assertEquals("JSONObject does not have a int64 field at root.time[0].", e.getMessage());
}
}
@Test
public void testMixedCaseFieldNames() throws Exception {
TableFieldSchema field =
TableFieldSchema.newBuilder()
.setName("fooBar")
.setType(TableFieldSchema.Type.STRING)
.setMode(TableFieldSchema.Mode.NULLABLE)
.build();
TableSchema tableSchema = TableSchema.newBuilder().addFields(field).build();
JSONObject json = new JSONObject();
json.put("fooBar", "hello");
DynamicMessage protoMsg =
JsonToProtoMessage.convertJsonToProtoMessage(
TestMixedCaseFieldNames.getDescriptor(), tableSchema, json);
}
@Test
public void testDouble() throws Exception {
TestDouble expectedProto =
TestDouble.newBuilder()
.setDouble(1.2)
.setFloat(3.4f)
.setByte(5)
.setShort(6)
.setInt(7)
.setLong(8)
.setString(9.1)
.build();
JSONObject json = new JSONObject();
json.put("double", 1.2);
json.put("float", 3.4f);
json.put("byte", new Byte((byte) 5));
json.put("short", new Short((short) 6));
json.put("int", 7);
json.put("long", 8L);
json.put("string", "9.1");
DynamicMessage protoMsg =
JsonToProtoMessage.convertJsonToProtoMessage(TestDouble.getDescriptor(), json);
assertEquals(expectedProto, protoMsg);
}
@Test
public void testTimestamp() throws Exception {
TableSchema tableSchema =
TableSchema.newBuilder()
.addFields(TableFieldSchema.newBuilder(TEST_TIMESTAMP).setName("test_string").build())
.addFields(
TableFieldSchema.newBuilder(TEST_TIMESTAMP).setName("test_string_T_Z").build())
.addFields(TableFieldSchema.newBuilder(TEST_TIMESTAMP).setName("test_long").build())
.addFields(TableFieldSchema.newBuilder(TEST_TIMESTAMP).setName("test_int").build())
.addFields(TableFieldSchema.newBuilder(TEST_TIMESTAMP).setName("test_float").build())
.addFields(TableFieldSchema.newBuilder(TEST_TIMESTAMP).setName("test_offset").build())
.addFields(TableFieldSchema.newBuilder(TEST_TIMESTAMP).setName("test_timezone").build())
.addFields(TableFieldSchema.newBuilder(TEST_TIMESTAMP).setName("test_saformat").build())
.build();
TestTimestamp expectedProto =
TestTimestamp.newBuilder()
.setTestString(10L)
.setTestStringTZ(1648493279010000L)
.setTestLong(0L)
.setTestInt(1534806950000000L)
.setTestFloat(1534680695000000000L)
.setTestOffset(1649135171000000L)
.setTestTimezone(1649174771000000L)
.setTestSaformat(1534680660000000L)
.build();
JSONObject json = new JSONObject();
json.put("test_string", "1970-01-01 00:00:00.000010");
json.put("test_string_T_Z", "2022-03-28T18:47:59.01Z");
json.put("test_long", 0L);
json.put("test_int", 153480695);
json.put("test_float", "1.534680695e11");
json.put("test_offset", "2022-04-05T09:06:11+04:00");
json.put("test_timezone", "2022-04-05 09:06:11 PST");
json.put("test_saformat", "2018/08/19 12:11");
DynamicMessage protoMsg =
JsonToProtoMessage.convertJsonToProtoMessage(
TestTimestamp.getDescriptor(), tableSchema, json);
assertEquals(expectedProto, protoMsg);
}
@Test
public void testDate() throws Exception {
TableSchema tableSchema =
TableSchema.newBuilder()
.addFields(TableFieldSchema.newBuilder(TEST_DATE).setName("test_string").build())
.addFields(TableFieldSchema.newBuilder(TEST_DATE).setName("test_long").build())
.build();
TestDate expectedProto = TestDate.newBuilder().setTestString(18935).setTestLong(18935).build();
JSONObject json = new JSONObject();
json.put("test_string", "2021-11-04");
json.put("test_long", 18935L);
DynamicMessage protoMsg =
JsonToProtoMessage.convertJsonToProtoMessage(TestDate.getDescriptor(), tableSchema, json);
assertEquals(expectedProto, protoMsg);
}
@Test
public void testAllTypes() throws Exception {
for (Map.Entry<Descriptor, String> entry : AllTypesToDebugMessageTest.entrySet()) {
int success = 0;
for (JSONObject json : simpleJSONObjects) {
try {
LOG.info("Testing " + json + " over " + entry.getKey().getFullName());
DynamicMessage protoMsg =
JsonToProtoMessage.convertJsonToProtoMessage(entry.getKey(), json);
LOG.info("Convert Success!");
assertEquals(protoMsg, AllTypesToCorrectProto.get(entry.getKey())[success]);
success += 1;
} catch (IllegalArgumentException e) {
assertEquals(
"JSONObject does not have a " + entry.getValue() + " field at root.test_field_type.",
e.getMessage());
}
}
if (entry.getKey() == DoubleType.getDescriptor()) {
assertEquals(entry.getKey().getFullName(), 3, success);
} else if (entry.getKey() == Int64Type.getDescriptor()
|| entry.getKey() == BytesType.getDescriptor()) {
assertEquals(entry.getKey().getFullName(), 2, success);
} else {
assertEquals(entry.getKey().getFullName(), 1, success);
}
}
}
@Test
public void testAllRepeatedTypesWithLimits() throws Exception {
for (Map.Entry<Descriptor, String> entry : AllRepeatedTypesToDebugMessageTest.entrySet()) {
int success = 0;
for (JSONObject json : simpleJSONArrays) {
try {
LOG.info("Testing " + json + " over " + entry.getKey().getFullName());
DynamicMessage protoMsg =
JsonToProtoMessage.convertJsonToProtoMessage(entry.getKey(), json);
LOG.info("Convert Success!");
assertEquals(
protoMsg.toString(),
protoMsg,
AllRepeatedTypesToCorrectProto.get(entry.getKey())[success]);
success += 1;
} catch (IllegalArgumentException e) {
LOG.info(e.getMessage());
assertTrue(
e.getMessage()
.equals(
"JSONObject does not have a "
+ entry.getValue()
+ " field at root.test_repeated[0].")
|| e.getMessage()
.equals("Error: root.test_repeated[0] could not be converted to byte[]."));
}
}
if (entry.getKey() == RepeatedDouble.getDescriptor()) {
assertEquals(entry.getKey().getFullName(), 4, success);
} else if (entry.getKey() == RepeatedInt64.getDescriptor()) {
assertEquals(entry.getKey().getFullName(), 2, success);
} else {
assertEquals(entry.getKey().getFullName(), 1, success);
}
}
}
@Test
public void testOptional() throws Exception {
TestInt64 expectedProto = TestInt64.newBuilder().setByte(1).build();
JSONObject json = new JSONObject();
json.put("byte", 1);
DynamicMessage protoMsg =
JsonToProtoMessage.convertJsonToProtoMessage(TestInt64.getDescriptor(), json);
assertEquals(expectedProto, protoMsg);
}
@Test
public void testRepeatedIsOptional() throws Exception {
TestRepeatedIsOptional expectedProto =
TestRepeatedIsOptional.newBuilder().setRequiredDouble(1.1).build();
JSONObject json = new JSONObject();
json.put("required_double", 1.1);
DynamicMessage protoMsg =
JsonToProtoMessage.convertJsonToProtoMessage(TestRepeatedIsOptional.getDescriptor(), json);
assertEquals(expectedProto, protoMsg);
}
@Test
public void testRequired() throws Exception {
JSONObject json = new JSONObject();
json.put("optional_double", 1.1);
try {
DynamicMessage protoMsg =
JsonToProtoMessage.convertJsonToProtoMessage(TestRequired.getDescriptor(), json);
Assert.fail("should fail");
} catch (IllegalArgumentException e) {
assertEquals(
"JSONObject does not have the required field root.required_double.", e.getMessage());
}
}
@Test
public void testStructSimple() throws Exception {
MessageType expectedProto =
MessageType.newBuilder()
.setTestFieldType(StringType.newBuilder().setTestFieldType("test").build())
.build();
JSONObject stringType = new JSONObject();
stringType.put("test_field_type", "test");
JSONObject json = new JSONObject();
json.put("test_field_type", stringType);
DynamicMessage protoMsg =
JsonToProtoMessage.convertJsonToProtoMessage(MessageType.getDescriptor(), json);
assertEquals(expectedProto, protoMsg);
}
@Test
public void testStructSimpleFail() throws Exception {
JSONObject stringType = new JSONObject();
stringType.put("test_field_type", 1);
JSONObject json = new JSONObject();
json.put("test_field_type", stringType);
try {
DynamicMessage protoMsg =
JsonToProtoMessage.convertJsonToProtoMessage(MessageType.getDescriptor(), json);
Assert.fail("should fail");
} catch (IllegalArgumentException e) {
assertEquals(
"JSONObject does not have a string field at root.test_field_type.test_field_type.",
e.getMessage());
}
}
@Test
public void testStructComplex() throws Exception {
ComplexRoot expectedProto =
ComplexRoot.newBuilder()
.setTestInt(1)
.addTestString("a")
.addTestString("b")
.addTestString("c")
.setTestBytes(ByteString.copyFrom("hello".getBytes()))
.setTestBool(true)
.addTestDouble(1.1)
.addTestDouble(2.2)
.addTestDouble(3.3)
.addTestDouble(4.4)
.setTestDate(1)
.setTestDatetime(1)
.addTestDatetimeStr(142258614586538368L)
.addTestDatetimeStr(142258525253402624L)
.setComplexLvl1(
ComplexLvl1.newBuilder()
.setTestInt(2)
.setComplexLvl2(ComplexLvl2.newBuilder().setTestInt(3).build())
.build())
.setComplexLvl2(ComplexLvl2.newBuilder().setTestInt(3).build())
.setTestNumeric(
BigDecimalByteStringEncoder.encodeToNumericByteString(new BigDecimal("1.23456")))
.setTestGeo("POINT(1,1)")
.setTestTimestamp(123456780000000L)
.setTestTime(CivilTimeEncoder.encodePacked64TimeMicros(LocalTime.of(1, 0, 1)))
.setTestTimeStr(89332507144L)
.addTestNumericRepeated(
BigDecimalByteStringEncoder.encodeToNumericByteString(new BigDecimal("0")))
.addTestNumericRepeated(
BigDecimalByteStringEncoder.encodeToNumericByteString(
new BigDecimal("99999999999999999999999999999.999999999")))
.addTestNumericRepeated(
BigDecimalByteStringEncoder.encodeToNumericByteString(
new BigDecimal("-99999999999999999999999999999.999999999")))
.setTestNumericStr(
BigDecimalByteStringEncoder.encodeToNumericByteString(new BigDecimal("12.4")))
.setTestNumericShort(
BigDecimalByteStringEncoder.encodeToNumericByteString(new BigDecimal(1)))
.setTestNumericInt(
BigDecimalByteStringEncoder.encodeToNumericByteString(new BigDecimal(2)))
.setTestNumericLong(
BigDecimalByteStringEncoder.encodeToNumericByteString(new BigDecimal(3L)))
.setTestNumericFloat(
BigDecimalByteStringEncoder.encodeToNumericByteString(new BigDecimal(4f)))
.setTestNumericDouble(
BigDecimalByteStringEncoder.encodeToNumericByteString(new BigDecimal(5D)))
.setTestBignumeric(
BigDecimalByteStringEncoder.encodeToBigNumericByteString(
new BigDecimal("578960446186580977117854925043439539266.3")))
.addTestBignumericStr(
BigDecimalByteStringEncoder.encodeToBigNumericByteString(new BigDecimal("1.23")))
.setTestBignumericShort(
BigDecimalByteStringEncoder.encodeToBigNumericByteString(new BigDecimal(1)))
.setTestBignumericInt(
BigDecimalByteStringEncoder.encodeToBigNumericByteString(new BigDecimal(2)))
.setTestBignumericLong(
BigDecimalByteStringEncoder.encodeToBigNumericByteString(new BigDecimal(3L)))
.setTestBignumericFloat(
BigDecimalByteStringEncoder.encodeToBigNumericByteString(new BigDecimal(4f)))
.setTestBignumericDouble(
BigDecimalByteStringEncoder.encodeToBigNumericByteString(new BigDecimal(5D)))
.setTestInterval("0-0 0 0:0:0.000005")
.addTestJson("{'a':'b'}")
.build();
JSONObject complex_lvl2 = new JSONObject();
complex_lvl2.put("test_int", 3);
JSONObject complex_lvl1 = new JSONObject();
complex_lvl1.put("test_int", 2);
complex_lvl1.put("complex_lvl2", complex_lvl2);
JSONObject json = new JSONObject();
json.put("test_int", 1);
json.put("test_string", new JSONArray(new String[] {"a", "b", "c"}));
json.put("test_bytes", ByteString.copyFromUtf8("hello"));
json.put("test_bool", true);
json.put("test_DOUBLe", new JSONArray(new Double[] {1.1, 2.2, 3.3, 4.4}));
json.put("test_date", 1);
json.put("test_datetime", 1);
json.put(
"test_datetime_str",
new JSONArray(new String[] {"2021-09-27T20:51:10.752", "2021-09-27T00:00:00"}));
json.put("complex_lvl1", complex_lvl1);
json.put("complex_lvl2", complex_lvl2);
json.put(
"test_numeric",
BigDecimalByteStringEncoder.encodeToNumericByteString(new BigDecimal("1.23456")));
json.put(
"test_numeric_repeated",
new JSONArray(
new byte[][] {
BigDecimalByteStringEncoder.encodeToNumericByteString(new BigDecimal("0"))
.toByteArray(),
BigDecimalByteStringEncoder.encodeToNumericByteString(
new BigDecimal("99999999999999999999999999999.999999999"))
.toByteArray(),
BigDecimalByteStringEncoder.encodeToNumericByteString(
new BigDecimal("-99999999999999999999999999999.999999999"))
.toByteArray(),
}));
json.put("test_geo", "POINT(1,1)");
json.put("test_timestamp", 12345678);
json.put("test_time", CivilTimeEncoder.encodePacked64TimeMicros(LocalTime.of(1, 0, 1)));
json.put("test_time_str", "20:51:10.1234");
json.put("test_numeric_str", "12.4");
json.put("test_numeric_short", 1);
json.put("test_numeric_int", 2);
json.put("test_numeric_long", 3L);
json.put("test_numeric_float", 4f);
json.put("test_numeric_double", 5D);
json.put(
"test_bignumeric",
BigDecimalByteStringEncoder.encodeToBigNumericByteString(
new BigDecimal("578960446186580977117854925043439539266.3")));
json.put("test_bignumeric_str", new JSONArray(new String[] {"1.23"}));
json.put("test_bignumeric_short", 1);
json.put("test_bignumeric_int", 2);