Search in sources :

Example 96 with BlogOwner

use of com.amplifyframework.testmodels.commentsblog.BlogOwner 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 97 with BlogOwner

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

the class ConflictResolverTest method conflictIsResolvedByRetryingLocalData.

/**
 * When the user elects to retry the mutation using the local copy of the data,
 * the following is expected:
 * 1. The AppSync API is invoked, with the local mutation data
 * 2. We assume that the AppSync API will respond differently
 *    upon retry (TODO: why? Will the user be expected to manually
 *    intervene and modify the backend state somehow?)
 * @throws DataStoreException On failure to arrange metadata into storage
 */
@Test
public void conflictIsResolvedByRetryingLocalData() throws DataStoreException {
    // Arrange for the user-provided conflict handler to always request local retry.
    when(configurationProvider.getConfiguration()).thenReturn(DataStoreConfiguration.builder().conflictHandler(DataStoreConflictHandler.alwaysRetryLocal()).build());
    // Arrange a pending mutation that includes the local data
    BlogOwner localModel = BlogOwner.builder().name("Local Blogger").build();
    PendingMutation<BlogOwner> mutation = PendingMutation.update(localModel, schema);
    // Arrange server state for the model, in conflict to local data
    BlogOwner serverModel = localModel.copyOfBuilder().name("Server Blogger").build();
    Temporal.Timestamp now = Temporal.Timestamp.now();
    ModelMetadata metadata = new ModelMetadata(serverModel.getId(), false, 4, now);
    ModelWithMetadata<BlogOwner> serverData = new ModelWithMetadata<>(serverModel, metadata);
    // Arrange a hypothetical conflict error from AppSync
    AppSyncConflictUnhandledError<BlogOwner> unhandledConflictError = AppSyncConflictUnhandledErrorFactory.createUnhandledConflictError(serverData);
    // Assume that the AppSync call succeeds this time.
    ModelWithMetadata<BlogOwner> versionFromAppSyncResponse = new ModelWithMetadata<>(localModel, metadata);
    AppSyncMocking.update(appSync).mockSuccessResponse(localModel, metadata.getVersion(), versionFromAppSyncResponse);
    // Act: when the resolver is invoked, we expect the resolved version
    // to include the server's metadata, but with the local data.
    resolver.resolve(mutation, unhandledConflictError).test().awaitDone(TIMEOUT_SECONDS, TimeUnit.SECONDS).assertValue(versionFromAppSyncResponse);
    // The handler should have called up to AppSync to update hte model
    verify(appSync).update(eq(localModel), any(), eq(metadata.getVersion()), any(), any());
}
Also used : ModelWithMetadata(com.amplifyframework.datastore.appsync.ModelWithMetadata) Temporal(com.amplifyframework.core.model.temporal.Temporal) BlogOwner(com.amplifyframework.testmodels.commentsblog.BlogOwner) ModelMetadata(com.amplifyframework.datastore.appsync.ModelMetadata) Test(org.junit.Test)

Example 98 with BlogOwner

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

the class ConflictResolverTest method conflictIsResolvedByRetryingLocalDataWithSerializedModel.

/**
 * When the user elects to retry the mutation using the local copy of the data,
 * the following is expected:
 * 1. The AppSync API is invoked, with the local mutation data
 * 2. We assume that the AppSync API will respond differently
 *    upon retry
 * @throws AmplifyException On failure to arrange metadata into storage
 */
@Test
public void conflictIsResolvedByRetryingLocalDataWithSerializedModel() throws AmplifyException {
    // Arrange for the user-provided conflict handler to always request local retry.
    when(configurationProvider.getConfiguration()).thenReturn(DataStoreConfiguration.builder().conflictHandler(DataStoreConflictHandler.alwaysRetryLocal()).build());
    // Arrange a pending mutation that includes the local data
    BlogOwner localModel = BlogOwner.builder().name("Local Blogger").build();
    Map<String, Object> ownerData = new HashMap<>();
    ownerData.put("id", localModel.getId());
    ownerData.put("name", localModel.getName());
    SerializedModel serializedOwner = SerializedModel.builder().serializedData(ownerData).modelSchema(ModelSchema.fromModelClass(BlogOwner.class)).build();
    PendingMutation<SerializedModel> mutation = PendingMutation.update(serializedOwner, schema);
    // Arrange server state for the model, in conflict to local data
    BlogOwner serverModel = localModel.copyOfBuilder().name("Server Blogger").build();
    Temporal.Timestamp now = Temporal.Timestamp.now();
    ModelMetadata metadata = new ModelMetadata(serverModel.getId(), false, 4, now);
    ModelWithMetadata<SerializedModel> serverData = new ModelWithMetadata<>(serializedOwner, metadata);
    // Arrange a hypothetical conflict error from AppSync
    AppSyncConflictUnhandledError<SerializedModel> unhandledConflictError = AppSyncConflictUnhandledErrorFactory.createUnhandledConflictError(serverData);
    // Assume that the AppSync call succeeds this time.
    ModelWithMetadata<BlogOwner> versionFromAppSyncResponse = new ModelWithMetadata<>(localModel, metadata);
    AppSyncMocking.update(appSync).mockSuccessResponse(localModel, metadata.getVersion(), versionFromAppSyncResponse);
    // Act: when the resolver is invoked, we expect the resolved version
    // to include the server's metadata, but with the local data.
    resolver.resolve(mutation, unhandledConflictError).test();
    // The handler should have called AppSync to update the model
    verify(appSync).update(eq(localModel), any(), eq(metadata.getVersion()), any(), any());
}
Also used : ModelWithMetadata(com.amplifyframework.datastore.appsync.ModelWithMetadata) HashMap(java.util.HashMap) SerializedModel(com.amplifyframework.core.model.SerializedModel) Temporal(com.amplifyframework.core.model.temporal.Temporal) BlogOwner(com.amplifyframework.testmodels.commentsblog.BlogOwner) ModelMetadata(com.amplifyframework.datastore.appsync.ModelMetadata) Test(org.junit.Test)

Example 99 with BlogOwner

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

the class ConflictResolverTest method conflictIsResolvedByRetryingWithCustomModel.

/**
 * When the user elects to retry with a custom model, and that model
 * is not null, it means to try an update mutation against AppSync.
 * We expect:
 * 1. The AppSync API is invoked with an update mutation request.
 * @throws DataStoreException upon failure to arrange metadata into storage
 */
@Test
public void conflictIsResolvedByRetryingWithCustomModel() throws DataStoreException {
    // Arrange local model
    BlogOwner localModel = BlogOwner.builder().name("Local model").build();
    PendingMutation<BlogOwner> mutation = PendingMutation.update(localModel, schema);
    // Arrange a server model
    BlogOwner remoteModel = localModel.copyOfBuilder().name("Remote model").build();
    Temporal.Timestamp now = Temporal.Timestamp.now();
    ModelMetadata remoteMetadata = new ModelMetadata(remoteModel.getId(), false, 4, now);
    ModelWithMetadata<BlogOwner> remoteData = new ModelWithMetadata<>(remoteModel, remoteMetadata);
    // Arrange an unhandled conflict error based on the server data
    AppSyncConflictUnhandledError<BlogOwner> unhandledConflictError = AppSyncConflictUnhandledErrorFactory.createUnhandledConflictError(remoteData);
    // Arrange a conflict handler that returns a custom model
    BlogOwner customModel = localModel.copyOfBuilder().name("Custom model").build();
    when(configurationProvider.getConfiguration()).thenReturn(DataStoreConfiguration.builder().conflictHandler(RetryWithModelHandler.create(customModel)).build());
    // When the AppSync update API is called, return a mock response
    ModelMetadata metadata = new ModelMetadata(customModel.getId(), false, remoteMetadata.getVersion(), now);
    ModelWithMetadata<BlogOwner> responseData = new ModelWithMetadata<>(customModel, metadata);
    AppSyncMocking.update(appSync).mockSuccessResponse(customModel, remoteMetadata.getVersion(), responseData);
    // When the resolver is called, the AppSync update API should be called,
    // and the resolver should return its response data.
    resolver.resolve(mutation, unhandledConflictError).test().awaitDone(TIMEOUT_SECONDS, TimeUnit.SECONDS).assertValue(responseData);
    // Verify that the update API was invoked
    verify(appSync).update(eq(customModel), any(), eq(remoteMetadata.getVersion()), any(), any());
}
Also used : ModelWithMetadata(com.amplifyframework.datastore.appsync.ModelWithMetadata) Temporal(com.amplifyframework.core.model.temporal.Temporal) BlogOwner(com.amplifyframework.testmodels.commentsblog.BlogOwner) ModelMetadata(com.amplifyframework.datastore.appsync.ModelMetadata) Test(org.junit.Test)

Example 100 with BlogOwner

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

the class MergerTest method mergeDeletionForNotExistingItem.

/**
 * Assume there is NOT an item in the store. Then, we try to
 * merge a mutation to delete item A. This should succeed, since
 * there was no work to be performed (it was already deleted.) After
 * the merge, there should STILL be no matching item in the store.
 * @throws DataStoreException On failure to query results for assertions
 * @throws InterruptedException If interrupted while awaiting terminal result in test observer
 */
@Test
public void mergeDeletionForNotExistingItem() throws DataStoreException, InterruptedException {
    // Arrange, to start, there are no items matching the incoming deletion request.
    BlogOwner blogOwner = BlogOwner.builder().name("Jameson").build();
    // Note that storageAdapter.save(...) does NOT happen!
    // storageAdapter.save(blogOwner, new ModelMetadata(blogOwner.getId(), false, 1, Time.now()));
    // Act: try to merge a deletion that refers to an item not in the store
    ModelMetadata deletionMetadata = new ModelMetadata(blogOwner.getId(), true, 1, 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: there is still nothing 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)

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