use of com.amplifyframework.core.model.ModelSchema in project amplify-android by aws-amplify.
the class MutationProcessor method update.
// For an item in the outbox, dispatch an update mutation
private <T extends Model> Single<ModelWithMetadata<T>> update(PendingMutation<T> mutation) {
final T updatedItem = mutation.getMutatedItem();
final ModelSchema updatedItemSchema = this.schemaRegistry.getModelSchemaForModelClass(updatedItem.getModelName());
return versionRepository.findModelVersion(updatedItem).flatMap(version -> publishWithStrategy(mutation, (model, onSuccess, onError) -> appSync.update(model, updatedItemSchema, version, mutation.getPredicate(), onSuccess, onError)));
}
use of com.amplifyframework.core.model.ModelSchema in project amplify-android by aws-amplify.
the class MutationProcessor method delete.
// For an item in the outbox, dispatch a delete mutation
private <T extends Model> Single<ModelWithMetadata<T>> delete(PendingMutation<T> mutation) {
final T deletedItem = mutation.getMutatedItem();
final ModelSchema deletedItemSchema = this.schemaRegistry.getModelSchemaForModelClass(deletedItem.getModelName());
return versionRepository.findModelVersion(deletedItem).flatMap(version -> publishWithStrategy(mutation, (model, onSuccess, onError) -> appSync.delete(deletedItem, deletedItemSchema, version, mutation.getPredicate(), onSuccess, onError)));
}
use of com.amplifyframework.core.model.ModelSchema in project amplify-android by aws-amplify.
the class BasicCloudSyncInstrumentationTest method syncDownFromCloudIsWorking.
/**
* The sync engine should receive mutations for its managed models, through its
* subscriptions. When we create a model remotely, the sync engine should respond
* by processing the subscription event and saving the model locally.
* @throws DataStoreException On failure to query the local data store for
* local presence of arranged data (second step)
* @throws AmplifyException On failure to arrange a {@link DataStoreCategory} via the
* {@link DataStoreCategoryConfigurator}
*/
@Test
public void syncDownFromCloudIsWorking() throws AmplifyException {
// This model will get saved to the cloud.
BlogOwner jameson = BlogOwner.builder().name("Jameson Williams").build();
// Start watching locally, to see if it shows up on the client.
HubAccumulator receiptAccumulator = HubAccumulator.create(HubChannel.DATASTORE, receiptOf(jameson.getId()), 1).start();
// Act: create the model in the cloud
ModelSchema schema = ModelSchema.fromModelClass(BlogOwner.class);
GraphQLResponse<ModelWithMetadata<BlogOwner>> createResponse = appSync.create(jameson, schema);
ModelMetadata metadata = createResponse.getData().getSyncMetadata();
assertEquals(Integer.valueOf(1), metadata.getVersion());
// Wait for the events to show up on Hub.
receiptAccumulator.awaitFirst(TIMEOUT_SECONDS, TimeUnit.SECONDS);
// Jameson should be in the local DataStore.
BlogOwner owner = dataStore.get(BlogOwner.class, jameson.getId());
assertEquals("Jameson Williams", owner.getName());
assertEquals(jameson.getId(), owner.getId());
}
use of com.amplifyframework.core.model.ModelSchema in project amplify-android by aws-amplify.
the class HybridAssociationSyncInstrumentationTest method associatedModelAreSyncedDownFromCloud.
/**
* When the cloud sees an update to its data, the new data should be reflected in the
* local store. What's more, we should be able to query for the updated data by its model names,
* and expect to see the result, that way. This should hold for associated models, too.
* @throws AmplifyException For a variety of reasons, including failure to build schema,
* or bad interaction with API or DataStore
*/
@Ignore("It passes. Not automating due to operational concerns as noted in class-level @Ignore.")
@Test
public void associatedModelAreSyncedDownFromCloud() throws AmplifyException {
// Create a BlogOwner on the remote system,
// and wait for it to trickle back to the client.
BlogOwner owner = BlogOwner.builder().name("Agent Texas").build();
String ownerModelName = BlogOwner.class.getSimpleName();
ModelSchema ownerSchema = schemaProvider.modelSchemas().get(ownerModelName);
assertNotNull(ownerSchema);
HubAccumulator ownerAccumulator = HubAccumulator.create(HubChannel.DATASTORE, receiptOf(owner.getId()), 1).start();
appSync.create(owner, ownerSchema);
ownerAccumulator.await(TIMEOUT_SECONDS, TimeUnit.SECONDS);
// Now, validate that we see the data locally, when we query for serialized models
// and by Java BlogOwners.
Map<String, Object> expectedOwnerData = new HashMap<>();
expectedOwnerData.put("id", owner.getId());
expectedOwnerData.put("name", owner.getName());
List<SerializedModel> actualSerializedOwners = hybridBehaviors.list(ownerSchema.getName());
assertTrue(actualSerializedOwners.contains(SerializedModel.builder().serializedData(expectedOwnerData).modelSchema(ownerSchema).build()));
assertTrue(normalBehaviors.list(BlogOwner.class).contains(owner));
// Now, remotely save a model that has an association to the owner above.
Blog blog = Blog.builder().name("Blog about Texas").owner(owner).build();
String blogModelName = Blog.class.getSimpleName();
ModelSchema blogSchema = schemaProvider.modelSchemas().get(blogModelName);
assertNotNull(blogSchema);
HubAccumulator blogAccumulator = HubAccumulator.create(HubChannel.DATASTORE, receiptOf(blog.getId()), 1).start();
appSync.create(blog, blogSchema);
blogAccumulator.await(TIMEOUT_SECONDS, TimeUnit.SECONDS);
// Validate that we can find the newly associated model locally, now.
Map<String, Object> expectedBlogData = new HashMap<>();
expectedBlogData.put("id", blog.getId());
expectedBlogData.put("name", blog.getName());
expectedBlogData.put("owner", SerializedModel.builder().serializedData(Collections.singletonMap("id", owner.getId())).modelSchema(null).build());
List<SerializedModel> expectedSerializedBlogs = hybridBehaviors.list(blogSchema.getName());
assertTrue(expectedSerializedBlogs.contains(SerializedModel.builder().serializedData(expectedBlogData).modelSchema(blogSchema).build()));
assertTrue(normalBehaviors.list(Blog.class).contains(blog));
}
use of com.amplifyframework.core.model.ModelSchema in project amplify-android by aws-amplify.
the class HybridAssociationSyncInstrumentationTest method associatedModelsAreSyncedUpToCloud.
/**
* When we save {@link SerializedModel}s, we should find them in the cloud,
* shortly there-after. Saving associated serialized models will work.
* @throws AmplifyException For a variety of reasons, including failure to build schema,
* or bad interaction with API or DataStore
*/
@Ignore("It passes. Not automating due to operational concerns as noted in class-level @Ignore.")
@Test
public void associatedModelsAreSyncedUpToCloud() throws AmplifyException {
// First up, we're going to save a "leaf" model, a BlogOwner.
String ownerModelName = BlogOwner.class.getSimpleName();
ModelSchema ownerSchema = schemaProvider.modelSchemas().get(ownerModelName);
assertNotNull(ownerSchema);
BlogOwner owner = BlogOwner.builder().name("Guillermo Esteban").build();
Map<String, Object> ownerData = new HashMap<>();
ownerData.put("id", owner.getId());
ownerData.put("name", owner.getName());
SerializedModel serializedOwner = SerializedModel.builder().serializedData(ownerData).modelSchema(ownerSchema).build();
// Setup an accumulator so we know when there has been a publication.
HubAccumulator ownerAccumulator = HubAccumulator.create(HubChannel.DATASTORE, publicationOf(ownerModelName, owner.getId()), 1).start();
hybridBehaviors.save(serializedOwner);
ownerAccumulator.await(TIMEOUT_SECONDS, TimeUnit.SECONDS);
// Validate that the Blog Owner was saved locally, and in the cloud.
List<SerializedModel> allCurrentSerializedOwners = hybridBehaviors.list(ownerSchema.getName());
assertTrue(allCurrentSerializedOwners.contains(serializedOwner));
List<BlogOwner> allCurrentBlogOwners = normalBehaviors.list(BlogOwner.class);
assertTrue(allCurrentBlogOwners.contains(owner));
assertEquals(owner, api.get(BlogOwner.class, owner.getId()));
// Now, we're going to save a type with a connection.
// Build a blog, and its serialized form. Blog has association to a BlogOwner.
Blog blog = Blog.builder().name("A wonderful blog").owner(owner).build();
Map<String, Object> blogData = new HashMap<>();
blogData.put("id", blog.getId());
blogData.put("name", blog.getName());
blogData.put("owner", SerializedModel.builder().serializedData(Collections.singletonMap("id", owner.getId())).modelSchema(null).build());
String blogSchemaName = Blog.class.getSimpleName();
ModelSchema blogSchema = schemaProvider.modelSchemas().get(blogSchemaName);
assertNotNull(blogSchema);
SerializedModel serializedBlog = SerializedModel.builder().serializedData(blogData).modelSchema(blogSchema).build();
// Save the blog
HubAccumulator blogAccumulator = HubAccumulator.create(HubChannel.DATASTORE, publicationOf(blogSchemaName, blog.getId()), 1).start();
hybridBehaviors.save(serializedBlog);
blogAccumulator.await(TIMEOUT_SECONDS, TimeUnit.SECONDS);
// Validate that we find the blog locally, and on the remote system.
List<SerializedModel> allCurrentSerializedBlogs = hybridBehaviors.list(blogSchema.getName());
assertTrue(allCurrentSerializedBlogs.contains(serializedBlog));
List<Blog> allCurrentBlogs = normalBehaviors.list(Blog.class);
assertTrue(allCurrentBlogs.contains(blog));
Blog foundBlog = api.get(Blog.class, blog.getId());
assertEquals(blog, foundBlog);
assertEquals(owner.getId(), foundBlog.getOwner().getId());
}
Aggregations