Search in sources :

Example 16 with ModelMetadata

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

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

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

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

Example 20 with ModelMetadata

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

the class MergerTest method itemWithoutVersionIsNotMerged.

/**
 * Gray-box, we know that "no version" evaluates to a version of 0.
 * So, this test should always behave like {@link #itemWithLowerVersionIsNotMerged()}.
 * But, it the inputs to the system are technically different, so it is
 * a distinct test in terms of system input/output.
 * @throws DataStoreException On failure to interact with storage during arrange/verification
 * @throws InterruptedException If interrupted while awaiting terminal result in test observer
 */
@Test
public void itemWithoutVersionIsNotMerged() throws DataStoreException, InterruptedException {
    // Arrange a model and metadata into storage.
    BlogOwner existingModel = BlogOwner.builder().name("Cornelius Daniels").build();
    ModelMetadata existingMetadata = new ModelMetadata(existingModel.getId(), false, 1, Temporal.Timestamp.now());
    storageAdapter.save(existingModel, existingMetadata);
    // Act: try to merge, but don't specify a version in the metadata being used to merge.
    BlogOwner incomingModel = existingModel.copyOfBuilder().name("Cornelius Daniels, but woke af, now.").build();
    ModelMetadata metadataWithoutVersion = new ModelMetadata(incomingModel.getId(), null, null, null);
    ModelWithMetadata<BlogOwner> incomingModelWithMetadata = new ModelWithMetadata<>(existingModel, metadataWithoutVersion);
    TestObserver<Void> mergeObserver = merger.merge(incomingModelWithMetadata).test();
    mergeObserver.await(REASONABLE_WAIT_TIME, TimeUnit.MILLISECONDS);
    mergeObserver.assertNoErrors().assertComplete();
    // Assert: Joey is still the same old Joey.
    assertEquals(Collections.singletonList(existingModel), storageAdapter.query(BlogOwner.class));
    // And his metadata is the still the same.
    assertEquals(Collections.singletonList(existingMetadata), storageAdapter.query(ModelMetadata.class, Where.id(existingModel.getId())));
}
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

ModelMetadata (com.amplifyframework.datastore.appsync.ModelMetadata)29 ModelWithMetadata (com.amplifyframework.datastore.appsync.ModelWithMetadata)23 Test (org.junit.Test)23 BlogOwner (com.amplifyframework.testmodels.commentsblog.BlogOwner)22 Temporal (com.amplifyframework.core.model.temporal.Temporal)7 GraphQLResponse (com.amplifyframework.api.graphql.GraphQLResponse)6 ModelSchema (com.amplifyframework.core.model.ModelSchema)6 HubAccumulator (com.amplifyframework.testutils.HubAccumulator)4 NonNull (androidx.annotation.NonNull)3 ApiCategory (com.amplifyframework.api.ApiCategory)3 SerializedModel (com.amplifyframework.core.model.SerializedModel)3 Person (com.amplifyframework.testmodels.personcar.Person)3 RandomString (com.amplifyframework.testutils.random.RandomString)3 SynchronousDataStore (com.amplifyframework.testutils.sync.SynchronousDataStore)3 HashMap (java.util.HashMap)3 CountDownLatch (java.util.concurrent.CountDownLatch)3 GraphQLLocation (com.amplifyframework.api.graphql.GraphQLLocation)2 GraphQLPathSegment (com.amplifyframework.api.graphql.GraphQLPathSegment)2 Amplify (com.amplifyframework.core.Amplify)2 Consumer (com.amplifyframework.core.Consumer)2