Search in sources :

Example 11 with BlogOwner

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

the class PersistentMutationOutboxTest method existingUpdateIncomingCreationYieldsError.

/**
 * When there is an existing update for a model, and a new creation for that
 * model comes in, an error should be returned. In other words, you can't create
 * something that already exists and is being updated.
 * @throws DataStoreException On failure to to query which mutations are in storage
 * @throws InterruptedException If interrupted while awaiting terminal result in test observer
 */
@Test
public void existingUpdateIncomingCreationYieldsError() throws DataStoreException, InterruptedException {
    // Arrange an existing update mutation
    BlogOwner modelInExistingMutation = BlogOwner.builder().name("Tony with improvements applied").build();
    PendingMutation<BlogOwner> existingUpdate = PendingMutation.update(modelInExistingMutation, schema);
    String exitingUpdateId = existingUpdate.getMutationId().toString();
    mutationOutbox.enqueue(existingUpdate).blockingAwait(TIMEOUT_MS, TimeUnit.MILLISECONDS);
    // Act: try to CREATE tony again -- but isn't he already created, if there's an update?
    BlogOwner modelInIncomingMutation = modelInExistingMutation.copyOfBuilder().name("Brand new tony").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(exitingUpdateId)).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(existingUpdate, 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 12 with BlogOwner

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

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

the class PersistentMutationOutboxTest method updateEventPostedWhenExistingOutboxItemUpdate.

/**
 * When an already-pending mutation is updated, then the {@link Observable} returned by
 * {@link MutationOutbox#events()} should emit an {@link OutboxEvent#CONTENT_AVAILABLE} event.
 */
@Test
public void updateEventPostedWhenExistingOutboxItemUpdate() {
    // Watch for events.
    TestObserver<OutboxEvent> eventsObserver = mutationOutbox.events().test();
    // Create tony.
    BlogOwner tonyWrongName = BlogOwner.builder().name("Tony Jon Swanssssssssson yee-haw!").build();
    PendingMutation<BlogOwner> originalCreation = PendingMutation.creation(tonyWrongName, schema);
    mutationOutbox.enqueue(originalCreation).blockingAwait(TIMEOUT_MS, TimeUnit.MILLISECONDS);
    // Update tony - we spelled his name wrong originally
    BlogOwner tonySpelledRight = tonyWrongName.copyOfBuilder().name("Tony Jon (\"TJ\") Swanson").build();
    mutationOutbox.enqueue(PendingMutation.update(tonySpelledRight, schema)).blockingAwait(TIMEOUT_MS, TimeUnit.MILLISECONDS);
    // Assert: an event for the original creation, then another for the update
    eventsObserver.awaitCount(2).assertNoErrors().assertNotComplete().assertValues(OutboxEvent.CONTENT_AVAILABLE, OutboxEvent.CONTENT_AVAILABLE);
}
Also used : BlogOwner(com.amplifyframework.testmodels.commentsblog.BlogOwner) OutboxEvent(com.amplifyframework.datastore.syncengine.MutationOutbox.OutboxEvent) Test(org.junit.Test)

Example 14 with BlogOwner

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

the class PersistentMutationOutboxTest method existingUpdateIncomingUpdateWithConditionAppendsMutation.

/**
 * When there is an existing update mutation, and a new update mutation with condition
 * comes in, then the existing one should remain and the new one should be appended.
 * @throws DataStoreException On failure to query storage for current mutations state
 * @throws InterruptedException If interrupted while awaiting terminal result in test observer
 */
@Test
public void existingUpdateIncomingUpdateWithConditionAppendsMutation() throws DataStoreException, InterruptedException {
    // Arrange an existing update mutation
    BlogOwner modelInExistingMutation = BlogOwner.builder().name("Papa Tony").build();
    PendingMutation<BlogOwner> existingUpdate = PendingMutation.update(modelInExistingMutation, schema);
    String existingUpdateId = existingUpdate.getMutationId().toString();
    mutationOutbox.enqueue(existingUpdate).blockingAwait(TIMEOUT_MS, TimeUnit.MILLISECONDS);
    // Act: try to enqueue a new update mutation when there already is one
    BlogOwner modelInIncomingMutation = modelInExistingMutation.copyOfBuilder().name("Tony Jr.").build();
    PendingMutation<BlogOwner> incomingUpdate = PendingMutation.update(modelInIncomingMutation, schema, BlogOwner.NAME.eq("Papa Tony"));
    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(existingUpdateId));
    assertEquals(1, recordsForExistingMutationId.size());
    // Assert: And the new one is also there
    List<PendingMutation.PersistentRecord> recordsForIncomingMutationId = storage.query(PersistentRecord.class, Where.id(incomingUpdateId));
    assertEquals(1, recordsForIncomingMutationId.size());
    // The original mutation should remain as is
    PendingMutation<BlogOwner> existingStoredMutation = converter.fromRecord(recordsForExistingMutationId.get(0));
    // This is the name from the second model, not the first!!
    assertEquals(modelInExistingMutation.getName(), existingStoredMutation.getMutatedItem().getName());
    PendingMutation<? extends Model> next = mutationOutbox.peek();
    assertNotNull(next);
    // The first one should be the existing mutation
    assertEquals(existingUpdate, next);
    // Remove the first one from the queue
    mutationOutbox.remove(existingUpdate.getMutationId()).blockingAwait(TIMEOUT_MS, TimeUnit.MILLISECONDS);
    // Get the next one
    next = mutationOutbox.peek();
    assertNotNull(next);
    // The first one should be the existing mutation
    assertEquals(incomingUpdate, next);
}
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 15 with BlogOwner

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

the class PersistentMutationOutboxTest method markInFlightIsSynchronized.

/**
 * Marking an item in flight should throw an error if the item is already removed from the queue.  This is similar
 * to {@link #errorWhenMarkingItemNotInQueue}, except that the removal and marking in flight Completables are
 * concatenated into the same stream.  This validates that markInFlight does not check if the item is in the queue
 * until after the removal is complete.
 * @throws InterruptedException If interrupted while awaiting terminal result in test observer
 */
@Test
public void markInFlightIsSynchronized() throws InterruptedException {
    // Enqueue and remove a mutation.
    BlogOwner tabby = BlogOwner.builder().name("Tabitha Stevens of Beaver Falls, Idaho").build();
    PendingMutation<BlogOwner> creation = PendingMutation.creation(tabby, schema);
    mutationOutbox.enqueue(creation).blockingAwait(TIMEOUT_MS, TimeUnit.MILLISECONDS);
    TestObserver<Void> observer = mutationOutbox.remove(creation.getMutationId()).andThen(mutationOutbox.markInFlight(creation.getMutationId())).test();
    // Now, we should see an error since we can't mark a mutation as in-flight that has already been removed.
    observer.await(TIMEOUT_MS, TimeUnit.MILLISECONDS);
    observer.assertError(DataStoreException.class).assertError(error -> error.getMessage() != null && error.getMessage().contains("there was no mutation with that ID in the outbox"));
}
Also used : DataStoreException(com.amplifyframework.datastore.DataStoreException) BlogOwner(com.amplifyframework.testmodels.commentsblog.BlogOwner) 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