use of com.amplifyframework.datastore.DataStoreItemChange in project amplify-android by aws-amplify.
the class RxDataStoreBindingTest method observeCompletesWhenCategoryBehaviorDoes.
/**
* The Rx binding for the DataStore's observe method is an Observable. It should
* complete when the Rx binding's completion callback is triggered.
* @throws InterruptedException If interrupted while test observer is awaiting terminal event
*/
@Test
public void observeCompletesWhenCategoryBehaviorDoes() throws InterruptedException {
// Category behavior is arranged to complete
doAnswer(invocation -> {
// 0 = clazz, 1 = onStart, 2 = onNext, 3 = onFailure, 4 = onComplete
final int positionOfOnStart = 2;
Consumer<Cancelable> onStart = invocation.getArgument(positionOfOnStart);
onStart.accept(new NoOpCancelable());
final int positionOfOnComplete = 4;
Action onComplete = invocation.getArgument(positionOfOnComplete);
onComplete.call();
// "void"
return null;
}).when(delegate).observe(eq(Model.class), anyConsumer(), anyConsumer(), anyConsumer(), anyAction());
// Act: observe via Rx binding
TestObserver<DataStoreItemChange<Model>> observer = rxDataStore.observe(Model.class).test();
observer.await(TIMEOUT_SECONDS, TimeUnit.SECONDS);
observer.assertComplete();
verify(delegate).observe(eq(Model.class), anyConsumer(), anyConsumer(), anyConsumer(), anyAction());
}
use of com.amplifyframework.datastore.DataStoreItemChange in project amplify-android by aws-amplify.
the class RxDataStoreBindingTest method saveCompletesWhenBehaviorEmitsResult.
/**
* When the DataStore behavior successfully saves a value, the Rx binding
* for save should just complete.
* @throws InterruptedException If interrupted while test observer is awaiting terminal event
*/
@Test
public void saveCompletesWhenBehaviorEmitsResult() throws InterruptedException {
Model model = RandomModel.model();
// Arrange: category returns notification of change when save is transacted
doAnswer(invocation -> {
// 0 = model, 1 = result consumer, 2 = failure consumer
final int indexOfModel = 0;
final int indexOfResultConsumer = 1;
Model modelFromInvocation = invocation.getArgument(indexOfModel);
Consumer<DataStoreItemChange<Model>> resultConsumer = invocation.getArgument(indexOfResultConsumer);
resultConsumer.accept(DataStoreItemChange.builder().uuid(modelFromInvocation.getId()).type(Type.CREATE).itemClass(Model.class).initiator(Initiator.LOCAL).item(modelFromInvocation).build());
return null;
}).when(delegate).save(eq(model), anyConsumer(), anyConsumer());
// Act: try to save something.
TestObserver<Void> observer = rxDataStore.save(model).test();
// Assert: operation completed
observer.await(TIMEOUT_SECONDS, TimeUnit.SECONDS);
observer.assertComplete();
// Assert: behavior was invoked
verify(delegate).save(eq(model), anyConsumer(), anyConsumer());
}
use of com.amplifyframework.datastore.DataStoreItemChange 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());
}
use of com.amplifyframework.datastore.DataStoreItemChange in project amplify-android by aws-amplify.
the class RxDataStoreBindingTest method deleteCompletesWhenBehaviorEmitsResult.
/**
* When the DataStore delete behavior returns a result, the Rx binding
* should just complete.
* @throws InterruptedException If interrupted while test observer is awaiting terminal event
*/
@Test
public void deleteCompletesWhenBehaviorEmitsResult() throws InterruptedException {
Model model = RandomModel.model();
// Arrange for the category to emit a result when delete() is called.
doAnswer(invocation -> {
final int indexOModel = 0;
final int indexOfResultConsumer = 1;
Model invokedModel = invocation.getArgument(indexOModel);
Consumer<DataStoreItemChange<Model>> resultConsumer = invocation.getArgument(indexOfResultConsumer);
resultConsumer.accept(DataStoreItemChange.builder().uuid(invokedModel.getId()).item(invokedModel).initiator(Initiator.LOCAL).type(Type.DELETE).itemClass(Model.class).build());
return null;
}).when(delegate).delete(eq(model), anyConsumer(), anyConsumer());
// Act: okay, call delete() on the Rx binding.
TestObserver<Void> observer = rxDataStore.delete(model).test();
observer.await(TIMEOUT_SECONDS, TimeUnit.SECONDS);
observer.assertComplete();
verify(delegate).delete(eq(model), anyConsumer(), anyConsumer());
}
use of com.amplifyframework.datastore.DataStoreItemChange in project amplify-android by aws-amplify.
the class RxDataStoreBindingTest method observeReturnsCategoryBehaviorChanges.
/**
* The Rx binding for observing the DataStore should be an Observable stream
* of DataStore changes. It should emit events whenever they are observed
* on the observe behavior.
*/
@Test
public void observeReturnsCategoryBehaviorChanges() {
// Arrange: observe(Class<?>) will spit out some values from category behavior.
Model model = RandomModel.model();
DataStoreItemChange<Model> changeEvent = DataStoreItemChange.builder().uuid(model.getId()).itemClass(Model.class).item(model).type(Type.CREATE).initiator(Initiator.LOCAL).build();
doAnswer(invocation -> {
// 0 = clazz, 1 = start consumer, 2 = item consumer, 3 = failure consumer, 4 = onComplete
final int positionOfStartConsumer = 1;
Consumer<Cancelable> onStart = invocation.getArgument(positionOfStartConsumer);
onStart.accept(new NoOpCancelable());
final int positionOfValueConsumer = 2;
Consumer<DataStoreItemChange<Model>> onNext = invocation.getArgument(positionOfValueConsumer);
onNext.accept(changeEvent);
// "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: event is observed
observer.awaitCount(1).assertValue(changeEvent);
verify(delegate).observe(eq(Model.class), anyConsumer(), anyConsumer(), anyConsumer(), anyAction());
}
Aggregations