Skip to content

Commit a356fea

Browse files
add two tests for B2Json exceptions
1 parent 68d7865 commit a356fea

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

src/test/b2SdkExamples/B2JsonTest.java

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import com.backblaze.b2.json.B2JsonException;
55
import com.google.gson.Gson;
66
import com.google.gson.GsonBuilder;
7+
import org.json.JSONArray;
78
import org.json.JSONException;
89
import org.junit.Ignore;
910
import org.junit.Test;
@@ -15,6 +16,7 @@
1516
import java.util.*;
1617

1718
import static org.junit.Assert.assertEquals;
19+
import static org.junit.Assert.assertThrows;
1820

1921
/**
2022
* 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 {
361363
JSONAssert.assertEquals(expected, gson.toJson(obj), false);
362364
}
363365

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+
364406
}

0 commit comments

Comments
 (0)