Search in sources :

Example 26 with SerializedModel

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());
}
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 27 with SerializedModel

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;
    }
}
Also used : ModelField(com.amplifyframework.core.model.annotations.ModelField) Field(java.lang.reflect.Field) SerializedModel(com.amplifyframework.core.model.SerializedModel) Nullable(androidx.annotation.Nullable)

Example 28 with SerializedModel

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));
}
Also used : Meeting(com.amplifyframework.testmodels.meeting.Meeting) HubAccumulator(com.amplifyframework.testutils.HubAccumulator) SerializedModel(com.amplifyframework.core.model.SerializedModel) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 29 with SerializedModel

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;
    }
}
Also used : ModelWithMetadata(com.amplifyframework.datastore.appsync.ModelWithMetadata) SerializedModel(com.amplifyframework.core.model.SerializedModel)

Example 30 with SerializedModel

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());
}
Also used : ModelWithMetadata(com.amplifyframework.datastore.appsync.ModelWithMetadata) SerializedModel(com.amplifyframework.core.model.SerializedModel)

Aggregations

SerializedModel (com.amplifyframework.core.model.SerializedModel)37 Test (org.junit.Test)21 HashMap (java.util.HashMap)18 ModelSchema (com.amplifyframework.core.model.ModelSchema)16 BlogOwner (com.amplifyframework.testmodels.commentsblog.BlogOwner)8 AmplifyException (com.amplifyframework.AmplifyException)7 Model (com.amplifyframework.core.model.Model)7 DataStoreException (com.amplifyframework.datastore.DataStoreException)6 ModelField (com.amplifyframework.core.model.ModelField)5 ArrayList (java.util.ArrayList)5 ModelAssociation (com.amplifyframework.core.model.ModelAssociation)4 SchemaRegistry (com.amplifyframework.core.model.SchemaRegistry)4 SerializedCustomType (com.amplifyframework.core.model.SerializedCustomType)4 StorageItemChange (com.amplifyframework.datastore.storage.StorageItemChange)4 Blog (com.amplifyframework.testmodels.commentsblog.Blog)4 HubAccumulator (com.amplifyframework.testutils.HubAccumulator)4 Map (java.util.Map)4 Ignore (org.junit.Ignore)4 Temporal (com.amplifyframework.core.model.temporal.Temporal)3 ModelWithMetadata (com.amplifyframework.datastore.appsync.ModelWithMetadata)3