Search in sources :

Example 6 with StorageItemChange

use of com.amplifyframework.datastore.storage.StorageItemChange 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 7 with StorageItemChange

use of com.amplifyframework.datastore.storage.StorageItemChange 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 8 with StorageItemChange

use of com.amplifyframework.datastore.storage.StorageItemChange 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)

Example 9 with StorageItemChange

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

the class SyncProcessorTest method syncAndExpect.

private void syncAndExpect(int numPages, int maxSyncRecords) throws AmplifyException, InterruptedException {
    initSyncProcessor(maxSyncRecords);
    // Arrange a subscription to the storage adapter. We're going to watch for changes.
    // We expect to see content here as a result of the SyncProcessor applying updates.
    final TestObserver<StorageItemChange<? extends Model>> adapterObserver = storageAdapter.observe().test();
    // Arrange: return some responses for the sync() call on the RemoteModelState
    AppSyncMocking.SyncConfigurator configurator = AppSyncMocking.sync(appSync);
    List<ModelWithMetadata<BlogOwner>> expectedResponseItems = new ArrayList<>();
    String token = null;
    for (int pageIndex = 0; pageIndex < numPages; pageIndex++) {
        String nextToken = pageIndex < numPages - 1 ? RandomString.string() : null;
        ModelWithMetadata<BlogOwner> randomBlogOwner = randomBlogOwnerWithMetadata();
        configurator.mockSuccessResponse(BlogOwner.class, token, nextToken, randomBlogOwner);
        if (expectedResponseItems.size() < maxSyncRecords) {
            expectedResponseItems.add(randomBlogOwner);
        }
        token = nextToken;
    }
    // Act: Call hydrate, and await its completion - assert it completed without error
    TestObserver<ModelWithMetadata<? extends Model>> hydrationObserver = TestObserver.create();
    syncProcessor.hydrate().subscribe(hydrationObserver);
    // Wait 2 seconds, or 1 second per 100 pages, whichever is greater
    long timeoutMs = Math.max(OP_TIMEOUT_MS, TimeUnit.SECONDS.toMillis(numPages / 100));
    assertTrue(hydrationObserver.await(timeoutMs, TimeUnit.MILLISECONDS));
    hydrationObserver.assertNoErrors();
    hydrationObserver.assertComplete();
    // Since hydrate() completed, the storage adapter observer should see some values.
    // There should be a total of four changes on storage adapter
    // A model and a metadata save for each of the two BlogOwner-type items
    // Additionally, there should be 4 last sync time records, one for each of the
    // models managed by the system.
    adapterObserver.awaitCount(expectedResponseItems.size() * 2 + 4);
    // Validate the changes emitted from the storage adapter's observe().
    assertEquals(// Expect items as described above.
    Observable.fromIterable(expectedResponseItems).flatMap(modelWithMutation -> Observable.fromArray(modelWithMutation.getModel(), modelWithMutation.getSyncMetadata())).toSortedList(SortByModelId::compare).blockingGet(), // Actually...
    Observable.fromIterable(adapterObserver.values()).map(StorageItemChange::item).filter(item -> !LastSyncMetadata.class.isAssignableFrom(item.getClass())).toSortedList(SortByModelId::compare).blockingGet());
    // Lastly: validate the current contents of the storage adapter.
    // There should be 2 BlogOwners, and 2 MetaData records.
    List<? extends Model> itemsInStorage = storageAdapter.query(modelProvider);
    assertEquals(itemsInStorage.toString(), expectedResponseItems.size() * 2 + modelProvider.models().size(), itemsInStorage.size());
    assertEquals(// Expect the 4 items for the bloggers (2 models and their metadata)
    Observable.fromIterable(expectedResponseItems).flatMap(blogger -> Observable.fromArray(blogger.getModel(), blogger.getSyncMetadata())).toList().map(HashSet::new).blockingGet(), Observable.fromIterable(storageAdapter.query(modelProvider)).filter(item -> !LastSyncMetadata.class.isAssignableFrom(item.getClass())).toList().map(HashSet::new).blockingGet());
    adapterObserver.dispose();
    hydrationObserver.dispose();
}
Also used : Arrays(java.util.Arrays) AmplifyException(com.amplifyframework.AmplifyException) ModelWithMetadata(com.amplifyframework.datastore.appsync.ModelWithMetadata) ModelProvider(com.amplifyframework.core.model.ModelProvider) StorageItemChange(com.amplifyframework.datastore.storage.StorageItemChange) Range(android.util.Range) Random(java.util.Random) Timer(java.util.Timer) SyncQueriesStartedEvent(com.amplifyframework.datastore.events.SyncQueriesStartedEvent) SynchronousStorageAdapter(com.amplifyframework.datastore.storage.SynchronousStorageAdapter) Time(com.amplifyframework.util.Time) GraphQLResponse(com.amplifyframework.api.graphql.GraphQLResponse) DRUM_POST(com.amplifyframework.datastore.appsync.TestModelWithMetadataInstances.DRUM_POST) TimerTask(java.util.TimerTask) BLOGGER_JAMESON(com.amplifyframework.datastore.appsync.TestModelWithMetadataInstances.BLOGGER_JAMESON) HubEvent(com.amplifyframework.hub.HubEvent) HubChannel(com.amplifyframework.hub.HubChannel) UUID(java.util.UUID) RobolectricTestRunner(org.robolectric.RobolectricTestRunner) DataStoreException(com.amplifyframework.datastore.DataStoreException) List(java.util.List) Assert.assertFalse(org.junit.Assert.assertFalse) RandomString(com.amplifyframework.testutils.random.RandomString) ForEach(com.amplifyframework.util.ForEach) HubEventFilter(com.amplifyframework.hub.HubEventFilter) Mockito.any(org.mockito.Mockito.any) Mockito.mock(org.mockito.Mockito.mock) DataStoreConfigurationProvider(com.amplifyframework.datastore.DataStoreConfigurationProvider) Single(io.reactivex.rxjava3.core.Single) AppSyncMocking(com.amplifyframework.datastore.appsync.AppSyncMocking) GraphQLRequest(com.amplifyframework.api.graphql.GraphQLRequest) DataStoreChannelEventName(com.amplifyframework.datastore.DataStoreChannelEventName) RunWith(org.junit.runner.RunWith) AppSync(com.amplifyframework.datastore.appsync.AppSync) SystemModelsProviderFactory(com.amplifyframework.datastore.model.SystemModelsProviderFactory) BLOGGER_ISLA(com.amplifyframework.datastore.appsync.TestModelWithMetadataInstances.BLOGGER_ISLA) Mockito.spy(org.mockito.Mockito.spy) HubAccumulator(com.amplifyframework.testutils.HubAccumulator) ArrayList(java.util.ArrayList) SchemaRegistry(com.amplifyframework.core.model.SchemaRegistry) HashSet(java.util.HashSet) ArgumentCaptor(org.mockito.ArgumentCaptor) TestObserver(io.reactivex.rxjava3.observers.TestObserver) Observable(io.reactivex.rxjava3.core.Observable) ModelSchema(com.amplifyframework.core.model.ModelSchema) ModelMetadata(com.amplifyframework.datastore.appsync.ModelMetadata) PaginatedResult(com.amplifyframework.api.graphql.PaginatedResult) LinkedHashSet(java.util.LinkedHashSet) Before(org.junit.Before) DataStoreConfiguration(com.amplifyframework.datastore.DataStoreConfiguration) BlogOwner(com.amplifyframework.testmodels.commentsblog.BlogOwner) Model(com.amplifyframework.core.model.Model) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) Completable(io.reactivex.rxjava3.core.Completable) Mockito.times(org.mockito.Mockito.times) DELETED_DRUM_POST(com.amplifyframework.datastore.appsync.TestModelWithMetadataInstances.DELETED_DRUM_POST) Mockito.when(org.mockito.Mockito.when) ModelSyncedEvent(com.amplifyframework.datastore.events.ModelSyncedEvent) Mockito.verify(org.mockito.Mockito.verify) TimeUnit(java.util.concurrent.TimeUnit) InMemoryStorageAdapter(com.amplifyframework.datastore.storage.InMemoryStorageAdapter) Assert.assertNull(org.junit.Assert.assertNull) Temporal(com.amplifyframework.core.model.temporal.Temporal) AmplifyModelProvider(com.amplifyframework.testmodels.commentsblog.AmplifyModelProvider) Post(com.amplifyframework.testmodels.commentsblog.Post) SimpleModelProvider(com.amplifyframework.datastore.model.SimpleModelProvider) Assert.assertEquals(org.junit.Assert.assertEquals) ModelWithMetadata(com.amplifyframework.datastore.appsync.ModelWithMetadata) StorageItemChange(com.amplifyframework.datastore.storage.StorageItemChange) ArrayList(java.util.ArrayList) RandomString(com.amplifyframework.testutils.random.RandomString) Model(com.amplifyframework.core.model.Model) BlogOwner(com.amplifyframework.testmodels.commentsblog.BlogOwner) AppSyncMocking(com.amplifyframework.datastore.appsync.AppSyncMocking) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet)

Example 10 with StorageItemChange

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

the class SQLiteStorageAdapterDeleteTest method deleteModelTypeWithPredicateCascades.

/**
 * Assert that delete model type with predicate deletes items in
 * the SQLite database without violating foreign key constraints.
 * @throws DataStoreException On unexpected failure manipulating items in/out of DataStore
 */
@Test
public void deleteModelTypeWithPredicateCascades() throws DataStoreException {
    // Create 1 blog owner, which has 3 blogs each, which has 3 posts each.
    // Insert 1 blog owner, 3 blogs, 9 posts
    Set<String> expected = new HashSet<>();
    BlogOwner ownerModel = BlogOwner.builder().name("Blog Owner 1").build();
    adapter.save(ownerModel);
    for (int blog = 1; blog <= 3; blog++) {
        Blog blogModel = Blog.builder().name("Blog " + blog).owner(ownerModel).build();
        adapter.save(blogModel);
        expected.add(blogModel.getId());
        for (int post = 1; post <= 3; post++) {
            Post postModel = Post.builder().title("Post " + blog + "-" + post).status(PostStatus.INACTIVE).rating(5).blog(blogModel).build();
            adapter.save(postModel);
            expected.add(postModel.getId());
        }
    }
    // Observe deletions
    TestObserver<String> deleteObserver = adapter.observe().filter(change -> StorageItemChange.Type.DELETE.equals(change.type())).map(StorageItemChange::item).map(Model::getId).test();
    // Triggers a delete of all blogs.
    // All posts will be deleted by cascade.
    adapter.delete(Blog.class, QueryPredicates.all());
    // Assert 3 blogs and 9 posts are deleted.
    deleteObserver.assertValueCount(12);
    assertEquals(expected, new HashSet<>(deleteObserver.values()));
    // Get the BlogOwner from the database. Should not have been deleted.
    final List<BlogOwner> blogOwners = adapter.query(BlogOwner.class);
    assertEquals(Collections.singletonList(ownerModel), blogOwners);
    // Get the Blogs and Posts from the database. Should be deleted.
    assertTrue(adapter.query(Blog.class).isEmpty());
    assertTrue(adapter.query(Post.class).isEmpty());
}
Also used : BeforeClass(org.junit.BeforeClass) Blog(com.amplifyframework.testmodels.commentsblog.Blog) QueryPredicates(com.amplifyframework.core.model.query.predicate.QueryPredicates) StorageItemChange(com.amplifyframework.datastore.storage.StorageItemChange) BlogOwner(com.amplifyframework.testmodels.commentsblog.BlogOwner) Model(com.amplifyframework.core.model.Model) Set(java.util.Set) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) StrictMode(com.amplifyframework.datastore.StrictMode) SynchronousStorageAdapter(com.amplifyframework.datastore.storage.SynchronousStorageAdapter) HashSet(java.util.HashSet) DataStoreException(com.amplifyframework.datastore.DataStoreException) List(java.util.List) TestObserver(io.reactivex.rxjava3.observers.TestObserver) After(org.junit.After) QueryPredicate(com.amplifyframework.core.model.query.predicate.QueryPredicate) AmplifyModelProvider(com.amplifyframework.testmodels.commentsblog.AmplifyModelProvider) Post(com.amplifyframework.testmodels.commentsblog.Post) PostStatus(com.amplifyframework.testmodels.commentsblog.PostStatus) Collections(java.util.Collections) Assert.assertEquals(org.junit.Assert.assertEquals) Before(org.junit.Before) Post(com.amplifyframework.testmodels.commentsblog.Post) StorageItemChange(com.amplifyframework.datastore.storage.StorageItemChange) BlogOwner(com.amplifyframework.testmodels.commentsblog.BlogOwner) Blog(com.amplifyframework.testmodels.commentsblog.Blog) HashSet(java.util.HashSet) 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