use of com.amplifyframework.api.graphql.PaginatedResult 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.PaginatedResult in project amplify-android by aws-amplify.
the class AWSApiPluginUserAgentTest method checkUserAgent.
private String checkUserAgent() throws Exception {
// Make a new query request
GraphQLRequest<PaginatedResult<Todo>> listTodos = ModelQuery.list(Todo.class);
// Ignore result
api.query(listTodos, NoOpConsumer.create(), NoOpConsumer.create());
// Wait for server to receive the request and return user agent
RecordedRequest request = server.takeRequest(REQUEST_TIMEOUT_SECONDS, TimeUnit.SECONDS);
assertNotNull(request);
return request.getHeader("User-Agent");
}
use of com.amplifyframework.api.graphql.PaginatedResult 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.PaginatedResult 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);
}
use of com.amplifyframework.api.graphql.PaginatedResult in project amplify-android by aws-amplify.
the class GsonGraphQLResponseFactoryTest method responseRendersAsPaginatedResult.
/**
* 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 AmplifyException From API configuration
*/
@Test
public void responseRendersAsPaginatedResult() throws AmplifyException {
// Expect
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());
String nextToken = "eyJ2ZXJzaW9uIjoyLCJ0b2tlbiI6IkFRSUNBSGg5OUIvN3BjWU41eE96NDZJMW5GeGM4";
Type responseType = TypeMaker.getParameterizedType(PaginatedResult.class, Todo.class);
AppSyncGraphQLRequest<PaginatedResult<Todo>> expectedRequest = buildDummyRequest(responseType);
expectedRequest = expectedRequest.newBuilder().variable("nextToken", "String", nextToken).build();
final PaginatedResult<Todo> expectedPaginatedResult = new PaginatedResult<>(expectedTodos, expectedRequest);
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));
}
final GraphQLResponse<PaginatedResult<Todo>> expectedResponse = new GraphQLResponse<>(expectedPaginatedResult, expectedErrors);
// Act
final String partialResponseJson = Resources.readAsString("partial-gql-response.json");
final GraphQLRequest<PaginatedResult<Todo>> request = buildDummyRequest(responseType);
final GraphQLResponse<PaginatedResult<Todo>> response = responseFactory.buildResponse(request, partialResponseJson);
// Assert
assertEquals(expectedResponse, response);
}
Aggregations