forked from skyscreamer/JSONassert
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJSONCompareResult.java
More file actions
256 lines (228 loc) · 7.41 KB
/
JSONCompareResult.java
File metadata and controls
256 lines (228 loc) · 7.41 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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.skyscreamer.jsonassert;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import org.json.JSONArray;
import org.json.JSONObject;
/**
* Bean for holding results from JSONCompare.
*/
public class JSONCompareResult {
private boolean _success;
private StringBuilder _message;
private String _field;
private Object _expected;
private Object _actual;
private final List<FieldComparisonFailure> _fieldFailures = new ArrayList<FieldComparisonFailure>();
private final List<FieldComparisonFailure> _fieldMissing = new ArrayList<FieldComparisonFailure>();
private final List<FieldComparisonFailure> _fieldUnexpected = new ArrayList<FieldComparisonFailure>();
/**
* Default constructor.
*/
public JSONCompareResult() {
this(true, null);
}
private JSONCompareResult(boolean success, String message) {
_success = success;
_message = new StringBuilder(message == null ? "" : message);
}
/**
* Did the comparison pass?
* @return True if it passed
*/
public boolean passed() {
return _success;
}
/**
* Did the comparison fail?
* @return True if it failed
*/
public boolean failed() {
return !_success;
}
/**
* Result message
* @return String explaining why if the comparison failed
*/
public String getMessage() {
return _message.toString();
}
/**
* Get the list of failures on field comparisons
* @return list of comparsion failures
*/
public List<FieldComparisonFailure> getFieldFailures() {
return Collections.unmodifiableList(_fieldFailures);
}
/**
* Get the list of missed on field comparisons
* @return list of comparsion failures
*/
public List<FieldComparisonFailure> getFieldMissing() {
return Collections.unmodifiableList(_fieldMissing);
}
/**
* Get the list of failures on field comparisons
* @return list of comparsion failures
*/
public List<FieldComparisonFailure> getFieldUnexpected() {
return Collections.unmodifiableList(_fieldUnexpected);
}
/**
* Actual field value
*
* @return a {@code JSONObject}, {@code JSONArray} or other {@code Object}
* instance, or {@code null} if the comparison did not fail on a
* particular field
* @deprecated Superseded by {@link #getFieldFailures()}
*/
@Deprecated
public Object getActual() {
return _actual;
}
/**
* Expected field value
*
* @return a {@code JSONObject}, {@code JSONArray} or other {@code Object}
* instance, or {@code null} if the comparison did not fail on a
* particular field
* @deprecated Superseded by {@link #getFieldFailures()}
*/
@Deprecated
public Object getExpected() {
return _expected;
}
/**
* Check if comparison failed on any particular fields
* @return true if there are field failures
*/
public boolean isFailureOnField() {
return !_fieldFailures.isEmpty();
}
/**
* Check if comparison failed with missing on any particular fields
* @return true if an expected field is missing
*/
public boolean isMissingOnField() {
return !_fieldMissing.isEmpty();
}
/**
* Check if comparison failed with unexpected on any particular fields
* @return true if an unexpected field is in the result
*/
public boolean isUnexpectedOnField() {
return !_fieldUnexpected.isEmpty();
}
/**
* Dot-separated path the the field that failed comparison
*
* @return a {@code String} instance, or {@code null} if the comparison did
* not fail on a particular field
* @deprecated Superseded by {@link #getFieldFailures()}
*/
@Deprecated
public String getField() {
return _field;
}
public void fail(String message) {
_success = false;
if (_message.length() == 0) {
_message.append(message);
} else {
_message.append(" ; ").append(message);
}
}
/**
* Identify that the comparison failed
* @param field Which field failed
* @param expected Expected result
* @param actual Actual result
* @return result of comparision
*/
public JSONCompareResult fail(String field, Object expected, Object actual) {
_fieldFailures.add(new FieldComparisonFailure(field, expected, actual));
this._field = field;
this._expected = expected;
this._actual = actual;
fail(formatFailureMessage(field, expected, actual));
return this;
}
/**
* Identify that the comparison failed
* @param field Which field failed
* @param exception exception containing details of match failure
* @return result of comparision
*/
public JSONCompareResult fail(String field, ValueMatcherException exception) {
fail(field + ": " + exception.getMessage(), exception.getExpected(), exception.getActual());
return this;
}
private String formatFailureMessage(String field, Object expected, Object actual) {
return field
+ "\nExpected: "
+ describe(expected)
+ "\n got: "
+ describe(actual)
+ "\n";
}
/**
* Identify the missing field
* @param field missing field
* @param expected expected result
* @return result of comparison
*/
public JSONCompareResult missing(String field, Object expected) {
_fieldMissing.add(new FieldComparisonFailure(field, expected, null));
fail(formatMissing(field, expected));
return this;
}
private String formatMissing(String field, Object expected) {
return field
+ "\nExpected: "
+ describe(expected)
+ "\n but none found\n";
}
/**
* Identify unexpected field
* @param field unexpected field
* @param actual actual result
* @return result of comparison
*/
public JSONCompareResult unexpected(String field, Object actual) {
_fieldUnexpected.add(new FieldComparisonFailure(field, null, actual));
fail(formatUnexpected(field, actual));
return this;
}
private String formatUnexpected(String field, Object actual) {
return field
+ "\nUnexpected: "
+ describe(actual)
+ "\n";
}
private static String describe(Object value) {
if (value instanceof JSONArray) {
return "a JSON array";
} else if (value instanceof JSONObject) {
return "a JSON object";
} else {
return value.toString();
}
}
@Override
public String toString() {
return _message.toString();
}
}