|
4 | 4 | import com.backblaze.b2.json.B2JsonException;
|
5 | 5 | import com.google.gson.Gson;
|
6 | 6 | import com.google.gson.GsonBuilder;
|
| 7 | +import org.json.JSONArray; |
7 | 8 | import org.json.JSONException;
|
8 | 9 | import org.junit.Ignore;
|
9 | 10 | import org.junit.Test;
|
|
15 | 16 | import java.util.*;
|
16 | 17 |
|
17 | 18 | import static org.junit.Assert.assertEquals;
|
| 19 | +import static org.junit.Assert.assertThrows; |
18 | 20 |
|
19 | 21 | /**
|
20 | 22 | * Reference: https://github.com/Backblaze/b2-sdk-java/blob/0ecd68df94691cbba5a6af363246b7193aead234/core/src/test/java/com/backblaze/b2/json/B2JsonTest.java
|
@@ -361,4 +363,44 @@ public void testRequestUsingGson() throws JSONException {
|
361 | 363 | JSONAssert.assertEquals(expected, gson.toJson(obj), false);
|
362 | 364 | }
|
363 | 365 |
|
| 366 | + @Test |
| 367 | + public void testRequest2UsingB2Json() { |
| 368 | + /** |
| 369 | + * the field zip is declared as an Integer, but we are passing in a string, in this case |
| 370 | + * it throws com.backblaze.b2.json.B2JsonException: Bad number |
| 371 | + * */ |
| 372 | + String request2Json = "{\n" + "\"zip\": \"95148\"\n" + "}"; |
| 373 | + assertThrows(B2JsonException.class, () -> b2Json.fromJson(request2Json, TestRequest2.class)); |
| 374 | + } |
| 375 | + |
| 376 | + private static class TestRequest2 { |
| 377 | + @B2Json.optional(omitNull = true) |
| 378 | + public final Integer zip; |
| 379 | + |
| 380 | + @B2Json.constructor(params = "zip") |
| 381 | + public TestRequest2(Integer zip) { |
| 382 | + this.zip = zip; |
| 383 | + } |
| 384 | + } |
| 385 | + |
| 386 | + @Test |
| 387 | + public void testRequest3UsingB2Json() { |
| 388 | + /** |
| 389 | + * the field zip is declared as a String, but we are passing in an Integer |
| 390 | + * in this case, it throws: com.backblaze.b2.json.B2JsonException: string does not start with quote |
| 391 | + * */ |
| 392 | + String request2Json = "{\n" + "\"zip\": 95148\n" + "}"; |
| 393 | + assertThrows(B2JsonException.class, () -> b2Json.fromJson(request2Json, TestRequest3.class)); |
| 394 | + } |
| 395 | + |
| 396 | + private static class TestRequest3 { |
| 397 | + @B2Json.optional(omitNull = true) |
| 398 | + public final String zip; |
| 399 | + |
| 400 | + @B2Json.constructor(params = "zip") |
| 401 | + public TestRequest3(String zip) { |
| 402 | + this.zip = zip; |
| 403 | + } |
| 404 | + } |
| 405 | + |
364 | 406 | }
|
0 commit comments