Polishing
This commit is contained in:
@@ -16,11 +16,11 @@
|
||||
|
||||
package org.springframework.http.converter.json;
|
||||
|
||||
|
||||
import java.net.URI;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.skyscreamer.jsonassert.JSONAssert;
|
||||
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ProblemDetail;
|
||||
@@ -34,64 +34,73 @@ import static org.assertj.core.api.Assertions.assertThat;
|
||||
* @author Rossen Stoyanchev
|
||||
* @since 6.0
|
||||
*/
|
||||
public class ProblemDetailJacksonMixinTests {
|
||||
class ProblemDetailJacksonMixinTests {
|
||||
|
||||
private final ObjectMapper mapper = new Jackson2ObjectMapperBuilder().build();
|
||||
|
||||
|
||||
@Test
|
||||
void writeStatusAndHeaders() throws Exception {
|
||||
testWrite(
|
||||
ProblemDetail.forStatusAndDetail(HttpStatus.BAD_REQUEST, "Missing header"),
|
||||
"{\"type\":\"about:blank\"," +
|
||||
"\"title\":\"Bad Request\"," +
|
||||
"\"status\":400," +
|
||||
"\"detail\":\"Missing header\"}");
|
||||
ProblemDetail detail = ProblemDetail.forStatusAndDetail(HttpStatus.BAD_REQUEST, "Missing header");
|
||||
testWrite(detail,
|
||||
"""
|
||||
{
|
||||
"type": "about:blank",
|
||||
"title": "Bad Request",
|
||||
"status": 400,
|
||||
"detail": "Missing header"
|
||||
}""");
|
||||
}
|
||||
|
||||
@Test
|
||||
void writeCustomProperty() throws Exception {
|
||||
ProblemDetail problemDetail = ProblemDetail.forStatusAndDetail(HttpStatus.BAD_REQUEST, "Missing header");
|
||||
problemDetail.setProperty("host", "abc.org");
|
||||
problemDetail.setProperty("user", null);
|
||||
ProblemDetail detail = ProblemDetail.forStatusAndDetail(HttpStatus.BAD_REQUEST, "Missing header");
|
||||
detail.setProperty("host", "abc.org");
|
||||
detail.setProperty("user", null);
|
||||
|
||||
testWrite(problemDetail,
|
||||
"{\"type\":\"about:blank\"," +
|
||||
"\"title\":\"Bad Request\"," +
|
||||
"\"status\":400," +
|
||||
"\"detail\":\"Missing header\"," +
|
||||
"\"host\":\"abc.org\"," +
|
||||
"\"user\":null}");
|
||||
testWrite(detail, """
|
||||
{
|
||||
"type": "about:blank",
|
||||
"title": "Bad Request",
|
||||
"status": 400,
|
||||
"detail": "Missing header",
|
||||
"host": "abc.org",
|
||||
"user": null
|
||||
}""");
|
||||
}
|
||||
|
||||
@Test
|
||||
void readCustomProperty() throws Exception {
|
||||
ProblemDetail detail = this.mapper.readValue(
|
||||
"{\"type\":\"about:blank\"," +
|
||||
"\"title\":\"Bad Request\"," +
|
||||
"\"status\":400," +
|
||||
"\"detail\":\"Missing header\"," +
|
||||
"\"host\":\"abc.org\"," +
|
||||
"\"user\":null}", ProblemDetail.class);
|
||||
ProblemDetail detail = this.mapper.readValue("""
|
||||
{
|
||||
"type": "about:blank",
|
||||
"title": "Bad Request",
|
||||
"status": 400,
|
||||
"detail": "Missing header",
|
||||
"host": "abc.org",
|
||||
"user": null
|
||||
}""", ProblemDetail.class);
|
||||
|
||||
assertThat(detail.getType()).isEqualTo(URI.create("about:blank"));
|
||||
assertThat(detail.getTitle()).isEqualTo("Bad Request");
|
||||
assertThat(detail.getStatus()).isEqualTo(HttpStatus.BAD_REQUEST.value());
|
||||
assertThat(detail.getDetail()).isEqualTo("Missing header");
|
||||
assertThat(detail.getProperties()).containsEntry("host", "abc.org");
|
||||
assertThat(detail.getProperties()).containsEntry("user", null);
|
||||
assertThat(detail.getProperties())
|
||||
.containsEntry("host", "abc.org")
|
||||
.containsEntry("user", null);
|
||||
}
|
||||
|
||||
@Test
|
||||
void readCustomPropertyFromXml() throws Exception {
|
||||
ObjectMapper xmlMapper = new Jackson2ObjectMapperBuilder().createXmlMapper(true).build();
|
||||
ProblemDetail detail = xmlMapper.readValue(
|
||||
"<problem xmlns=\"urn:ietf:rfc:7807\">" +
|
||||
"<type>about:blank</type>" +
|
||||
"<title>Bad Request</title>" +
|
||||
"<status>400</status>" +
|
||||
"<detail>Missing header</detail>" +
|
||||
"<host>abc.org</host>" +
|
||||
"</problem>", ProblemDetail.class);
|
||||
ProblemDetail detail = xmlMapper.readValue("""
|
||||
<problem xmlns="urn:ietf:rfc:7807">
|
||||
<type>about:blank</type>
|
||||
<title>Bad Request</title>
|
||||
<status>400</status>
|
||||
<detail>Missing header</detail>
|
||||
<host>abc.org</host>
|
||||
</problem>""", ProblemDetail.class);
|
||||
|
||||
assertThat(detail.getType()).isEqualTo(URI.create("about:blank"));
|
||||
assertThat(detail.getTitle()).isEqualTo("Bad Request");
|
||||
@@ -102,7 +111,7 @@ public class ProblemDetailJacksonMixinTests {
|
||||
|
||||
private void testWrite(ProblemDetail problemDetail, String expected) throws Exception {
|
||||
String output = this.mapper.writeValueAsString(problemDetail);
|
||||
assertThat(output).isEqualTo(expected);
|
||||
JSONAssert.assertEquals(expected, output, false);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user