forked from auth0/auth0-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJsonMatcher.java
More file actions
136 lines (121 loc) · 4.09 KB
/
JsonMatcher.java
File metadata and controls
136 lines (121 loc) · 4.09 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
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
127
128
129
130
131
132
133
134
135
136
package com.auth0.json;
import org.hamcrest.Description;
import org.hamcrest.Matcher;
import org.hamcrest.TypeSafeDiagnosingMatcher;
import java.lang.reflect.Array;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
public class JsonMatcher extends TypeSafeDiagnosingMatcher<String> {
private final String entry;
private final String key;
private final Matcher matcher;
private JsonMatcher(String key, Object value, Matcher valueMatcher) {
this.key = key;
this.matcher = valueMatcher;
if (value != null) {
String stringValue = objectToString(value);
entry = getStringKey(key) + stringValue;
} else {
entry = null;
}
}
@Override
protected boolean matchesSafely(String item, Description mismatchDescription) {
if (item == null) {
mismatchDescription.appendText("JSON was null");
return false;
}
if (matcher != null) {
if (!matcher.matches(item)) {
matcher.describeMismatch(item, mismatchDescription);
return false;
}
if (!item.contains(getStringKey(key))) {
mismatchDescription.appendText("JSON didn't contained the key ").appendValue(key);
return false;
}
}
if (entry != null && !item.contains(entry)) {
mismatchDescription.appendText("JSON was ").appendValue(item);
return false;
}
return true;
}
@Override
public void describeTo(Description description) {
if (matcher == null) {
description.appendText("A JSON with entry ")
.appendValue(entry);
} else {
matcher.describeTo(description);
}
}
public static JsonMatcher hasEntry(String key, Object value) {
return new JsonMatcher(key, value, null);
}
public static JsonMatcher hasEntry(String key, Matcher valueMatcher) {
return new JsonMatcher(key, null, valueMatcher);
}
private String getStringKey(String key) {
return "\"" + key + "\":";
}
private String objectToString(Object value) {
String stringValue;
if (value == null) {
stringValue = "null";
} else if (value instanceof String) {
stringValue = "\"" + value + "\"";
} else if (value instanceof Map) {
stringValue = mapToString((Map<String, Object>) value);
} else if (value instanceof Array) {
stringValue = arrayToString((Object[]) value);
} else if (value instanceof List) {
stringValue = listToString((List<Object>) value);
} else {
stringValue = value.toString();
}
return stringValue;
}
private String arrayToString(Object[] array) {
StringBuilder sb = new StringBuilder();
sb.append("[");
for (int i = 0; i < array.length; i++) {
Object o = array[i];
sb.append(objectToString(o));
if (i + 1 < array.length) {
sb.append(",");
}
}
sb.append("]");
return sb.toString();
}
private String listToString(List<Object> list) {
StringBuilder sb = new StringBuilder();
sb.append("[");
Iterator<Object> it = list.iterator();
while (it.hasNext()) {
Object o = it.next();
sb.append(objectToString(o));
if (it.hasNext()) {
sb.append(",");
}
}
sb.append("]");
return sb.toString();
}
private String mapToString(Map<String, Object> map) {
StringBuilder sb = new StringBuilder();
sb.append("{");
Iterator<Map.Entry<String, Object>> it = map.entrySet().iterator();
while (it.hasNext()) {
Map.Entry<String, Object> e = it.next();
sb.append("\"" + e.getKey() + "\":" + objectToString(e.getValue()));
if (it.hasNext()) {
sb.append(",");
}
}
sb.append("}");
return sb.toString();
}
}