use of com.amplifyframework.core.async.NoOpCancelable in project amplify-android by aws-amplify.
the class RxDataStoreBindingTest method observeQueryReturnsCategoryBehaviorChanges.
/**
* The Rx binding for observeQuerying 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 observeQueryReturnsCategoryBehaviorChanges() {
// Arrange: observe(Class<?>) will spit out some values from category behavior.
List<Model> modelList = new ArrayList<>();
modelList.add(RandomModel.model());
DataStoreQuerySnapshot<Model> changeEvent = new DataStoreQuerySnapshot<>(new ArrayList<>(), true);
doAnswer(invocation -> {
// 0 = clazz, 1 = options, 2 = start consumer, 3 = item consumer, 4 = failure consumer, 5 = onComplete
final int positionOfStartConsumer = 2;
Consumer<Cancelable> onStart = invocation.getArgument(positionOfStartConsumer);
onStart.accept(new NoOpCancelable());
final int positionOfValueConsumer = 3;
Consumer<DataStoreQuerySnapshot<Model>> onNext = invocation.getArgument(positionOfValueConsumer);
onNext.accept(changeEvent);
// "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: event is observed
observer.awaitCount(1).assertValue(changeEvent);
verify(delegate).observeQuery(eq(Model.class), any(), anyConsumer(), anyConsumer(), anyConsumer(), anyAction());
}
use of com.amplifyframework.core.async.NoOpCancelable 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.core.async.NoOpCancelable in project amplify-android by aws-amplify.
the class RxDataStoreBindingTest method observeQueryCompletesWhenCategoryBehaviorDoes.
/**
* The Rx binding for the DataStore's observeQuery 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 observeQueryCompletesWhenCategoryBehaviorDoes() throws InterruptedException {
// Category behavior is arranged to complete
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 positionOfOnComplete = 5;
Action onComplete = invocation.getArgument(positionOfOnComplete);
onComplete.call();
// "void"
return null;
}).when(delegate).observeQuery(eq(Model.class), any(), anyConsumer(), anyConsumer(), anyConsumer(), anyAction());
// Act: observe via Rx binding
TestObserver<DataStoreQuerySnapshot<Model>> observer = rxDataStore.observeQuery(Model.class, new ObserveQueryOptions()).test();
observer.await(TIMEOUT_SECONDS, TimeUnit.SECONDS);
observer.assertComplete();
verify(delegate).observeQuery(eq(Model.class), any(), anyConsumer(), anyConsumer(), anyConsumer(), anyAction());
}
use of com.amplifyframework.core.async.NoOpCancelable in project amplify-android by aws-amplify.
the class SubscriptionProcessorTest method arrangeDataEmittingSubscription.
@SuppressWarnings("SameParameterValue")
private static <T extends Model> void arrangeDataEmittingSubscription(AppSync appSync, ModelSchema modelSchema, SubscriptionType subscriptionType, GraphQLResponse<ModelWithMetadata<T>> response) throws DataStoreException {
Answer<Cancelable> answer = invocation -> {
final int startConsumerIndex = 1;
Consumer<String> onStart = invocation.getArgument(startConsumerIndex);
onStart.accept(RandomString.string());
final int dataConsumerIndex = 2;
Consumer<GraphQLResponse<ModelWithMetadata<T>>> onData = invocation.getArgument(dataConsumerIndex);
onData.accept(response);
return new NoOpCancelable();
};
arrangeSubscription(appSync, answer, modelSchema, subscriptionType);
}
use of com.amplifyframework.core.async.NoOpCancelable in project amplify-android by aws-amplify.
the class AppSyncClient method mutation.
private <T extends Model> Cancelable mutation(final GraphQLRequest<ModelWithMetadata<T>> request, final Consumer<GraphQLResponse<ModelWithMetadata<T>>> onResponse, final Consumer<DataStoreException> onFailure) {
final Consumer<GraphQLResponse<ModelWithMetadata<T>>> responseConsumer = response -> {
if (response.hasErrors()) {
onResponse.accept(new GraphQLResponse<>(null, response.getErrors()));
} else {
onResponse.accept(response);
}
};
final Consumer<ApiException> failureConsumer = failure -> onFailure.accept(new DataStoreException("Failure during mutation.", failure, "Check details."));
final Cancelable cancelable = api.mutate(request, responseConsumer, failureConsumer);
if (cancelable != null) {
return cancelable;
}
return new NoOpCancelable();
}
Aggregations