Search in sources :

Example 41 with ModelSchema

use of com.amplifyframework.core.model.ModelSchema in project amplify-android by aws-amplify.

the class AppSyncRequestFactoryTest method validatePredicateOperationForSyncExpressionIsWrappedWithAnd.

/**
 * If a QueryPredicateOperation is provided, it should be wrapped in an AND group.  This enables AppSync to
 * optimize by performing an DDB query instead of scan.
 * @throws AmplifyException On failure to parse ModelSchema from model class
 * @throws JSONException from JSONAssert.assertEquals.
 */
@Test
public void validatePredicateOperationForSyncExpressionIsWrappedWithAnd() throws AmplifyException, JSONException {
    String id = "426f8e8d-ea0f-4839-a73f-6a2a38565ba1";
    ModelSchema schema = ModelSchema.fromModelClass(BlogOwner.class);
    final GraphQLRequest<Iterable<Post>> request = AppSyncRequestFactory.buildSyncRequest(schema, null, null, BlogOwner.ID.eq(id));
    JSONAssert.assertEquals(Resources.readAsString("base-sync-request-with-predicate-operation.txt"), request.getContent(), true);
}
Also used : ModelSchema(com.amplifyframework.core.model.ModelSchema) Test(org.junit.Test)

Example 42 with ModelSchema

use of com.amplifyframework.core.model.ModelSchema in project amplify-android by aws-amplify.

the class AppSyncRequestFactoryTest method validateSubscriptionGenerationOnCreateForNestedCustomType.

/**
 * Validates that a GraphQL request document can be created, to get onCreate for nested custom type
 * subscription notifications for a Parent.class.
 * @throws DataStoreException On failure to interrogate the Blog.class.
 * @throws AmplifyException On failure to parse ModelSchema from model class
 * @throws JSONException from JSONAssert.assertEquals.
 */
@Test
public void validateSubscriptionGenerationOnCreateForNestedCustomType() throws AmplifyException, JSONException {
    ModelSchema schema = ModelSchema.fromModelClass(Parent.class);
    JSONAssert.assertEquals(Resources.readAsString("on-create-request-for-parent.txt"), AppSyncRequestFactory.buildSubscriptionRequest(schema, SubscriptionType.ON_CREATE, DEFAULT_STRATEGY).getContent(), true);
}
Also used : ModelSchema(com.amplifyframework.core.model.ModelSchema) Test(org.junit.Test)

Example 43 with ModelSchema

use of com.amplifyframework.core.model.ModelSchema in project amplify-android by aws-amplify.

the class AppSyncRequestFactoryTest method validateSubscriptionGenerationOnUpdatePost.

/**
 * Validates generation of a GraphQL document which requests a subscription for updates
 * to the Blog.class.
 * @throws DataStoreException On failure to interrogate fields in Blog.class.
 * @throws AmplifyException On failure to parse ModelSchema from model class
 * @throws JSONException from JSONAssert.assertEquals.
 */
@Test
public void validateSubscriptionGenerationOnUpdatePost() throws AmplifyException, JSONException {
    ModelSchema schema = ModelSchema.fromModelClass(Post.class);
    JSONAssert.assertEquals(Resources.readAsString("on-update-request-for-post.txt"), AppSyncRequestFactory.buildSubscriptionRequest(schema, SubscriptionType.ON_UPDATE, DEFAULT_STRATEGY).getContent(), true);
}
Also used : ModelSchema(com.amplifyframework.core.model.ModelSchema) Test(org.junit.Test)

Example 44 with ModelSchema

use of com.amplifyframework.core.model.ModelSchema in project amplify-android by aws-amplify.

the class TopologicalOrdering method forRegisteredModels.

/**
 * Gets a TopologicalOrdering of the ModelSchema in the SchemaRegistry.
 * The set of ModelSchema in that registry is not expected to change during runtime,
 * so the results of this TopologicalOrdering should likewise be stable at runtime.
 * @param schemaRegistry A registry of ModelSchema
 * @param modelProvider A ModelProvider
 * @return A topological ordering of the model schema in the registry
 */
@SuppressLint("SyntheticAccessor")
static TopologicalOrdering forRegisteredModels(@NonNull SchemaRegistry schemaRegistry, @NonNull ModelProvider modelProvider) {
    Objects.requireNonNull(modelProvider);
    final List<ModelSchema> schemaForModels = new ArrayList<>();
    for (String modelClassName : modelProvider.modelNames()) {
        final ModelSchema schemaForModelClass = schemaRegistry.getModelSchemaForModelClass(modelClassName);
        schemaForModels.add(schemaForModelClass);
    }
    return new TopologicalOrdering(new TopologicalSort(schemaForModels).result());
}
Also used : ModelSchema(com.amplifyframework.core.model.ModelSchema) ArrayList(java.util.ArrayList) SuppressLint(android.annotation.SuppressLint)

Example 45 with ModelSchema

use of com.amplifyframework.core.model.ModelSchema in project amplify-android by aws-amplify.

the class AppSyncClientTest method validateBaseSyncQueryGen.

/**
 * Validates the construction of a base-sync query.
 * @throws JSONException On bad request JSON found in API category call
 * @throws DataStoreException If no valid response returned from AppSync endpoint during sync
 * @throws AmplifyException On failure to arrange model schema
 */
@Test
public void validateBaseSyncQueryGen() throws JSONException, AmplifyException {
    ModelSchema schema = ModelSchema.fromModelClass(BlogOwner.class);
    Await.result((Consumer<GraphQLResponse<PaginatedResult<ModelWithMetadata<BlogOwner>>>> onResult, Consumer<DataStoreException> onError) -> {
        try {
            GraphQLRequest<PaginatedResult<ModelWithMetadata<BlogOwner>>> request = endpoint.buildSyncRequest(schema, null, null, QueryPredicates.all());
            endpoint.sync(request, onResult, onError);
        } catch (DataStoreException datastoreException) {
            onError.accept(datastoreException);
        }
    });
    // Now, capture the request argument on API, so we can see what was passed.
    // Recall that we pass a raw doc to API.
    ArgumentCaptor<GraphQLRequest<ModelWithMetadata<BlogOwner>>> requestCaptor = ArgumentCaptor.forClass(GraphQLRequest.class);
    verify(api).query(requestCaptor.capture(), any(Consumer.class), any(Consumer.class));
    GraphQLRequest<ModelWithMetadata<BlogOwner>> capturedRequest = requestCaptor.getValue();
    Type type = TypeMaker.getParameterizedType(PaginatedResult.class, ModelWithMetadata.class, BlogOwner.class);
    assertEquals(type, capturedRequest.getResponseType());
    // The request was sent as JSON. It has a null variables field, and a present query field.
    JSONAssert.assertEquals(Resources.readAsString("base-sync-request-document-for-blog-owner.txt"), capturedRequest.getContent(), true);
}
Also used : ModelSchema(com.amplifyframework.core.model.ModelSchema) GraphQLRequest(com.amplifyframework.api.graphql.GraphQLRequest) DataStoreException(com.amplifyframework.datastore.DataStoreException) Type(java.lang.reflect.Type) Consumer(com.amplifyframework.core.Consumer) PaginatedResult(com.amplifyframework.api.graphql.PaginatedResult) BlogOwner(com.amplifyframework.testmodels.commentsblog.BlogOwner) Test(org.junit.Test)

Aggregations

ModelSchema (com.amplifyframework.core.model.ModelSchema)109 Test (org.junit.Test)69 SerializedModel (com.amplifyframework.core.model.SerializedModel)34 BlogOwner (com.amplifyframework.testmodels.commentsblog.BlogOwner)30 Model (com.amplifyframework.core.model.Model)28 DataStoreException (com.amplifyframework.datastore.DataStoreException)26 HashMap (java.util.HashMap)23 ArrayList (java.util.ArrayList)22 SchemaRegistry (com.amplifyframework.core.model.SchemaRegistry)21 AmplifyException (com.amplifyframework.AmplifyException)19 Consumer (com.amplifyframework.core.Consumer)19 List (java.util.List)17 NonNull (androidx.annotation.NonNull)14 Cancelable (com.amplifyframework.core.async.Cancelable)14 TimeUnit (java.util.concurrent.TimeUnit)14 QueryPredicate (com.amplifyframework.core.model.query.predicate.QueryPredicate)13 Action (com.amplifyframework.core.Action)12 ModelWithMetadata (com.amplifyframework.datastore.appsync.ModelWithMetadata)12 Collections (java.util.Collections)12 ModelProvider (com.amplifyframework.core.model.ModelProvider)11