Search in sources :

Example 91 with ModelSchema

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

the class AppSyncRequestFactoryTest method validateUpdateMutationOnlyContainsChangedFields.

/**
 * Verify that only the fields included in serializedData on the SerializedModel are included on the API request.
 * To test this, create a SerializedModel of a BlogOwner, which only has "id", and "name", not the "wea" field.
 * Then, verify that the request does not contain "wea" field.
 *
 * @throws JSONException from JSONAssert.assertEquals JSON parsing error
 * @throws AmplifyException from ModelSchema.fromModelClass to convert model to schema
 */
@Test
public void validateUpdateMutationOnlyContainsChangedFields() throws JSONException, AmplifyException {
    ModelSchema modelSchema = ModelSchema.fromModelClass(BlogOwner.class);
    Map<String, Object> serializedData = new HashMap<>();
    serializedData.put("id", "5aef1282-64d6-4fa8-ba2c-290f9d9c6973");
    serializedData.put("name", "John Smith");
    SerializedModel blogOwner = SerializedModel.builder().serializedData(serializedData).modelSchema(modelSchema).build();
    // Assert
    JSONAssert.assertEquals(Resources.readAsString("update-blog-owner-only-changed-fields.txt"), AppSyncRequestFactory.buildUpdateRequest(modelSchema, blogOwner, 1, QueryPredicates.all(), DEFAULT_STRATEGY).getContent(), true);
}
Also used : ModelSchema(com.amplifyframework.core.model.ModelSchema) HashMap(java.util.HashMap) SerializedModel(com.amplifyframework.core.model.SerializedModel) Test(org.junit.Test)

Example 92 with ModelSchema

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

the class AppSyncRequestFactoryTest method validateUpdateNestedCustomTypeWithPredicateGeneration.

/**
 * Checks that we're getting the expected output for a mutation with predicate.
 * @throws DataStoreException If the output does not match.
 * @throws AmplifyException On failure to parse ModelSchema from model class
 * @throws JSONException from JSONAssert.assertEquals.
 */
@Test
public void validateUpdateNestedCustomTypeWithPredicateGeneration() throws AmplifyException, JSONException {
    ModelSchema schema = ModelSchema.fromModelClass(Parent.class);
    JSONAssert.assertEquals(Resources.readAsString("update-parent-with-predicate.txt"), AppSyncRequestFactory.buildUpdateRequest(schema, buildTestParentModel(), 42, Parent.NAME.contains("Jane Doe"), DEFAULT_STRATEGY).getContent(), true);
}
Also used : ModelSchema(com.amplifyframework.core.model.ModelSchema) Test(org.junit.Test)

Example 93 with ModelSchema

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

the class AppSyncRequestFactoryTest method validateRequestGenerationForDeltaSync.

/**
 * Validates the construction of a delta-sync query document.
 * @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 validateRequestGenerationForDeltaSync() throws AmplifyException, JSONException {
    ModelSchema schema = ModelSchema.fromModelClass(Post.class);
    JSONAssert.assertEquals(Resources.readAsString("delta-sync-request-document-for-post.txt"), AppSyncRequestFactory.buildSyncRequest(schema, 123123123L, null, QueryPredicates.all(), DEFAULT_STRATEGY).getContent(), true);
}
Also used : ModelSchema(com.amplifyframework.core.model.ModelSchema) Test(org.junit.Test)

Example 94 with ModelSchema

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

the class TopologicalOrderingTest method orderingOfBlogPostComment.

/**
 * Checks the topological ordering of the Blog, Post, and Comment classes.
 * @throws AmplifyException On failure to load models into registry
 */
@Test
public void orderingOfBlogPostComment() throws AmplifyException {
    // Load the models into the registry.
    // They are provided intentionally out of topological order.
    final SimpleModelProvider provider = SimpleModelProvider.withRandomVersion(Comment.class, Blog.class, BlogOwner.class, Post.class);
    final SchemaRegistry registry = SchemaRegistry.instance();
    registry.clear();
    registry.register(provider.models());
    // Find the schema that the registry created for each of the models.
    ModelSchema commentSchema = findSchema(registry, Comment.class);
    ModelSchema postSchema = findSchema(registry, Post.class);
    ModelSchema blogSchema = findSchema(registry, Blog.class);
    // Act: get a topological ordering of the models.
    TopologicalOrdering topologicalOrdering = TopologicalOrdering.forRegisteredModels(registry, provider);
    // Assert: Blog comes before Post, and Post comes before Comment.
    assertTrue(topologicalOrdering.check(blogSchema).isBefore(postSchema));
    assertTrue(topologicalOrdering.check(postSchema).isBefore(commentSchema));
    // Assert: in other words, Comment is after Post, and Post is after Blog.
    assertTrue(topologicalOrdering.check(commentSchema).isAfter(postSchema));
    assertTrue(topologicalOrdering.check(postSchema).isAfter(blogSchema));
}
Also used : ModelSchema(com.amplifyframework.core.model.ModelSchema) SimpleModelProvider(com.amplifyframework.datastore.model.SimpleModelProvider) SchemaRegistry(com.amplifyframework.core.model.SchemaRegistry) Test(org.junit.Test)

Example 95 with ModelSchema

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

the class AppSyncClient method subscription.

private <T extends Model> Cancelable subscription(SubscriptionType subscriptionType, ModelSchema modelSchema, Consumer<String> onSubscriptionStarted, Consumer<GraphQLResponse<ModelWithMetadata<T>>> onNextResponse, Consumer<DataStoreException> onSubscriptionFailure, Action onSubscriptionCompleted) {
    final GraphQLRequest<ModelWithMetadata<T>> request;
    try {
        request = AppSyncRequestFactory.buildSubscriptionRequest(modelSchema, subscriptionType, authModeStrategyType);
    } catch (DataStoreException requestGenerationException) {
        onSubscriptionFailure.accept(requestGenerationException);
        return new NoOpCancelable();
    }
    final Consumer<GraphQLResponse<ModelWithMetadata<T>>> responseConsumer = response -> {
        if (response.hasErrors()) {
            onSubscriptionFailure.accept(new DataStoreException.GraphQLResponseException("Subscription error for " + modelSchema.getName() + ": " + response.getErrors(), response.getErrors()));
        } else {
            onNextResponse.accept(response);
        }
    };
    final Consumer<ApiException> failureConsumer = failure -> onSubscriptionFailure.accept(new DataStoreException("Error during subscription.", failure, "Evaluate details."));
    final Cancelable cancelable = api.subscribe(request, onSubscriptionStarted, responseConsumer, failureConsumer, onSubscriptionCompleted);
    if (cancelable != null) {
        return cancelable;
    }
    return new NoOpCancelable();
}
Also used : Amplify(com.amplifyframework.core.Amplify) AmplifyException(com.amplifyframework.AmplifyException) NonNull(androidx.annotation.NonNull) QueryPredicates(com.amplifyframework.core.model.query.predicate.QueryPredicates) GraphQLRequest(com.amplifyframework.api.graphql.GraphQLRequest) Model(com.amplifyframework.core.model.Model) Action(com.amplifyframework.core.Action) ApiException(com.amplifyframework.api.ApiException) Logger(com.amplifyframework.logging.Logger) GraphQLBehavior(com.amplifyframework.api.graphql.GraphQLBehavior) DataStoreException(com.amplifyframework.datastore.DataStoreException) Consumer(com.amplifyframework.core.Consumer) Nullable(androidx.annotation.Nullable) Cancelable(com.amplifyframework.core.async.Cancelable) SubscriptionType(com.amplifyframework.api.graphql.SubscriptionType) AuthModeStrategyType(com.amplifyframework.api.aws.AuthModeStrategyType) NoOpCancelable(com.amplifyframework.core.async.NoOpCancelable) ModelSchema(com.amplifyframework.core.model.ModelSchema) GraphQLResponse(com.amplifyframework.api.graphql.GraphQLResponse) ApiCategoryBehavior(com.amplifyframework.api.ApiCategoryBehavior) PaginatedResult(com.amplifyframework.api.graphql.PaginatedResult) QueryPredicate(com.amplifyframework.core.model.query.predicate.QueryPredicate) DataStoreException(com.amplifyframework.datastore.DataStoreException) GraphQLResponse(com.amplifyframework.api.graphql.GraphQLResponse) NoOpCancelable(com.amplifyframework.core.async.NoOpCancelable) Cancelable(com.amplifyframework.core.async.Cancelable) NoOpCancelable(com.amplifyframework.core.async.NoOpCancelable) ApiException(com.amplifyframework.api.ApiException)

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