forked from GJWT/javaOIDCMsg
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJsonMatcher.java
More file actions
155 lines (139 loc) · 5.19 KB
/
JsonMatcher.java
File metadata and controls
155 lines (139 loc) · 5.19 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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
// Copyright (c) 2017 The Authors of 'JWTS for Java'
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of
// this software and associated documentation files (the "Software"), to deal in
// the Software without restriction, including without limitation the rights to
// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
// the Software, and to permit persons to whom the Software is furnished to do so,
// subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
package com.auth0.jwt;
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();
}
}