SecurityContextTokenProvider extends AbstractStupsAccessTokenProvider { @Override public OAuth2AccessToken obtainAccessToken(final OAuth2ProtectedResourceDetails details, final AccessTokenRequest parameters) { final Optional<String> accessToken = AccessTokenUtils.getAccessTokenFromSecurityContext(); if(!accessToken.isPresent()){ throw new OAuth2Exception("No access token available in current security context"); } final Map<String, String> tokenParams = new HashMap<>(); tokenParams.put(ACCESS_TOKEN, accessToken.get()); tokenParams.put(TOKEN_TYPE, BEARER_TYPE); return DefaultOAuth2AccessToken.valueOf(tokenParams); }  @Override OAuth2AccessToken obtainAccessToken(final OAuth2ProtectedResourceDetails details,
            final AccessTokenRequest parameters);  }
@Test public void testObtainAccessToken() { final OAuth2AccessToken oAuth2AccessToken = tokenProvider.obtainAccessToken(mock( OAuth2ProtectedResourceDetails.class), mock(AccessTokenRequest.class)); assertThat(oAuth2AccessToken).isNotNull(); assertThat(oAuth2AccessToken.getValue()).isEqualTo("1234567890"); }
@Test(expected = OAuth2Exception.class) public void testTokenUnavailable() { SecurityContextHolder.clearContext(); tokenProvider.obtainAccessToken(mock(OAuth2ProtectedResourceDetails.class), mock(AccessTokenRequest.class)); }