Search in sources :

Example 81 with BlogOwner

use of com.amplifyframework.testmodels.commentsblog.BlogOwner in project amplify-android by aws-amplify.

the class SQLiteStorageAdapterSaveTest method patchItemOnlyHasAllFields.

/**
 * 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 patchItemOnlyHasAllFields() 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");
    serializedData.put("wea", johnAdams.getWea());
    serializedData.put("createdAt", johnAdams.getCreatedAt());
    serializedData.put("updatedAt", johnAdams.getUpdatedAt());
    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)

Example 82 with BlogOwner

use of com.amplifyframework.testmodels.commentsblog.BlogOwner in project amplify-android by aws-amplify.

the class SQLiteStorageAdapterSaveTest method saveModelWithPredicateUpdatesConditionally.

/**
 * Test save with predicate. Conditional write is useful for making sure that
 * no data is overwritten with outdated assumptions.
 * @throws DataStoreException On unexpected failure manipulating items in/out of DataStore
 */
@Test
public void saveModelWithPredicateUpdatesConditionally() throws DataStoreException {
    final BlogOwner john = BlogOwner.builder().name("John").build();
    final BlogOwner jane = BlogOwner.builder().name("Jane").build();
    final BlogOwner mark = BlogOwner.builder().name("Mark").build();
    adapter.save(john);
    adapter.save(jane);
    adapter.save(mark);
    // Only update John and Jane
    final QueryPredicate predicate = BlogOwner.NAME.beginsWith("J");
    final BlogOwner newJohn = john.copyOfBuilder().name("John Doe").build();
    final BlogOwner newJane = jane.copyOfBuilder().name("Jane Doe").build();
    final BlogOwner newMark = mark.copyOfBuilder().name("Mark Doe").build();
    adapter.save(newJohn, predicate);
    adapter.save(newJane, predicate);
    // noinspection ThrowableNotThrown
    // Should not update
    adapter.saveExpectingError(newMark, predicate);
    assertEquals(Observable.fromArray(newJohn, newJane, mark).toList().map(HashSet::new).blockingGet(), Observable.fromIterable(adapter.query(BlogOwner.class)).toList().map(HashSet::new).blockingGet());
}
Also used : QueryPredicate(com.amplifyframework.core.model.query.predicate.QueryPredicate) BlogOwner(com.amplifyframework.testmodels.commentsblog.BlogOwner) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 83 with BlogOwner

use of com.amplifyframework.testmodels.commentsblog.BlogOwner in project amplify-android by aws-amplify.

the class MutationPersistenceInstrumentationTest method deletionIsObservedForPersistentRecord.

/**
 * When an {@link PendingMutation.PersistentRecord} is deleted from the DataStore, we will expect
 * to see an event on item consumer of {@link LocalStorageAdapter#observe(Consumer, Consumer, Action)}.
 * @throws DataStoreException from storage item change, or on failure to manipulate I/O to DataStore
 */
@Test
public void deletionIsObservedForPersistentRecord() throws DataStoreException {
    // We are observing a stream of changes to models.
    // In this test, the <? extends Model> type happens to be PersistentRecord (itself, implementing Model.)
    TestObserver<StorageItemChange<? extends Model>> storageObserver = storage.observe().test();
    BlogOwner beatrice = BlogOwner.builder().name("Beatrice Stone").build();
    ModelSchema schema = schemaRegistry.getModelSchemaForModelClass(BlogOwner.class);
    PendingMutation<BlogOwner> createBeatrice = PendingMutation.creation(beatrice, schema);
    PendingMutation.PersistentRecord createBeatriceRecord = converter.toRecord(createBeatrice);
    storage.save(createBeatriceRecord);
    // Assert that we do observe the PersistentRecord being saved ...
    assertEquals(createBeatriceRecord, storageObserver.awaitCount(1).values().get(0).item());
    storageObserver.dispose();
    TestObserver<StorageItemChange<? extends Model>> deletionObserver = storage.observe().test();
    // Try to delete Beatrice's record.
    storage.delete(createBeatriceRecord);
    // Should receive a notification of the deletion on the observer.
    // The notification refers to the deleted item, in its contents.
    assertEquals(createBeatriceRecord, deletionObserver.awaitCount(1).values().get(0).item());
    deletionObserver.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 84 with BlogOwner

use of com.amplifyframework.testmodels.commentsblog.BlogOwner in project amplify-android by aws-amplify.

the class MutationPersistenceInstrumentationTest method adapterCanSaveAndQueryPersistentRecords.

/**
 * The adapter must be able to save a {@link PendingMutation.PersistentRecord}.
 * When we query the adapter for that same {@link PendingMutation.PersistentRecord},
 * we will expect to find an exact replica of the one we had saved.
 * @throws DataStoreException from storage item change, or on failure to manipulate I/O to DataStore
 */
@Test
public void adapterCanSaveAndQueryPersistentRecords() throws DataStoreException {
    final BlogOwner tonyDaniels = BlogOwner.builder().name("Tony Daniels").build();
    ModelSchema schema = schemaRegistry.getModelSchemaForModelClass(BlogOwner.class);
    PendingMutation<BlogOwner> originalTonyCreation = PendingMutation.creation(tonyDaniels, schema);
    // Save the creation mutation for Tony, as a PersistentRecord object.
    PendingMutation.PersistentRecord originalTonyCreationAsRecord = converter.toRecord(originalTonyCreation);
    storage.save(originalTonyCreationAsRecord);
    // Now, lookup what PersistentRecord we have in the storage.
    List<PendingMutation.PersistentRecord> recordsInStorage = storage.query(PendingMutation.PersistentRecord.class);
    // There should be 1, and it should be the original creation for Tony.
    assertEquals(1, recordsInStorage.size());
    PendingMutation.PersistentRecord firstRecordFoundInStorage = recordsInStorage.get(0);
    assertEquals(originalTonyCreationAsRecord, firstRecordFoundInStorage);
    // After we convert back from PersistentRecord, we should get back a copy of
    // what we created above
    PendingMutation<BlogOwner> reconstructedCreationOfTony = converter.fromRecord(firstRecordFoundInStorage);
    assertEquals(originalTonyCreation, reconstructedCreationOfTony);
}
Also used : ModelSchema(com.amplifyframework.core.model.ModelSchema) BlogOwner(com.amplifyframework.testmodels.commentsblog.BlogOwner) Test(org.junit.Test)

Example 85 with BlogOwner

use of com.amplifyframework.testmodels.commentsblog.BlogOwner in project amplify-android by aws-amplify.

the class MutationPersistenceInstrumentationTest method updatesAreObservedForPersistentRecords.

/**
 * When {@link LocalStorageAdapter#save(Model, StorageItemChange.Initiator, QueryPredicate, Consumer, Consumer)}
 * is called to save a {@link PendingMutation.PersistentRecord}, we should expect to observe a change event
 * /containing/ that record within it. It will be received by the value consumer of
 * {@link LocalStorageAdapter#observe(Consumer, Consumer, Action)}.
 *
 * Similarly, when we update the {@link PendingMutation.PersistentRecord} that we had just saved,
 * we should see an update notification on the subscription consumer. The type will be
 * StorageItemChange, and inside of it ill be a reference to the {@link PendingMutation.PersistentRecord}.
 *
 * @throws DataStoreException from storage item change, or on failure to manipulate I/O to DataStore
 */
@Test
public void updatesAreObservedForPersistentRecords() throws DataStoreException {
    // Establish a subscription to listen for storage change records
    TestObserver<StorageItemChange<? extends Model>> storageObserver = storage.observe().test();
    // Create a record for Joe, and a change to save him into storage
    BlogOwner joeLastNameMisspelled = BlogOwner.builder().name("Joe Sweeneyy").build();
    ModelSchema schema = schemaRegistry.getModelSchemaForModelClass(BlogOwner.class);
    PendingMutation<BlogOwner> createJoeWrongLastName = PendingMutation.creation(joeLastNameMisspelled, schema);
    // Save our saveJoeWrongLastName change item, as a PersistentRecord.
    PendingMutation.PersistentRecord createJoeWrongLastNameAsRecord = converter.toRecord(createJoeWrongLastName);
    storage.save(createJoeWrongLastNameAsRecord);
    // Now, suppose we have to update that pending mutation. Maybe it contained a bad item payload.
    BlogOwner joeWithLastNameFix = BlogOwner.builder().name("Joe Sweeney").id(joeLastNameMisspelled.getId()).build();
    PendingMutation<BlogOwner> createJoeCorrectLastName = PendingMutation.creation(joeWithLastNameFix, schema);
    // Save an update (same model type, same unique ID) to the mutation we saved previously.
    PendingMutation.PersistentRecord createJoeCorrectLastNameAsRecord = converter.toRecord(createJoeCorrectLastName);
    storage.save(createJoeCorrectLastNameAsRecord);
    // Our observer got the records to save Joe with wrong age, and also to save joe with right age
    assertEquals(Arrays.asList(createJoeWrongLastNameAsRecord, createJoeCorrectLastNameAsRecord), Observable.fromIterable(storageObserver.awaitCount(2).values()).map(StorageItemChange::item).toList().blockingGet());
    storageObserver.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)

Aggregations

BlogOwner (com.amplifyframework.testmodels.commentsblog.BlogOwner)150 Test (org.junit.Test)146 DataStoreException (com.amplifyframework.datastore.DataStoreException)35 Blog (com.amplifyframework.testmodels.commentsblog.Blog)32 ModelSchema (com.amplifyframework.core.model.ModelSchema)31 Post (com.amplifyframework.testmodels.commentsblog.Post)31 ArrayList (java.util.ArrayList)29 QueryPredicate (com.amplifyframework.core.model.query.predicate.QueryPredicate)25 Assert.assertEquals (org.junit.Assert.assertEquals)25 ModelMetadata (com.amplifyframework.datastore.appsync.ModelMetadata)24 Collections (java.util.Collections)24 HashSet (java.util.HashSet)23 List (java.util.List)23 PostStatus (com.amplifyframework.testmodels.commentsblog.PostStatus)22 HubAccumulator (com.amplifyframework.testutils.HubAccumulator)22 Arrays (java.util.Arrays)22 TimeUnit (java.util.concurrent.TimeUnit)22 Consumer (com.amplifyframework.core.Consumer)21 ModelWithMetadata (com.amplifyframework.datastore.appsync.ModelWithMetadata)21 Before (org.junit.Before)21