use of com.amplifyframework.core.model.SerializedModel 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());
}
use of com.amplifyframework.core.model.SerializedModel in project amplify-android by aws-amplify.
the class FieldFinder method extractFieldValue.
/**
* Extract the value of a field in an Object by field name.
* @param object Object to obtain field value from
* @param fieldName Name of the field being examined
* @return Value of the field if the field exists
* @throws NoSuchFieldException if object does not contain
* a field that matches fieldName
*/
@Nullable
public static Object extractFieldValue(@NonNull Object object, @NonNull String fieldName) throws NoSuchFieldException {
if (object instanceof SerializedModel) {
SerializedModel serializedModel = (SerializedModel) object;
Map<String, Object> serializedData = serializedModel.getSerializedData();
return serializedData.get(fieldName);
}
try {
Field objectField = object.getClass().getDeclaredField(fieldName);
objectField.setAccessible(true);
return objectField.get(object);
} catch (NoSuchFieldException noSuchFieldException) {
throw noSuchFieldException;
} catch (Exception exception) {
return null;
}
}
use of com.amplifyframework.core.model.SerializedModel in project amplify-android by aws-amplify.
the class HybridTemporalSyncInstrumentationTest method temporalTypesAreSyncedUpToCloud.
/**
* It is possible to dispatch a model that contain temporal types. After publishing
* such a model to the cloud, we can query AppSync and find it there.
* @throws ApiException on failure to communicate with AppSync API in verification phase of test
*/
@Ignore("It passes. Not automating due to operational concerns as noted in class-level @Ignore.")
@Test
public void temporalTypesAreSyncedUpToCloud() throws ApiException {
// Prepare a SerializedModel that we will save to DataStore.
Meeting meeting = createMeeting();
Map<String, Object> sentData = toMap(meeting);
SerializedModel sentModel = SerializedModel.builder().serializedData(sentData).modelSchema(modelSchema).build();
HubAccumulator publicationAccumulator = HubAccumulator.create(HubChannel.DATASTORE, publicationOf(modelSchema.getName(), sentModel.getId()), 1).start();
hybridBehaviors.save(sentModel);
publicationAccumulator.await(TIMEOUT_SECONDS, TimeUnit.SECONDS);
// Retrieve the model from AppSync.
Meeting remoteMeeting = api.get(Meeting.class, sentModel.getId());
// Inspect the fields of the data in AppSync, and prepare it into a map
// that we can compare with what we sent. Are they the same? They should be.
assertEquals(sentData, toMap(remoteMeeting));
}
use of com.amplifyframework.core.model.SerializedModel in project amplify-android by aws-amplify.
the class SyncProcessor method hydrateSchemaIfNeeded.
// Cast to T
@SuppressWarnings("unchecked")
private <T extends Model> ModelWithMetadata<T> hydrateSchemaIfNeeded(ModelWithMetadata<T> original, ModelSchema schema) {
if (original.getModel() instanceof SerializedModel) {
SerializedModel originalModel = (SerializedModel) original.getModel();
SerializedModel newModel = SerializedModel.builder().serializedData(SerializedModel.parseSerializedData(originalModel.getSerializedData(), schema.getName(), schemaRegistry)).modelSchema(schema).build();
return new ModelWithMetadata<>((T) newModel, original.getSyncMetadata());
} else {
return original;
}
}
use of com.amplifyframework.core.model.SerializedModel in project amplify-android by aws-amplify.
the class MutationProcessor method modelWithSchemaAdded.
private <T extends Model> ModelWithMetadata<? extends Model> modelWithSchemaAdded(ModelWithMetadata<T> modelWithMetadata, ModelSchema modelSchema) {
final SerializedModel originalModel = (SerializedModel) modelWithMetadata.getModel();
final SerializedModel newModel = SerializedModel.builder().serializedData(SerializedModel.parseSerializedData(originalModel.getSerializedData(), modelSchema.getName(), schemaRegistry)).modelSchema(modelSchema).build();
return new ModelWithMetadata<>(newModel, modelWithMetadata.getSyncMetadata());
}
Aggregations