use of com.amplifyframework.api.graphql.GraphQLLocation in project amplify-android by aws-amplify.
the class GsonGraphQLResponseFactoryTest method partialResponseRendersWithTodoDataAndErrors.
/**
* Validates that the converter is able to parse a partial GraphQL
* response into a result. In this case, the result contains some
* data, but also a list of errors.
* @throws ApiException From API configuration
*/
@Test
public void partialResponseRendersWithTodoDataAndErrors() throws ApiException {
// Arrange some JSON string from a "server"
final String partialResponseJson = Resources.readAsString("partial-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);
// Assert that the model contained things...
assertNotNull(response);
assertNotNull(response.getData());
assertNotNull(response.getData().getItems());
// Assert that all of the fields of the different todos
// match what we would expect from a manual inspection of the
// JSON.
final Iterable<Todo> actualTodos = response.getData().getItems();
final List<Todo> expectedTodos = Arrays.asList(Todo.builder().id("fa1c21cc-0458-4bca-bcb1-101579fb85c7").name(null).description("Test").build(), Todo.builder().id("68bad242-dec5-415b-acb3-daee3b069ce5").name(null).description("Test").build(), Todo.builder().id("f64e2e9a-42ad-4455-b8ee-d1cfae7e9f01").name(null).description("Test").build());
assertEquals(expectedTodos, actualTodos);
// Assert that we parsed the errors successfully.
assertNotNull(response.getErrors());
final List<GraphQLResponse.Error> expectedErrors = new ArrayList<>();
for (int i = 0; i < 3; i++) {
String message = "failed";
List<GraphQLLocation> locations = Collections.singletonList(new GraphQLLocation(5, 7));
List<GraphQLPathSegment> path = Arrays.asList(new GraphQLPathSegment("listTodos"), new GraphQLPathSegment("items"), new GraphQLPathSegment(i), new GraphQLPathSegment("name"));
Map<String, Object> extensions = new HashMap<>();
extensions.put("errorType", null);
extensions.put("errorInfo", null);
extensions.put("data", null);
expectedErrors.add(new GraphQLResponse.Error(message, locations, path, extensions));
}
assertEquals(expectedErrors, response.getErrors());
}
Aggregations