Search in sources :

Example 31 with BlogOwner

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

the class MutationProcessorTest method outboxStatusIsPublishedToHubOnProcess.

/**
 * Processing a mutation should publish current outbox status.
 */
@Test
public void outboxStatusIsPublishedToHubOnProcess() {
    BlogOwner raphael = BlogOwner.builder().name("Raphael Kim").build();
    ModelSchema schema = schemaRegistry.getModelSchemaForModelClass(BlogOwner.class);
    PendingMutation<BlogOwner> createRaphael = PendingMutation.creation(raphael, schema);
    // Mock up a response from AppSync and enqueue a mutation.
    AppSyncMocking.create(appSync).mockSuccessResponse(raphael);
    assertTrue(mutationOutbox.enqueue(createRaphael).blockingAwait(TIMEOUT_SECONDS, TimeUnit.SECONDS));
    // Start listening for publication events.
    // outbox should be empty after processing its only mutation
    HubAccumulator statusAccumulator = HubAccumulator.create(HubChannel.DATASTORE, isOutboxEmpty(true), 1).start();
    // Start draining the outbox which has one mutation enqueued,
    // and make sure that outbox status is published to hub.
    mutationProcessor.startDrainingMutationOutbox();
    statusAccumulator.await();
}
Also used : ModelSchema(com.amplifyframework.core.model.ModelSchema) BlogOwner(com.amplifyframework.testmodels.commentsblog.BlogOwner) HubAccumulator(com.amplifyframework.testutils.HubAccumulator) Test(org.junit.Test)

Example 32 with BlogOwner

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

the class MutationProcessorTest method hubEventPublishedForPublicationError.

/**
 * If the AppSync response to the mutation contains not-empty GraphQLResponse error
 * list without any ConflictUnhandled error, then
 * {@link DataStoreChannelEventName#OUTBOX_MUTATION_FAILED} event is published via Hub.
 * @throws DataStoreException On failure to save model and metadata
 */
@Test
public void hubEventPublishedForPublicationError() throws DataStoreException {
    // Save a model, its metadata, and its last sync data.
    BlogOwner model = BlogOwner.builder().name("Average Joe").build();
    ModelMetadata metadata = new ModelMetadata(model.getModelName() + "|" + model.getId(), false, 1, Temporal.Timestamp.now());
    ModelSchema schema = schemaRegistry.getModelSchemaForModelClass(BlogOwner.class);
    synchronousStorageAdapter.save(model, metadata);
    // Enqueue an update in the mutation outbox
    assertTrue(mutationOutbox.enqueue(PendingMutation.update(model, schema)).blockingAwait(TIMEOUT_SECONDS, TimeUnit.SECONDS));
    // When AppSync receives that update, have it respond with an error.
    AppSyncMocking.update(appSync).mockErrorResponse(model, 1);
    // Start listening for publication events.
    HubAccumulator errorAccumulator = HubAccumulator.create(HubChannel.DATASTORE, DataStoreChannelEventName.OUTBOX_MUTATION_FAILED, 1).start();
    // Start the mutation processor and wait for hub event.
    mutationProcessor.startDrainingMutationOutbox();
    errorAccumulator.await();
}
Also used : ModelSchema(com.amplifyframework.core.model.ModelSchema) BlogOwner(com.amplifyframework.testmodels.commentsblog.BlogOwner) HubAccumulator(com.amplifyframework.testutils.HubAccumulator) ModelMetadata(com.amplifyframework.datastore.appsync.ModelMetadata) Test(org.junit.Test)

Example 33 with BlogOwner

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

the class MutationQueueTest method removePendingMutationTest.

/**
 * Prepare a {@link PendingMutation} instance and insert into the tail of
 * the {@link MutationQueue} and then remove it, verify the {@link MutationQueue}
 * is empty after the remove.
 */
@Test
public void removePendingMutationTest() {
    mutationQueue.clear();
    BlogOwner qing = BlogOwner.builder().name("Qing Zhong").build();
    PendingMutation<BlogOwner> createQing = PendingMutation.creation(qing, schema);
    mutationQueue.add(createQing);
    assertEquals(mutationQueue.size(), 1);
    mutationQueue.remove(createQing);
    assertTrue(mutationQueue.isEmpty());
}
Also used : BlogOwner(com.amplifyframework.testmodels.commentsblog.BlogOwner) Test(org.junit.Test)

Example 34 with BlogOwner

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

the class MutationQueueTest method peekingPendingMutationsTest.

/**
 * Prepare two {@link PendingMutation} instances and insert them into the tail of
 * the {@link MutationQueue}, verify the first element in the queue should be the
 * first element we insert, which is FIFO.
 */
@Test
public void peekingPendingMutationsTest() {
    mutationQueue.clear();
    BlogOwner qing = BlogOwner.builder().name("Qing Zhong").build();
    PendingMutation<BlogOwner> createQing = PendingMutation.creation(qing, schema);
    BlogOwner tony = BlogOwner.builder().name("The Real Papa Tony").build();
    PendingMutation<BlogOwner> createTony = PendingMutation.creation(tony, schema);
    mutationQueue.add(createQing);
    mutationQueue.add(createTony);
    assertEquals(createQing, mutationQueue.peek());
}
Also used : BlogOwner(com.amplifyframework.testmodels.commentsblog.BlogOwner) Test(org.junit.Test)

Example 35 with BlogOwner

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

the class MutationQueueTest method addPendingMutationTest.

/**
 * Prepare a {@link PendingMutation} instance and insert into the tail of
 * the {@link MutationQueue}, verify the instance exist by checking its {@link TimeBasedUuid}.
 */
@Test
public void addPendingMutationTest() {
    mutationQueue.clear();
    BlogOwner qing = BlogOwner.builder().name("Qing Zhong").build();
    PendingMutation<BlogOwner> createQing = PendingMutation.creation(qing, schema);
    mutationQueue.add(createQing);
    assertEquals(mutationQueue.size(), 1);
    TimeBasedUuid id = createQing.getMutationId();
    assertEquals(mutationQueue.getMutationById(id), createQing);
}
Also used : 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