use of com.amplifyframework.core.model.Model in project amplify-android by aws-amplify.
the class RxApiBindingTest method subscribeStartsAndFails.
/**
* When the subscribe API behavior starts and then immediately fails,
* the Rx binding should emit that same failure.
* @throws InterruptedException If interrupted while test observer is awaiting terminal event
*/
@Test
public void subscribeStartsAndFails() throws InterruptedException {
// Arrange a category behavior which starts and then fails
ApiException expectedFailure = new ApiException("Expected", "Failure");
String token = RandomString.string();
ConnectionStateEvent expectedConnectionStateEvent = new ConnectionStateEvent(ConnectionState.CONNECTED, token);
final GraphQLRequest<Model> request = createMockSubscriptionRequest(Model.class);
doAnswer(invocation -> {
final int onStartPosition = 1;
final int onFailurePosition = 3;
Consumer<String> onStart = invocation.getArgument(onStartPosition);
Consumer<ApiException> onFailure = invocation.getArgument(onFailurePosition);
onStart.accept(token);
onFailure.accept(expectedFailure);
return null;
}).when(delegate).subscribe(eq(request), anyConsumer(), anyConsumer(), anyConsumer(), anyAction());
RxSubscriptionOperation<GraphQLResponse<Model>> rxOperation = rxApi.subscribe(request);
// Act: subscribe via binding
TestObserver<GraphQLResponse<Model>> dataObserver = rxOperation.observeSubscriptionData().test();
TestObserver<ConnectionStateEvent> startObserver = rxOperation.observeConnectionState().test();
startObserver.await(TIMEOUT_SECONDS, TimeUnit.SECONDS);
startObserver.assertValue(expectedConnectionStateEvent);
startObserver.assertNoErrors();
dataObserver.await(TIMEOUT_SECONDS, TimeUnit.SECONDS);
dataObserver.assertNoValues();
dataObserver.assertError(expectedFailure);
}
use of com.amplifyframework.core.model.Model in project amplify-android by aws-amplify.
the class RxApiBindingTest method subscribeStartsEmitsValuesAndCompletes.
/**
* When the API subscribe operation emits values and then completes, the Rx
* binding should follow suit.
* @throws InterruptedException If interrupted while test observer is awaiting terminal event
*/
@Test
public void subscribeStartsEmitsValuesAndCompletes() throws InterruptedException {
// Arrange a category behavior which emits an expected sequence of callback events
String token = RandomString.string();
Model model = RandomModel.model();
ConnectionStateEvent expectedConnectionStateEvent = new ConnectionStateEvent(ConnectionState.CONNECTED, token);
GraphQLResponse<Model> response = new GraphQLResponse<>(model, Collections.emptyList());
GraphQLRequest<Model> request = createMockSubscriptionRequest(Model.class);
doAnswer(invocation -> {
final int onStartPosition = 1;
final int onNextPosition = 2;
final int onCompletePosition = 4;
Consumer<String> onStart = invocation.getArgument(onStartPosition);
Consumer<GraphQLResponse<Model>> onNext = invocation.getArgument(onNextPosition);
Action onComplete = invocation.getArgument(onCompletePosition);
onStart.accept(token);
onNext.accept(response);
onComplete.call();
return null;
}).when(delegate).subscribe(eq(request), anyConsumer(), anyConsumer(), anyConsumer(), anyAction());
// Act: subscribe via binding
RxSubscriptionOperation<GraphQLResponse<Model>> rxOperation = rxApi.subscribe(request);
// Act: subscribe via binding
TestObserver<GraphQLResponse<Model>> dataObserver = rxOperation.observeSubscriptionData().test();
TestObserver<ConnectionStateEvent> startObserver = rxOperation.observeConnectionState().test();
startObserver.await(TIMEOUT_SECONDS, TimeUnit.SECONDS);
startObserver.assertValue(expectedConnectionStateEvent);
startObserver.assertNoErrors();
dataObserver.await(TIMEOUT_SECONDS, TimeUnit.SECONDS);
dataObserver.assertValue(response);
dataObserver.assertNoErrors();
}
use of com.amplifyframework.core.model.Model 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.model.Model in project amplify-android by aws-amplify.
the class RxDataStoreBindingTest method queryEmitsFailureWhenCategoryBehaviorDoes.
/**
* When the DataStore emits a failure for a query, the Rx binding should terminate
* with that failure.
* @throws InterruptedException If interrupted while test observer is awaiting terminal event
*/
@Test
public void queryEmitsFailureWhenCategoryBehaviorDoes() throws InterruptedException {
DataStoreException expectedFailure = new DataStoreException("Expected", "Failure");
doAnswer(invocation -> {
final int positionOrFailureConsumer = 2;
Consumer<DataStoreException> failureConsumer = invocation.getArgument(positionOrFailureConsumer);
failureConsumer.accept(expectedFailure);
return null;
}).when(delegate).query(eq(Model.class), anyConsumer(), anyConsumer());
TestObserver<Model> observer = rxDataStore.query(Model.class).test();
observer.await(TIMEOUT_SECONDS, TimeUnit.SECONDS);
observer.assertError(expectedFailure);
verify(delegate).query(eq(Model.class), anyConsumer(), anyConsumer());
}
use of com.amplifyframework.core.model.Model 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());
}
Aggregations