OAuthTokenProvider implements TokenProvider { private void generateAuthenticationContext() throws SmartlingApiException { if (accessTokenIsNotValid()) { synchronized (authApiClient) { if (accessTokenIsNotValid()) { if (refreshTokenIsValid()) { final String refreshToken = authenticationContext.getRefreshToken(); try { authenticationContext = authApiClient.refresh(refreshToken).retrieveData(); } catch (SmartlingApiException ex){ authenticationContext = null; logRefreshTokenDetails(refreshToken); throw ex; } } else { authenticationContext = authApiClient.authenticate(userId, userSecret).retrieveData(); } LOGGER.trace("Got a new access token which will expire at " + authenticationContext.calculateAccessTokenExpireTime()); } } } } OAuthTokenProvider(final String userId, final String userSecret, final AuthApiClient authApiClient); @Override AuthenticationToken getAuthenticationToken();  }
@Test public void testGenerateAuthenticationContext() throws Exception { AuthApiClient authApiClient = mock(AuthApiClient.class); OAuthTokenProvider oAuthTokenProvider = new OAuthTokenProvider("userId", "userSecret", authApiClient); Response<AuthenticationContext> response = new Response<>(); AuthenticationContext context = new AuthenticationContext(); context.setExpiresIn(1); context.setAccessToken("111"); context.setRefreshToken("222"); response.setData(context); response.setCode(ResponseCode.SUCCESS); when(authApiClient.authenticate(anyString(), anyString())).thenReturn(response); when(authApiClient.refresh(anyString())).thenReturn(response); oAuthTokenProvider.getAuthenticationToken(); verify(authApiClient).authenticate("userId", "userSecret"); context.setRefreshExpiresIn(1000); oAuthTokenProvider.getAuthenticationToken(); verify(authApiClient).refresh("222"); context.setRefreshExpiresIn(1); verify(authApiClient).authenticate("userId", "userSecret"); }