CognitoIdentityProvider { @Nullable public Cognito synchronouslyRefreshCognitoToken() { try { return refreshCognitoToken() .onErrorReturn(throwable -> Optional.absent()) .blockingGet().get(); } catch (Exception e) { return null; } } @Inject  CognitoIdentityProvider(@NonNull IdentityManager identityManager,
                                   @NonNull LocalCognitoTokenStore localCognitoTokenStore); @NonNull Single<Optional<Cognito>> prefetchCognitoTokenIfNeeded(); @NonNull Single<Optional<Cognito>> refreshCognitoToken(); @Nullable Cognito synchronouslyRefreshCognitoToken(); @NonNull Optional<Cognito> getCachedCognitoToken();  }
@Test public void synchronouslyRefreshCognitoTokenThrowsException() { Mockito.when(identityManager.getMe()).thenReturn(Observable.<MeResponse>error(new IOException())); assertEquals(null, cognitoIdentityProvider.synchronouslyRefreshCognitoToken()); }
@Test public void synchronouslyRefreshCognitoTokenReturnsNullUserResponse() { Mockito.when(meResponse.getUser()).thenReturn(null); Mockito.when(identityManager.getMe()).thenReturn(Observable.just(meResponse)); assertEquals(null, cognitoIdentityProvider.synchronouslyRefreshCognitoToken()); }
@Test public void synchronouslyRefreshCognitoTokenIsValid() { Mockito.when(identityManager.getMe()).thenReturn(Observable.just(meResponse)); Mockito.when(user.getCognitoToken()).thenReturn(TOKEN); Mockito.when(user.getIdentityId()).thenReturn(IDENTITY_ID); Mockito.when(user.getCognitoTokenExpiresAt()).thenReturn(EXPIRES_AT); final Cognito cognito = new Cognito(TOKEN, IDENTITY_ID, EXPIRES_AT); assertEquals(cognito, cognitoIdentityProvider.synchronouslyRefreshCognitoToken()); }