use of com.amplifyframework.api.graphql.GraphQLResponse in project amplify-android by aws-amplify.
the class AWSApiPluginTest method requestUsesIamForAuth.
/**
* Ensure the auth mode used for the request is AWS_IAM. We verify this by
* @throws AmplifyException Not expected.
* @throws InterruptedException Not expected.
*/
@Test
public void requestUsesIamForAuth() throws AmplifyException, InterruptedException {
webServer.enqueue(new MockResponse().setBody(Resources.readAsString("blog-owners-query-results.json")));
AppSyncGraphQLRequest<PaginatedResult<BlogOwner>> appSyncGraphQLRequest = createQueryRequestWithAuthMode(BlogOwner.class, AuthorizationType.AWS_IAM);
GraphQLResponse<PaginatedResult<BlogOwner>> actualResponse = Await.<GraphQLResponse<PaginatedResult<BlogOwner>>, ApiException>result((onResult, onError) -> plugin.query(appSyncGraphQLRequest, onResult, onError));
RecordedRequest recordedRequest = webServer.takeRequest();
assertNull(recordedRequest.getHeader("x-api-key"));
assertNotNull(recordedRequest.getHeader("authorization"));
assertTrue(recordedRequest.getHeader("authorization").startsWith("AWS4-HMAC-SHA256"));
assertEquals(Arrays.asList("Curly", "Moe", "Larry"), Observable.fromIterable(actualResponse.getData()).map(BlogOwner::getName).toList().blockingGet());
}
use of com.amplifyframework.api.graphql.GraphQLResponse in project amplify-android by aws-amplify.
the class AWSApiPluginTest method requestUsesCognitoForAuth.
/**
* If the auth mode is set for the individual request, ensure that the resulting request
* to AppSync has the correct auth header.
* @throws AmplifyException Not expected.
* @throws InterruptedException Not expected.
*/
@Test
public void requestUsesCognitoForAuth() throws AmplifyException, InterruptedException {
webServer.enqueue(new MockResponse().setBody(Resources.readAsString("blog-owners-query-results.json")));
AppSyncGraphQLRequest<PaginatedResult<BlogOwner>> appSyncGraphQLRequest = createQueryRequestWithAuthMode(BlogOwner.class, AuthorizationType.AMAZON_COGNITO_USER_POOLS);
GraphQLResponse<PaginatedResult<BlogOwner>> actualResponse = Await.<GraphQLResponse<PaginatedResult<BlogOwner>>, ApiException>result((onResult, onError) -> plugin.query(appSyncGraphQLRequest, onResult, onError));
RecordedRequest recordedRequest = webServer.takeRequest();
assertNull(recordedRequest.getHeader("x-api-key"));
assertNotNull(recordedRequest.getHeader("authorization"));
assertEquals("FAKE_TOKEN", recordedRequest.getHeader("authorization"));
assertEquals(Arrays.asList("Curly", "Moe", "Larry"), Observable.fromIterable(actualResponse.getData()).map(BlogOwner::getName).toList().blockingGet());
}
use of com.amplifyframework.api.graphql.GraphQLResponse in project amplify-android by aws-amplify.
the class AWSApiPluginTest method graphQlMutationGetsResponse.
/**
* It should be possible to perform a successful call to
* {@link AWSApiPlugin#mutate(GraphQLRequest, Consumer, Consumer)}.
* When the server returns a valid response, then the mutate methods should
* emit content via their value consumer.
* @throws ApiException If call to mutate(...) itself emits such an exception
* @throws JSONException On failure to arrange response JSON
*/
@Test
public void graphQlMutationGetsResponse() throws JSONException, ApiException {
HubAccumulator networkStatusObserver = HubAccumulator.create(HubChannel.API, ApiChannelEventName.API_ENDPOINT_STATUS_CHANGED, 1).start();
// Arrange a response from the "server"
String expectedName = RandomString.string();
webServer.enqueue(new MockResponse().setBody(new JSONObject().put("data", new JSONObject().put("createBlogOwner", new JSONObject().put("name", expectedName))).toString()));
// Try to perform a mutation.
BlogOwner tony = BlogOwner.builder().name(expectedName).build();
GraphQLResponse<BlogOwner> actualResponse = Await.<GraphQLResponse<BlogOwner>, ApiException>result(((onResult, onError) -> plugin.mutate(ModelMutation.create(tony), onResult, onError)));
// Assert that the expected response was received
assertEquals(expectedName, actualResponse.getData().getName());
// Verify that the expected hub event fired.
HubEvent<?> event = networkStatusObserver.awaitFirst();
assertNotNull(event);
assertTrue(event.getData() instanceof ApiEndpointStatusChangeEvent);
ApiEndpointStatusChangeEvent eventData = (ApiEndpointStatusChangeEvent) event.getData();
assertEquals(ApiEndpointStatusChangeEvent.ApiEndpointStatus.REACHABLE, eventData.getCurrentStatus());
}
use of com.amplifyframework.api.graphql.GraphQLResponse in project amplify-android by aws-amplify.
the class GsonGraphQLResponseFactoryTest method errorResponseDeserializesExtensionsMap.
/**
* This tests the GsonErrorDeserializer. The test JSON response has 4 errors, which are all in
* different formats, but are expected to be parsed into the same resulting object:
* 1. Error contains errorType, errorInfo, data (AppSync specific fields) at root level
* 2. Error contains errorType, errorInfo, data inside extensions object
* 3. Error contains errorType, errorInfo, data at root AND inside extensions (fields inside
* extensions take precedence)
* 4. Error contains errorType at root, and errorInfo, data inside extensions (all should be
* merged into extensions)
*
* @throws ApiException From API configuration
*/
@Test
public void errorResponseDeserializesExtensionsMap() throws ApiException {
// Arrange some JSON string from a "server"
final String partialResponseJson = Resources.readAsString("error-extensions-gql-response.json");
// Act! Parse it into a model.
Type responseType = TypeMaker.getParameterizedType(PaginatedResult.class, Todo.class);
GraphQLRequest<PaginatedResult<Todo>> request = buildDummyRequest(responseType);
final GraphQLResponse<PaginatedResult<Todo>> response = responseFactory.buildResponse(request, partialResponseJson);
// Build the expected response.
String message = "Conflict resolver rejects mutation.";
List<GraphQLLocation> locations = Collections.singletonList(new GraphQLLocation(11, 3));
List<GraphQLPathSegment> path = Arrays.asList(new GraphQLPathSegment("listTodos"), new GraphQLPathSegment("items"), new GraphQLPathSegment(0), new GraphQLPathSegment("name"));
Map<String, Object> data = new HashMap<>();
data.put("id", "EF48518C-92EB-4F7A-A64E-D1B9325205CF");
data.put("title", "new3");
data.put("content", "Original content from DataStoreEndToEndTests at 2020-03-26 21:55:47 " + "+0000");
data.put("_version", 2);
Map<String, Object> extensions = new HashMap<>();
extensions.put("errorType", "ConflictUnhandled");
extensions.put("errorInfo", null);
extensions.put("data", data);
GraphQLResponse.Error expectedError = new GraphQLResponse.Error(message, locations, path, extensions);
GraphQLResponse<PaginatedResult<Todo>> expectedResponse = new GraphQLResponse<>(null, Arrays.asList(expectedError, expectedError, expectedError, expectedError));
// Assert that the response is expected
assertEquals(expectedResponse, response);
}
use of com.amplifyframework.api.graphql.GraphQLResponse in project amplify-android by aws-amplify.
the class GsonGraphQLResponseFactoryTest method errorWithNullFieldsCanBeParsed.
/**
* If an {@link GraphQLResponse} contains a non-null {@link GraphQLResponse.Error},
* and if that error object itself contains some null-valued fields, the response factory
* should be resilient to this, and continue to render a response, anyway, without
* throwing an exception over the issue.
* @throws ApiException On failure to build a response, perhaps because the null
* valued items inside of the {@link GraphQLResponse.Error}
* could not be parsed
*/
@Test
public void errorWithNullFieldsCanBeParsed() throws ApiException {
// Arrange some JSON string from a "server"
final String responseJson = Resources.readAsString("error-null-properties.json");
// Act! Parse it into a model.
Type responseType = TypeMaker.getParameterizedType(PaginatedResult.class, Todo.class);
GraphQLRequest<PaginatedResult<Todo>> request = buildDummyRequest(responseType);
final GraphQLResponse<PaginatedResult<Todo>> response = responseFactory.buildResponse(request, responseJson);
// Build the expected response.
Map<String, Object> extensions = new HashMap<>();
extensions.put("errorType", null);
extensions.put("errorInfo", null);
extensions.put("data", null);
GraphQLResponse.Error expectedError = new GraphQLResponse.Error("the message", null, null, extensions);
GraphQLResponse<PaginatedResult<Todo>> expectedResponse = new GraphQLResponse<>(null, Collections.singletonList(expectedError));
// Assert that the response is expected
assertEquals(expectedResponse, response);
}
Aggregations