22
33import static org .assertj .core .api .Assertions .assertThat ;
44
5+ import com .retailsvc .http .internal .ProblemDetail .Entry ;
56import java .nio .charset .StandardCharsets ;
7+ import java .util .List ;
68import org .junit .jupiter .api .Test ;
79
810class ProblemDetailRendererTest {
911
1012 @ Test
11- void rendersAllFieldsWhenPresent () {
13+ void rendersSingleEntryErrorsArray () {
1214 ProblemDetail pd =
13- new ProblemDetail ("about:blank" , "Bad Request" , 400 , "expected string" , "/x" , "type" );
15+ new ProblemDetail (
16+ "about:blank" ,
17+ "Bad Request" ,
18+ 400 ,
19+ "expected string" ,
20+ List .of (new Entry ("#/x" , "type" , "expected string" )));
1421 assertThat (asString (ProblemDetailRenderer .renderJson (pd )))
1522 .isEqualTo (
1623 "{\" type\" :\" about:blank\" ,\" title\" :\" Bad Request\" ,\" status\" :400,"
17- + "\" detail\" :\" expected string\" ,\" pointer\" :\" /x\" ,\" keyword\" :\" type\" }" );
24+ + "\" detail\" :\" expected string\" ,\" errors\" :[{\" pointer\" :\" #/x\" ,"
25+ + "\" keyword\" :\" type\" ,\" detail\" :\" expected string\" }]}" );
1826 }
1927
2028 @ Test
21- void omitsNullPointerAndKeyword () {
29+ void rendersMultipleErrorEntries () {
2230 ProblemDetail pd =
23- new ProblemDetail ("about:blank" , "Unauthorized" , 401 , "missing token" , null , null );
31+ new ProblemDetail (
32+ "about:blank" ,
33+ "Bad Request" ,
34+ 400 ,
35+ "matched 0 of 2 oneOf branches" ,
36+ List .of (
37+ new Entry ("#/collar/size" , "type" , "expected integer" ),
38+ new Entry ("#/bark" , "type" , "expected boolean" )));
39+ assertThat (asString (ProblemDetailRenderer .renderJson (pd )))
40+ .isEqualTo (
41+ "{\" type\" :\" about:blank\" ,\" title\" :\" Bad"
42+ + " Request\" ,\" status\" :400,\" detail\" :\" matched 0 of 2 oneOf"
43+ + " branches\" ,\" errors\" :[{\" pointer\" :\" #/collar/size\" ,\" keyword\" :\" type\" ,\" detail\" :\" expected"
44+ + " integer\" },{\" pointer\" :\" #/bark\" ,\" keyword\" :\" type\" ,\" detail\" :\" expected"
45+ + " boolean\" }]}" );
46+ }
47+
48+ @ Test
49+ void omitsKeywordWithinEntryWhenNull () {
50+ ProblemDetail pd =
51+ new ProblemDetail (
52+ "about:blank" ,
53+ "Unprocessable Content" ,
54+ 422 ,
55+ "email taken" ,
56+ List .of (new Entry ("#/email" , null , "email taken" )));
57+ assertThat (asString (ProblemDetailRenderer .renderJson (pd )))
58+ .isEqualTo (
59+ "{\" type\" :\" about:blank\" ,\" title\" :\" Unprocessable Content\" ,\" status\" :422,"
60+ + "\" detail\" :\" email taken\" ,\" errors\" :[{\" pointer\" :\" #/email\" ,"
61+ + "\" detail\" :\" email taken\" }]}" );
62+ }
63+
64+ @ Test
65+ void omitsEmptyErrorsArray () {
66+ ProblemDetail pd =
67+ new ProblemDetail ("about:blank" , "Unauthorized" , 401 , "missing token" , List .of ());
2468 assertThat (asString (ProblemDetailRenderer .renderJson (pd )))
2569 .isEqualTo (
2670 "{\" type\" :\" about:blank\" ,\" title\" :\" Unauthorized\" ,\" status\" :401,"
2771 + "\" detail\" :\" missing token\" }" );
2872 }
2973
3074 @ Test
31- void omitsNullDetail () {
32- ProblemDetail pd = new ProblemDetail ("about:blank" , "Not Found" , 404 , null , null , null );
75+ void omitsNullDetailAndEmptyErrors () {
76+ ProblemDetail pd = new ProblemDetail ("about:blank" , "Not Found" , 404 , null , List . of () );
3377 assertThat (asString (ProblemDetailRenderer .renderJson (pd )))
3478 .isEqualTo ("{\" type\" :\" about:blank\" ,\" title\" :\" Not Found\" ,\" status\" :404}" );
3579 }
3680
3781 @ Test
3882 void escapesQuoteAndBackslashInDetail () {
39- ProblemDetail pd = new ProblemDetail ("about:blank" , "Bad Request" , 400 , "a\" b\\ c" , null , null );
83+ ProblemDetail pd = new ProblemDetail ("about:blank" , "Bad Request" , 400 , "a\" b\\ c" , List . of () );
4084 assertThat (asString (ProblemDetailRenderer .renderJson (pd )))
4185 .isEqualTo (
4286 "{\" type\" :\" about:blank\" ,\" title\" :\" Bad Request\" ,\" status\" :400,"
@@ -46,25 +90,16 @@ void escapesQuoteAndBackslashInDetail() {
4690 @ Test
4791 void escapesNamedControlCharsInDetail () {
4892 ProblemDetail pd =
49- new ProblemDetail ("about:blank" , "Bad Request" , 400 , "\b \f \n \r \t " , null , null );
93+ new ProblemDetail ("about:blank" , "Bad Request" , 400 , "\b \f \n \r \t " , List . of () );
5094 assertThat (asString (ProblemDetailRenderer .renderJson (pd )))
5195 .isEqualTo (
5296 "{\" type\" :\" about:blank\" ,\" title\" :\" Bad Request\" ,\" status\" :400,"
5397 + "\" detail\" :\" \\ b\\ f\\ n\\ r\\ t\" }" );
5498 }
5599
56- @ Test
57- void escapesUnnamedControlCharsAsHexUnicode () {
58- ProblemDetail pd = new ProblemDetail ("about:blank" , "Bad Request" , 400 , "" , null , null );
59- assertThat (asString (ProblemDetailRenderer .renderJson (pd )))
60- .isEqualTo (
61- "{\" type\" :\" about:blank\" ,\" title\" :\" Bad Request\" ,\" status\" :400,"
62- + "\" detail\" :\" \\ u0001\\ u001f\" }" );
63- }
64-
65100 @ Test
66101 void passesThroughNonAsciiCharactersVerbatim () {
67- ProblemDetail pd = new ProblemDetail ("about:blank" , "Bad Request" , 400 , "café-é" , null , null );
102+ ProblemDetail pd = new ProblemDetail ("about:blank" , "Bad Request" , 400 , "café-é" , List . of () );
68103 assertThat (asString (ProblemDetailRenderer .renderJson (pd )))
69104 .isEqualTo (
70105 "{\" type\" :\" about:blank\" ,\" title\" :\" Bad"
0 commit comments