Search in sources :

Example 6 with GraphQLResponse

use of com.amplifyframework.api.graphql.GraphQLResponse in project amplify-android by aws-amplify.

the class RxApiBindingTest method mutateEmitsResult.

/**
 * When the API behavior emits a result for a mutation, so too should the Rx binding.
 * @throws InterruptedException If interrupted while test observer is awaiting terminal event
 */
@Test
public void mutateEmitsResult() throws InterruptedException {
    // Arrange: category behaviour will yield a response
    Model model = RandomModel.model();
    GraphQLResponse<Model> response = new GraphQLResponse<>(model, Collections.emptyList());
    GraphQLRequest<Model> deleteRequest = createMockMutationRequest(Model.class);
    doAnswer(invocation -> {
        final int positionOfResultConsumer = 1;
        Consumer<GraphQLResponse<Model>> onResponse = invocation.getArgument(positionOfResultConsumer);
        onResponse.accept(response);
        return null;
    }).when(delegate).mutate(eq(deleteRequest), anyConsumer(), anyConsumer());
    // Act: mutation via the Rx binding
    TestObserver<GraphQLResponse<Model>> observer = rxApi.mutate(deleteRequest).test();
    // Assert: response is propagated via Rx
    observer.await(TIMEOUT_SECONDS, TimeUnit.SECONDS);
    observer.assertValue(response);
    verify(delegate).mutate(eq(deleteRequest), anyConsumer(), anyConsumer());
}
Also used : RandomModel(com.amplifyframework.testutils.random.RandomModel) Model(com.amplifyframework.core.model.Model) GraphQLResponse(com.amplifyframework.api.graphql.GraphQLResponse) Test(org.junit.Test)

Example 7 with GraphQLResponse

use of com.amplifyframework.api.graphql.GraphQLResponse in project amplify-android by aws-amplify.

the class RxApiBindingTest method mutateEmitsFailure.

/**
 * When the API behavior emits a failure for a mutation, so too should the Rx binding.
 * @throws InterruptedException If interrupted while test observer is awaiting terminal event
 */
@Test
public void mutateEmitsFailure() throws InterruptedException {
    // Arrange category behavior to fail
    ApiException expectedFailure = new ApiException("Expected", "Failure");
    GraphQLRequest<Model> deleteRequest = createMockMutationRequest(Model.class);
    doAnswer(invocation -> {
        final int positionOfFailureConsumer = 2;
        Consumer<ApiException> onFailure = invocation.getArgument(positionOfFailureConsumer);
        onFailure.accept(expectedFailure);
        return null;
    }).when(delegate).mutate(eq(deleteRequest), anyConsumer(), anyConsumer());
    // Act: access it via binding
    TestObserver<GraphQLResponse<Model>> observer = rxApi.mutate(deleteRequest).test();
    // Assert: failure is propagated
    observer.await(TIMEOUT_SECONDS, TimeUnit.SECONDS);
    observer.assertError(expectedFailure);
    verify(delegate).mutate(eq(deleteRequest), anyConsumer(), anyConsumer());
}
Also used : RandomModel(com.amplifyframework.testutils.random.RandomModel) Model(com.amplifyframework.core.model.Model) GraphQLResponse(com.amplifyframework.api.graphql.GraphQLResponse) ApiException(com.amplifyframework.api.ApiException) Test(org.junit.Test)

Example 8 with GraphQLResponse

use of com.amplifyframework.api.graphql.GraphQLResponse 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 9 with GraphQLResponse

use of com.amplifyframework.api.graphql.GraphQLResponse 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 10 with GraphQLResponse

use of com.amplifyframework.api.graphql.GraphQLResponse in project amplify-android by aws-amplify.

the class AWSApiPluginTest method headerInterceptorsAreConfigured.

/**
 * Validates that the plugin adds custom headers into the outgoing OkHttp request.
 * @throws ApiException Thrown from the query() call.
 * @throws InterruptedException Possible thrown from takeRequest()
 */
@Test
public void headerInterceptorsAreConfigured() throws ApiException, InterruptedException {
    // Arrange some response. This isn't the point of the test,
    // but it keeps the mock web server from freezing up.
    webServer.enqueue(new MockResponse().setBody(Resources.readAsString("blog-owners-query-results.json")));
    // Fire off a request
    Await.<GraphQLResponse<PaginatedResult<BlogOwner>>, ApiException>result((onResult, onError) -> plugin.query(ModelQuery.list(BlogOwner.class), onResult, onError));
    RecordedRequest recordedRequest = webServer.takeRequest(5, TimeUnit.MILLISECONDS);
    assertNotNull(recordedRequest);
    assertEquals("specialValue", recordedRequest.getHeader("specialKey"));
}
Also used : RecordedRequest(okhttp3.mockwebserver.RecordedRequest) MockResponse(okhttp3.mockwebserver.MockResponse) GraphQLResponse(com.amplifyframework.api.graphql.GraphQLResponse) BlogOwner(com.amplifyframework.testmodels.commentsblog.BlogOwner) ApiException(com.amplifyframework.api.ApiException) Test(org.junit.Test)

Aggregations

GraphQLResponse (com.amplifyframework.api.graphql.GraphQLResponse)30 Test (org.junit.Test)23 PaginatedResult (com.amplifyframework.api.graphql.PaginatedResult)14 Model (com.amplifyframework.core.model.Model)11 ApiException (com.amplifyframework.api.ApiException)10 Action (com.amplifyframework.core.Action)9 Consumer (com.amplifyframework.core.Consumer)9 ModelWithMetadata (com.amplifyframework.datastore.appsync.ModelWithMetadata)9 JSONObject (org.json.JSONObject)9 BlogOwner (com.amplifyframework.testmodels.commentsblog.BlogOwner)8 GraphQLRequest (com.amplifyframework.api.graphql.GraphQLRequest)7 RandomString (com.amplifyframework.testutils.random.RandomString)7 AmplifyException (com.amplifyframework.AmplifyException)6 ModelSchema (com.amplifyframework.core.model.ModelSchema)6 ModelMetadata (com.amplifyframework.datastore.appsync.ModelMetadata)6 RandomModel (com.amplifyframework.testutils.random.RandomModel)6 HashMap (java.util.HashMap)6 QueryType (com.amplifyframework.api.graphql.QueryType)5 SubscriptionType (com.amplifyframework.api.graphql.SubscriptionType)5 Cancelable (com.amplifyframework.core.async.Cancelable)5