StupsTokensAccessTokenProvider extends AbstractStupsAccessTokenProvider { @Override public OAuth2AccessToken obtainAccessToken(final OAuth2ProtectedResourceDetails details, final AccessTokenRequest parameters) { final AccessToken accessToken; try { accessToken = tokens.getAccessToken(tokenId); } catch (final AccessTokenUnavailableException e) { throw new OAuth2Exception("Could not obtain access token.", e); } final Map<String, String> tokenParams = new HashMap<>(); tokenParams.put(ACCESS_TOKEN, accessToken.getToken()); tokenParams.put(TOKEN_TYPE, OAuth2AccessToken.BEARER_TYPE); if (accessToken.getValidUntil() != null) { tokenParams.put(EXPIRES_IN, secondsTo(accessToken.getValidUntil())); } return DefaultOAuth2AccessToken.valueOf(tokenParams); } StupsTokensAccessTokenProvider(String tokenId, AccessTokens tokens); @Override OAuth2AccessToken obtainAccessToken(final OAuth2ProtectedResourceDetails details,
            final AccessTokenRequest parameters);  }
@Test public void testObtainAccessToken() { when(mockAccessTokens.getAccessToken(anyString())).thenReturn( new AccessToken("12345", "bearer", 3600, tomorrow())); final OAuth2AccessToken accessToken = accessTokenProvider.obtainAccessToken( new BaseOAuth2ProtectedResourceDetails(), new DefaultAccessTokenRequest()); assertThat(accessToken).isNotNull(); assertThat(accessToken.getValue()).isEqualTo("12345"); assertThat(accessToken.getTokenType()).isEqualTo("Bearer"); assertThat(accessToken.isExpired()).isFalse(); verify(mockAccessTokens).getAccessToken(eq(TOKEN_ID)); }
@Test public void testObtainExpiredAccessToken() { when(mockAccessTokens.getAccessToken(anyString())).thenReturn( new AccessToken("12345", "bearer", 3600, yesterday())); final OAuth2AccessToken accessToken = accessTokenProvider.obtainAccessToken( new BaseOAuth2ProtectedResourceDetails(), new DefaultAccessTokenRequest()); assertThat(accessToken).isNotNull(); assertThat(accessToken.getValue()).isEqualTo("12345"); assertThat(accessToken.getTokenType()).isEqualTo("Bearer"); assertThat(accessToken.isExpired()).isTrue(); verify(mockAccessTokens).getAccessToken(eq(TOKEN_ID)); }
@Test public void testObtainAccessTokenException() { final AccessTokenUnavailableException unavailableAccessToken = new AccessTokenUnavailableException(); when(mockAccessTokens.getAccessToken(any())).thenThrow(unavailableAccessToken); try { accessTokenProvider.obtainAccessToken( new BaseOAuth2ProtectedResourceDetails(), new DefaultAccessTokenRequest()); failBecauseExceptionWasNotThrown(OAuth2Exception.class); } catch (OAuth2Exception e) { assertThat(e).hasCause(unavailableAccessToken); verify(mockAccessTokens).getAccessToken(eq(TOKEN_ID)); } }
@Test public void testObtainAccessWithValidUntilNull() { when(mockAccessTokens.getAccessToken(anyString())).thenReturn( new AccessToken("12345", "bearer", 3600, null)); final OAuth2AccessToken accessToken = accessTokenProvider.obtainAccessToken( new BaseOAuth2ProtectedResourceDetails(), new DefaultAccessTokenRequest()); assertThat(accessToken).isNotNull(); assertThat(accessToken.getExpiresIn()).isEqualTo(0); assertThat(accessToken.getExpiration()).isNull(); assertThat(accessToken.isExpired()).isFalse(); verify(mockAccessTokens).getAccessToken(eq(TOKEN_ID)); }