use of com.amplifyframework.core.model.SerializedModel in project amplify-android by aws-amplify.
the class PersistentMutationOutboxTest method existingSerializedModelUpdateIncomingUpdateWithoutConditionMergesWithExistingMutation.
/**
* When there is an existing SerializedModel update mutation, and a new SerializedModel update mutation comes in,
* then we need to merge any existing mutations for that modelId and create the new one of type Update.
* @throws AmplifyException On failure to find the serializedModel difference.
* @throws InterruptedException If interrupted while awaiting terminal result in test observer
*/
@Test
public void existingSerializedModelUpdateIncomingUpdateWithoutConditionMergesWithExistingMutation() throws AmplifyException, InterruptedException {
// Arrange an existing update mutation
BlogOwner modelInSqlLite = BlogOwner.builder().name("Papa Tony").wea("Something").build();
BlogOwner initialUpdate = BlogOwner.builder().name("Tony Jr").id(modelInSqlLite.getId()).build();
PendingMutation<SerializedModel> initialUpdatePendingMutation = PendingMutation.update(SerializedModel.difference(initialUpdate, modelInSqlLite, schema), schema);
String existingUpdateId = initialUpdatePendingMutation.getMutationId().toString();
mutationOutbox.enqueue(initialUpdatePendingMutation).blockingAwait();
// Act: try to enqueue a new update mutation when there already is one
BlogOwner incomingUpdatedModel = BlogOwner.builder().name("Papa Tony").wea("something else").id(modelInSqlLite.getId()).build();
PendingMutation<SerializedModel> incomingUpdate = PendingMutation.update(SerializedModel.difference(incomingUpdatedModel, modelInSqlLite, schema), schema);
String incomingUpdateId = incomingUpdate.getMutationId().toString();
TestObserver<Void> enqueueObserver = mutationOutbox.enqueue(incomingUpdate).test();
// Assert: OK. The new mutation is accepted
enqueueObserver.await(TIMEOUT_MS, TimeUnit.MILLISECONDS);
enqueueObserver.assertComplete();
// Assert: the existing mutation has been removed
assertRecordCountForMutationId(existingUpdateId, 0);
// And the new one has been added to the queue
assertRecordCountForMutationId(incomingUpdateId, 0);
List<PersistentRecord> pendingMutationsFromStorage = getAllPendingMutationRecordFromStorage();
for (PersistentRecord record : pendingMutationsFromStorage) {
if (!record.getContainedModelId().equals(incomingUpdate.getMutatedItem().getId())) {
pendingMutationsFromStorage.remove(record);
}
}
// Ensure the new one is in storage.
PendingMutation<SerializedModel> storedMutation = converter.fromRecord(pendingMutationsFromStorage.get(0));
// This is the name from the second model, not the first!!
assertEquals(initialUpdate.getName(), storedMutation.getMutatedItem().getSerializedData().get("name"));
// wea got merged from existing model!!
assertEquals(incomingUpdatedModel.getWea(), storedMutation.getMutatedItem().getSerializedData().get("wea"));
assertEquals(PendingMutation.Type.UPDATE, storedMutation.getMutationType());
}
use of com.amplifyframework.core.model.SerializedModel in project amplify-android by aws-amplify.
the class ConflictResolverTest method conflictIsResolvedByRetryingLocalDataWithFlutterSerializedModel.
/**
* 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 conflictIsResolvedByRetryingLocalDataWithFlutterSerializedModel() 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
Map<String, Object> blogOwnerData = new HashMap<>();
blogOwnerData.put("name", "A seasoned writer");
blogOwnerData.put("id", "e50ffa8f-783b-4780-89b4-27043ffc35be");
SerializedModel serializedOwner = SerializedModel.builder().serializedData(blogOwnerData).modelSchema(schemaFrom()).build();
PendingMutation<SerializedModel> mutation = PendingMutation.update(serializedOwner, schema);
// Arrange server state for the model, in conflict to local data
Map<String, Object> serverBlogOwnerData = new HashMap<>();
serverBlogOwnerData.put("name", "A seasoned writer");
serverBlogOwnerData.put("id", "e50ffa8f-783b-4780-89b4-27043ffc35be");
SerializedModel serverModel = SerializedModel.builder().serializedData(serverBlogOwnerData).modelSchema(null).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<SerializedModel> versionFromAppSyncResponse = new ModelWithMetadata<>(serializedOwner, metadata);
AppSyncMocking.update(appSync).mockSuccessResponse(serializedOwner, 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 up to AppSync to update the model
verify(appSync).update(eq(serializedOwner), any(), eq(metadata.getVersion()), any(), any());
}
use of com.amplifyframework.core.model.SerializedModel in project amplify-android by aws-amplify.
the class GsonPendingMutationConverterTest method convertPendingMutationWithSerializedModelToRecordAndBack.
/**
* Validate that the {@link GsonPendingMutationConverter} can be
* used to convert a sample {@link PendingMutation} to a
* {@link PendingMutation.PersistentRecord}, and vice-versa.
* @throws DataStoreException from DataStore conversion
* @throws AmplifyException On failure to arrange model schema
*/
@Test
public void convertPendingMutationWithSerializedModelToRecordAndBack() throws AmplifyException {
// Arrange a PendingMutation<SerializedModel>
BlogOwner blogOwner = BlogOwner.builder().name("Joe Swanson").build();
ModelSchema schema = ModelSchema.fromModelClass(BlogOwner.class);
SerializedModel serializedBlogOwner = SerializedModel.create(blogOwner, schema);
PendingMutation<SerializedModel> originalMutation = PendingMutation.creation(serializedBlogOwner, schema);
String expectedMutationId = originalMutation.getMutationId().toString();
// Instantiate the object under test
PendingMutation.Converter converter = new GsonPendingMutationConverter();
// Try to construct a record from the PendingMutation instance.
PendingMutation.PersistentRecord record = converter.toRecord(originalMutation);
assertNotNull(record);
assertEquals(expectedMutationId, record.getId());
// Now, try to convert it back...
PendingMutation<SerializedModel> reconstructedItemChange = converter.fromRecord(record);
assertEquals(originalMutation, reconstructedItemChange);
}
use of com.amplifyframework.core.model.SerializedModel in project amplify-android by aws-amplify.
the class GsonPendingMutationConverterTest method convertPendingMutationWithSerializedModelWithChildToRecordAndBackWithNestedModelSchema.
/**
* Validate that the {@link GsonPendingMutationConverter} can be
* used to convert a sample {@link PendingMutation} to a
* {@link PendingMutation.PersistentRecord}, and vice-versa.
* @throws DataStoreException from DataStore conversion
* @throws AmplifyException On failure to arrange model schema
*/
@Test
public void convertPendingMutationWithSerializedModelWithChildToRecordAndBackWithNestedModelSchema() throws AmplifyException {
ModelSchema blogOwnerSchema = ModelSchema.fromModelClass(BlogOwner.class);
// register BlogOwner schema to ensure nested SerializedModel to be set with it's schema
schemaRegistry.register("BlogOwner", blogOwnerSchema);
// Arrange a PendingMutation<SerializedModel>
Blog blog = Blog.builder().name("A neat blog").owner(BlogOwner.builder().name("Joe Swanson").build()).build();
ModelSchema schema = ModelSchema.fromModelClass(Blog.class);
SerializedModel serializedBlog = SerializedModel.create(blog, schema);
PendingMutation<SerializedModel> originalMutation = PendingMutation.creation(serializedBlog, schema);
String expectedMutationId = originalMutation.getMutationId().toString();
// Instantiate the object under test
PendingMutation.Converter converter = new GsonPendingMutationConverter();
// Try to construct a record from the PendingMutation instance.
PendingMutation.PersistentRecord record = converter.toRecord(originalMutation);
assertNotNull(record);
assertEquals(expectedMutationId, record.getId());
// Now, try to convert it back...
PendingMutation<SerializedModel> reconstructedItemChange = converter.fromRecord(record);
assertEquals(originalMutation, reconstructedItemChange);
}
use of com.amplifyframework.core.model.SerializedModel in project amplify-android by aws-amplify.
the class GsonPendingMutationConverterTest method convertPendingMutationWithSerializedModelWithChildToRecordAndBack.
/**
* Validate that the {@link GsonPendingMutationConverter} can be
* used to convert a sample {@link PendingMutation} to a
* {@link PendingMutation.PersistentRecord}, and vice-versa.
* @throws DataStoreException from DataStore conversion
* @throws AmplifyException On failure to arrange model schema
*/
@Test
public void convertPendingMutationWithSerializedModelWithChildToRecordAndBack() throws AmplifyException {
// Arrange a PendingMutation<SerializedModel>
Blog blog = Blog.builder().name("A neat blog").owner(BlogOwner.builder().name("Joe Swanson").build()).build();
ModelSchema schema = ModelSchema.fromModelClass(Blog.class);
SerializedModel serializedBlog = SerializedModel.create(blog, schema);
PendingMutation<SerializedModel> originalMutation = PendingMutation.creation(serializedBlog, schema);
String expectedMutationId = originalMutation.getMutationId().toString();
// Instantiate the object under test
PendingMutation.Converter converter = new GsonPendingMutationConverter();
// Try to construct a record from the PendingMutation instance.
PendingMutation.PersistentRecord record = converter.toRecord(originalMutation);
assertNotNull(record);
assertEquals(expectedMutationId, record.getId());
// Now, try to convert it back...
PendingMutation<SerializedModel> reconstructedItemChange = converter.fromRecord(record);
assertEquals(originalMutation, reconstructedItemChange);
}
Aggregations