25
25
import org .springframework .security .jackson2 .SecurityJackson2Modules ;
26
26
import org .springframework .security .oauth2 .core .OAuth2AuthenticationException ;
27
27
import org .springframework .security .oauth2 .core .OAuth2Error ;
28
+ import org .springframework .security .oauth2 .core .endpoint .OAuth2AuthorizationRequest ;
28
29
29
30
import static org .assertj .core .api .Assertions .assertThat ;
31
+ import static org .assertj .core .api .Assertions .assertThatThrownBy ;
30
32
31
33
/**
32
34
* Tests for {@link OAuth2AuthenticationExceptionMixin}.
@@ -45,44 +47,88 @@ public void setup() {
45
47
this .mapper .registerModules (SecurityJackson2Modules .getModules (loader ));
46
48
}
47
49
48
- // @formatter:off
49
- private static final String EXCEPTION_JSON
50
- = "\n {"
51
- + "\n \" @class\" : \" org.springframework.security.oauth2.core.OAuth2AuthenticationException\" ,"
52
- + "\n \" error\" :"
53
- + "\n {"
54
- + "\n \" @class\" :\" org.springframework.security.oauth2.core.OAuth2Error\" ,"
55
- + "\n \" errorCode\" :\" authorization_request_not_found\" ,"
56
- + "\n \" description\" :null,"
57
- + "\n \" uri\" :null"
58
- + "\n },"
59
- + "\n \" message\" :\" [authorization_request_not_found] \" ,"
60
- + "\n \" suppressed\" :[\" [Ljava.lang.Throwable;\" ,[]],"
61
- + "\n \" localizedMessage\" :\" [authorization_request_not_found] \" "
62
- + "\n }" ;
63
- // @formatter:on
50
+ @ Test
51
+ public void serializeWhenMixinRegisteredThenSerializes () throws JsonProcessingException , JSONException {
52
+ OAuth2AuthenticationException exception = new OAuth2AuthenticationException (new OAuth2Error (
53
+ "[authorization_request_not_found] " ,
54
+ "Authorization Request Not Found" ,
55
+ "/foo/bar"
56
+ ), "Authorization Request Not Found" );
57
+
58
+ String serializedJson = mapper .writeValueAsString (exception );
59
+ String expected = asJson (exception );
60
+ JSONAssert .assertEquals (expected , serializedJson , true );
61
+ }
64
62
65
63
@ Test
66
- public void serializeOAuth2AuthenticationExceptionMixinTest () throws JsonProcessingException , JSONException {
67
- OAuth2Error oauth2Error = new OAuth2Error ("authorization_request_not_found" );
68
- OAuth2AuthenticationException exception = new OAuth2AuthenticationException (oauth2Error , oauth2Error .toString ());
64
+ public void serializeWhenRequiredAttributesOnlyThenSerializes () throws JsonProcessingException , JSONException {
65
+ OAuth2AuthenticationException exception = new OAuth2AuthenticationException (
66
+ new OAuth2Error ("[authorization_request_not_found] " )
67
+ );
68
+
69
69
String serializedJson = mapper .writeValueAsString (exception );
70
- JSONAssert .assertEquals (EXCEPTION_JSON , serializedJson , true );
70
+ String expected = asJson (exception );
71
+ JSONAssert .assertEquals (expected , serializedJson , true );
72
+ }
73
+
74
+ @ Test
75
+ public void deserializeWhenMixinRegisteredThenDeserializes () throws IOException {
76
+ OAuth2AuthenticationException expected = new OAuth2AuthenticationException (new OAuth2Error (
77
+ "[authorization_request_not_found] " ,
78
+ "Authorization Request Not Found" ,
79
+ "/foo/bar"
80
+ ), "Authorization Request Not Found" );
81
+
82
+ OAuth2AuthenticationException exception = mapper .readValue (asJson (expected ), OAuth2AuthenticationException .class );
83
+ assertThat (exception ).isNotNull ();
84
+ assertThat (exception .getCause ()).isNull ();
85
+ assertThat (exception .getMessage ()).isEqualTo (expected .getMessage ());
86
+
87
+ OAuth2Error oauth2Error = exception .getError ();
88
+ assertThat (oauth2Error ).isNotNull ();
89
+ assertThat (oauth2Error .getErrorCode ()).isEqualTo (expected .getError ().getErrorCode ());
90
+ assertThat (oauth2Error .getDescription ()).isEqualTo (expected .getError ().getDescription ());
91
+ assertThat (oauth2Error .getUri ()).isEqualTo (expected .getError ().getUri ());
71
92
}
72
93
73
94
@ Test
74
- public void deserializeOAuth2AuthenticationExceptionMixinTest () throws IOException {
75
- OAuth2AuthenticationException exception = mapper .readValue (EXCEPTION_JSON , OAuth2AuthenticationException .class );
95
+ public void deserializeWhenRequiredAttributesOnlyThenDeserializes () throws IOException {
96
+ OAuth2AuthenticationException expected = new OAuth2AuthenticationException (
97
+ new OAuth2Error ("[authorization_request_not_found] " )
98
+ );
99
+
100
+ OAuth2AuthenticationException exception = mapper .readValue (asJson (expected ), OAuth2AuthenticationException .class );
76
101
assertThat (exception ).isNotNull ();
77
102
assertThat (exception .getCause ()).isNull ();
78
- assertThat (exception .getMessage ()).isEqualTo ("[authorization_request_not_found] " );
79
- assertThat (exception .getLocalizedMessage ()).isEqualTo ("[authorization_request_not_found] " );
103
+ assertThat (exception .getMessage ()).isNull ();
80
104
81
105
OAuth2Error oauth2Error = exception .getError ();
82
106
assertThat (oauth2Error ).isNotNull ();
83
- assertThat (oauth2Error .getErrorCode ()).isEqualTo ("authorization_request_not_found" );
107
+ assertThat (oauth2Error .getErrorCode ()).isEqualTo (expected . getError (). getErrorCode () );
84
108
assertThat (oauth2Error .getDescription ()).isNull ();
85
109
assertThat (oauth2Error .getUri ()).isNull ();
86
110
}
87
111
112
+ private String asJson (OAuth2AuthenticationException exception ) {
113
+ OAuth2Error error = exception .getError ();
114
+ // @formatter:off
115
+ return "\n {"
116
+ + "\n \" @class\" : \" org.springframework.security.oauth2.core.OAuth2AuthenticationException\" ,"
117
+ + "\n \" error\" :"
118
+ + "\n {"
119
+ + "\n \" @class\" :\" org.springframework.security.oauth2.core.OAuth2Error\" ,"
120
+ + "\n \" errorCode\" :\" " + error .getErrorCode () + "\" ,"
121
+ + "\n \" description\" :" + jsonStringOrNull (error .getDescription ()) + ","
122
+ + "\n \" uri\" :" + jsonStringOrNull (error .getUri ())
123
+ + "\n },"
124
+ + "\n \" detailMessage\" :" + jsonStringOrNull (exception .getMessage ())
125
+ + "\n }" ;
126
+ // @formatter:on
127
+ }
128
+
129
+ private String jsonStringOrNull (String input ) {
130
+ return input != null
131
+ ? "\" " + input + "\" "
132
+ : "null" ;
133
+ }
88
134
}
0 commit comments