-
Notifications
You must be signed in to change notification settings - Fork 79
/
JsonToProtoMessageTest.java
482 lines (438 loc) · 16.8 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
/*
* 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
*
* 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.bigquery.storage.v1alpha2;
import static org.junit.Assert.*;
import static org.mockito.Mockito.*;
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.Descriptors.Descriptor;
import com.google.protobuf.Descriptors.FieldDescriptor;
import com.google.protobuf.DynamicMessage;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import org.json.JSONArray;
import org.json.JSONObject;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
@RunWith(JUnit4.class)
public class JsonToProtoMessageTest {
private static ImmutableMap<Descriptor, String> AllTypesToDebugMessageTest =
new ImmutableMap.Builder<Descriptor, String>()
.put(BoolType.getDescriptor(), "boolean")
.put(BytesType.getDescriptor(), "string")
.put(Int64Type.getDescriptor(), "int64")
.put(DoubleType.getDescriptor(), "double")
.put(StringType.getDescriptor(), "string")
.put(RepeatedType.getDescriptor(), "array")
.put(ObjectType.getDescriptor(), "object")
.build();
private static ImmutableMap<Descriptor, String> AllRepeatedTypesToDebugMessageTest =
new ImmutableMap.Builder<Descriptor, String>()
.put(RepeatedBool.getDescriptor(), "boolean")
.put(RepeatedBytes.getDescriptor(), "string")
.put(RepeatedInt64.getDescriptor(), "int64")
.put(RepeatedDouble.getDescriptor(), "double")
.put(RepeatedObject.getDescriptor(), "object")
.build();
private static JSONObject[] simpleJSONObjects = {
new JSONObject().put("test_field_type", 123),
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 Double[] {
Double.MAX_VALUE,
Double.MIN_VALUE,
(double) Float.MAX_VALUE,
(double) 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 JSONObject[] {
new JSONObject().put("test_int", 1),
new JSONObject().put("test_int", 2),
new JSONObject().put("test_int", 3)
}))
};
private void AreMatchingFieldsFilledIn(DynamicMessage proto, JSONObject json) {
for (Map.Entry<FieldDescriptor, java.lang.Object> entry : proto.getAllFields().entrySet()) {
FieldDescriptor key = entry.getKey();
java.lang.Object value = entry.getValue();
if (key.isRepeated()) {
isProtoArrayJsonArrayEqual(key, value, json);
} else {
isProtoFieldJsonFieldEqual(key, value, json);
}
}
}
private void isProtoFieldJsonFieldEqual(
FieldDescriptor key, java.lang.Object value, JSONObject json) {
String fieldName = key.getName();
switch (key.getType()) {
case BOOL:
assertTrue((Boolean) value == json.getBoolean(fieldName));
break;
case BYTES:
assertTrue(Arrays.equals((byte[]) value, json.getString(fieldName).getBytes()));
break;
case INT64:
assertTrue((long) value == json.getLong(fieldName));
break;
case STRING:
assertTrue(((String) value).equals(json.getString(fieldName)));
break;
case DOUBLE:
assertTrue((double) value == json.getDouble(fieldName));
break;
case MESSAGE:
AreMatchingFieldsFilledIn((DynamicMessage) value, json.getJSONObject(fieldName));
break;
}
}
private void isProtoArrayJsonArrayEqual(
FieldDescriptor key, java.lang.Object value, JSONObject json) {
String fieldName = key.getName();
JSONArray jsonArray = json.getJSONArray(fieldName);
switch (key.getType()) {
case BOOL:
List<Boolean> boolArr = (List<Boolean>) value;
for (int i = 0; i < jsonArray.length(); i++) {
assertTrue((boolArr.get(i) == jsonArray.getBoolean(i)));
}
break;
case BYTES:
List<byte[]> byteArr = (List<byte[]>) value;
for (int i = 0; i < jsonArray.length(); i++) {
assertTrue(Arrays.equals(byteArr.get(i), jsonArray.getString(i).getBytes()));
}
break;
case INT64:
List<Long> longArr = (List<Long>) value;
for (int i = 0; i < jsonArray.length(); i++) {
assertTrue((longArr.get(i) == jsonArray.getLong(i)));
}
break;
case STRING:
List<String> stringArr = (List<String>) value;
for (int i = 0; i < jsonArray.length(); i++) {
assertTrue(stringArr.get(i).equals(jsonArray.getString(i)));
}
break;
case DOUBLE:
List<Double> doubleArr = (List<Double>) value;
for (int i = 0; i < jsonArray.length(); i++) {
assertTrue((doubleArr.get(i) == jsonArray.getDouble(i)));
}
break;
case MESSAGE:
List<DynamicMessage> messageArr = (List<DynamicMessage>) value;
for (int i = 0; i < jsonArray.length(); i++) {
AreMatchingFieldsFilledIn(messageArr.get(i), jsonArray.getJSONObject(i));
}
break;
}
}
@Test
public void testInt64() throws Exception {
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);
AreMatchingFieldsFilledIn(protoMsg, json);
}
@Test
public void testDouble() throws Exception {
JSONObject json = new JSONObject();
json.put("double", 1.2);
json.put("float", 3.4f);
DynamicMessage protoMsg =
JsonToProtoMessage.convertJsonToProtoMessage(TestDouble.getDescriptor(), json);
AreMatchingFieldsFilledIn(protoMsg, json);
}
@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);
AreMatchingFieldsFilledIn(protoMsg, json);
success += 1;
} catch (IllegalArgumentException e) {
assertEquals(
e.getMessage(),
"JSONObject does not have a " + entry.getValue() + " field at root.test_field_type.");
}
}
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);
success += 1;
} catch (IllegalArgumentException e) {
assertEquals(
e.getMessage(),
"JSONObject does not have a "
+ entry.getValue()
+ " field at root.test_repeated[0].");
}
}
assertEquals(1, success);
}
}
@Test
public void testOptional() throws Exception {
JSONObject json = new JSONObject();
json.put("byte", 1);
DynamicMessage protoMsg =
JsonToProtoMessage.convertJsonToProtoMessage(TestInt64.getDescriptor(), json);
AreMatchingFieldsFilledIn(protoMsg, json);
}
@Test
public void testRepeatedIsOptional() throws Exception {
JSONObject json = new JSONObject();
json.put("required_double", 1.1);
DynamicMessage protoMsg =
JsonToProtoMessage.convertJsonToProtoMessage(TestRepeatedIsOptional.getDescriptor(), json);
AreMatchingFieldsFilledIn(protoMsg, json);
}
@Test
public void testRequired() throws Exception {
JSONObject json = new JSONObject();
json.put("test_required_double", 1.1);
json.put("optional_double", 1.1);
try {
DynamicMessage protoMsg =
JsonToProtoMessage.convertJsonToProtoMessage(TestRequired.getDescriptor(), json);
} catch (IllegalArgumentException e) {
assertEquals(
e.getMessage(), "JSONObject does not have the required field root.required_double.");
}
}
@Test
public void testStructSimple() throws Exception {
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);
AreMatchingFieldsFilledIn(protoMsg, json);
}
@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);
} catch (IllegalArgumentException e) {
assertEquals(
e.getMessage(),
"JSONObject does not have a string field at root.test_field_type.test_field_type.");
}
}
@Test
public void testStructComplex() throws Exception {
JSONObject complexLvl2 = new JSONObject();
complexLvl2.put("test_int", 3);
JSONObject complexLvl1 = new JSONObject();
complexLvl1.put("test_int", 2);
complexLvl1.put("complexLvl2", complexLvl2);
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("complexLvl1", complexLvl1);
json.put("complexLvl2", complexLvl2);
DynamicMessage protoMsg =
JsonToProtoMessage.convertJsonToProtoMessage(ComplexRoot.getDescriptor(), json);
AreMatchingFieldsFilledIn(protoMsg, json);
}
@Test
public void testStructComplexFail() throws Exception {
JSONObject complexLvl2 = new JSONObject();
complexLvl2.put("test_int", 3);
JSONObject complexLvl1 = new JSONObject();
complexLvl1.put("test_int", "not_int");
complexLvl1.put("complexLvl2", complexLvl2);
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("complexLvl1", complexLvl1);
json.put("complexLvl2", complexLvl2);
try {
DynamicMessage protoMsg =
JsonToProtoMessage.convertJsonToProtoMessage(ComplexRoot.getDescriptor(), json);
} catch (IllegalArgumentException e) {
assertEquals(
e.getMessage(), "JSONObject does not have a int64 field at root.complexLvl1.test_int.");
}
}
@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);
} catch (IllegalArgumentException e) {
assertEquals(
e.getMessage(), "JSONObject does not have a double field at root.test_repeated[2].");
}
}
@Test
public void testNestedRepeatedComplex() throws Exception {
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);
AreMatchingFieldsFilledIn(protoMsg, json);
}
@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);
} catch (IllegalArgumentException e) {
assertEquals(
e.getMessage(),
"JSONObject does not have a string field at root.repeated_string.test_repeated[0].");
}
}
@Test
public void testAllowUnknownFields() throws Exception {
JSONObject json = new JSONObject();
json.put("test_repeated", new JSONArray(new int[] {1, 2, 3, 4, 5}));
json.put("string", "hello");
DynamicMessage protoMsg =
JsonToProtoMessage.convertJsonToProtoMessage(RepeatedInt64.getDescriptor(), json, true);
AreMatchingFieldsFilledIn(protoMsg, json);
}
@Test
public void testAllowUnknownFieldsError() throws Exception {
JSONObject json = new JSONObject();
json.put("double", 1.1);
json.put("string", "hello");
try {
DynamicMessage protoMsg =
JsonToProtoMessage.convertJsonToProtoMessage(RepeatedInt64.getDescriptor(), json);
} catch (IllegalArgumentException e) {
assertEquals(
e.getMessage(),
"JSONObject has unknown fields. Set allowUnknownFields to True to ignore unknown fields.");
}
}
@Test
public void testTopLevelMatchFail() throws Exception {
JSONObject json = new JSONObject();
json.put("double", 1.1);
try {
DynamicMessage protoMsg =
JsonToProtoMessage.convertJsonToProtoMessage(Int64Type.getDescriptor(), json);
} catch (IllegalArgumentException e) {
assertEquals(
e.getMessage(),
"There are no matching fields found for the JSONObject and the protocol buffer descriptor.");
}
}
@Test
public void testTopLevelMatchSecondLevelNoMatch() throws Exception {
JSONObject complexLvl2 = new JSONObject();
complexLvl2.put("no_match", 1);
JSONObject json = new JSONObject();
json.put("test_int", 1);
json.put("complexLvl2", complexLvl2);
DynamicMessage protoMsg =
JsonToProtoMessage.convertJsonToProtoMessage(ComplexLvl1.getDescriptor(), json);
AreMatchingFieldsFilledIn(protoMsg, json);
}
@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);
} catch (IllegalArgumentException e) {
assertEquals(e.getMessage(), "JSONObject does not have a int64 field at root.long.");
}
}
}