Skip to content

Commit 1d60a6a

Browse files
Hronomsdeleuze
authored andcommitted
Added strict compare mode for Json
Issue: SPR-13607
1 parent 9334fab commit 1d60a6a

File tree

3 files changed

+91
-13
lines changed

3 files changed

+91
-13
lines changed

spring-test/src/main/java/org/springframework/test/util/JsonExpectationsHelper.java

+49-4
Original file line numberDiff line numberDiff line change
@@ -32,26 +32,71 @@ public class JsonExpectationsHelper {
3232
/**
3333
* Parse the expected and actual strings as JSON and assert the two
3434
* are "similar" - i.e. they contain the same attribute-value pairs
35-
* regardless of order and formatting.
35+
* regardless of formatting with a lenient checking (extensible, and non-strict
36+
* array ordering).
3637
*
3738
* @param expected the expected JSON content
3839
* @param actual the actual JSON content
3940
* @since 4.1
41+
* @see #assertJsonEqual(String, String, boolean)
4042
*/
4143
public void assertJsonEqual(String expected, String actual) throws Exception {
42-
JSONAssert.assertEquals(expected, actual, false);
44+
assertJsonEqual(expected, actual, false);
45+
}
46+
47+
/**
48+
* Parse the expected and actual strings as JSON and assert the two
49+
* are "similar" - i.e. they contain the same attribute-value pairs
50+
* regardless of formatting.
51+
*
52+
* <p>Can compare in two modes, depending on {@code strict} parameter value:
53+
* <ul>
54+
* <li>{@code true}: strict checking. Not extensible, and strict array ordering.</li>
55+
* <li>{@code false}: lenient checking. Extensible, and non-strict array ordering.</li>
56+
* </ul>
57+
*
58+
* @param expected the expected JSON content
59+
* @param actual the actual JSON content
60+
* @param strict enables strict checking
61+
* @since 4.2
62+
*/
63+
public void assertJsonEqual(String expected, String actual, boolean strict) throws Exception {
64+
JSONAssert.assertEquals(expected, actual, strict);
4365
}
4466

4567
/**
4668
* Parse the expected and actual strings as JSON and assert the two
4769
* are "not similar" - i.e. they contain different attribute-value pairs
48-
* regardless of order and formatting.
70+
* regardless of formatting with a lenient checking (extensible, and non-strict
71+
* array ordering).
4972
*
5073
* @param expected the expected JSON content
5174
* @param actual the actual JSON content
5275
* @since 4.1
76+
* @see #assertJsonNotEqual(String, String, boolean)
5377
*/
5478
public void assertJsonNotEqual(String expected, String actual) throws Exception {
55-
JSONAssert.assertNotEquals(expected, actual, false);
79+
assertJsonNotEqual(expected, actual, false);
5680
}
81+
82+
/**
83+
* Parse the expected and actual strings as JSON and assert the two
84+
* are "not similar" - i.e. they contain different attribute-value pairs
85+
* regardless of formatting.
86+
*
87+
* <p>Can compare in two modes, depending on {@code strict} parameter value:
88+
* <ul>
89+
* <li>{@code true}: strict checking. Not extensible, and strict array ordering.</li>
90+
* <li>{@code false}: lenient checking. Extensible, and non-strict array ordering.</li>
91+
* </ul>
92+
*
93+
* @param expected the expected JSON content
94+
* @param actual the actual JSON content
95+
* @param strict enables strict checking
96+
* @since 4.2
97+
*/
98+
public void assertJsonNotEqual(String expected, String actual, boolean strict) throws Exception {
99+
JSONAssert.assertNotEquals(expected, actual, strict);
100+
}
101+
57102
}

spring-test/src/main/java/org/springframework/test/web/servlet/result/ContentResultMatchers.java

+27-5
Original file line numberDiff line numberDiff line change
@@ -212,21 +212,43 @@ public void match(MvcResult result) throws Exception {
212212
};
213213
}
214214

215+
/**
216+
* Parse the expected and actual strings as JSON and assert the two
217+
* are "similar" - i.e. they contain the same attribute-value pairs
218+
* regardless of formatting with a lenient checking (extensible, and non-strict array
219+
* ordering).
220+
*
221+
* @param jsonContent the expected JSON content
222+
* @since 4.1
223+
*/
224+
public ResultMatcher json(final String jsonContent) {
225+
return json(jsonContent, false);
226+
}
227+
215228
/**
216229
* Parse the response content and the given string as JSON and assert the two
217-
* are "similar" &mdash; i.e. they contain the same attribute-value pairs
218-
* regardless of order and formatting.
230+
* are "similar" - i.e. they contain the same attribute-value pairs
231+
* regardless of formatting.
232+
*
233+
* <p>Can compare in two modes, depending on {@code strict} parameter value:
234+
* <ul>
235+
* <li>{@code true}: strict checking. Not extensible, and strict array ordering.</li>
236+
* <li>{@code false}: lenient checking. Extensible, and non-strict array ordering.</li>
237+
* </ul>
238+
*
219239
* <p>Use of this matcher requires the <a
220240
* href="http://jsonassert.skyscreamer.org/">JSONassert<a/> library.
241+
*
221242
* @param jsonContent the expected JSON content
222-
* @since 4.1
243+
* @param strict enables strict checking
244+
* @since 4.2
223245
*/
224-
public ResultMatcher json(final String jsonContent) {
246+
public ResultMatcher json(final String jsonContent, final boolean strict) {
225247
return new ResultMatcher() {
226248
@Override
227249
public void match(MvcResult result) throws Exception {
228250
String content = result.getResponse().getContentAsString();
229-
jsonHelper.assertJsonEqual(jsonContent, content);
251+
jsonHelper.assertJsonEqual(jsonContent, content, strict);
230252
}
231253
};
232254
}

spring-test/src/test/java/org/springframework/test/web/servlet/result/ContentResultMatchersTests.java

+15-4
Original file line numberDiff line numberDiff line change
@@ -79,17 +79,28 @@ public void bytesNoMatch() throws Exception {
7979
}
8080

8181
@Test
82-
public void json() throws Exception {
83-
new ContentResultMatchers().json("{\n \"foo\" : \"bar\" \n}").match(getStubMvcResult());
82+
public void jsonLenientMatch() throws Exception {
83+
new ContentResultMatchers().json("{\n \"foo\" : \"bar\" \n}").match(getStubMvcResult());
84+
new ContentResultMatchers().json("{\n \"foo\" : \"bar\" \n}", false).match(getStubMvcResult());
85+
}
86+
87+
@Test
88+
public void jsonStrictMatch() throws Exception {
89+
new ContentResultMatchers().json("{\n \"foo\":\"bar\", \"foo array\":[\"foo\",\"bar\"] \n}", true).match(getStubMvcResult());
90+
new ContentResultMatchers().json("{\n \"foo array\":[\"foo\",\"bar\"], \"foo\":\"bar\" \n}", true).match(getStubMvcResult());
8491
}
8592

8693
@Test(expected=AssertionError.class)
87-
public void jsonNoMatch() throws Exception {
94+
public void jsonLenientNoMatch() throws Exception {
8895
new ContentResultMatchers().json("{\n\"fooo\":\"bar\"\n}").match(getStubMvcResult());
8996
}
9097

98+
@Test(expected=AssertionError.class)
99+
public void jsonStrictNoMatch() throws Exception {
100+
new ContentResultMatchers().json("{\"foo\":\"bar\", \"foo array\":[\"bar\",\"foo\"]}", true).match(getStubMvcResult());
101+
}
91102

92-
private static final String CONTENT = "{\"foo\":\"bar\"}";
103+
private static final String CONTENT = "{\"foo\":\"bar\",\"foo array\":[\"foo\",\"bar\"]}";
93104

94105
private StubMvcResult getStubMvcResult() throws Exception {
95106
MockHttpServletResponse response = new MockHttpServletResponse();

0 commit comments

Comments
 (0)