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"));
}
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();
}
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());
}
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());
}
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();
}
Aggregations