-
Notifications
You must be signed in to change notification settings - Fork 74
/
JsonFormattingTest.java
79 lines (76 loc) · 2.53 KB
/
JsonFormattingTest.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
package org.approvaltests;
import com.google.gson.GsonBuilder;
import com.spun.util.JsonUtils;
import org.junit.jupiter.api.Test;
import java.time.Instant;
public class JsonFormattingTest
{
@Test
public void testGsonCircular()
{
Approvals.verifyException(() -> {
JsonApprovals.verifyAsJson(Circular.getIndirectCircularReference());
});
}
@Test
public void testBasicFormatting()
{
String json = "{\"infos\":{\"address\":\"my address\",\"phone\":\"my phone\"},\"insurance\":{\"forks\":[14,53,123],\"prices\":[5,8,\"3%\"]}}";
JsonApprovals.verifyJson(json);
}
@Test
public void testIncorrectFormatting()
{
String json = "{\"infos\":{address:my address,\"phone\":\"my phone\"},\"insurance\":{\"forks\":[14,53,123],\"prices\":[5,8,\"3%\"]}}";
JsonApprovals.verifyJson(json);
}
@Test
void demonstrateNullIssueInVerifyJson()
{
// begin-snippet: CustomGsonBuilderShowingNull
Person person = new Person("Max", null, 1);
JsonApprovals.verifyAsJson(person, GsonBuilder::serializeNulls);
// end-snippet
}
static class Person
{
public String firstName;
public String lastName;
public int age;
public Person(String firstName, String lastName, int age)
{
this.firstName = firstName;
this.lastName = lastName;
this.age = age;
}
}
@Test
public void testInstantAndOtherDateObjects()
{
DateStuff dateStuff = new DateStuff();
dateStuff.instant = Instant.ofEpochSecond(1651708441);
JsonApprovals.verifyAsJson(dateStuff);
}
private static class DateStuff
{
public Instant instant;
}
@Test
public void testJsonFieldOrdering()
{
Approvals.settings().allowMultipleVerifyCallsForThisMethod();
String json1 = "{\"infos\":{\"address\":\"my address\",\"phone\":\"my phone\"},\"insurance\":{\"forks\":[14,53,123],\"prices\":[5,8,\"3%\"]}}";
String json2 = "{\"insurance\":{\"forks\":[14,53,123],\"prices\":[5,8,\"3%\"]},\"infos\":{\"phone\":\"my phone\",\"address\":\"my address\"}}";
JsonApprovals.verifyJson(json1, true);
JsonApprovals.verifyJson(json2, true);
JsonApprovals.verifyJson(JsonUtils.reorderFields(json1));
JsonApprovals.verifyJson(JsonUtils.reorderFields(json2));
}
@Test
public void testJsonFieldOrderingWithNulls()
{
Approvals.settings().allowMultipleVerifyCallsForThisMethod();
String json1 = "{\"infos\":{\"address\":null,\"phone\":\"my phone\"},\"insurance\":{\"forks\":[14,53,123],\"prices\":[5,8,\"3%\"]}}";
JsonApprovals.verifyJson(json1, true, g -> g.serializeNulls());
}
}