Search in sources :

Example 6 with PaginatedResult

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());
}
Also used : RecordedRequest(okhttp3.mockwebserver.RecordedRequest) MockResponse(okhttp3.mockwebserver.MockResponse) PaginatedResult(com.amplifyframework.api.graphql.PaginatedResult) GraphQLResponse(com.amplifyframework.api.graphql.GraphQLResponse) BlogOwner(com.amplifyframework.testmodels.commentsblog.BlogOwner) ApiException(com.amplifyframework.api.ApiException) Test(org.junit.Test)

Example 7 with PaginatedResult

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");
}
Also used : RecordedRequest(okhttp3.mockwebserver.RecordedRequest) PaginatedResult(com.amplifyframework.api.graphql.PaginatedResult)

Example 8 with PaginatedResult

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);
}
Also used : GraphQLLocation(com.amplifyframework.api.graphql.GraphQLLocation) HashMap(java.util.HashMap) PaginatedResult(com.amplifyframework.api.graphql.PaginatedResult) GraphQLResponse(com.amplifyframework.api.graphql.GraphQLResponse) GraphQLPathSegment(com.amplifyframework.api.graphql.GraphQLPathSegment) QueryType(com.amplifyframework.api.graphql.QueryType) Type(java.lang.reflect.Type) JSONObject(org.json.JSONObject) Test(org.junit.Test)

Example 9 with PaginatedResult

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);
}
Also used : QueryType(com.amplifyframework.api.graphql.QueryType) Type(java.lang.reflect.Type) HashMap(java.util.HashMap) PaginatedResult(com.amplifyframework.api.graphql.PaginatedResult) GraphQLResponse(com.amplifyframework.api.graphql.GraphQLResponse) JSONObject(org.json.JSONObject) Test(org.junit.Test)

Example 10 with PaginatedResult

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);
}
Also used : GraphQLLocation(com.amplifyframework.api.graphql.GraphQLLocation) HashMap(java.util.HashMap) PaginatedResult(com.amplifyframework.api.graphql.PaginatedResult) ArrayList(java.util.ArrayList) GraphQLResponse(com.amplifyframework.api.graphql.GraphQLResponse) GraphQLPathSegment(com.amplifyframework.api.graphql.GraphQLPathSegment) QueryType(com.amplifyframework.api.graphql.QueryType) Type(java.lang.reflect.Type) JSONObject(org.json.JSONObject) Test(org.junit.Test)

Aggregations

PaginatedResult (com.amplifyframework.api.graphql.PaginatedResult)18 Test (org.junit.Test)13 GraphQLResponse (com.amplifyframework.api.graphql.GraphQLResponse)12 Type (java.lang.reflect.Type)8 GraphQLRequest (com.amplifyframework.api.graphql.GraphQLRequest)7 QueryType (com.amplifyframework.api.graphql.QueryType)7 Consumer (com.amplifyframework.core.Consumer)7 HashMap (java.util.HashMap)6 JSONObject (org.json.JSONObject)6 ModelSchema (com.amplifyframework.core.model.ModelSchema)5 BlogOwner (com.amplifyframework.testmodels.commentsblog.BlogOwner)5 ApiException (com.amplifyframework.api.ApiException)4 GraphQLLocation (com.amplifyframework.api.graphql.GraphQLLocation)4 GraphQLPathSegment (com.amplifyframework.api.graphql.GraphQLPathSegment)4 DataStoreException (com.amplifyframework.datastore.DataStoreException)4 ModelWithMetadata (com.amplifyframework.datastore.appsync.ModelWithMetadata)4 ArrayList (java.util.ArrayList)4 AmplifyException (com.amplifyframework.AmplifyException)3 ApiCategory (com.amplifyframework.api.ApiCategory)3 ApiCategoryConfiguration (com.amplifyframework.api.ApiCategoryConfiguration)3