Search in sources :

Example 86 with DataStoreException

use of com.amplifyframework.datastore.DataStoreException in project amplify-android by aws-amplify.

the class RxDataStoreBindingTest method saveEmitsErrorWhenBehaviorDoes.

/**
 * When the DataStore save behavior emits an error, the Rx binding should
 * do the same.
 * @throws InterruptedException If interrupted while test observer is awaiting terminal event
 */
@Test
public void saveEmitsErrorWhenBehaviorDoes() throws InterruptedException {
    Model model = RandomModel.model();
    // Arrange: The underlying category behavior returns an error.
    DataStoreException expectedFailure = new DataStoreException("Expected", "Failure");
    doAnswer(invocation -> {
        // 0 = model, 1 = result consumer, 2 = failure consumer
        int indexOfFailureConsumer = 2;
        Consumer<DataStoreException> failureConsumer = invocation.getArgument(indexOfFailureConsumer);
        failureConsumer.accept(expectedFailure);
        return null;
    }).when(delegate).save(eq(model), anyConsumer(), anyConsumer());
    // Act: try to save something.
    TestObserver<?> observer = rxDataStore.save(model).test();
    observer.await(TIMEOUT_SECONDS, TimeUnit.SECONDS);
    observer.assertError(expectedFailure);
    verify(delegate).save(eq(model), anyConsumer(), anyConsumer());
}
Also used : DataStoreException(com.amplifyframework.datastore.DataStoreException) RandomModel(com.amplifyframework.testutils.random.RandomModel) Model(com.amplifyframework.core.model.Model) Test(org.junit.Test)

Example 87 with DataStoreException

use of com.amplifyframework.datastore.DataStoreException in project amplify-android by aws-amplify.

the class RxDataStoreBindingTest method observeFailsWhenCategoryBehaviorDoes.

/**
 * The Rx binding for the DataStore's observe behavior is an Observable. It should
 * fail with an exception when the DataStore observe method calls back its error consumer.
 * @throws InterruptedException If interrupted while test observer is awaiting terminal event
 */
@Test
public void observeFailsWhenCategoryBehaviorDoes() throws InterruptedException {
    // Arrange for observer() to callback failure
    DataStoreException expectedFailure = new DataStoreException("Expected", "Failure");
    doAnswer(invocation -> {
        // 0 = clazz, 1 = onStart, 2 = onNext, 3 = onFailure, 4 = onComplete
        final int positionOfOnStart = 1;
        Consumer<Cancelable> onStart = invocation.getArgument(positionOfOnStart);
        onStart.accept(new NoOpCancelable());
        final int positionOfOnFailure = 3;
        Consumer<DataStoreException> onFailure = invocation.getArgument(positionOfOnFailure);
        onFailure.accept(expectedFailure);
        // "void"
        return null;
    }).when(delegate).observe(eq(Model.class), anyConsumer(), anyConsumer(), anyConsumer(), anyAction());
    // Act: observe the DataStore via Rx binding
    TestObserver<DataStoreItemChange<Model>> observer = rxDataStore.observe(Model.class).test();
    // Assert: failure is propagated
    observer.await(TIMEOUT_SECONDS, TimeUnit.SECONDS);
    observer.assertError(expectedFailure);
    verify(delegate).observe(eq(Model.class), anyConsumer(), anyConsumer(), anyConsumer(), anyAction());
}
Also used : DataStoreException(com.amplifyframework.datastore.DataStoreException) DataStoreItemChange(com.amplifyframework.datastore.DataStoreItemChange) RandomModel(com.amplifyframework.testutils.random.RandomModel) Model(com.amplifyframework.core.model.Model) NoOpCancelable(com.amplifyframework.core.async.NoOpCancelable) NoOpCancelable(com.amplifyframework.core.async.NoOpCancelable) Cancelable(com.amplifyframework.core.async.Cancelable) Test(org.junit.Test)

Example 88 with DataStoreException

use of com.amplifyframework.datastore.DataStoreException in project amplify-android by aws-amplify.

the class RxDataStoreBindingTest method observeQueryFailsWhenCategoryBehaviorDoes.

/**
 * The Rx binding for the DataStore's observeQuery behavior is an Observable. It should
 * fail with an exception when the DataStore observe method calls back its error consumer.
 * @throws InterruptedException If interrupted while test observer is awaiting terminal event
 */
@Test
public void observeQueryFailsWhenCategoryBehaviorDoes() throws InterruptedException {
    // Arrange for observer() to callback failure
    DataStoreException expectedFailure = new DataStoreException("Expected", "Failure");
    doAnswer(invocation -> {
        // 0 = clazz, 1 = options, 2 = start consumer, 3 = item consumer, 4 = failure consumer, 5 = onComplete
        final int positionOfOnStart = 3;
        Consumer<Cancelable> onStart = invocation.getArgument(positionOfOnStart);
        onStart.accept(new NoOpCancelable());
        final int positionOfOnFailure = 4;
        Consumer<DataStoreException> onFailure = invocation.getArgument(positionOfOnFailure);
        onFailure.accept(expectedFailure);
        // "void"
        return null;
    }).when(delegate).observeQuery(eq(Model.class), any(), anyConsumer(), anyConsumer(), anyConsumer(), anyAction());
    // Act: observe the DataStore via Rx binding
    TestObserver<DataStoreQuerySnapshot<Model>> observer = rxDataStore.observeQuery(Model.class, new ObserveQueryOptions()).test();
    // Assert: failure is propagated
    observer.await(TIMEOUT_SECONDS, TimeUnit.SECONDS);
    observer.assertError(expectedFailure);
    verify(delegate).observeQuery(eq(Model.class), any(), anyConsumer(), anyConsumer(), anyConsumer(), anyAction());
}
Also used : DataStoreException(com.amplifyframework.datastore.DataStoreException) ObserveQueryOptions(com.amplifyframework.core.model.query.ObserveQueryOptions) RandomModel(com.amplifyframework.testutils.random.RandomModel) Model(com.amplifyframework.core.model.Model) NoOpCancelable(com.amplifyframework.core.async.NoOpCancelable) NoOpCancelable(com.amplifyframework.core.async.NoOpCancelable) Cancelable(com.amplifyframework.core.async.Cancelable) DataStoreQuerySnapshot(com.amplifyframework.datastore.DataStoreQuerySnapshot) Test(org.junit.Test)

Example 89 with DataStoreException

use of com.amplifyframework.datastore.DataStoreException in project amplify-android by aws-amplify.

the class RxDataStoreBindingTest method deleteEmitsErrorWhenBehaviorDoes.

/**
 * When the DataStore delete behavior emits a result, the Rx binding
 * should just complete.
 * @throws InterruptedException If interrupted while test observer is awaiting terminal event
 */
@Test
public void deleteEmitsErrorWhenBehaviorDoes() throws InterruptedException {
    // Arrange: delete() category behavior will callback failure consumer
    Model model = RandomModel.model();
    DataStoreException expectedFailure = new DataStoreException("Expected", "Failure");
    doAnswer(invocation -> {
        final int indexOfFailureConsumer = 2;
        Consumer<DataStoreException> failureConsumer = invocation.getArgument(indexOfFailureConsumer);
        failureConsumer.accept(expectedFailure);
        return null;
    }).when(delegate).delete(eq(model), anyConsumer(), anyConsumer());
    // Act: try to delete a model via the Rx binding
    TestObserver<Void> observer = rxDataStore.delete(model).test();
    // Assert: the same failure bubbled out from the category behavior
    observer.await(TIMEOUT_SECONDS, TimeUnit.SECONDS);
    observer.assertError(expectedFailure);
    verify(delegate).delete(eq(model), anyConsumer(), anyConsumer());
}
Also used : DataStoreException(com.amplifyframework.datastore.DataStoreException) RandomModel(com.amplifyframework.testutils.random.RandomModel) Model(com.amplifyframework.core.model.Model) Test(org.junit.Test)

Aggregations

DataStoreException (com.amplifyframework.datastore.DataStoreException)89 Test (org.junit.Test)52 BlogOwner (com.amplifyframework.testmodels.commentsblog.BlogOwner)36 Consumer (com.amplifyframework.core.Consumer)32 List (java.util.List)32 Cancelable (com.amplifyframework.core.async.Cancelable)31 Model (com.amplifyframework.core.model.Model)31 ArrayList (java.util.ArrayList)31 AmplifyException (com.amplifyframework.AmplifyException)29 ModelSchema (com.amplifyframework.core.model.ModelSchema)28 Collections (java.util.Collections)28 Action (com.amplifyframework.core.Action)27 QueryPredicate (com.amplifyframework.core.model.query.predicate.QueryPredicate)27 TimeUnit (java.util.concurrent.TimeUnit)25 Post (com.amplifyframework.testmodels.commentsblog.Post)23 PostStatus (com.amplifyframework.testmodels.commentsblog.PostStatus)23 Arrays (java.util.Arrays)23 Assert.assertEquals (org.junit.Assert.assertEquals)23 ObserveQueryOptions (com.amplifyframework.core.model.query.ObserveQueryOptions)22 DataStoreQuerySnapshot (com.amplifyframework.datastore.DataStoreQuerySnapshot)21