Search in sources :

Example 1 with NoOpCancelable

use of com.amplifyframework.core.async.NoOpCancelable in project amplify-android by aws-amplify.

the class SubscriptionProcessorTest method arrangeStartedSubscriptions.

private static void arrangeStartedSubscriptions(AppSync appSync, List<ModelSchema> modelSchemas, SubscriptionType[] subscriptionTypes) {
    Answer<Cancelable> answer = invocation -> {
        final int startConsumerIndex = 1;
        Consumer<String> onStart = invocation.getArgument(startConsumerIndex);
        onStart.accept(RandomString.string());
        return new NoOpCancelable();
    };
    arrangeSubscriptions(appSync, answer, modelSchemas, subscriptionTypes);
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) ModelWithMetadata(com.amplifyframework.datastore.appsync.ModelWithMetadata) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) ModelProvider(com.amplifyframework.core.model.ModelProvider) Pair(android.util.Pair) RunWith(org.junit.runner.RunWith) AppSync(com.amplifyframework.datastore.appsync.AppSync) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) SchemaRegistry(com.amplifyframework.core.model.SchemaRegistry) Answer(org.mockito.stubbing.Answer) Consumer(com.amplifyframework.core.Consumer) SubscriptionType(com.amplifyframework.api.graphql.SubscriptionType) Observable(io.reactivex.rxjava3.core.Observable) Map(java.util.Map) Mockito.doAnswer(org.mockito.Mockito.doAnswer) NoOpCancelable(com.amplifyframework.core.async.NoOpCancelable) ModelSchema(com.amplifyframework.core.model.ModelSchema) ModelMetadata(com.amplifyframework.datastore.appsync.ModelMetadata) GraphQLResponse(com.amplifyframework.api.graphql.GraphQLResponse) Before(org.junit.Before) DataStoreConfiguration(com.amplifyframework.datastore.DataStoreConfiguration) BlogOwner(com.amplifyframework.testmodels.commentsblog.BlogOwner) Model(com.amplifyframework.core.model.Model) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) Completable(io.reactivex.rxjava3.core.Completable) Action(com.amplifyframework.core.Action) RobolectricTestRunner(org.robolectric.RobolectricTestRunner) TimeUnit(java.util.concurrent.TimeUnit) DataStoreException(com.amplifyframework.datastore.DataStoreException) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) Cancelable(com.amplifyframework.core.async.Cancelable) Assert.assertFalse(org.junit.Assert.assertFalse) RandomString(com.amplifyframework.testutils.random.RandomString) Temporal(com.amplifyframework.core.model.temporal.Temporal) AmplifyModelProvider(com.amplifyframework.testmodels.commentsblog.AmplifyModelProvider) Collections(java.util.Collections) Mockito.mock(org.mockito.Mockito.mock) Consumer(com.amplifyframework.core.Consumer) NoOpCancelable(com.amplifyframework.core.async.NoOpCancelable) NoOpCancelable(com.amplifyframework.core.async.NoOpCancelable) Cancelable(com.amplifyframework.core.async.Cancelable)

Example 2 with NoOpCancelable

use of com.amplifyframework.core.async.NoOpCancelable in project amplify-android by aws-amplify.

the class SubscriptionProcessorTest method appSyncInvokedWhenSubscriptionsStarted.

/**
 * When {@link SubscriptionProcessor#startSubscriptions()} is invoked,
 * the {@link AppSync} client receives subscription requests.
 */
@Test
public void appSyncInvokedWhenSubscriptionsStarted() {
    // For every Class-SubscriptionType pairing, use a CountDownLatch
    // to tell whether or not we've "seen" a subscription event for it.
    Map<Pair<ModelSchema, SubscriptionType>, CountDownLatch> seen = new HashMap<>();
    // Build a stream of such pairs.
    Observable.fromIterable(modelSchemas).flatMap(modelSchema -> Observable.fromArray(SubscriptionType.values()).map(value -> Pair.create(modelSchema, value))).blockingForEach(pair -> {
        // For each one, store a latch. Add a mocking behavior to count down
        // the latch when the subscription API is hit, for that class and subscription type.
        CountDownLatch latch = new CountDownLatch(1);
        seen.put(Pair.create(pair.first, pair.second), latch);
        Answer<Cancelable> answer = invocation -> {
            latch.countDown();
            return new NoOpCancelable();
        };
        arrangeSubscription(appSync, answer, pair.first, pair.second);
    });
    // Act: start some subscriptions.
    try {
        subscriptionProcessor.startSubscriptions();
    } catch (DataStoreException exception) {
    // startSubscriptions throws this exception if it doesn't receive the start_ack messages after a time out.
    // This test doesn't mock those start_ack messages, so this expection is expected.  That's okay though -
    // we just want to verify that the subscriptions were requested.
    }
    // Make sure that all of the subscriptions have been
    Observable.fromIterable(seen.entrySet()).blockingForEach(entry -> {
        CountDownLatch latch = entry.getValue();
        assertTrue(latch.await(OPERATION_TIMEOUT_MS, TimeUnit.MILLISECONDS));
    });
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) ModelWithMetadata(com.amplifyframework.datastore.appsync.ModelWithMetadata) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) ModelProvider(com.amplifyframework.core.model.ModelProvider) Pair(android.util.Pair) RunWith(org.junit.runner.RunWith) AppSync(com.amplifyframework.datastore.appsync.AppSync) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) SchemaRegistry(com.amplifyframework.core.model.SchemaRegistry) Answer(org.mockito.stubbing.Answer) Consumer(com.amplifyframework.core.Consumer) SubscriptionType(com.amplifyframework.api.graphql.SubscriptionType) Observable(io.reactivex.rxjava3.core.Observable) Map(java.util.Map) Mockito.doAnswer(org.mockito.Mockito.doAnswer) NoOpCancelable(com.amplifyframework.core.async.NoOpCancelable) ModelSchema(com.amplifyframework.core.model.ModelSchema) ModelMetadata(com.amplifyframework.datastore.appsync.ModelMetadata) GraphQLResponse(com.amplifyframework.api.graphql.GraphQLResponse) Before(org.junit.Before) DataStoreConfiguration(com.amplifyframework.datastore.DataStoreConfiguration) BlogOwner(com.amplifyframework.testmodels.commentsblog.BlogOwner) Model(com.amplifyframework.core.model.Model) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) Completable(io.reactivex.rxjava3.core.Completable) Action(com.amplifyframework.core.Action) RobolectricTestRunner(org.robolectric.RobolectricTestRunner) TimeUnit(java.util.concurrent.TimeUnit) DataStoreException(com.amplifyframework.datastore.DataStoreException) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) Cancelable(com.amplifyframework.core.async.Cancelable) Assert.assertFalse(org.junit.Assert.assertFalse) RandomString(com.amplifyframework.testutils.random.RandomString) Temporal(com.amplifyframework.core.model.temporal.Temporal) AmplifyModelProvider(com.amplifyframework.testmodels.commentsblog.AmplifyModelProvider) Collections(java.util.Collections) Mockito.mock(org.mockito.Mockito.mock) DataStoreException(com.amplifyframework.datastore.DataStoreException) HashMap(java.util.HashMap) NoOpCancelable(com.amplifyframework.core.async.NoOpCancelable) CountDownLatch(java.util.concurrent.CountDownLatch) NoOpCancelable(com.amplifyframework.core.async.NoOpCancelable) Cancelable(com.amplifyframework.core.async.Cancelable) Pair(android.util.Pair) Test(org.junit.Test)

Example 3 with NoOpCancelable

use of com.amplifyframework.core.async.NoOpCancelable in project amplify-android by aws-amplify.

the class RxAdaptersTest method singleFiresErrorWhenErrorEmitted.

/**
 * The {@link Single} returned by
 * {@link CancelableBehaviors#toSingle(CancelableBehaviors.ResultEmitter)}
 * will dispatch an error when the {@link CancelableBehaviors.ResultEmitter}'s error consumer is
 * invoked.
 */
@Test
public void singleFiresErrorWhenErrorEmitted() {
    Throwable expected = new Throwable(RandomString.string());
    CancelableBehaviors.toSingle((onResult, onError) -> {
        onError.accept(expected);
        return new NoOpCancelable();
    }).test().assertError(expected).assertNoValues();
}
Also used : Single(io.reactivex.rxjava3.core.Single) SimpleCancelable(com.amplifyframework.testutils.SimpleCancelable) CancelableBehaviors(com.amplifyframework.rx.RxAdapters.CancelableBehaviors) VoidBehaviors(com.amplifyframework.rx.RxAdapters.VoidBehaviors) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) Completable(io.reactivex.rxjava3.core.Completable) Sleep(com.amplifyframework.testutils.Sleep) TestObserver(io.reactivex.rxjava3.observers.TestObserver) Cancelable(com.amplifyframework.core.async.Cancelable) Observable(io.reactivex.rxjava3.core.Observable) RandomString(com.amplifyframework.testutils.random.RandomString) NoOpCancelable(com.amplifyframework.core.async.NoOpCancelable) Disposable(io.reactivex.rxjava3.disposables.Disposable) UndeliverableException(io.reactivex.rxjava3.exceptions.UndeliverableException) NoOpCancelable(com.amplifyframework.core.async.NoOpCancelable) Test(org.junit.Test)

Example 4 with NoOpCancelable

use of com.amplifyframework.core.async.NoOpCancelable in project amplify-android by aws-amplify.

the class RxAdaptersTest method singleFiresResultWhenEmitted.

/**
 * The {@link Single} returned by
 * {@link CancelableBehaviors#toSingle(CancelableBehaviors.ResultEmitter)}
 * will dispatch a result when the {@link CancelableBehaviors.ResultEmitter}'s value consumer
 * is invoked.
 */
@Test
public void singleFiresResultWhenEmitted() {
    String result = RandomString.string();
    CancelableBehaviors.toSingle((onResult, onError) -> {
        onResult.accept(result);
        return new NoOpCancelable();
    }).test().assertValue(result).assertComplete();
}
Also used : Single(io.reactivex.rxjava3.core.Single) SimpleCancelable(com.amplifyframework.testutils.SimpleCancelable) CancelableBehaviors(com.amplifyframework.rx.RxAdapters.CancelableBehaviors) VoidBehaviors(com.amplifyframework.rx.RxAdapters.VoidBehaviors) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) Completable(io.reactivex.rxjava3.core.Completable) Sleep(com.amplifyframework.testutils.Sleep) TestObserver(io.reactivex.rxjava3.observers.TestObserver) Cancelable(com.amplifyframework.core.async.Cancelable) Observable(io.reactivex.rxjava3.core.Observable) RandomString(com.amplifyframework.testutils.random.RandomString) NoOpCancelable(com.amplifyframework.core.async.NoOpCancelable) Disposable(io.reactivex.rxjava3.disposables.Disposable) UndeliverableException(io.reactivex.rxjava3.exceptions.UndeliverableException) RandomString(com.amplifyframework.testutils.random.RandomString) NoOpCancelable(com.amplifyframework.core.async.NoOpCancelable) Test(org.junit.Test)

Example 5 with NoOpCancelable

use of com.amplifyframework.core.async.NoOpCancelable in project amplify-android by aws-amplify.

the class RxAdaptersTest method observableFiresValuesAndCompletesWhenEmitterDoes.

/**
 * The {@link Observable} returned by
 * {@link CancelableBehaviors#toObservable(CancelableBehaviors.StreamEmitter)}
 * will contain a stream of values corresponding to those that have been passed via the emitter's
 * item consumer. When the emitters' completion action is invoked, the Observable completes.
 */
@Test
public void observableFiresValuesAndCompletesWhenEmitterDoes() {
    String first = RandomString.string();
    String second = RandomString.string();
    CancelableBehaviors.toObservable((onStart, onItem, onError, onComplete) -> {
        onStart.accept(RandomString.string());
        onItem.accept(first);
        onItem.accept(second);
        onComplete.call();
        return new NoOpCancelable();
    }).test().assertValues(first, second).assertComplete().assertNoErrors();
}
Also used : RandomString(com.amplifyframework.testutils.random.RandomString) NoOpCancelable(com.amplifyframework.core.async.NoOpCancelable) Test(org.junit.Test)

Aggregations

NoOpCancelable (com.amplifyframework.core.async.NoOpCancelable)15 Cancelable (com.amplifyframework.core.async.Cancelable)14 Model (com.amplifyframework.core.model.Model)12 Test (org.junit.Test)12 Action (com.amplifyframework.core.Action)8 DataStoreException (com.amplifyframework.datastore.DataStoreException)8 GraphQLResponse (com.amplifyframework.api.graphql.GraphQLResponse)6 SubscriptionType (com.amplifyframework.api.graphql.SubscriptionType)6 Consumer (com.amplifyframework.core.Consumer)6 ModelSchema (com.amplifyframework.core.model.ModelSchema)6 RandomModel (com.amplifyframework.testutils.random.RandomModel)6 RandomString (com.amplifyframework.testutils.random.RandomString)6 Completable (io.reactivex.rxjava3.core.Completable)5 Observable (io.reactivex.rxjava3.core.Observable)5 Assert.assertTrue (org.junit.Assert.assertTrue)4 Pair (android.util.Pair)3 NonNull (androidx.annotation.NonNull)3 Nullable (androidx.annotation.Nullable)3 AmplifyException (com.amplifyframework.AmplifyException)3 ApiCategoryBehavior (com.amplifyframework.api.ApiCategoryBehavior)3