Skip to content

Commit b3100af

Browse files
add test for @B2Json.optionalWithDefault annotation
1 parent ba157aa commit b3100af

File tree

1 file changed

+55
-13
lines changed

1 file changed

+55
-13
lines changed

src/test/b2SdkExamples/B2JsonTest.java

Lines changed: 55 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
import java.time.LocalDate;
1212
import java.time.LocalDateTime;
13+
import java.util.Objects;
1314

1415
import static org.junit.Assert.assertEquals;
1516

@@ -51,20 +52,29 @@ private static final class Container {
5152
@B2Json.ignored
5253
public int c;
5354

54-
@B2Json.constructor(params = "a, b")
55-
public Container(int a, String b) {
55+
@B2Json.optionalWithDefault(defaultValue = "0")
56+
public int d;
57+
58+
@B2Json.constructor(params = "a, b, d")
59+
public Container(int a, String b, int d) {
5660
this.a = a;
5761
this.b = b;
5862
this.c = 5;
63+
this.d = d;
5964
}
6065

6166
@Override
6267
public boolean equals(Object o) {
63-
if (!(o instanceof Container)) {
64-
return false;
65-
}
66-
Container other = (Container) o;
67-
return a == other.a && (b == null ? other.b == null : b.equals(other.b));
68+
System.out.println("Entered overridden equals() method to check now...");
69+
if (this == o) return true;
70+
if (o == null || getClass() != o.getClass()) return false;
71+
Container container = (Container) o;
72+
return a == container.a && d == container.d && Objects.equals(b, container.b);
73+
}
74+
75+
@Override
76+
public int hashCode() {
77+
return Objects.hash(a, b, c, d);
6878
}
6979
}
7080

@@ -169,18 +179,50 @@ public TestResponse(String str, String message, String reason, boolean succeeded
169179

170180
@Test
171181
public void testObject() throws B2JsonException {
172-
String json =
173-
"{\n" +
174-
" \"a\": 41,\n" +
175-
" \"b\": \"hello\"\n" +
176-
"}";
177-
Container obj = new Container(41, "hello");
182+
String json = "{\n" +
183+
" \"a\": 41,\n" +
184+
" \"b\": \"hello\",\n" +
185+
" \"d\": 15\n" +
186+
"}";
187+
Container obj = new Container(41, "hello", 15);
178188
assertEquals(json, b2Json.toJson(obj));
179189
System.out.println("obj is: " + obj);
180190
System.out.println("json is: " + json);
181191
assertEquals(obj, b2Json.fromJson(json, Container.class));
182192
}
183193

194+
@Test
195+
public void testObjectUsingDefaultValue() throws B2JsonException {
196+
//in this json string, there's no field d which is an optional field with a default value
197+
String json = "{\n" +
198+
" \"a\": 2023,\n" +
199+
" \"b\": \"hello\"\n" +
200+
"}";
201+
System.out.println("json is: " + json);
202+
Container fromB2Json = b2Json.fromJson(json, Container.class);
203+
System.out.println("b2Json.fromJson(json, Container.class) is: " + fromB2Json);
204+
Container fromGson = gson.fromJson(json, Container.class);
205+
System.out.println("gson.fromJson(json) is: " + fromGson);
206+
System.out.println("about to check the equality between Gson and B2Json results..");
207+
assertEquals(fromGson, fromB2Json);
208+
String fromB2JsonString = b2Json.toJson(fromB2Json);
209+
String fromGsonString = gson.toJson(fromB2Json);
210+
String expectedFromGson = "{\n" +
211+
" \"a\": 2023,\n" +
212+
" \"b\": \"hello\",\n" +
213+
" \"c\": 5,\n" +
214+
" \"d\": 0\n" +
215+
"}";
216+
assertEquals(expectedFromGson, fromGsonString);
217+
//there's no field c as it's annotated by B2Json.ignored
218+
String expectedFromB2Json = "{\n" +
219+
" \"a\": 2023,\n" +
220+
" \"b\": \"hello\",\n" +
221+
" \"d\": 0\n" +
222+
"}";
223+
assertEquals(expectedFromB2Json, fromB2JsonString);
224+
}
225+
184226
@Test
185227
public void testRequestUsingB2Json() throws B2JsonException, JSONException {
186228
//B2Json always reorders the fields in the object in alphabetical order

0 commit comments

Comments
 (0)