@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(); }
@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); } }); }
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); } }); } }
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); }
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(); }
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();  }