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());
}
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());
}
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);
}
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();
}
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"));
}
Aggregations