1 | |
package uk.co.datumedge.hamcrest.json; |
2 | |
|
3 | |
import static uk.co.datumedge.hamcrest.json.JSONComparisonResult.comparisonPassed; |
4 | |
|
5 | |
import org.hamcrest.Description; |
6 | |
import org.hamcrest.SelfDescribing; |
7 | |
import org.skyscreamer.jsonassert.FieldComparisonFailure; |
8 | |
import org.skyscreamer.jsonassert.JSONCompareResult; |
9 | |
|
10 | |
final class JSONAssertComparisonResult { |
11 | 0 | private JSONAssertComparisonResult() { } |
12 | |
|
13 | |
static JSONComparisonResult resultOf(JSONCompareResult result) { |
14 | 22 | if (result.failed()) { |
15 | 13 | return diagnose(result); |
16 | |
} else { |
17 | 9 | return comparisonPassed(); |
18 | |
} |
19 | |
} |
20 | |
|
21 | |
private static JSONComparisonResult diagnose(final JSONCompareResult result) { |
22 | 13 | return new JSONComparisonResult(new SelfDescribing() { |
23 | |
@Override |
24 | |
public void describeTo(Description description) { |
25 | 3 | boolean first = true; |
26 | |
|
27 | 3 | for (FieldComparisonFailure failure : result.getFieldFailures()) { |
28 | 3 | if (!first) description.appendText(" and "); |
29 | 3 | description |
30 | |
.appendText("field ").appendText(failure.getField()) |
31 | |
.appendText(" was ").appendValue(failure.getActual()) |
32 | |
.appendText(" instead of ").appendValue(failure.getExpected()); |
33 | 3 | first = false; |
34 | 3 | } |
35 | |
|
36 | 3 | if (result.getFieldFailures().isEmpty()) { |
37 | 1 | description.appendText(result.getMessage()); |
38 | |
} |
39 | 3 | } |
40 | |
}); |
41 | |
} |
42 | |
} |