Search in sources :

Example 91 with BlogOwner

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

the class PersistentMutationOutboxTest method errorWhenMarkingItemNotInQueue.

/**
 * It is an error to mark an item as in-flight, if it isn't even in the dang queue.
 * @throws InterruptedException If interrupted while awaiting terminal result in test observer
 */
@Test
public void errorWhenMarkingItemNotInQueue() 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);
    mutationOutbox.remove(creation.getMutationId()).blockingAwait(TIMEOUT_MS, TimeUnit.MILLISECONDS);
    // Now, if we try to make that mutation as in-flight, its an error, since its already processed.
    TestObserver<Void> observer = mutationOutbox.markInFlight(creation.getMutationId()).test();
    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)

Example 92 with BlogOwner

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

the class PersistentMutationOutboxTest method notifiesWhenContentAvailableAfterDelete.

/**
 * When there are multiple pending mutations in the outbox, and one is removed,
 * we will see a {@link OutboxEvent#CONTENT_AVAILABLE} after the removal. This
 * notifies the system that there is more work to be done, even though we've successfully
 * processed an event. The system will continue processing items from the outbox until
 * all have been processed.
 * @throws DataStoreException On failure to arrange data into storage
 * @throws InterruptedException If thread interrupted while waiting for events
 */
@Test
public void notifiesWhenContentAvailableAfterDelete() throws DataStoreException, InterruptedException {
    // Start watching the events stream. We'll expect a notification here once,
    // after the first deletion.
    TestObserver<OutboxEvent> firstEventObserver = mutationOutbox.events().test();
    // Arrange a few mutations into the queue.
    BlogOwner senatorBernie = BlogOwner.builder().name("Senator Bernard Sanders").build();
    PendingMutation<BlogOwner> createSenatorBernie = PendingMutation.creation(senatorBernie, schema);
    storage.save(converter.toRecord(createSenatorBernie));
    BlogOwner candidateBernie = senatorBernie.copyOfBuilder().name("Democratic Presidential Candidate, Bernard Sanders").build();
    PendingMutation<BlogOwner> updateCandidateBernie = PendingMutation.update(candidateBernie, schema);
    storage.save(converter.toRecord(updateCandidateBernie));
    mutationOutbox.load().blockingAwait(TIMEOUT_MS, TimeUnit.MILLISECONDS);
    // Remove first item.
    TestObserver<Void> firstRemoval = mutationOutbox.remove(createSenatorBernie.getMutationId()).test();
    firstRemoval.await(TIMEOUT_MS, TimeUnit.MILLISECONDS);
    firstRemoval.assertNoErrors().assertComplete().dispose();
    // One event is observed on events(), since there are still some pending mutations
    // that need to be processed.
    firstEventObserver.awaitCount(1).assertValues(OutboxEvent.CONTENT_AVAILABLE).assertNoErrors();
    // Get ready to watch the events() again.
    TestObserver<OutboxEvent> secondEventObserver = mutationOutbox.events().test();
    // Remove the next item.
    TestObserver<Void> secondRemoval = mutationOutbox.remove(updateCandidateBernie.getMutationId()).test();
    secondRemoval.await(TIMEOUT_MS, TimeUnit.MILLISECONDS);
    secondRemoval.assertNoErrors().assertComplete().dispose();
    // This time, we don't see any event on events(), since the outbox has become empty.
    secondEventObserver.await(TIMEOUT_MS, TimeUnit.MILLISECONDS);
    secondEventObserver.assertNoValues().assertNoErrors();
}
Also used : BlogOwner(com.amplifyframework.testmodels.commentsblog.BlogOwner) OutboxEvent(com.amplifyframework.datastore.syncengine.MutationOutbox.OutboxEvent) Test(org.junit.Test)

Example 93 with BlogOwner

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

the class PersistentMutationOutboxTest method loadPreparesOutbox.

/**
 * Calling load() will populate the outbox with content from disk.
 * @throws DataStoreException On failure to arrange models into storage before test action
 * @throws InterruptedException If interrupted while awaiting terminal result in test observer
 */
@Test
public void loadPreparesOutbox() throws DataStoreException, InterruptedException {
    // Arrange: some mutations.
    BlogOwner tony = BlogOwner.builder().name("Tony Daniels").build();
    PendingMutation<BlogOwner> updateTony = PendingMutation.update(tony, schema);
    BlogOwner sam = BlogOwner.builder().name("Sam Watson").build();
    PendingMutation<BlogOwner> insertSam = PendingMutation.creation(sam, schema);
    storage.save(converter.toRecord(updateTony), converter.toRecord(insertSam));
    // Act: load the outbox.
    TestObserver<Void> loadObserver = mutationOutbox.load().test();
    // Assert: load worked.
    loadObserver.await(TIMEOUT_MS, TimeUnit.MILLISECONDS);
    loadObserver.assertNoErrors().assertComplete();
    loadObserver.dispose();
    // Assert: items are in the outbox.
    assertTrue(mutationOutbox.hasPendingMutation(tony.getId()));
    assertTrue(mutationOutbox.hasPendingMutation(sam.getId()));
    // Tony is first, since he is the older of the two mutations.
    assertEquals(updateTony, mutationOutbox.peek());
}
Also used : BlogOwner(com.amplifyframework.testmodels.commentsblog.BlogOwner) Test(org.junit.Test)

Example 94 with BlogOwner

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

the class PersistentMutationOutboxTest method enqueuePersistsMutationAndNotifiesObserver.

/**
 * Enqueueing a mutation should have the result of persisting
 * the mutation to storage, and notifying any observers that
 * a new mutation has been enqueued.
 * @throws DataStoreException On failure to query results, for assertions
 * @throws InterruptedException If interrupted while awaiting terminal result in test observer
 */
@Test
public void enqueuePersistsMutationAndNotifiesObserver() throws DataStoreException, InterruptedException {
    // Observe the queue
    TestObserver<OutboxEvent> queueObserver = mutationOutbox.events().test();
    BlogOwner jameson = BlogOwner.builder().name("Jameson Williams").build();
    PendingMutation<BlogOwner> createJameson = PendingMutation.creation(jameson, schema);
    HubAccumulator savedMutationsAccumulator = HubAccumulator.create(HubChannel.DATASTORE, isEnqueued(jameson), 1).start();
    // Enqueue an save for a Jameson BlogOwner object,
    // and make sure that it calls back onComplete().
    TestObserver<Void> saveObserver = mutationOutbox.enqueue(createJameson).test();
    saveObserver.await(TIMEOUT_MS, TimeUnit.MILLISECONDS);
    saveObserver.assertNoErrors().assertComplete();
    saveObserver.dispose();
    // Wait for a Hub event telling us that the model got successfully enqueued.
    savedMutationsAccumulator.await();
    // Expected to observe the mutation on the subject
    queueObserver.awaitCount(1);
    queueObserver.assertValue(OutboxEvent.CONTENT_AVAILABLE);
    queueObserver.dispose();
    // Assert that the storage contains the mutation
    assertEquals(Collections.singletonList(converter.toRecord(createJameson)), storage.query(PersistentRecord.class));
    assertTrue(mutationOutbox.hasPendingMutation(jameson.getId()));
    assertEquals(createJameson, mutationOutbox.peek());
}
Also used : BlogOwner(com.amplifyframework.testmodels.commentsblog.BlogOwner) OutboxEvent(com.amplifyframework.datastore.syncengine.MutationOutbox.OutboxEvent) HubAccumulator(com.amplifyframework.testutils.HubAccumulator) PersistentRecord(com.amplifyframework.datastore.syncengine.PendingMutation.PersistentRecord) Test(org.junit.Test)

Example 95 with BlogOwner

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

the class PersistentMutationOutboxTest method outboxStatusIsPublishedToHubOnEnqueue.

/**
 * Enqueueing a mutation should publish current outbox status.
 */
@Test
public void outboxStatusIsPublishedToHubOnEnqueue() {
    BlogOwner raphael = BlogOwner.builder().name("Raphael Kim").build();
    PendingMutation<BlogOwner> createRaphael = PendingMutation.creation(raphael, schema);
    // Start listening for publication events.
    // outbox should not be empty
    HubAccumulator statusAccumulator = HubAccumulator.create(HubChannel.DATASTORE, isOutboxEmpty(false), 1).start();
    // Enqueue an save for a Raphael BlogOwner object,
    // and make sure that outbox status is published to hub.
    mutationOutbox.enqueue(createRaphael).test();
    statusAccumulator.await();
}
Also used : BlogOwner(com.amplifyframework.testmodels.commentsblog.BlogOwner) HubAccumulator(com.amplifyframework.testutils.HubAccumulator) 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