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);
}
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);
}
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);
}
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));
}
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();
}
Aggregations