Search in sources :

Example 1 with PersistentRecord

use of com.amplifyframework.datastore.syncengine.PendingMutation.PersistentRecord 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());
}
Also used : BlogOwner(com.amplifyframework.testmodels.commentsblog.BlogOwner) RandomString(com.amplifyframework.testutils.random.RandomString) SerializedModel(com.amplifyframework.core.model.SerializedModel) PersistentRecord(com.amplifyframework.datastore.syncengine.PendingMutation.PersistentRecord) Test(org.junit.Test)

Example 2 with PersistentRecord

use of com.amplifyframework.datastore.syncengine.PendingMutation.PersistentRecord in project amplify-android by aws-amplify.

the class PersistentMutationOutboxTest method existingDeletionIncomingDeletionOverwritesExisting.

/**
 * If there is an existing deletion mutation, and then we get another one, update the original
 * with the new one.
 * @throws DataStoreException On failure to query storage for records
 */
@Test
public void existingDeletionIncomingDeletionOverwritesExisting() throws DataStoreException {
    BlogOwner sammy = BlogOwner.builder().name("Sammy Swanson").build();
    PendingMutation<BlogOwner> exitingDeletion = PendingMutation.deletion(sammy, schema);
    PendingMutation<BlogOwner> incomingDeletion = PendingMutation.deletion(sammy, schema);
    assertNotEquals(exitingDeletion.getMutationId(), incomingDeletion.getMutationId());
    mutationOutbox.enqueue(exitingDeletion).blockingAwait(TIMEOUT_MS, TimeUnit.MILLISECONDS);
    mutationOutbox.enqueue(incomingDeletion).blockingAwait(TIMEOUT_MS, TimeUnit.MILLISECONDS);
    // Existing record is still there
    List<PersistentRecord> existingMutationRecords = storage.query(PersistentRecord.class, Where.id(exitingDeletion.getMutationId().toString()));
    assertEquals(1, existingMutationRecords.size());
    // Incoming is not present
    List<PersistentRecord> incomingMutationRecords = storage.query(PersistentRecord.class, Where.id(incomingDeletion.getMutationId().toString()));
    assertEquals(0, incomingMutationRecords.size());
    // Still a deletion, as the next outbox item
    assertEquals(exitingDeletion, mutationOutbox.peek());
}
Also used : BlogOwner(com.amplifyframework.testmodels.commentsblog.BlogOwner) PersistentRecord(com.amplifyframework.datastore.syncengine.PendingMutation.PersistentRecord) Test(org.junit.Test)

Example 3 with PersistentRecord

use of com.amplifyframework.datastore.syncengine.PendingMutation.PersistentRecord in project amplify-android by aws-amplify.

the class PersistentMutationOutboxTest method existingSerializedModelCreateIncomingUpdateMergesWithExistingMutation.

/**
 * When there is an existing SerializedModel create 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 Create.
 * @throws AmplifyException On failure to find the serializedModel difference.
 * @throws InterruptedException If interrupted while awaiting terminal result in test observer
 */
@Test
public void existingSerializedModelCreateIncomingUpdateMergesWithExistingMutation() 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.creation(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.CREATE, storedMutation.getMutationType());
}
Also used : BlogOwner(com.amplifyframework.testmodels.commentsblog.BlogOwner) RandomString(com.amplifyframework.testutils.random.RandomString) SerializedModel(com.amplifyframework.core.model.SerializedModel) PersistentRecord(com.amplifyframework.datastore.syncengine.PendingMutation.PersistentRecord) Test(org.junit.Test)

Aggregations

PersistentRecord (com.amplifyframework.datastore.syncengine.PendingMutation.PersistentRecord)3 BlogOwner (com.amplifyframework.testmodels.commentsblog.BlogOwner)3 Test (org.junit.Test)3 SerializedModel (com.amplifyframework.core.model.SerializedModel)2 RandomString (com.amplifyframework.testutils.random.RandomString)2