From 97647ef5f69a586bbfe4b096c2a535972ce92a0a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Nicoll?= Date: Fri, 24 May 2024 11:03:33 +0200 Subject: [PATCH] Flag tests using deprecated json method deprecated See gh-32791 --- .../match/ContentRequestMatchersTests.java | 49 +++++++++++++++++++ .../result/ContentResultMatchersTests.java | 34 +++++++++++-- 2 files changed, 80 insertions(+), 3 deletions(-) diff --git a/spring-test/src/test/java/org/springframework/test/web/client/match/ContentRequestMatchersTests.java b/spring-test/src/test/java/org/springframework/test/web/client/match/ContentRequestMatchersTests.java index d4eef94bac5d..ee76368ab92d 100644 --- a/spring-test/src/test/java/org/springframework/test/web/client/match/ContentRequestMatchersTests.java +++ b/spring-test/src/test/java/org/springframework/test/web/client/match/ContentRequestMatchersTests.java @@ -24,6 +24,7 @@ import org.springframework.http.MediaType; import org.springframework.mock.http.client.MockClientHttpRequest; +import org.springframework.test.json.JsonCompareMode; import org.springframework.util.LinkedMultiValueMap; import org.springframework.util.MultiValueMap; @@ -234,6 +235,16 @@ public void testJsonLenientMatch() throws Exception { MockRestRequestMatchers.content().json("{\n \"foo array\":[\"second\",\"first\"] \n}") .match(this.request); + MockRestRequestMatchers.content().json("{\n \"foo array\":[\"second\",\"first\"] \n}", JsonCompareMode.LENIENT) + .match(this.request); + } + + @Test + @Deprecated + public void testJsonLenientMatchWithDeprecatedBooleanFlag() throws Exception { + String content = "{\n \"foo array\":[\"first\",\"second\"] , \"someExtraProperty\": \"which is allowed\" \n}"; + this.request.getBody().write(content.getBytes()); + MockRestRequestMatchers.content().json("{\n \"foo array\":[\"second\",\"first\"] \n}", false) .match(this.request); } @@ -243,6 +254,18 @@ public void testJsonStrictMatch() throws Exception { String content = "{\n \"foo\": \"bar\", \"foo array\":[\"first\",\"second\"] \n}"; this.request.getBody().write(content.getBytes()); + MockRestRequestMatchers + .content() + .json("{\n \"foo array\":[\"first\",\"second\"] , \"foo\": \"bar\" \n}", JsonCompareMode.STRICT) + .match(this.request); + } + + @Test + @Deprecated + public void testJsonStrictMatchWithDeprecatedBooleanFlag() throws Exception { + String content = "{\n \"foo\": \"bar\", \"foo array\":[\"first\",\"second\"] \n}"; + this.request.getBody().write(content.getBytes()); + MockRestRequestMatchers .content() .json("{\n \"foo array\":[\"first\",\"second\"] , \"foo\": \"bar\" \n}", true) @@ -259,6 +282,19 @@ public void testJsonLenientNoMatch() throws Exception { .content() .json("{\n \"foo\" : \"bar\" \n}") .match(this.request)); + assertThatExceptionOfType(AssertionError.class).isThrownBy(() -> + MockRestRequestMatchers + .content() + .json("{\n \"foo\" : \"bar\" \n}", JsonCompareMode.LENIENT) + .match(this.request)); + } + + @Test + @Deprecated + public void testJsonLenientNoMatchWithDeprecatedBooleanFlag() throws Exception { + String content = "{\n \"bar\" : \"foo\" \n}"; + this.request.getBody().write(content.getBytes()); + assertThatExceptionOfType(AssertionError.class).isThrownBy(() -> MockRestRequestMatchers .content() @@ -271,6 +307,19 @@ public void testJsonStrictNoMatch() throws Exception { String content = "{\n \"foo array\":[\"first\",\"second\"] , \"someExtraProperty\": \"which is NOT allowed\" \n}"; this.request.getBody().write(content.getBytes()); + assertThatExceptionOfType(AssertionError.class).isThrownBy(() -> + MockRestRequestMatchers + .content() + .json("{\n \"foo array\":[\"second\",\"first\"] \n}", JsonCompareMode.STRICT) + .match(this.request)); + } + + @Test + @Deprecated + public void testJsonStrictNoMatchWithDeprecatedBooleanFlag() throws Exception { + String content = "{\n \"foo array\":[\"first\",\"second\"] , \"someExtraProperty\": \"which is NOT allowed\" \n}"; + this.request.getBody().write(content.getBytes()); + assertThatExceptionOfType(AssertionError.class).isThrownBy(() -> MockRestRequestMatchers .content() diff --git a/spring-test/src/test/java/org/springframework/test/web/servlet/result/ContentResultMatchersTests.java b/spring-test/src/test/java/org/springframework/test/web/servlet/result/ContentResultMatchersTests.java index fa98d3d1ea2d..4c28ae7fb938 100644 --- a/spring-test/src/test/java/org/springframework/test/web/servlet/result/ContentResultMatchersTests.java +++ b/spring-test/src/test/java/org/springframework/test/web/servlet/result/ContentResultMatchersTests.java @@ -20,6 +20,7 @@ import org.junit.jupiter.api.Test; import org.springframework.mock.web.MockHttpServletResponse; +import org.springframework.test.json.JsonCompareMode; import org.springframework.test.web.servlet.StubMvcResult; import static java.nio.charset.StandardCharsets.UTF_8; @@ -80,13 +81,31 @@ void bytesNoMatch() { @Test void jsonLenientMatch() throws Exception { new ContentResultMatchers().json("{\n \"foo\" : \"bar\" \n}").match(getStubMvcResult(CONTENT)); + new ContentResultMatchers().json("{\n \"foo\" : \"bar\" \n}", + JsonCompareMode.LENIENT).match(getStubMvcResult(CONTENT)); + } + + @Test + @Deprecated + void jsonLenientMatchWithDeprecatedBooleanFlag() throws Exception { new ContentResultMatchers().json("{\n \"foo\" : \"bar\" \n}", false).match(getStubMvcResult(CONTENT)); } @Test void jsonStrictMatch() throws Exception { - new ContentResultMatchers().json("{\n \"foo\":\"bar\", \"foo array\":[\"foo\",\"bar\"] \n}", true).match(getStubMvcResult(CONTENT)); - new ContentResultMatchers().json("{\n \"foo array\":[\"foo\",\"bar\"], \"foo\":\"bar\" \n}", true).match(getStubMvcResult(CONTENT)); + new ContentResultMatchers().json("{\n \"foo\":\"bar\", \"foo array\":[\"foo\",\"bar\"] \n}", + JsonCompareMode.STRICT).match(getStubMvcResult(CONTENT)); + new ContentResultMatchers().json("{\n \"foo array\":[\"foo\",\"bar\"], \"foo\":\"bar\" \n}", + JsonCompareMode.STRICT).match(getStubMvcResult(CONTENT)); + } + + @Test + @Deprecated + void jsonStrictMatchWithDeprecatedBooleanFlag() throws Exception { + new ContentResultMatchers().json("{\n \"foo\":\"bar\", \"foo array\":[\"foo\",\"bar\"] \n}", true) + .match(getStubMvcResult(CONTENT)); + new ContentResultMatchers().json("{\n \"foo array\":[\"foo\",\"bar\"], \"foo\":\"bar\" \n}", true) + .match(getStubMvcResult(CONTENT)); } @Test @@ -98,7 +117,16 @@ void jsonLenientNoMatch() { @Test void jsonStrictNoMatch() { assertThatExceptionOfType(AssertionError.class).isThrownBy(() -> - new ContentResultMatchers().json("{\"foo\":\"bar\", \"foo array\":[\"bar\",\"foo\"]}", true).match(getStubMvcResult(CONTENT))); + new ContentResultMatchers().json("{\"foo\":\"bar\", \"foo array\":[\"bar\",\"foo\"]}", + JsonCompareMode.STRICT).match(getStubMvcResult(CONTENT))); + } + + @Test + @Deprecated + void jsonStrictNoMatchWithDeprecatedBooleanFlag() { + assertThatExceptionOfType(AssertionError.class).isThrownBy(() -> + new ContentResultMatchers().json("{\"foo\":\"bar\", \"foo array\":[\"bar\",\"foo\"]}", true) + .match(getStubMvcResult(CONTENT))); } @Test // gh-23622