### Question:
OAuthTokenSafe { public String getToken() { if (isTokenInvalid()) { refreshToken(); } return token != null ? token.getAccessToken() : null; } OAuthTokenSafe(TrasierClientConfiguration clientConfiguration, String authUrl, AsyncHttpClient client); String getToken(); void refreshToken();  }### Answer:
@Test public void testRefreshTokenKeepsFailing() { when(response.getStatusCode()).thenReturn(404); OAuthTokenSafe sut = new OAuthTokenSafe(clientConfig, appConfig.getAuthEndpoint(), client); String token1 = sut.getToken(); String token2 = sut.getToken(); assertNull(token1); assertNull(token2); verify(client, times(2)).executeRequest(any(Request.class), any(AsyncHandler.class)); }
@Test public void testRefreshTokenFailedOnceOnce() throws JsonProcessingException { OAuthToken token = new OAuthToken(); token.setAccessToken("accessTokenMock"); token.setRefreshToken("refreshTokenMock"); token.setExpiresIn("" + (80 * 1000)); token.setRefreshExpiresIn("" + (160 * 1000)); when(response.getStatusCode()).thenReturn(404).thenReturn(200); when(response.getResponseBody()).thenReturn(mapper.writeValueAsString(token)); OAuthTokenSafe sut = new OAuthTokenSafe(clientConfig, appConfig.getAuthEndpoint(), client); String token1 = sut.getToken(); String token2 = sut.getToken(); assertNull(token1); assertNotNull(token2); verify(client, times(2)).executeRequest(any(Request.class), any(AsyncHandler.class)); }
@Test public void testRefreshTokenRequestedOnce() throws JsonProcessingException { OAuthToken token = new OAuthToken(); token.setAccessToken("accessTokenMock"); token.setRefreshToken("refreshTokenMock"); token.setExpiresIn("" + (80 * 1000)); token.setRefreshExpiresIn("" + (160 * 1000)); when(response.getStatusCode()).thenReturn(200); when(response.getResponseBody()).thenReturn(mapper.writeValueAsString(token)); OAuthTokenSafe sut = new OAuthTokenSafe(clientConfig, appConfig.getAuthEndpoint(), client); sut.getToken(); sut.getToken(); verify(client, times(1)).executeRequest(any(Request.class), any(AsyncHandler.class)); }
@Test public void testRefreshExpiredToken() throws JsonProcessingException { OAuthToken token = new OAuthToken(); token.setAccessToken("accessTokenMock"); token.setRefreshToken("refreshTokenMock"); token.setExpiresIn("" + (-80 * 1000)); token.setRefreshExpiresIn("" + (-40 * 1000)); when(response.getStatusCode()).thenReturn(200); when(response.getResponseBody()).thenReturn(mapper.writeValueAsString(token)); OAuthTokenSafe sut = new OAuthTokenSafe(clientConfig, appConfig.getAuthEndpoint(), client); sut.getToken(); sut.getToken(); verify(client, times(2)).executeRequest(any(Request.class), any(AsyncHandler.class)); }