forked from skyscreamer/JSONassert
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJSONCustomComparatorTest.java
More file actions
63 lines (54 loc) · 2.28 KB
/
JSONCustomComparatorTest.java
File metadata and controls
63 lines (54 loc) · 2.28 KB
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
package org.skyscreamer.jsonassert;
import org.json.JSONException;
import org.junit.Test;
import org.skyscreamer.jsonassert.comparator.CustomComparator;
import org.skyscreamer.jsonassert.comparator.JSONComparator;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.skyscreamer.jsonassert.JSONCompare.compareJSON;
public class JSONCustomComparatorTest {
String actual = "{\"first\":\"actual\", \"second\":1}";
String expected = "{\"first\":\"expected\", \"second\":1}";
String deepActual = "{\n" +
" \"outer\":\n" +
" {\n" +
" \"inner\":\n" +
" {\n" +
" \"value\": \"actual\",\n" +
" \"otherValue\": \"foo\"\n" +
" }\n" +
" }\n" +
"}";
String deepExpected = "{\n" +
" \"outer\":\n" +
" {\n" +
" \"inner\":\n" +
" {\n" +
" \"value\": \"expected\",\n" +
" \"otherValue\": \"foo\"\n" +
" }\n" +
" }\n" +
"}";
int comparatorCallCount = 0;
ValueMatcher<Object> comparator = new ValueMatcher<Object>() {
@Override
public boolean equal(Object o1, Object o2) {
comparatorCallCount++;
return o1.toString().equals("actual") && o2.toString().equals("expected");
}
};
@Test
public void whenPathMatchesInCustomizationThenCallCustomMatcher() throws JSONException {
JSONComparator jsonCmp = new CustomComparator(JSONCompareMode.STRICT, new Customization("first", comparator));
JSONCompareResult result = compareJSON(expected, actual, jsonCmp);
assertTrue(result.getMessage(), result.passed());
assertEquals(1, comparatorCallCount);
}
@Test
public void whenDeepPathMatchesCallCustomMatcher() throws JSONException {
JSONComparator jsonCmp = new CustomComparator(JSONCompareMode.STRICT, new Customization("outer.inner.value", comparator));
JSONCompareResult result = compareJSON(deepExpected, deepActual, jsonCmp);
assertTrue(result.getMessage(), result.passed());
assertEquals(1, comparatorCallCount);
}
}