CognitoIdentityProvider { @NonNull public Single<Optional<Cognito>> prefetchCognitoTokenIfNeeded() { return Single.fromCallable(this::getCachedCognitoToken) .flatMap(cognitoOptional -> { if (!cognitoOptional.isPresent() || cognitoOptional.get().getCognitoToken() == null || cognitoOptional.get().getIdentityId() == null) { Logger.debug(CognitoIdentityProvider.this, "Existing cognito token is invalid. Pre-fetching..."); return refreshCognitoToken(); } else { Logger.debug(CognitoIdentityProvider.this, "Existing cognito token is valid"); return Single.just(cognitoOptional); } }); } @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 prefetchNullToken() { final Cognito preCognito = null; Mockito.when(localCognitoTokenStore.getCognitoToken()).thenReturn(preCognito); 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); TestObserver<Optional<Cognito>> testObserver = cognitoIdentityProvider.prefetchCognitoTokenIfNeeded().test(); final Cognito cognito = new Cognito(TOKEN, IDENTITY_ID, EXPIRES_AT); Mockito.verify(localCognitoTokenStore).persist(null); Mockito.verify(localCognitoTokenStore).persist(cognito); testObserver.assertValue(Optional.of(cognito)) .assertComplete() .assertNoErrors(); }
@Test public void prefetchInvalidTokenWillNullToken() { final Cognito preCognito = new Cognito(null, IDENTITY_ID, EXPIRES_AT); Mockito.when(localCognitoTokenStore.getCognitoToken()).thenReturn(preCognito); 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 TestObserver<Optional<Cognito>> testObserver = cognitoIdentityProvider.prefetchCognitoTokenIfNeeded().test(); final Cognito cognito = new Cognito(TOKEN, IDENTITY_ID, EXPIRES_AT); Mockito.verify(localCognitoTokenStore).persist(null); Mockito.verify(localCognitoTokenStore).persist(cognito); testObserver.assertValue(Optional.of(cognito)) .assertComplete() .assertNoErrors(); }
@Test public void prefetchInvalidTokenWillNullIdentityId() { final Cognito preCognito = new Cognito(TOKEN, null, EXPIRES_AT); Mockito.when(localCognitoTokenStore.getCognitoToken()).thenReturn(preCognito); 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 TestObserver<Optional<Cognito>> testObserver = cognitoIdentityProvider.prefetchCognitoTokenIfNeeded().test(); final Cognito cognito = new Cognito(TOKEN, IDENTITY_ID, EXPIRES_AT); Mockito.verify(localCognitoTokenStore).persist(null); Mockito.verify(localCognitoTokenStore).persist(cognito); testObserver.assertValue(Optional.of(cognito)) .assertComplete() .assertNoErrors(); }
@Test public void prefetchValidToken() { final Cognito preCognito = new Cognito(TOKEN, IDENTITY_ID, EXPIRES_AT); Mockito.when(localCognitoTokenStore.getCognitoToken()).thenReturn(preCognito); final TestObserver<Optional<Cognito>> testObserver = cognitoIdentityProvider.prefetchCognitoTokenIfNeeded().test(); Mockito.verify(localCognitoTokenStore, Mockito.never()).persist(ArgumentMatchers.any(Cognito.class)); testObserver.assertValue(Optional.of(preCognito)) .assertComplete() .assertNoErrors(); }