Search in sources :

Example 1 with BlogOwner

use of com.amplifyframework.testmodels.commentsblog.BlogOwner in project amplify-android by aws-amplify.

the class PersistentMutationOutboxTest method existingCreationIncomingCreationYieldsError.

/**
 * When there is an existing creation for a model, and a new creation for that
 * model comes in, an error should be returned. In other words, it is illegal to
 * create a mutation twice.
 * @throws DataStoreException On failure to query storage to assert post-action value of mutation
 * @throws InterruptedException If interrupted while awaiting terminal result in test observer
 */
@Test
public void existingCreationIncomingCreationYieldsError() throws DataStoreException, InterruptedException {
    // Arrange an existing creation mutation
    BlogOwner modelInExistingMutation = BlogOwner.builder().name("The Real Papa Tony").build();
    PendingMutation<BlogOwner> existingCreation = PendingMutation.creation(modelInExistingMutation, schema);
    String existingCreationId = existingCreation.getMutationId().toString();
    mutationOutbox.enqueue(existingCreation).blockingAwait(TIMEOUT_MS, TimeUnit.MILLISECONDS);
    // Act: try to create the blog owner again -- but there's already a pending creation
    BlogOwner modelInIncomingMutation = modelInExistingMutation.copyOfBuilder().name("Someone Posing as Papa Tony Who isn't \uD83D\uDCAF legit.").build();
    PendingMutation<BlogOwner> incomingCreation = PendingMutation.creation(modelInIncomingMutation, schema);
    String incomingCreationId = incomingCreation.getMutationId().toString();
    TestObserver<Void> enqueueObserver = mutationOutbox.enqueue(incomingCreation).test();
    // Assert: caused a failure.
    enqueueObserver.await(TIMEOUT_MS, TimeUnit.MILLISECONDS);
    enqueueObserver.assertError(throwable -> throwable instanceof DataStoreException);
    // Assert: original mutation is present, but the new one isn't.
    PendingMutation.PersistentRecord storedMutation = storage.query(PersistentRecord.class, Where.id(existingCreationId)).get(0);
    assertEquals(modelInExistingMutation, converter.fromRecord(storedMutation).getMutatedItem());
    assertTrue(storage.query(PersistentRecord.class, Where.id(incomingCreationId)).isEmpty());
    // Existing mutation still attainable as next mutation (right now, its the ONLY mutation in outbox)
    assertTrue(mutationOutbox.hasPendingMutation(modelInExistingMutation.getId()));
    assertEquals(existingCreation, mutationOutbox.peek());
}
Also used : DataStoreException(com.amplifyframework.datastore.DataStoreException) PersistentRecord(com.amplifyframework.datastore.syncengine.PendingMutation.PersistentRecord) BlogOwner(com.amplifyframework.testmodels.commentsblog.BlogOwner) RandomString(com.amplifyframework.testutils.random.RandomString) PersistentRecord(com.amplifyframework.datastore.syncengine.PendingMutation.PersistentRecord) Test(org.junit.Test)

Example 2 with BlogOwner

use of com.amplifyframework.testmodels.commentsblog.BlogOwner in project amplify-android by aws-amplify.

the class PersistentMutationOutboxTest method removeRemovesChangesFromQueue.

/**
 * Tests {@link MutationOutbox#remove(TimeBasedUuid)}.
 * @throws DataStoreException On failure to query results, for assertions
 * @throws InterruptedException If interrupted while awaiting terminal result in test observer
 */
@Test
public void removeRemovesChangesFromQueue() throws DataStoreException, InterruptedException {
    // Arrange: there is a change in the queue.
    BlogOwner bill = BlogOwner.builder().name("Bill Gates").build();
    PendingMutation<BlogOwner> deleteBillGates = PendingMutation.deletion(bill, schema);
    storage.save(converter.toRecord(deleteBillGates));
    mutationOutbox.load().blockingAwait(TIMEOUT_MS, TimeUnit.MILLISECONDS);
    TestObserver<Void> testObserver = mutationOutbox.remove(deleteBillGates.getMutationId()).test();
    testObserver.await(TIMEOUT_MS, TimeUnit.MILLISECONDS);
    testObserver.assertNoErrors().assertComplete();
    testObserver.dispose();
    assertEquals(0, storage.query(PersistentRecord.class).size());
    assertNull(mutationOutbox.peek());
    assertFalse(mutationOutbox.hasPendingMutation(bill.getId()));
}
Also used : BlogOwner(com.amplifyframework.testmodels.commentsblog.BlogOwner) Test(org.junit.Test)

Example 3 with BlogOwner

use of com.amplifyframework.testmodels.commentsblog.BlogOwner in project amplify-android by aws-amplify.

the class PersistentMutationOutboxTest method nextItemForModelIdReturnsFirstEnqueued.

/**
 * If the queue contains multiple items, then
 * {@link MutationQueue#nextMutationForModelId(String)}
 * returns the first one.
 * @throws DataStoreException On failure to arrange content into storage
 */
@Test
public void nextItemForModelIdReturnsFirstEnqueued() throws DataStoreException {
    BlogOwner originalJoe = BlogOwner.builder().name("Joe Swanson").build();
    PendingMutation<BlogOwner> firstMutation = PendingMutation.update(originalJoe, schema);
    storage.save(originalJoe, converter.toRecord(firstMutation));
    BlogOwner updatedJoe = originalJoe.copyOfBuilder().name("Joe Swanson, MD. (He finished med school, I guess?)").build();
    PendingMutation<BlogOwner> secondMutation = PendingMutation.update(updatedJoe, schema);
    storage.save(updatedJoe, converter.toRecord(secondMutation));
    mutationOutbox.load().blockingAwait(TIMEOUT_MS, TimeUnit.MILLISECONDS);
    assertEquals(firstMutation, mutationQueue.nextMutationForModelId(originalJoe.getId()));
}
Also used : BlogOwner(com.amplifyframework.testmodels.commentsblog.BlogOwner) Test(org.junit.Test)

Example 4 with BlogOwner

use of com.amplifyframework.testmodels.commentsblog.BlogOwner in project amplify-android by aws-amplify.

the class PersistentMutationOutboxTest method existingCreationIncomingUpdateRewritesExitingMutation.

/**
 * When there is an existing creation mutation, and an update comes in,
 * the exiting creation should be updated with the contents of the incoming
 * mutation. The original creation mutation ID should be retained, for ordering.
 * @throws DataStoreException On failure to query the storage to examine which mutations were saved
 * @throws InterruptedException If interrupted while awaiting terminal result in test observer
 */
@Test
public void existingCreationIncomingUpdateRewritesExitingMutation() throws DataStoreException, InterruptedException {
    // Arrange an existing creation mutation
    BlogOwner modelInExistingMutation = BlogOwner.builder().name("Papa Tony").build();
    PendingMutation<BlogOwner> existingCreation = PendingMutation.creation(modelInExistingMutation, schema);
    String existingCreationId = existingCreation.getMutationId().toString();
    mutationOutbox.enqueue(existingCreation).blockingAwait(TIMEOUT_MS, TimeUnit.MILLISECONDS);
    // Act: try to enqueue an update even whilst the creation is pending
    BlogOwner modelInIncomingMutation = modelInExistingMutation.copyOfBuilder().name("Tony Jr.").build();
    PendingMutation<BlogOwner> incomingUpdate = PendingMutation.update(modelInIncomingMutation, 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 is still there, by id ....
    List<PendingMutation.PersistentRecord> recordsForExistingMutationId = storage.query(PersistentRecord.class, Where.id(existingCreationId));
    assertEquals(1, recordsForExistingMutationId.size());
    // And the new one is not, by ID...
    List<PendingMutation.PersistentRecord> recordsForIncomingMutationId = storage.query(PersistentRecord.class, Where.id(incomingUpdateId));
    assertEquals(0, recordsForIncomingMutationId.size());
    // However, the original mutation has been updated to include the contents of the
    // incoming mutation. This is true even whilst the mutation retains its original ID.
    PendingMutation<BlogOwner> storedMutation = converter.fromRecord(recordsForExistingMutationId.get(0));
    // This is the name from the second model, not the first!
    assertEquals("Tony Jr.", storedMutation.getMutatedItem().getName());
    // There is a mutation in the outbox, it has the original ID.
    // This is STILL a creation, just using the new model data.
    assertEquals(PendingMutation.instance(existingCreation.getMutationId(), modelInIncomingMutation, schema, PendingMutation.Type.CREATE, QueryPredicates.all()), mutationOutbox.peek());
}
Also used : BlogOwner(com.amplifyframework.testmodels.commentsblog.BlogOwner) RandomString(com.amplifyframework.testutils.random.RandomString) PersistentRecord(com.amplifyframework.datastore.syncengine.PendingMutation.PersistentRecord) Test(org.junit.Test)

Example 5 with BlogOwner

use of com.amplifyframework.testmodels.commentsblog.BlogOwner 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)

Aggregations

BlogOwner (com.amplifyframework.testmodels.commentsblog.BlogOwner)150 Test (org.junit.Test)146 DataStoreException (com.amplifyframework.datastore.DataStoreException)35 Blog (com.amplifyframework.testmodels.commentsblog.Blog)32 ModelSchema (com.amplifyframework.core.model.ModelSchema)31 Post (com.amplifyframework.testmodels.commentsblog.Post)31 ArrayList (java.util.ArrayList)29 QueryPredicate (com.amplifyframework.core.model.query.predicate.QueryPredicate)25 Assert.assertEquals (org.junit.Assert.assertEquals)25 ModelMetadata (com.amplifyframework.datastore.appsync.ModelMetadata)24 Collections (java.util.Collections)24 HashSet (java.util.HashSet)23 List (java.util.List)23 PostStatus (com.amplifyframework.testmodels.commentsblog.PostStatus)22 HubAccumulator (com.amplifyframework.testutils.HubAccumulator)22 Arrays (java.util.Arrays)22 TimeUnit (java.util.concurrent.TimeUnit)22 Consumer (com.amplifyframework.core.Consumer)21 ModelWithMetadata (com.amplifyframework.datastore.appsync.ModelWithMetadata)21 Before (org.junit.Before)21