OAuthCredentialsCache { HeaderToken getHeader(long timeout, TimeUnit timeUnit) { try { return getHeaderUnsafe(timeout, timeUnit).getToken(); } catch (Exception e) { LOG.warn("Got an unexpected exception while trying to refresh google credentials.", e); Status status = Status.UNAUTHENTICATED.withDescription("Unexpected failure get auth token").withCause(e); return new HeaderToken(status, null); } } OAuthCredentialsCache(ExecutorService scheduler, OAuth2Credentials credentials);   }
@SuppressWarnings("unchecked") @Test public void testExecutorProblem() throws Exception { ExecutorService mockExecutor = Mockito.mock(ExecutorService.class); when(mockExecutor.submit(any(Callable.class))).thenThrow(new RuntimeException("")); underTest = new OAuthCredentialsCache(mockExecutor, mockCredentials); Assert.assertFalse(underTest.getHeader(TIMEOUT_SECONDS, TimeUnit.SECONDS).getStatus().isOk()); }
@Test public void testRefreshAfterFailure() throws Exception { underTest = new OAuthCredentialsCache(executorService, mockCredentials); final AccessToken accessToken = new AccessToken("hi", new Date(HeaderCacheElement.TOKEN_STALENESS_MS + 1)); Mockito.when(mockCredentials.refreshAccessToken()) .thenThrow(new IOException()) .thenReturn(accessToken); OAuthCredentialsCache.HeaderToken token1 = underTest.getHeader(TIMEOUT_SECONDS, TimeUnit.SECONDS); Assert.assertFalse(token1.getStatus().isOk()); OAuthCredentialsCache.HeaderToken token2 = underTest.getHeader(TIMEOUT_SECONDS, TimeUnit.SECONDS); Assert.assertTrue(token2.getStatus().isOk()); Assert.assertThat(token2.getHeader(), containsString("hi")); Mockito.verify(mockCredentials, times(2)).refreshAccessToken(); }