-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIgnorableJsonComparator.java
73 lines (63 loc) · 2.77 KB
/
IgnorableJsonComparator.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
package com.jcp.automation.common.utils;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.skyscreamer.jsonassert.JSONCompareMode;
import org.skyscreamer.jsonassert.JSONCompareResult;
import org.skyscreamer.jsonassert.comparator.DefaultComparator;
import java.util.List;
import java.util.Objects;
import java.util.Set;
import static org.skyscreamer.jsonassert.comparator.JSONCompareUtil.getKeys;
import static org.skyscreamer.jsonassert.comparator.JSONCompareUtil.qualify;
public class IgnorableJsonComparator extends DefaultComparator {
private List<String> ignoreKeys;
public IgnorableJsonComparator(JSONCompareMode mode, List<String> ignoreKeys) {
super(mode);
this.ignoreKeys = ignoreKeys;
}
@Override
protected void checkJsonObjectKeysExpectedInActual(String prefix, JSONObject expected,
JSONObject actual, JSONCompareResult result) throws JSONException {
Set<String> expectedKeys = getKeys(expected);
for (String key : expectedKeys) {
Object expectedValue = expected.get(key);
if (ignoreKeys.contains(key)) {
continue;
}
if (actual.has(key)) {
Object actualValue = actual.get(key);
compareValues(qualify(prefix, key), expectedValue, actualValue, result);
} else {
result.missing(prefix, key);
}
}
}
@Override
public void compareValues(String prefix, Object expectedValue, Object actualValue, JSONCompareResult result)
throws JSONException {
if (isBoolean(expectedValue)) {
if (!Boolean.valueOf(expectedValue.toString()).equals(Boolean.valueOf(actualValue.toString()))) {
result.fail(prefix, expectedValue, actualValue);
}
} else if (expectedValue instanceof Number || actualValue instanceof Number) {
if ((!Objects.equals(Double.valueOf(expectedValue.toString()), Double.valueOf(actualValue.toString())))) {
result.fail(prefix, expectedValue, actualValue);
}
} else if (expectedValue.getClass().isAssignableFrom(actualValue.getClass())) {
if (expectedValue instanceof JSONArray) {
compareJSONArray(prefix, (JSONArray) expectedValue, (JSONArray) actualValue, result);
} else if (expectedValue instanceof JSONObject) {
compareJSON(prefix, (JSONObject) expectedValue, (JSONObject) actualValue, result);
} else if (!expectedValue.equals(actualValue)) {
result.fail(prefix, expectedValue, actualValue);
}
} else {
result.fail(prefix, expectedValue, actualValue);
}
}
private boolean isBoolean(Object val) {
return String.valueOf(val).equalsIgnoreCase("true") ||
String.valueOf(val).equalsIgnoreCase("false");
}
}