-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathB2JsonTest.java
444 lines (393 loc) · 16.2 KB
/
B2JsonTest.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
package b2SdkExamples;
import com.backblaze.b2.json.B2Json;
import com.backblaze.b2.json.B2JsonException;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import org.json.JSONException;
import org.junit.jupiter.api.Test;
import org.skyscreamer.jsonassert.JSONAssert;
import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.*;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
/**
* Reference: https://github.com/Backblaze/b2-sdk-java/blob/0ecd68df94691cbba5a6af363246b7193aead234/core/src/test/java/com/backblaze/b2/json/B2JsonTest.java
*/
public class B2JsonTest {
private static final LocalDateTime LOCAL_DATE_TIME = LocalDateTime.of(2022, 12, 18, 16, 21);
private static final LocalDate LOCAL_DATE = LocalDate.of(2022, 12, 18);
private static final String JSON_STRING_1 = "{\n" +
// " \"localDate\": \"20221218\",\n" +
// " \"localDateTime\": \"d20221218_m162100\",\n" +
" \"message\": \"message\",\n" +
" \"str\": 123,\n" +
" \"reason\": \"A TEST STRING FOR REASON\"\n" +
"}";
private static final String JSON_STRING_2 = "{\n" +
" \"aMessage\": \"aMessage\",\n" +
" \"bMessage\": \"bMessage\",\n" +
" \"message\": \"message\",\n" +
" \"pMessage\": \"pMessage\",\n" +
" \"reason\": \"A TEST STRING FOR REASON\",\n" +
" \"str\": 123,\n" +
" \"zMessage\": \"zMessage\"\n" +
"}";
private static final String REASON_STR = "A TEST STRING FOR REASON";
private static final class Container {
@B2Json.required
public final int a;
@B2Json.optional
public final String b;
@B2Json.ignored
public int c;
@B2Json.optionalWithDefault(defaultValue = "0")
public int d;
@B2Json.constructor(params = "a, b, d")
public Container(int a, String b, int d) {
this.a = a;
this.b = b;
this.c = 5;
this.d = d;
}
@Override
public boolean equals(Object o) {
System.out.println("Entered overridden equals() method to check now...");
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Container container = (Container) o;
return a == container.a && d == container.d && Objects.equals(b, container.b);
}
@Override
public int hashCode() {
return Objects.hash(a, b, c, d);
}
}
private static final B2Json b2Json = B2Json.get();
private static final Gson gson = (new GsonBuilder())
.disableHtmlEscaping()
.setPrettyPrinting().create();
private static class TestRequest {
@B2Json.optional(omitNull = true)
public final Integer str;
@B2Json.optional(omitNull = true)
public final String message;
@B2Json.optional(omitNull = true)
public final String aMessage;
@B2Json.optional(omitNull = true)
public final String bMessage;
@B2Json.optional(omitNull = true)
public final String pMessage;
@B2Json.optional(omitNull = true)
public final String zMessage;
@B2Json.optional(omitNull = true)
public final LocalDateTime localDateTime;
@B2Json.optional(omitNull = true)
public final LocalDate localDate;
@B2Json.optional(omitNull = true)
public final String reason;
@B2Json.constructor(params = "" +
"str," +
"message," +
"aMessage," +
"bMessage," +
"pMessage," +
"zMessage," +
"localDateTime," +
"localDate," +
"reason"
)
public TestRequest(Integer str, String message, String aMessage, String bMessage, String pMessage, String zMessage, LocalDateTime localDateTime, LocalDate localDate, String reason) {
this.str = str;
this.message = message;
this.aMessage = aMessage;
this.bMessage = bMessage;
this.pMessage = pMessage;
this.zMessage = zMessage;
this.localDateTime = localDateTime;
this.localDate = localDate;
this.reason = reason;
}
}
private static class TestResponse {
@B2Json.optional(omitNull = true)
public final String str;
@B2Json.optional(omitNull = true)
public final String message;
@B2Json.optional(omitNull = true)
public final String reason;
@B2Json.optional(omitNull = true)
public final Boolean succeeded;
@B2Json.optional(omitNull = true)
public final Integer status;
@B2Json.optional(omitNull = true)
public final LocalDateTime localDateTime;
@B2Json.optional(omitNull = true)
public final LocalDate localDate;
@B2Json.required
public final Map<String, Map<String, BigDecimal>> revenueMap;
@B2Json.required
public final Map<String, Long> simpleMap;
@B2Json.required
public final Set<String> categories;
@B2Json.constructor(params = "" +
"str," +
"message," +
"reason," +
"succeeded," +
"status," +
"localDateTime," +
"localDate," +
"revenueMap," +
"simpleMap," +
"categories"
)
public TestResponse(String str, String message, String reason, boolean succeeded, int status, LocalDateTime localDateTime, LocalDate localDate,
Map<String, Map<String, BigDecimal>> revenueMap,
Map<String, Long> simpleMap,
Set<String> categories) {
this.str = str;
this.message = message;
this.reason = reason;
this.succeeded = succeeded;
this.status = status;
this.localDateTime = localDateTime;
this.localDate = localDate;
this.simpleMap = simpleMap;
this.revenueMap = revenueMap;
this.categories = categories;
}
}
@Test
public void testResponseUsingB2Json() throws B2JsonException, JSONException {
Map<String, Map<String, BigDecimal>> revenueMap = new TreeMap<>();
Map<String, Long> simpleMap = new TreeMap<>();
TestResponse obj = new TestResponse("str",
"message",
"reason",
true,
200,
LocalDateTime.of(2023, 03, 31, 12, 21),
LocalDate.of(2023, 03, 31),
revenueMap,
simpleMap,
Set.of("test1", "test2"));
System.out.println("obj is: " + obj);
String expected = "{\n" +
" \"categories\": [\n" +
" \"test2\",\n" +
" \"test1\"\n" +
" ],\n" +
" \"localDate\": \"20230331\",\n" +
" \"localDateTime\": \"d20230331_m122100\",\n" +
" \"message\": \"message\",\n" +
" \"reason\": \"reason\",\n" +
" \"revenueMap\": {},\n" +
" \"simpleMap\": {},\n" +
" \"status\": 200,\n" +
" \"str\": \"str\",\n" +
" \"succeeded\": true\n" +
"}";
System.out.println("b2Json.toJson(obj): " + b2Json.toJson(obj));
JSONAssert.assertEquals(expected, b2Json.toJson(obj), true);
}
@Test
public void testResponseUsingGson() throws JSONException {
Map<String, Map<String, BigDecimal>> revenueMap = new TreeMap<>();
revenueMap.put("123", new HashMap<>());
Map<String, Long> simpleMap = new TreeMap<>();
TestResponse obj = new TestResponse("str",
"message",
"reason",
true,
200,
LocalDateTime.of(2023, 03, 31, 12, 21),
LocalDate.of(2023, 03, 31),
revenueMap,
simpleMap,
Set.of("test"));
String expected = "{\n" +
" \"str\": \"str\",\n" +
" \"message\": \"message\",\n" +
" \"reason\": \"reason\",\n" +
" \"succeeded\": true,\n" +
" \"status\": 200,\n" +
" \"localDateTime\": {\n" +
" \"date\": {\n" +
" \"year\": 2023,\n" +
" \"month\": 3,\n" +
" \"day\": 31\n" +
" },\n" +
" \"time\": {\n" +
" \"hour\": 12,\n" +
" \"minute\": 21,\n" +
" \"second\": 0,\n" +
" \"nano\": 0\n" +
" }\n" +
" },\n" +
" \"localDate\": {\n" +
" \"year\": 2023,\n" +
" \"month\": 3,\n" +
" \"day\": 31\n" +
" },\n" +
" \"revenueMap\": {\n" +
" \"123\": {}\n" +
" },\n" +
" \"simpleMap\": {},\n" +
" \"categories\": [\n" +
" \"test\"\n" +
" ]\n" +
"}";
JSONAssert.assertEquals(expected, gson.toJson(obj), true);
}
@Test
public void testObject() throws B2JsonException {
String json = "{\n" +
" \"a\": 41,\n" +
" \"b\": \"hello\",\n" +
" \"d\": 15\n" +
"}";
Container obj = new Container(41, "hello", 15);
assertEquals(json, b2Json.toJson(obj));
System.out.println("obj is: " + obj);
System.out.println("json is: " + json);
assertEquals(obj, b2Json.fromJson(json, Container.class));
}
@Test
public void testObjectUsingDefaultValue() throws B2JsonException {
//in this json string, there's no field d which is an optional field with a default value
String json = "{\n" +
" \"a\": 2023,\n" +
" \"b\": \"hello\"\n" +
"}";
System.out.println("json is: " + json);
Container fromB2Json = b2Json.fromJson(json, Container.class);
System.out.println("b2Json.fromJson(json, Container.class) is: " + fromB2Json);
Container fromGson = gson.fromJson(json, Container.class);
System.out.println("gson.fromJson(json) is: " + fromGson);
System.out.println("about to check the equality between Gson and B2Json results..");
assertEquals(fromGson, fromB2Json);
String fromB2JsonString = b2Json.toJson(fromB2Json);
String fromGsonString = gson.toJson(fromGson);
String expectedFromGson = "{\n" +
" \"a\": 2023,\n" +
" \"b\": \"hello\",\n" +
" \"c\": 0,\n" +
" \"d\": 0\n" +
"}";
assertEquals(expectedFromGson, fromGsonString);
//there's no field c as it's annotated by B2Json.ignored
String expectedFromB2Json = "{\n" +
" \"a\": 2023,\n" +
" \"b\": \"hello\",\n" +
" \"d\": 0\n" +
"}";
assertEquals(expectedFromB2Json, fromB2JsonString);
}
@Test
public void testRequestUsingB2Json() throws B2JsonException, JSONException {
//B2Json always reorders the fields in the object in alphabetical order
TestRequest obj = new TestRequest(123, "message", "aMessage", "bMessage", "pMessage", "zMessage", null, null, REASON_STR);
System.out.println("obj is: " + obj);
String expected = "{\n" +
" \"aMessage\": \"aMessage\",\n" +
" \"bMessage\": \"bMessage\",\n" +
" \"message\": \"message\",\n" +
" \"pMessage\": \"pMessage\",\n" +
" \"reason\": \"A TEST STRING FOR REASON\",\n" +
" \"str\": 123,\n" +
" \"zMessage\": \"zMessage\"\n" +
"}";
JSONAssert.assertEquals(expected, b2Json.toJson(obj), true);
}
@Test
public void testRequestUsingGson() throws JSONException {
//GSON deserializes object fields in their given order in the constructor
TestRequest obj = new TestRequest(123, "message", "aMessage", "bMessage", "pMessage", "zMessage", null, null, REASON_STR);
System.out.println("obj is: " + obj);
String expected = "{\n" +
" \"aMessage\": \"aMessage\",\n" +
" \"bMessage\": \"bMessage\",\n" +
" \"message\": \"message\",\n" +
" \"pMessage\": \"pMessage\",\n" +
" \"reason\": \"A TEST STRING FOR REASON\",\n" +
" \"str\": 123,\n" +
" \"zMessage\": \"zMessage\"\n" +
"}";
JSONAssert.assertEquals(expected, gson.toJson(obj), false);
}
@Test
public void testRequest2UsingB2Json() {
/**
* the field zip is declared as an Integer, but we are passing in a string, in this case
* it throws com.backblaze.b2.json.B2JsonException: Bad number
* */
String request2Json = "{\n" + "\"zip\": \"95148\"\n" + "}";
assertThrows(B2JsonException.class, () -> b2Json.fromJson(request2Json, TestRequest2.class));
}
private static class TestRequest2 {
@B2Json.optional(omitNull = true)
public final Integer zip;
@B2Json.constructor(params = "zip")
public TestRequest2(Integer zip) {
this.zip = zip;
}
}
@Test
public void testRequest3UsingB2Json() {
/**
* the field zip is declared as a String, but we are passing in an Integer
* in this case, it throws: com.backblaze.b2.json.B2JsonException: string does not start with quote
* */
String request2Json = "{\n" + "\"zip\": 95148\n" + "}";
assertThrows(B2JsonException.class, () -> b2Json.fromJson(request2Json, TestRequest3.class));
}
private static class TestRequest3 {
@B2Json.optional(omitNull = true)
public final String zip;
@B2Json.constructor(params = "zip")
public TestRequest3(String zip) {
this.zip = zip;
}
}
@Test
public void testGsonFailure() {
// there's an unknown field called e in this json string, in this case, GSON library will just ignore it
String json = "{\n" +
" \"a\": 2023,\n" +
" \"b\": \"hello\",\n" +
" \"e\": \"hello\"\n" +
"}";
System.out.println("json is: " + json);
Container fromGson = gson.fromJson(json, Container.class);
System.out.println("gson.fromJson(json) is: " + fromGson);
System.out.println("about to check the equality between Gson and B2Json results..");
String fromGsonString = gson.toJson(fromGson);
String expectedFromGson = "{\n" +
" \"a\": 2023,\n" +
" \"b\": \"hello\",\n" +
" \"c\": 0,\n" +
" \"d\": 0\n" +
"}";
assertEquals(expectedFromGson, fromGsonString);
// in this json, the field named a is missing
json = "{\n" +
" \"b\": \"hello\",\n" +
" \"e\": \"hello\"\n" +
"}";
System.out.println("2nd time, json is: " + json);
fromGson = gson.fromJson(json, Container.class);
System.out.println("2nd time, gson.fromJson(json) is: " + fromGson);
System.out.println("about to check the equality between Gson and B2Json results..");
fromGsonString = gson.toJson(fromGson);
System.out.println("2nd time, fromGsonString is: " + fromGsonString);
expectedFromGson = "{\n" +
" \"a\": 0,\n" +
" \"b\": \"hello\",\n" +
" \"c\": 0,\n" +
" \"d\": 0\n" +
"}";
assertEquals(expectedFromGson, fromGsonString);
}
}