Skip to content

Commit

Permalink
Flag tests using deprecated json method deprecated
Browse files Browse the repository at this point in the history
  • Loading branch information
snicoll committed May 24, 2024
1 parent 0110c5a commit 97647ef
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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);
}
Expand All @@ -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)
Expand All @@ -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()
Expand All @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down

0 comments on commit 97647ef

Please sign in to comment.