/
JsonToProtoMessageTest.java
750 lines (697 loc) · 27.6 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
/*
* 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.v1beta2;
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.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;
@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(), "string")
.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.copyFrom("test".getBytes()))
.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(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(), new String[] {"boolean"})
.put(RepeatedBytes.getDescriptor(), new String[] {"string", "bytes"})
.put(RepeatedInt64.getDescriptor(), new String[] {"int64"})
.put(RepeatedInt32.getDescriptor(), new String[] {"int32"})
.put(RepeatedDouble.getDescriptor(), new String[] {"double"})
.put(RepeatedString.getDescriptor(), new String[] {"string"})
.put(RepeatedObject.getDescriptor(), new String[] {"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("hello".getBytes()))
.addTestRepeated(ByteString.copyFrom("test".getBytes()))
.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(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", "test"),
new JSONObject().put("test_field_type", new JSONArray("[1, 2, 3]")),
new JSONObject().put("test_field_type", new JSONObject().put("test_int", 1))
};
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 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)
}))
};
@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 testInt64() 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 testInt32() throws Exception {
TestInt32 expectedProto = TestInt32.newBuilder().setByte(1).setShort(1).setInt(1).build();
JSONObject json = new JSONObject();
json.put("byte", (byte) 1);
json.put("short", (short) 1);
json.put("int", 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 testDouble() throws Exception {
TestDouble expectedProto = TestDouble.newBuilder().setDouble(1.2).setFloat(3.4f).build();
JSONObject json = new JSONObject();
json.put("double", 1.2);
json.put("float", 3.4f);
DynamicMessage protoMsg =
JsonToProtoMessage.convertJsonToProtoMessage(TestDouble.getDescriptor(), 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 {
DynamicMessage protoMsg =
JsonToProtoMessage.convertJsonToProtoMessage(entry.getKey(), json);
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() == Int64Type.getDescriptor()) {
assertEquals(2, success);
} else {
assertEquals(1, success);
}
}
}
@Test
public void testAllRepeatedTypesWithLimits() throws Exception {
for (Map.Entry<Descriptor, String[]> entry : AllRepeatedTypesToDebugMessageTest.entrySet()) {
int success = 0;
for (JSONObject json : simpleJSONArrays) {
try {
DynamicMessage protoMsg =
JsonToProtoMessage.convertJsonToProtoMessage(entry.getKey(), json);
assertEquals(protoMsg, AllRepeatedTypesToCorrectProto.get(entry.getKey())[success]);
success += 1;
} catch (IllegalArgumentException e) {
assertTrue(
e.getMessage()
.equals(
"JSONObject does not have a "
+ entry.getValue()[0]
+ " field at root.test_repeated[0].")
|| e.getMessage()
.equals("Error: root.test_repeated[0] could not be converted to byte[]."));
}
}
if (entry.getKey() == RepeatedInt64.getDescriptor()
|| entry.getKey() == RepeatedDouble.getDescriptor()
|| entry.getKey() == RepeatedBytes.getDescriptor()) {
assertEquals(2, success);
} else {
assertEquals(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)
.setComplexLvl1(
ComplexLvl1.newBuilder()
.setTestInt(2)
.setComplexLvl2(ComplexLvl2.newBuilder().setTestInt(3).build())
.build())
.setComplexLvl2(ComplexLvl2.newBuilder().setTestInt(3).build())
.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", "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("complex_lvl1", complex_lvl1);
json.put("complex_lvl2", complex_lvl2);
DynamicMessage protoMsg =
JsonToProtoMessage.convertJsonToProtoMessage(ComplexRoot.getDescriptor(), json);
assertEquals(expectedProto, protoMsg);
}
@Test
public void testStructComplexFail() throws Exception {
JSONObject complex_lvl2 = new JSONObject();
complex_lvl2.put("test_int", 3);
JSONObject complex_lvl1 = new JSONObject();
complex_lvl1.put("test_int", "not_int");
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", "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("complex_lvl1", complex_lvl1);
json.put("complex_lvl2", complex_lvl2);
try {
DynamicMessage protoMsg =
JsonToProtoMessage.convertJsonToProtoMessage(ComplexRoot.getDescriptor(), json);
Assert.fail("should fail");
} catch (IllegalArgumentException e) {
assertEquals(
"JSONObject does not have a int64 field at root.complex_lvl1.test_int.", e.getMessage());
}
}
@Test
public void testRepeatedWithMixedTypes() throws Exception {
JSONObject json = new JSONObject();
json.put("test_repeated", new JSONArray("[1.1, 2.2, true]"));
try {
DynamicMessage protoMsg =
JsonToProtoMessage.convertJsonToProtoMessage(RepeatedDouble.getDescriptor(), json);
Assert.fail("should fail");
} catch (IllegalArgumentException e) {
assertEquals(
"JSONObject does not have a double field at root.test_repeated[2].", e.getMessage());
}
}
@Test
public void testNestedRepeatedComplex() throws Exception {
NestedRepeated expectedProto =
NestedRepeated.newBuilder()
.addDouble(1.1)
.addDouble(2.2)
.addDouble(3.3)
.addDouble(4.4)
.addDouble(5.5)
.addInt(1)
.addInt(2)
.addInt(3)
.addInt(4)
.addInt(5)
.setRepeatedString(
RepeatedString.newBuilder()
.addTestRepeated("hello")
.addTestRepeated("this")
.addTestRepeated("is")
.addTestRepeated("a")
.addTestRepeated("test")
.build())
.build();
double[] doubleArr = {1.1, 2.2, 3.3, 4.4, 5.5};
String[] stringArr = {"hello", "this", "is", "a", "test"};
int[] intArr = {1, 2, 3, 4, 5};
JSONObject json = new JSONObject();
json.put("double", new JSONArray(doubleArr));
json.put("int", new JSONArray(intArr));
JSONObject jsonRepeatedString = new JSONObject();
jsonRepeatedString.put("test_repeated", new JSONArray(stringArr));
json.put("repeated_string", jsonRepeatedString);
DynamicMessage protoMsg =
JsonToProtoMessage.convertJsonToProtoMessage(NestedRepeated.getDescriptor(), json);
assertEquals(protoMsg, expectedProto);
}
@Test
public void testNestedRepeatedComplexFail() throws Exception {
double[] doubleArr = {1.1, 2.2, 3.3, 4.4, 5.5};
Boolean[] fakeStringArr = {true, false};
int[] intArr = {1, 2, 3, 4, 5};
JSONObject json = new JSONObject();
json.put("double", new JSONArray(doubleArr));
json.put("int", new JSONArray(intArr));
JSONObject jsonRepeatedString = new JSONObject();
jsonRepeatedString.put("test_repeated", new JSONArray(fakeStringArr));
json.put("repeated_string", jsonRepeatedString);
try {
DynamicMessage protoMsg =
JsonToProtoMessage.convertJsonToProtoMessage(NestedRepeated.getDescriptor(), json);
Assert.fail("should fail");
} catch (IllegalArgumentException e) {
assertEquals(
"JSONObject does not have a string field at root.repeated_string.test_repeated[0].",
e.getMessage());
}
}
@Test
public void testEmptySecondLevelObject() throws Exception {
ComplexLvl1 expectedProto =
ComplexLvl1.newBuilder()
.setTestInt(1)
.setComplexLvl2(ComplexLvl2.newBuilder().build())
.build();
JSONObject complexLvl2 = new JSONObject();
JSONObject json = new JSONObject();
json.put("test_int", 1);
json.put("complex_lvl2", complexLvl2);
DynamicMessage protoMsg =
JsonToProtoMessage.convertJsonToProtoMessage(ComplexLvl1.getDescriptor(), json);
assertEquals(expectedProto, protoMsg);
}
@Test
public void testAllowUnknownFieldsError() throws Exception {
JSONObject json = new JSONObject();
json.put("test_repeated", new JSONArray(new int[] {1, 2, 3, 4, 5}));
json.put("string", "hello");
try {
DynamicMessage protoMsg =
JsonToProtoMessage.convertJsonToProtoMessage(RepeatedInt64.getDescriptor(), json);
Assert.fail("Should fail");
} catch (IllegalArgumentException e) {
assertEquals("JSONObject has fields unknown to BigQuery: root.string.", e.getMessage());
}
}
@Test
public void testEmptyProtoMessage() throws Exception {
JSONObject json = new JSONObject();
json.put("test_repeated", new JSONArray(new int[0]));
try {
DynamicMessage protoMsg =
JsonToProtoMessage.convertJsonToProtoMessage(RepeatedInt64.getDescriptor(), json);
Assert.fail("Should fail");
} catch (IllegalArgumentException e) {
assertEquals("The created protobuf message is empty.", e.getMessage());
}
}
@Test
public void testEmptyJSONObject() throws Exception {
JSONObject json = new JSONObject();
try {
DynamicMessage protoMsg =
JsonToProtoMessage.convertJsonToProtoMessage(Int64Type.getDescriptor(), json);
Assert.fail("Should fail");
} catch (IllegalStateException e) {
assertEquals("JSONObject is empty.", e.getMessage());
}
}
@Test
public void testNullJson() throws Exception {
try {
DynamicMessage protoMsg =
JsonToProtoMessage.convertJsonToProtoMessage(Int64Type.getDescriptor(), null);
Assert.fail("Should fail");
} catch (NullPointerException e) {
assertEquals("JSONObject is null.", e.getMessage());
}
}
@Test
public void testNullDescriptor() throws Exception {
try {
DynamicMessage protoMsg =
JsonToProtoMessage.convertJsonToProtoMessage(null, new JSONObject());
Assert.fail("Should fail");
} catch (NullPointerException e) {
assertEquals("Protobuf descriptor is null.", e.getMessage());
}
}
@Test
public void testAllowUnknownFieldsSecondLevel() throws Exception {
JSONObject complex_lvl2 = new JSONObject();
complex_lvl2.put("no_match", 1);
JSONObject json = new JSONObject();
json.put("test_int", 1);
json.put("complex_lvl2", complex_lvl2);
try {
DynamicMessage protoMsg =
JsonToProtoMessage.convertJsonToProtoMessage(ComplexLvl1.getDescriptor(), json);
Assert.fail("Should fail");
} catch (IllegalArgumentException e) {
assertEquals(
"JSONObject has fields unknown to BigQuery: root.complex_lvl2.no_match.", e.getMessage());
}
}
@Test
public void testTopLevelMatchSecondLevelMismatch() throws Exception {
ComplexLvl1 expectedProto =
ComplexLvl1.newBuilder()
.setTestInt(1)
.setComplexLvl2(ComplexLvl2.newBuilder().build())
.build();
JSONObject complex_lvl2 = new JSONObject();
JSONObject json = new JSONObject();
json.put("test_int", 1);
json.put("complex_lvl2", complex_lvl2);
DynamicMessage protoMsg =
JsonToProtoMessage.convertJsonToProtoMessage(ComplexLvl1.getDescriptor(), json);
assertEquals(expectedProto, protoMsg);
}
@Test
public void testJsonNullValue() throws Exception {
JSONObject json = new JSONObject();
json.put("long", JSONObject.NULL);
json.put("int", 1);
try {
DynamicMessage protoMsg =
JsonToProtoMessage.convertJsonToProtoMessage(TestInt64.getDescriptor(), json);
Assert.fail("Should fail");
} catch (IllegalArgumentException e) {
assertEquals(e.getMessage(), "JSONObject does not have a int64 field at root.long.");
}
}
}