Search in sources :

Example 16 with StorageItemChange

use of com.amplifyframework.datastore.storage.StorageItemChange in project amplify-android by aws-amplify.

the class MutationPersistenceInstrumentationTest method saveIsObservedForPersistentRecord.

/**
 * When {@link LocalStorageAdapter#save(Model, StorageItemChange.Initiator, QueryPredicate, Consumer, Consumer)}
 * is called to save a {@link PendingMutation.PersistentRecord}, we should see an event emitted on the
 * {@link LocalStorageAdapter#observe(Consumer, Consumer, Action)}'s item consumer.
 * @throws DataStoreException On failure to convert received value out of record format, or
 *                            on failure to manipulate data in/out of DataStore
 */
@Test
public void saveIsObservedForPersistentRecord() throws DataStoreException {
    // Start watching observe().
    // The storage adapter emits StorageItemChange.
    TestObserver<StorageItemChange<? extends Model>> saveObserver = storage.observe().test();
    // Save something ..
    BlogOwner juan = BlogOwner.builder().name("Juan Gonzales").build();
    ModelSchema schema = schemaRegistry.getModelSchemaForModelClass(BlogOwner.class);
    PendingMutation<BlogOwner> change = PendingMutation.creation(juan, schema);
    PendingMutation.PersistentRecord thingWeSaved = converter.toRecord(change);
    // Wait for it to save...
    storage.save(thingWeSaved);
    // Assert that our observer got the item;
    // The observed change makes reference to an item. That referred item is our
    // PendingMutation.PersistentRecord. It should be identical to the item that we saved.
    assertEquals(thingWeSaved, saveObserver.awaitCount(1).values().get(0).item());
    saveObserver.dispose();
}
Also used : ModelSchema(com.amplifyframework.core.model.ModelSchema) StorageItemChange(com.amplifyframework.datastore.storage.StorageItemChange) Model(com.amplifyframework.core.model.Model) BlogOwner(com.amplifyframework.testmodels.commentsblog.BlogOwner) Test(org.junit.Test)

Example 17 with StorageItemChange

use of com.amplifyframework.datastore.storage.StorageItemChange in project amplify-android by aws-amplify.

the class SQLiteStorageAdapterSaveTest method patchItemOnlyHasChangedFields.

/**
 * Verify that saving an item that already exists emits a StorageItemChange event with a patchItem that only
 * contains the fields that are different.
 *
 * @throws AmplifyException On failure to obtain ModelSchema from model class.
 * @throws InterruptedException If interrupted while awaiting terminal result in test observer
 */
@Test
public void patchItemOnlyHasChangedFields() throws AmplifyException, InterruptedException {
    // Create a BlogOwner.
    final BlogOwner johnSmith = BlogOwner.builder().name("John Smith").wea("ther").build();
    adapter.save(johnSmith);
    // Start observing for changes
    TestObserver<StorageItemChange<? extends Model>> observer = adapter.observe().test();
    // Update one field on the BlogOwner.
    BlogOwner johnAdams = johnSmith.copyOfBuilder().name("John Adams").build();
    adapter.save(johnAdams);
    // Observe that the StorageItemChange contains an item with only the fields that changed (`id`, and `name`, but
    // not `wea`)
    Map<String, Object> serializedData = new HashMap<>();
    serializedData.put("id", johnAdams.getId());
    serializedData.put("name", "John Adams");
    SerializedModel expectedItem = SerializedModel.builder().serializedData(serializedData).modelSchema(ModelSchema.fromModelClass(BlogOwner.class)).build();
    observer.await(1, TimeUnit.SECONDS);
    observer.assertValueCount(1);
    observer.assertValueAt(0, storageItemChange -> storageItemChange.patchItem().equals(expectedItem));
}
Also used : HashMap(java.util.HashMap) StorageItemChange(com.amplifyframework.datastore.storage.StorageItemChange) SerializedModel(com.amplifyframework.core.model.SerializedModel) Model(com.amplifyframework.core.model.Model) BlogOwner(com.amplifyframework.testmodels.commentsblog.BlogOwner) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) SerializedModel(com.amplifyframework.core.model.SerializedModel) Test(org.junit.Test)

Aggregations

StorageItemChange (com.amplifyframework.datastore.storage.StorageItemChange)17 Model (com.amplifyframework.core.model.Model)16 BlogOwner (com.amplifyframework.testmodels.commentsblog.BlogOwner)16 Test (org.junit.Test)16 ModelSchema (com.amplifyframework.core.model.ModelSchema)12 DataStoreException (com.amplifyframework.datastore.DataStoreException)12 Post (com.amplifyframework.testmodels.commentsblog.Post)11 List (java.util.List)11 Assert.assertEquals (org.junit.Assert.assertEquals)11 SerializedModel (com.amplifyframework.core.model.SerializedModel)10 QueryPredicates (com.amplifyframework.core.model.query.predicate.QueryPredicates)10 PostStatus (com.amplifyframework.testmodels.commentsblog.PostStatus)10 Collections (java.util.Collections)10 AmplifyException (com.amplifyframework.AmplifyException)9 SchemaRegistry (com.amplifyframework.core.model.SchemaRegistry)8 DataStoreConfiguration (com.amplifyframework.datastore.DataStoreConfiguration)8 ArrayList (java.util.ArrayList)8 Arrays (java.util.Arrays)8 UUID (java.util.UUID)8 TimeUnit (java.util.concurrent.TimeUnit)8