Search in sources :

Example 16 with ModelWithMetadata

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

the class SubscriptionProcessorTest method arrangeDataEmittingSubscription.

@SuppressWarnings("SameParameterValue")
private static <T extends Model> void arrangeDataEmittingSubscription(AppSync appSync, ModelSchema modelSchema, SubscriptionType subscriptionType, GraphQLResponse<ModelWithMetadata<T>> response) throws DataStoreException {
    Answer<Cancelable> answer = invocation -> {
        final int startConsumerIndex = 1;
        Consumer<String> onStart = invocation.getArgument(startConsumerIndex);
        onStart.accept(RandomString.string());
        final int dataConsumerIndex = 2;
        Consumer<GraphQLResponse<ModelWithMetadata<T>>> onData = invocation.getArgument(dataConsumerIndex);
        onData.accept(response);
        return new NoOpCancelable();
    };
    arrangeSubscription(appSync, answer, modelSchema, subscriptionType);
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) ModelWithMetadata(com.amplifyframework.datastore.appsync.ModelWithMetadata) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) ModelProvider(com.amplifyframework.core.model.ModelProvider) Pair(android.util.Pair) RunWith(org.junit.runner.RunWith) AppSync(com.amplifyframework.datastore.appsync.AppSync) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) SchemaRegistry(com.amplifyframework.core.model.SchemaRegistry) Answer(org.mockito.stubbing.Answer) Consumer(com.amplifyframework.core.Consumer) SubscriptionType(com.amplifyframework.api.graphql.SubscriptionType) Observable(io.reactivex.rxjava3.core.Observable) Map(java.util.Map) Mockito.doAnswer(org.mockito.Mockito.doAnswer) NoOpCancelable(com.amplifyframework.core.async.NoOpCancelable) ModelSchema(com.amplifyframework.core.model.ModelSchema) ModelMetadata(com.amplifyframework.datastore.appsync.ModelMetadata) GraphQLResponse(com.amplifyframework.api.graphql.GraphQLResponse) 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) Action(com.amplifyframework.core.Action) RobolectricTestRunner(org.robolectric.RobolectricTestRunner) TimeUnit(java.util.concurrent.TimeUnit) DataStoreException(com.amplifyframework.datastore.DataStoreException) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) Cancelable(com.amplifyframework.core.async.Cancelable) Assert.assertFalse(org.junit.Assert.assertFalse) RandomString(com.amplifyframework.testutils.random.RandomString) Temporal(com.amplifyframework.core.model.temporal.Temporal) AmplifyModelProvider(com.amplifyframework.testmodels.commentsblog.AmplifyModelProvider) Collections(java.util.Collections) Mockito.mock(org.mockito.Mockito.mock) ModelWithMetadata(com.amplifyframework.datastore.appsync.ModelWithMetadata) Consumer(com.amplifyframework.core.Consumer) NoOpCancelable(com.amplifyframework.core.async.NoOpCancelable) NoOpCancelable(com.amplifyframework.core.async.NoOpCancelable) Cancelable(com.amplifyframework.core.async.Cancelable)

Example 17 with ModelWithMetadata

use of com.amplifyframework.datastore.appsync.ModelWithMetadata 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 18 with ModelWithMetadata

use of com.amplifyframework.datastore.appsync.ModelWithMetadata 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 19 with ModelWithMetadata

use of com.amplifyframework.datastore.appsync.ModelWithMetadata 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 20 with ModelWithMetadata

use of com.amplifyframework.datastore.appsync.ModelWithMetadata 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)

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