1818
1919import java .util .Collections ;
2020import java .util .Map ;
21+ import java .util .function .Consumer ;
2122
2223import org .junit .jupiter .api .Test ;
2324
25+ import org .springframework .security .authentication .TestingAuthenticationToken ;
2426import org .springframework .security .core .Authentication ;
2527import org .springframework .security .oauth2 .client .registration .ClientRegistration ;
2628import org .springframework .security .oauth2 .client .registration .ClientRegistrationRepository ;
2729import org .springframework .security .oauth2 .client .registration .InMemoryClientRegistrationRepository ;
2830import org .springframework .security .oauth2 .client .registration .TestClientRegistrations ;
2931import org .springframework .security .oauth2 .core .OAuth2AccessToken ;
32+ import org .springframework .security .oauth2 .core .OAuth2RefreshToken ;
3033
3134import static org .assertj .core .api .Assertions .assertThat ;
3235import static org .assertj .core .api .Assertions .assertThatIllegalArgumentException ;
@@ -126,35 +129,35 @@ public void loadAuthorizedClientWhenClientRegistrationFoundAndAssociatedToPrinci
126129 this .authorizedClientService .saveAuthorizedClient (authorizedClient , authentication );
127130 OAuth2AuthorizedClient loadedAuthorizedClient = this .authorizedClientService
128131 .loadAuthorizedClient (this .registration1 .getRegistrationId (), this .principalName1 );
129- assertAuthorizedClientEquals ( authorizedClient , loadedAuthorizedClient );
132+ assertThat ( loadedAuthorizedClient ). satisfies ( isEqualTo ( authorizedClient ) );
130133 }
131134
132135 @ Test
133136 public void loadAuthorizedClientWhenClientRegistrationIsUpdatedThenReturnAuthorizedClientWithUpdatedClientRegistration () {
134137 ClientRegistration updatedRegistration = ClientRegistration .withClientRegistration (this .registration1 )
135138 .clientSecret ("updated secret" )
136139 .build ();
137- ClientRegistrationRepository repository = mock (ClientRegistrationRepository .class );
138- given (repository .findByRegistrationId (this .registration1 .getRegistrationId ())).willReturn (this .registration1 ,
139- updatedRegistration );
140140
141- Authentication authentication = mock (Authentication .class );
142- given (authentication .getName ()).willReturn (this .principalName1 );
141+ ClientRegistrationRepository clientRegistrationRepository = mock (ClientRegistrationRepository .class );
142+ given (clientRegistrationRepository .findByRegistrationId (this .registration1 .getRegistrationId ()))
143+ .willReturn (this .registration1 , updatedRegistration );
143144
144- InMemoryOAuth2AuthorizedClientService service = new InMemoryOAuth2AuthorizedClientService (repository );
145+ InMemoryOAuth2AuthorizedClientService authorizedClientService = new InMemoryOAuth2AuthorizedClientService (
146+ clientRegistrationRepository );
145147
146- OAuth2AuthorizedClient authorizedClient = new OAuth2AuthorizedClient (this .registration1 , this .principalName1 ,
147- mock (OAuth2AccessToken .class ));
148- service .saveAuthorizedClient (authorizedClient , authentication );
148+ OAuth2AuthorizedClient cachedAuthorizedClient = new OAuth2AuthorizedClient (this .registration1 ,
149+ this .principalName1 , mock (OAuth2AccessToken .class ), mock (OAuth2RefreshToken .class ));
150+ authorizedClientService .saveAuthorizedClient (cachedAuthorizedClient ,
151+ new TestingAuthenticationToken (this .principalName1 , null ));
149152
150153 OAuth2AuthorizedClient authorizedClientWithUpdatedRegistration = new OAuth2AuthorizedClient (updatedRegistration ,
151- this .principalName1 , mock (OAuth2AccessToken .class ));
152- OAuth2AuthorizedClient firstLoadedClient = service . loadAuthorizedClient ( this . registration1 . getRegistrationId (),
153- this .principalName1 );
154- OAuth2AuthorizedClient secondLoadedClient = service . loadAuthorizedClient ( this . registration1 . getRegistrationId (),
155- this .principalName1 );
156- assertAuthorizedClientEquals ( authorizedClient , firstLoadedClient );
157- assertAuthorizedClientEquals ( authorizedClientWithUpdatedRegistration , secondLoadedClient );
154+ this .principalName1 , mock (OAuth2AccessToken .class ), mock ( OAuth2RefreshToken . class ) );
155+ OAuth2AuthorizedClient firstLoadedClient = authorizedClientService
156+ . loadAuthorizedClient ( this . registration1 . getRegistrationId (), this .principalName1 );
157+ OAuth2AuthorizedClient secondLoadedClient = authorizedClientService
158+ . loadAuthorizedClient ( this . registration1 . getRegistrationId (), this .principalName1 );
159+ assertThat ( firstLoadedClient ). satisfies ( isEqualTo ( cachedAuthorizedClient ) );
160+ assertThat ( secondLoadedClient ). satisfies ( isEqualTo ( authorizedClientWithUpdatedRegistration ) );
158161 }
159162
160163 @ Test
@@ -178,7 +181,7 @@ public void saveAuthorizedClientWhenSavedThenCanLoad() {
178181 this .authorizedClientService .saveAuthorizedClient (authorizedClient , authentication );
179182 OAuth2AuthorizedClient loadedAuthorizedClient = this .authorizedClientService
180183 .loadAuthorizedClient (this .registration3 .getRegistrationId (), this .principalName2 );
181- assertAuthorizedClientEquals ( authorizedClient , loadedAuthorizedClient );
184+ assertThat ( loadedAuthorizedClient ). satisfies ( isEqualTo ( authorizedClient ) );
182185 }
183186
184187 @ Test
@@ -210,29 +213,38 @@ public void removeAuthorizedClientWhenSavedThenRemoved() {
210213 assertThat (loadedAuthorizedClient ).isNull ();
211214 }
212215
213- private static void assertAuthorizedClientEquals (OAuth2AuthorizedClient expected , OAuth2AuthorizedClient actual ) {
214- assertThat (actual ).isNotNull ();
215- assertThat (actual .getClientRegistration ().getRegistrationId ())
216- .isEqualTo (expected .getClientRegistration ().getRegistrationId ());
217- assertThat (actual .getClientRegistration ().getClientName ())
218- .isEqualTo (expected .getClientRegistration ().getClientName ());
219- assertThat (actual .getClientRegistration ().getRedirectUri ())
220- .isEqualTo (expected .getClientRegistration ().getRedirectUri ());
221- assertThat (actual .getClientRegistration ().getAuthorizationGrantType ())
222- .isEqualTo (expected .getClientRegistration ().getAuthorizationGrantType ());
223- assertThat (actual .getClientRegistration ().getClientAuthenticationMethod ())
224- .isEqualTo (expected .getClientRegistration ().getClientAuthenticationMethod ());
225- assertThat (actual .getClientRegistration ().getClientId ())
226- .isEqualTo (expected .getClientRegistration ().getClientId ());
227- assertThat (actual .getClientRegistration ().getClientSecret ())
228- .isEqualTo (expected .getClientRegistration ().getClientSecret ());
229- assertThat (actual .getPrincipalName ()).isEqualTo (expected .getPrincipalName ());
230- assertThat (actual .getAccessToken ().getTokenType ()).isEqualTo (expected .getAccessToken ().getTokenType ());
231- assertThat (actual .getAccessToken ().getTokenValue ()).isEqualTo (expected .getAccessToken ().getTokenValue ());
232- assertThat (actual .getAccessToken ().getIssuedAt ()).isEqualTo (expected .getAccessToken ().getIssuedAt ());
233- assertThat (actual .getAccessToken ().getExpiresAt ()).isEqualTo (expected .getAccessToken ().getExpiresAt ());
234- assertThat (actual .getAccessToken ().getScopes ()).isEqualTo (expected .getAccessToken ().getScopes ());
235- assertThat (actual .getRefreshToken ()).isEqualTo (expected .getRefreshToken ());
216+ private static Consumer <OAuth2AuthorizedClient > isEqualTo (OAuth2AuthorizedClient expected ) {
217+ return (actual ) -> {
218+ assertThat (actual ).isNotNull ();
219+ assertThat (actual .getClientRegistration ().getRegistrationId ())
220+ .isEqualTo (expected .getClientRegistration ().getRegistrationId ());
221+ assertThat (actual .getClientRegistration ().getClientName ())
222+ .isEqualTo (expected .getClientRegistration ().getClientName ());
223+ assertThat (actual .getClientRegistration ().getRedirectUri ())
224+ .isEqualTo (expected .getClientRegistration ().getRedirectUri ());
225+ assertThat (actual .getClientRegistration ().getAuthorizationGrantType ())
226+ .isEqualTo (expected .getClientRegistration ().getAuthorizationGrantType ());
227+ assertThat (actual .getClientRegistration ().getClientAuthenticationMethod ())
228+ .isEqualTo (expected .getClientRegistration ().getClientAuthenticationMethod ());
229+ assertThat (actual .getClientRegistration ().getClientId ())
230+ .isEqualTo (expected .getClientRegistration ().getClientId ());
231+ assertThat (actual .getClientRegistration ().getClientSecret ())
232+ .isEqualTo (expected .getClientRegistration ().getClientSecret ());
233+ assertThat (actual .getPrincipalName ()).isEqualTo (expected .getPrincipalName ());
234+ assertThat (actual .getAccessToken ().getTokenType ()).isEqualTo (expected .getAccessToken ().getTokenType ());
235+ assertThat (actual .getAccessToken ().getTokenValue ()).isEqualTo (expected .getAccessToken ().getTokenValue ());
236+ assertThat (actual .getAccessToken ().getIssuedAt ()).isEqualTo (expected .getAccessToken ().getIssuedAt ());
237+ assertThat (actual .getAccessToken ().getExpiresAt ()).isEqualTo (expected .getAccessToken ().getExpiresAt ());
238+ assertThat (actual .getAccessToken ().getScopes ()).isEqualTo (expected .getAccessToken ().getScopes ());
239+ if (expected .getRefreshToken () != null ) {
240+ assertThat (actual .getRefreshToken ()).isNotNull ();
241+ assertThat (actual .getRefreshToken ().getTokenValue ())
242+ .isEqualTo (expected .getRefreshToken ().getTokenValue ());
243+ assertThat (actual .getRefreshToken ().getIssuedAt ()).isEqualTo (expected .getRefreshToken ().getIssuedAt ());
244+ assertThat (actual .getRefreshToken ().getExpiresAt ())
245+ .isEqualTo (expected .getRefreshToken ().getExpiresAt ());
246+ }
247+ };
236248 }
237249
238250}
0 commit comments