Search in sources :

Example 51 with ModelSchema

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)));
}
Also used : Amplify(com.amplifyframework.core.Amplify) Single(io.reactivex.rxjava3.core.Single) HubChannel(com.amplifyframework.hub.HubChannel) SerializedModel(com.amplifyframework.core.model.SerializedModel) NonNull(androidx.annotation.NonNull) ModelWithMetadata(com.amplifyframework.datastore.appsync.ModelWithMetadata) Model(com.amplifyframework.core.model.Model) AppSync(com.amplifyframework.datastore.appsync.AppSync) Completable(io.reactivex.rxjava3.core.Completable) OutboxStatusEvent(com.amplifyframework.datastore.events.OutboxStatusEvent) AppSyncConflictUnhandledError(com.amplifyframework.datastore.appsync.AppSyncConflictUnhandledError) Logger(com.amplifyframework.logging.Logger) SchemaRegistry(com.amplifyframework.core.model.SchemaRegistry) Objects(java.util.Objects) TimeUnit(java.util.concurrent.TimeUnit) Schedulers(io.reactivex.rxjava3.schedulers.Schedulers) DataStoreException(com.amplifyframework.datastore.DataStoreException) Consumer(com.amplifyframework.core.Consumer) List(java.util.List) CompositeDisposable(io.reactivex.rxjava3.disposables.CompositeDisposable) ModelSchema(com.amplifyframework.core.model.ModelSchema) GraphQLResponse(com.amplifyframework.api.graphql.GraphQLResponse) HubEvent(com.amplifyframework.hub.HubEvent) ModelSchema(com.amplifyframework.core.model.ModelSchema)

Example 52 with ModelSchema

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)));
}
Also used : Amplify(com.amplifyframework.core.Amplify) Single(io.reactivex.rxjava3.core.Single) HubChannel(com.amplifyframework.hub.HubChannel) SerializedModel(com.amplifyframework.core.model.SerializedModel) NonNull(androidx.annotation.NonNull) ModelWithMetadata(com.amplifyframework.datastore.appsync.ModelWithMetadata) Model(com.amplifyframework.core.model.Model) AppSync(com.amplifyframework.datastore.appsync.AppSync) Completable(io.reactivex.rxjava3.core.Completable) OutboxStatusEvent(com.amplifyframework.datastore.events.OutboxStatusEvent) AppSyncConflictUnhandledError(com.amplifyframework.datastore.appsync.AppSyncConflictUnhandledError) Logger(com.amplifyframework.logging.Logger) SchemaRegistry(com.amplifyframework.core.model.SchemaRegistry) Objects(java.util.Objects) TimeUnit(java.util.concurrent.TimeUnit) Schedulers(io.reactivex.rxjava3.schedulers.Schedulers) DataStoreException(com.amplifyframework.datastore.DataStoreException) Consumer(com.amplifyframework.core.Consumer) List(java.util.List) CompositeDisposable(io.reactivex.rxjava3.disposables.CompositeDisposable) ModelSchema(com.amplifyframework.core.model.ModelSchema) GraphQLResponse(com.amplifyframework.api.graphql.GraphQLResponse) HubEvent(com.amplifyframework.hub.HubEvent) ModelSchema(com.amplifyframework.core.model.ModelSchema)

Example 53 with ModelSchema

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());
}
Also used : ModelSchema(com.amplifyframework.core.model.ModelSchema) ModelWithMetadata(com.amplifyframework.datastore.appsync.ModelWithMetadata) BlogOwner(com.amplifyframework.testmodels.commentsblog.BlogOwner) HubAccumulator(com.amplifyframework.testutils.HubAccumulator) ModelMetadata(com.amplifyframework.datastore.appsync.ModelMetadata) Test(org.junit.Test)

Example 54 with ModelSchema

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));
}
Also used : ModelSchema(com.amplifyframework.core.model.ModelSchema) HashMap(java.util.HashMap) BlogOwner(com.amplifyframework.testmodels.commentsblog.BlogOwner) HubAccumulator(com.amplifyframework.testutils.HubAccumulator) SerializedModel(com.amplifyframework.core.model.SerializedModel) Blog(com.amplifyframework.testmodels.commentsblog.Blog) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 55 with ModelSchema

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());
}
Also used : ModelSchema(com.amplifyframework.core.model.ModelSchema) HashMap(java.util.HashMap) BlogOwner(com.amplifyframework.testmodels.commentsblog.BlogOwner) HubAccumulator(com.amplifyframework.testutils.HubAccumulator) SerializedModel(com.amplifyframework.core.model.SerializedModel) Blog(com.amplifyframework.testmodels.commentsblog.Blog) Ignore(org.junit.Ignore) 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