Search in sources :

Example 21 with Model

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);
}
Also used : RandomModel(com.amplifyframework.testutils.random.RandomModel) Model(com.amplifyframework.core.model.Model) GraphQLResponse(com.amplifyframework.api.graphql.GraphQLResponse) RandomString(com.amplifyframework.testutils.random.RandomString) ConnectionStateEvent(com.amplifyframework.rx.RxOperations.RxSubscriptionOperation.ConnectionStateEvent) ApiException(com.amplifyframework.api.ApiException) Test(org.junit.Test)

Example 22 with Model

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();
}
Also used : Matchers.anyAction(com.amplifyframework.rx.Matchers.anyAction) Action(com.amplifyframework.core.Action) GraphQLResponse(com.amplifyframework.api.graphql.GraphQLResponse) RandomString(com.amplifyframework.testutils.random.RandomString) ConnectionStateEvent(com.amplifyframework.rx.RxOperations.RxSubscriptionOperation.ConnectionStateEvent) RandomModel(com.amplifyframework.testutils.random.RandomModel) Model(com.amplifyframework.core.model.Model) Test(org.junit.Test)

Example 23 with Model

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());
}
Also used : ArrayList(java.util.ArrayList) DataStoreQuerySnapshot(com.amplifyframework.datastore.DataStoreQuerySnapshot) 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) Test(org.junit.Test)

Example 24 with Model

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());
}
Also used : DataStoreException(com.amplifyframework.datastore.DataStoreException) RandomModel(com.amplifyframework.testutils.random.RandomModel) Model(com.amplifyframework.core.model.Model) Test(org.junit.Test)

Example 25 with Model

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());
}
Also used : Matchers.anyAction(com.amplifyframework.rx.Matchers.anyAction) Action(com.amplifyframework.core.Action) 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)

Aggregations

Model (com.amplifyframework.core.model.Model)62 Test (org.junit.Test)37 DataStoreException (com.amplifyframework.datastore.DataStoreException)34 ModelSchema (com.amplifyframework.core.model.ModelSchema)32 SerializedModel (com.amplifyframework.core.model.SerializedModel)30 AmplifyException (com.amplifyframework.AmplifyException)23 Cancelable (com.amplifyframework.core.async.Cancelable)22 Action (com.amplifyframework.core.Action)19 Consumer (com.amplifyframework.core.Consumer)19 StorageItemChange (com.amplifyframework.datastore.storage.StorageItemChange)19 BlogOwner (com.amplifyframework.testmodels.commentsblog.BlogOwner)19 ArrayList (java.util.ArrayList)19 List (java.util.List)19 SchemaRegistry (com.amplifyframework.core.model.SchemaRegistry)18 RandomModel (com.amplifyframework.testutils.random.RandomModel)17 GraphQLResponse (com.amplifyframework.api.graphql.GraphQLResponse)16 QueryPredicates (com.amplifyframework.core.model.query.predicate.QueryPredicates)16 TimeUnit (java.util.concurrent.TimeUnit)16 Collections (java.util.Collections)15 ObserveQueryOptions (com.amplifyframework.core.model.query.ObserveQueryOptions)13