-
Notifications
You must be signed in to change notification settings - Fork 192
/
Copy pathJsonPatchOperationTest.java
127 lines (109 loc) · 3.98 KB
/
JsonPatchOperationTest.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
124
125
126
/*
* 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;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectReader;
import com.github.fge.jackson.JacksonUtils;
import com.github.fge.jackson.JsonLoader;
import com.github.fge.jackson.JsonNumEquals;
import com.github.fge.msgsimple.bundle.MessageBundle;
import com.github.fge.msgsimple.load.MessageBundles;
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 org.testng.Assert.*;
@Test
public abstract class JsonPatchOperationTest
{
private static final MessageBundle BUNDLE
= MessageBundles.getBundle(JsonPatchMessages.class);
private static final JsonNumEquals EQUIVALENCE
= JsonNumEquals.getInstance();
private final JsonNode errors;
private final JsonNode ops;
private final ObjectReader reader;
protected JsonPatchOperationTest(final String prefix)
throws IOException
{
final String resource = "/jsonpatch/" + prefix + ".json";
final JsonNode node = JsonLoader.fromResource(resource);
errors = node.get("errors");
ops = node.get("ops");
reader = JacksonUtils.getReader().forType(JsonPatchOperation.class);
}
@DataProvider
public final Iterator<Object[]> getErrors()
throws NoSuchFieldException, IllegalAccessException
{
final List<Object[]> list = Lists.newArrayList();
for (final JsonNode node: errors)
list.add(new Object[]{
node.get("op"),
node.get("node"),
BUNDLE.getMessage(node.get("message").textValue())
});
return list.iterator();
}
@Test(dataProvider = "getErrors")
public final void errorsAreCorrectlyReported(final JsonNode patch,
final JsonNode node, final String message)
throws IOException
{
final JsonPatchOperation op = reader.readValue(patch);
try {
op.apply(node);
fail("No exception thrown!!");
} catch (JsonPatchException e) {
String msg = message;
if(node!=null) {
msg = node.toString() + " " + msg;
}
assertEquals(e.getMessage(), msg);
}
}
@DataProvider
public final Iterator<Object[]> getOps()
{
final List<Object[]> list = Lists.newArrayList();
for (final JsonNode node: ops)
list.add(new Object[]{
node.get("op"),
node.get("node"),
node.get("expected")
});
return list.iterator();
}
@Test(dataProvider = "getOps")
public final void operationsYieldExpectedResults(final JsonNode patch,
final JsonNode node, final JsonNode expected)
throws IOException, JsonPatchException
{
final JsonPatchOperation op = reader.readValue(patch);
final JsonNode actual = op.apply(node);
assertTrue(EQUIVALENCE.equivalent(actual, expected),
"patched node differs from expectations: expected " + expected
+ " but found " + actual);
if (EQUIVALENCE.equivalent(node, actual) && node.isContainerNode())
assertNotSame(node, actual,
"operation didn't make a copy of the input node");
}
}