Search in sources :

Example 6 with ModelWithMetadata

use of com.amplifyframework.datastore.appsync.ModelWithMetadata in project amplify-android by aws-amplify.

the class MergerTest method mergeDeletionForExistingItem.

/**
 * Assume there is a item A in the store. Then, we try to merge
 * a mutation to delete item A. This should succeed. After the
 * merge, A should NOT be in the store anymore.
 * @throws DataStoreException On failure to arrange test data into store,
 *                            or on failure to query results for test assertions
 * @throws InterruptedException If interrupted while awaiting terminal result in test observer
 */
@Test
public void mergeDeletionForExistingItem() throws DataStoreException, InterruptedException {
    // Arrange: A blog owner, and some metadata about it, are in the store.
    BlogOwner blogOwner = BlogOwner.builder().name("Jameson").build();
    ModelMetadata originalMetadata = new ModelMetadata(blogOwner.getId(), false, 1, Temporal.Timestamp.now());
    storageAdapter.save(blogOwner, originalMetadata);
    // Just to be sure, our arrangement worked, and that thing is in there, right? Good.
    assertEquals(Collections.singletonList(blogOwner), storageAdapter.query(BlogOwner.class));
    // Act: merge a model deletion.
    ModelMetadata deletionMetadata = new ModelMetadata(blogOwner.getId(), true, 2, Temporal.Timestamp.now());
    TestObserver<Void> observer = merger.merge(new ModelWithMetadata<>(blogOwner, deletionMetadata)).test();
    assertTrue(observer.await(REASONABLE_WAIT_TIME, TimeUnit.MILLISECONDS));
    observer.assertNoErrors().assertComplete();
    // Assert: the blog owner is no longer in the store.
    assertEquals(0, storageAdapter.query(BlogOwner.class).size());
}
Also used : ModelWithMetadata(com.amplifyframework.datastore.appsync.ModelWithMetadata) BlogOwner(com.amplifyframework.testmodels.commentsblog.BlogOwner) ModelMetadata(com.amplifyframework.datastore.appsync.ModelMetadata) Test(org.junit.Test)

Example 7 with ModelWithMetadata

use of com.amplifyframework.datastore.appsync.ModelWithMetadata in project amplify-android by aws-amplify.

the class MergerTest method itemWithSameVersionIsNotMerged.

/**
 * If the incoming change has the SAME version as the data currently in the DB, we refuse to update it.
 * The user may have updated the data locally via the DataStore API. So we would clobber it.
 * The version must be strictly HIGHER than the current version, in order for the merge to succeed.
 * @throws DataStoreException On failure to interact with storage during arrange/verify
 * @throws InterruptedException If interrupted while awaiting terminal result in test observer
 */
@Test
public void itemWithSameVersionIsNotMerged() throws DataStoreException, InterruptedException {
    // Arrange a model and metadata into storage.
    BlogOwner existingModel = BlogOwner.builder().name("Cornelius Daniels").build();
    ModelMetadata existingMetadata = new ModelMetadata(existingModel.getModelName() + "|" + existingModel.getId(), false, 55, Temporal.Timestamp.now());
    storageAdapter.save(existingModel, existingMetadata);
    // Act: try to merge, but specify a LOWER version.
    BlogOwner incomingModel = existingModel.copyOfBuilder().name("Cornelius Daniels, but woke af, now.").build();
    ModelMetadata lowerVersionMetadata = new ModelMetadata(incomingModel.getId(), false, 33, Temporal.Timestamp.now());
    ModelWithMetadata<BlogOwner> modelWithLowerVersionMetadata = new ModelWithMetadata<>(incomingModel, lowerVersionMetadata);
    TestObserver<Void> mergeObserver = merger.merge(modelWithLowerVersionMetadata).test();
    mergeObserver.await(REASONABLE_WAIT_TIME, TimeUnit.MILLISECONDS);
    mergeObserver.assertNoErrors().assertComplete();
    // Assert: Joey is still the same old Joey.
    List<BlogOwner> actualBlogOwners = storageAdapter.query(BlogOwner.class);
    assertEquals(1, actualBlogOwners.size());
    assertEquals(existingModel, actualBlogOwners.get(0));
    // And his metadata is the still the same.
    assertEquals(Collections.singletonList(existingMetadata), storageAdapter.query(ModelMetadata.class, Where.id(existingModel.getModelName() + "|" + existingModel.getId())));
}
Also used : ModelWithMetadata(com.amplifyframework.datastore.appsync.ModelWithMetadata) BlogOwner(com.amplifyframework.testmodels.commentsblog.BlogOwner) ModelMetadata(com.amplifyframework.datastore.appsync.ModelMetadata) Test(org.junit.Test)

Example 8 with ModelWithMetadata

use of com.amplifyframework.datastore.appsync.ModelWithMetadata in project amplify-android by aws-amplify.

the class MergerTest method mergeSaveForExistingItem.

/**
 * Assume there is an item A in the store. We try to merge a save for A.
 * This should succeed, and it should be treated as an update. After the merge,
 * A should have the updates from the merge.
 * @throws DataStoreException On failure to arrange data into store
 * @throws InterruptedException If interrupted while awaiting terminal result in test observer
 */
@Test
public void mergeSaveForExistingItem() throws DataStoreException, InterruptedException {
    // Arrange: an item is already in the store.
    BlogOwner originalModel = BlogOwner.builder().name("Jameson The Original").build();
    ModelMetadata originalMetadata = new ModelMetadata(originalModel.getModelName() + "|" + originalModel.getId(), false, 1, Temporal.Timestamp.now());
    storageAdapter.save(originalModel, originalMetadata);
    // Act: merge a save.
    BlogOwner updatedModel = originalModel.copyOfBuilder().name("Jameson The New and Improved").build();
    ModelMetadata updatedMetadata = new ModelMetadata(originalMetadata.getId(), false, 2, Temporal.Timestamp.now());
    TestObserver<Void> observer = merger.merge(new ModelWithMetadata<>(updatedModel, updatedMetadata)).test();
    assertTrue(observer.await(REASONABLE_WAIT_TIME, TimeUnit.MILLISECONDS));
    observer.assertComplete().assertNoErrors();
    // Assert: the *UPDATED* stuff is in the store, *only*.
    assertEquals(Collections.singletonList(updatedModel), storageAdapter.query(BlogOwner.class));
    assertEquals(Collections.singletonList(updatedMetadata), storageAdapter.query(ModelMetadata.class));
}
Also used : ModelWithMetadata(com.amplifyframework.datastore.appsync.ModelWithMetadata) BlogOwner(com.amplifyframework.testmodels.commentsblog.BlogOwner) ModelMetadata(com.amplifyframework.datastore.appsync.ModelMetadata) Test(org.junit.Test)

Example 9 with ModelWithMetadata

use of com.amplifyframework.datastore.appsync.ModelWithMetadata in project amplify-android by aws-amplify.

the class MergerTest method mergeSaveForNotExistingItem.

/**
 * Assume there is NO item A. Then, we try to merge a save for a
 * item A. This should succeed, with A being in the store, at the end.
 * @throws DataStoreException On failure to query results for assertions
 * @throws InterruptedException If interrupted while awaiting terminal result in test observer
 */
@Test
public void mergeSaveForNotExistingItem() throws DataStoreException, InterruptedException {
    // Arrange: nothing in the store, to start.
    BlogOwner blogOwner = BlogOwner.builder().name("Jameson").build();
    ModelMetadata metadata = new ModelMetadata(blogOwner.getModelName() + "|" + blogOwner.getId(), false, 1, Temporal.Timestamp.now());
    // Note that storageAdapter.save(...) is NOT called!
    // storageAdapter.save(blogOwner, metadata);
    // Act: merge a creation for an item
    TestObserver<Void> observer = merger.merge(new ModelWithMetadata<>(blogOwner, metadata)).test();
    assertTrue(observer.await(REASONABLE_WAIT_TIME, TimeUnit.MILLISECONDS));
    observer.assertNoErrors().assertComplete();
    // Assert: the item & its associated metadata are now in the store.
    assertEquals(Collections.singletonList(blogOwner), storageAdapter.query(BlogOwner.class));
    assertEquals(Collections.singletonList(metadata), storageAdapter.query(ModelMetadata.class));
}
Also used : ModelWithMetadata(com.amplifyframework.datastore.appsync.ModelWithMetadata) BlogOwner(com.amplifyframework.testmodels.commentsblog.BlogOwner) ModelMetadata(com.amplifyframework.datastore.appsync.ModelMetadata) Test(org.junit.Test)

Example 10 with ModelWithMetadata

use of com.amplifyframework.datastore.appsync.ModelWithMetadata in project amplify-android by aws-amplify.

the class MergerTest method itemWithLowerVersionIsNotMerged.

/**
 * An incoming mutation whose model has a LOWER version than an already existing model
 * shall be rejected from the merger.
 * @throws DataStoreException On failure interacting with local store during test arrange/verify.
 * @throws InterruptedException If interrupted while awaiting terminal result in test observer
 */
@Test
public void itemWithLowerVersionIsNotMerged() throws DataStoreException, InterruptedException {
    // Arrange a model and metadata into storage.
    BlogOwner existingModel = BlogOwner.builder().name("Cornelius Daniels").build();
    ModelMetadata existingMetadata = new ModelMetadata(existingModel.getId(), false, 55, Temporal.Timestamp.now());
    storageAdapter.save(existingModel, existingMetadata);
    // Act: try to merge, but specify a LOWER version.
    BlogOwner incomingModel = existingModel.copyOfBuilder().name("Cornelius Daniels, but woke af, now.").build();
    ModelMetadata lowerVersionMetadata = new ModelMetadata(incomingModel.getId(), false, 33, Temporal.Timestamp.now());
    ModelWithMetadata<BlogOwner> modelWithLowerVersionMetadata = new ModelWithMetadata<>(existingModel, lowerVersionMetadata);
    TestObserver<Void> mergeObserver = merger.merge(modelWithLowerVersionMetadata).test();
    mergeObserver.await(REASONABLE_WAIT_TIME, TimeUnit.MILLISECONDS);
    mergeObserver.assertNoErrors().assertComplete();
    // Assert: Joey is still the same old Joey.
    assertEquals(Collections.singletonList(existingModel), storageAdapter.query(BlogOwner.class));
    // And his metadata is the still the same.
    assertEquals(Collections.singletonList(existingMetadata), storageAdapter.query(ModelMetadata.class, Where.id(existingModel.getId())));
}
Also used : ModelWithMetadata(com.amplifyframework.datastore.appsync.ModelWithMetadata) BlogOwner(com.amplifyframework.testmodels.commentsblog.BlogOwner) ModelMetadata(com.amplifyframework.datastore.appsync.ModelMetadata) Test(org.junit.Test)

Aggregations

ModelWithMetadata (com.amplifyframework.datastore.appsync.ModelWithMetadata)35 ModelMetadata (com.amplifyframework.datastore.appsync.ModelMetadata)25 Test (org.junit.Test)22 BlogOwner (com.amplifyframework.testmodels.commentsblog.BlogOwner)21 GraphQLResponse (com.amplifyframework.api.graphql.GraphQLResponse)13 ModelSchema (com.amplifyframework.core.model.ModelSchema)10 Temporal (com.amplifyframework.core.model.temporal.Temporal)10 Consumer (com.amplifyframework.core.Consumer)9 Model (com.amplifyframework.core.model.Model)9 SerializedModel (com.amplifyframework.core.model.SerializedModel)9 SchemaRegistry (com.amplifyframework.core.model.SchemaRegistry)8 HubEvent (com.amplifyframework.hub.HubEvent)8 Completable (io.reactivex.rxjava3.core.Completable)8 DataStoreException (com.amplifyframework.datastore.DataStoreException)7 AppSync (com.amplifyframework.datastore.appsync.AppSync)7 HubChannel (com.amplifyframework.hub.HubChannel)7 List (java.util.List)7 NonNull (androidx.annotation.NonNull)6 Amplify (com.amplifyframework.core.Amplify)6 ModelProvider (com.amplifyframework.core.model.ModelProvider)6