-
Notifications
You must be signed in to change notification settings - Fork 192
/
Copy pathJsonDiffTest.java
123 lines (105 loc) · 4.09 KB
/
JsonDiffTest.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
/*
* Copyright (c) 2014, Francis Galiegue ([email protected])
*
* This software is dual-licensed under:
*
* - the Lesser General Public License (LGPL) version 3.0 or, at your option, any
* later version;
* - the Apache Software License (ASL) version 2.0.
*
* The text of this file and of both licenses is available at the root of this
* project or, if you have the jar distribution, in directory META-INF/, under
* the names LGPL-3.0.txt and ASL-2.0.txt respectively.
*
* Direct link to the sources:
*
* - LGPL 3.0: https://www.gnu.org/licenses/lgpl-3.0.txt
* - ASL 2.0: http://www.apache.org/licenses/LICENSE-2.0.txt
*/
package com.github.fge.jsonpatch.diff;
import com.fasterxml.jackson.databind.JsonNode;
import com.github.fge.jackson.JsonLoader;
import com.github.fge.jackson.JsonNumEquals;
import com.github.fge.jsonpatch.JsonPatch;
import com.github.fge.jsonpatch.JsonPatchException;
import com.google.common.collect.Lists;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;
import static com.github.fge.jsonpatch.diff.DiffProcessor.DIFF_DOESNT_REQUIRE_SOURCE;
import static org.assertj.core.api.Assertions.*;
public final class JsonDiffTest
{
private static final JsonNumEquals EQUIVALENCE = JsonNumEquals.getInstance();
private final JsonNode testData;
public JsonDiffTest()
throws IOException
{
final String resource = "/jsonpatch/diff/diff.json";
testData = JsonLoader.fromResource(resource);
}
@DataProvider
public Iterator<Object[]> getPatchesOnly()
{
final List<Object[]> list = Lists.newArrayList();
for (final JsonNode node: testData)
list.add(new Object[] {
node.get("first"), node.get("second"),
node.has("options") ? getLiteralOptions(node.get("options")) : DiffOptions.DEFAULT_OPTIONS
});
return list.iterator();
}
@Test(dataProvider = "getPatchesOnly")
public void generatedPatchAppliesCleanly(final JsonNode first,
final JsonNode second, final DiffOptions options)
throws JsonPatchException
{
final JsonPatch patch = JsonDiff.asJsonPatch(first, second, options);
final JsonNode actual = patch.apply(first);
assertThat(EQUIVALENCE.equivalent(second, actual)).overridingErrorMessage(
"Generated patch failed to apply\nexpected: %s\nactual: %s",
second, actual
).isTrue();
}
@DataProvider
public Iterator<Object[]> getLiteralPatches()
{
final List<Object[]> list = Lists.newArrayList();
for (final JsonNode node: testData) {
if (!node.has("patch"))
continue;
list.add(new Object[] {
node.get("message").textValue(), node.get("first"),
node.get("second"), node.get("patch"),
node.has("options") ? getLiteralOptions(node.get("options")) : DiffOptions.DEFAULT_OPTIONS
});
}
return list.iterator();
}
public DiffOptions getLiteralOptions(JsonNode jsonNode) {
DiffOptions.Builder builder = new DiffOptions.Builder();
if (jsonNode.has("diffDoesntRequireSource")) {
if (jsonNode.get("diffDoesntRequireSource").booleanValue()){
builder.diffDoesntRequireSource();
} else {
builder.diffRequireSource();
}
}
return builder.build();
}
@Test(
dataProvider = "getLiteralPatches",
dependsOnMethods = "generatedPatchAppliesCleanly"
)
public void generatedPatchesAreWhatIsExpected(final String message,
final JsonNode first, final JsonNode second, final JsonNode expected, final DiffOptions options)
{
final JsonNode actual = JsonDiff.asJson(first, second, options);
assertThat(EQUIVALENCE.equivalent(expected, actual)).overridingErrorMessage(
"patch is not what was expected\nscenario: %s\n"
+ "expected: %s\nactual: %s\n", message, expected, actual
).isTrue();
}
}