Search in sources :

Example 1 with AddPostMutation

use of com.amazonaws.mobileconnectors.appsync.demo.AddPostMutation in project aws-mobile-appsync-sdk-android by awslabs.

the class QueryInstrumentationTest method mutationQueueIsEmptyAfterMutationCompletes.

@Test
public void mutationQueueIsEmptyAfterMutationCompletes() {
    AWSAppSyncClient awsAppSyncClient = AWSAppSyncClients.withIAMFromAWSConfiguration(true, REASONABLE_WAIT_TIME_MS);
    assertTrue(awsAppSyncClient.isMutationQueueEmpty());
    // Note: when the test starts, we assume the mutation queue is going to be empty.
    // Act: Put something in the queue - this AddPostMutation.
    LatchedGraphQLCallback<AddPostMutation.Data> addPostCallback = LatchedGraphQLCallback.instance(EXTENDED_WAIT_TIME_MS);
    awsAppSyncClient.mutate(AddPostMutation.builder().input(CreatePostInput.builder().title("Lonely Day").author("SOAD" + System.currentTimeMillis()).url("SOAD.com").content("Such a lonely day").ups(1).downs(0).build()).build(), new AddPostMutation.Data(new AddPostMutation.CreatePost("Post", "", "", "", "", "", null, null, 0))).enqueue(addPostCallback);
    // At first, the mutation is enqueued.
    assertFalse(awsAppSyncClient.isMutationQueueEmpty());
    // Once it executes, though, it's no longer in the queue.
    addPostCallback.awaitResponse();
    // Adding a little buffer ...
    Sleep.milliseconds(REASONABLE_WAIT_TIME_MS);
    assertTrue(awsAppSyncClient.isMutationQueueEmpty());
}
Also used : AWSAppSyncClient(com.amazonaws.mobileconnectors.appsync.AWSAppSyncClient) AddPostMutation(com.amazonaws.mobileconnectors.appsync.demo.AddPostMutation) Test(org.junit.Test)

Example 2 with AddPostMutation

use of com.amazonaws.mobileconnectors.appsync.demo.AddPostMutation in project aws-mobile-appsync-sdk-android by awslabs.

the class MultiClientInstrumentationTest method testClearMutationsCacheOnly.

@Test
public void testClearMutationsCacheOnly() throws ClearCacheException {
    AWSAppSyncClient client = AWSAppSyncClients.withAPIKEYFromAWSConfiguration();
    Response<AllPostsQuery.Data> networkResponse = Posts.list(client, AppSyncResponseFetchers.NETWORK_FIRST).get("NETWORK");
    assertNotNull(networkResponse);
    assertNotNull(networkResponse.data());
    goOffline();
    // Add a post
    AddPostMutation.Data addPostMutationData = new AddPostMutation.Data(new AddPostMutation.CreatePost("Post", "", "", "", "", "", null, null, 0));
    AddPostMutation addPostMutation = AddPostMutation.builder().input(CreatePostInput.builder().title("Learning to Live ").author("Dream Theater @ ").url("Dream Theater Station").content("No energy for anger @" + System.currentTimeMillis()).ups(1).downs(0).build()).build();
    client.mutate(addPostMutation, addPostMutationData).enqueue(NoOpGraphQLCallback.instance());
    // Act: clear the caches.
    client.clearCaches(ClearCacheOptions.builder().clearMutations().build());
    // Check Mutation Queue
    assertTrue(client.isMutationQueueEmpty());
    // Query from cache and check data is available since only mutations queue is cleared.
    Response<AllPostsQuery.Data> cacheResponse = Posts.list(client, AppSyncResponseFetchers.CACHE_ONLY).get("CACHE");
    assertNotNull(cacheResponse);
    assertNotNull(cacheResponse.data());
}
Also used : AWSAppSyncClient(com.amazonaws.mobileconnectors.appsync.AWSAppSyncClient) AddPostMutation(com.amazonaws.mobileconnectors.appsync.demo.AddPostMutation) Test(org.junit.Test)

Example 3 with AddPostMutation

use of com.amazonaws.mobileconnectors.appsync.demo.AddPostMutation in project aws-mobile-appsync-sdk-android by awslabs.

the class MultiClientInstrumentationTest method testClearCache.

@Test
public void testClearCache() throws ClearCacheException {
    AWSAppSyncClient client = AWSAppSyncClients.withAPIKEYFromAWSConfiguration();
    goOffline();
    // Add a post
    AddPostMutation.Data expected = new AddPostMutation.Data(new AddPostMutation.CreatePost("Post", "", "", "", "", "", null, null, 0));
    AddPostMutation addPostMutation = AddPostMutation.builder().input(CreatePostInput.builder().title("Learning to Live ").author("Dream Theater @ ").url("Dream Theater Station").content("No energy for anger @" + System.currentTimeMillis()).ups(1).downs(0).build()).build();
    client.mutate(addPostMutation, expected).enqueue(NoOpGraphQLCallback.instance());
    assertFalse(client.isMutationQueueEmpty());
    client.clearCaches();
    // Check Mutation Queue
    assertTrue(client.isMutationQueueEmpty());
    // Query from cache and check no data is available
    Response<AllPostsQuery.Data> listPostsResponse = Posts.list(client, AppSyncResponseFetchers.CACHE_ONLY).get("CACHE");
    assertNotNull(listPostsResponse);
    assertNull(listPostsResponse.data());
}
Also used : AWSAppSyncClient(com.amazonaws.mobileconnectors.appsync.AWSAppSyncClient) AddPostMutation(com.amazonaws.mobileconnectors.appsync.demo.AddPostMutation) Test(org.junit.Test)

Example 4 with AddPostMutation

use of com.amazonaws.mobileconnectors.appsync.demo.AddPostMutation in project aws-mobile-appsync-sdk-android by awslabs.

the class QueryInstrumentationTest method testCancelMutationWithinCallback.

/**
 * TODO: not clear why this (unusual) edge-case is worth testing.
 */
@Test
public void testCancelMutationWithinCallback() {
    AWSAppSyncClient awsAppSyncClient = AWSAppSyncClients.withIAMFromAWSConfiguration();
    final CountDownLatch add2CountDownLatch = new CountDownLatch(1);
    AddPostMutation.Data expected = new AddPostMutation.Data(new AddPostMutation.CreatePost("Post", "", "", "", "", "", null, null, 0));
    CreatePostInput createPostInput1 = CreatePostInput.builder().title("L.A. Woman").author("Doors" + System.currentTimeMillis()).url("Doors.com").content("City at night").ups(1).downs(0).build();
    AddPostMutation addPostMutation1 = AddPostMutation.builder().input(createPostInput1).build();
    final AppSyncMutationCall<AddPostMutation.Data> call1 = awsAppSyncClient.mutate(addPostMutation1, expected);
    CreatePostInput createPostInput2 = CreatePostInput.builder().title("Break On Through").author("Doors" + System.currentTimeMillis()).url("Doors.com").content("To the other side").ups(1).downs(0).build();
    AddPostMutation addPostMutation2 = AddPostMutation.builder().input(createPostInput2).build();
    final AppSyncMutationCall<AddPostMutation.Data> call2 = awsAppSyncClient.mutate(addPostMutation2, expected);
    call1.enqueue(new GraphQLCall.Callback<AddPostMutation.Data>() {

        @Override
        public void onResponse(@NonNull Response<AddPostMutation.Data> response) {
            call1.cancel();
        }

        @Override
        public void onFailure(@NonNull ApolloException e) {
            fail("OnError received for first mutation. Unexpected exception: " + e.getMessage());
        }
    });
    call2.enqueue(new GraphQLCall.Callback<AddPostMutation.Data>() {

        @Override
        public void onResponse(@NonNull Response<AddPostMutation.Data> response) {
            call2.cancel();
            add2CountDownLatch.countDown();
        }

        @Override
        public void onFailure(@NonNull ApolloException e) {
            fail("OnError received for Second mutation. Unexpected exception: " + e.getMessage());
            add2CountDownLatch.countDown();
        }
    });
    Await.latch(add2CountDownLatch, REASONABLE_WAIT_TIME_MS);
}
Also used : GraphQLCall(com.apollographql.apollo.GraphQLCall) CreatePostInput(com.amazonaws.mobileconnectors.appsync.demo.type.CreatePostInput) CountDownLatch(java.util.concurrent.CountDownLatch) AddPostMutation(com.amazonaws.mobileconnectors.appsync.demo.AddPostMutation) AWSAppSyncClient(com.amazonaws.mobileconnectors.appsync.AWSAppSyncClient) ApolloException(com.apollographql.apollo.exception.ApolloException) Test(org.junit.Test)

Aggregations

AWSAppSyncClient (com.amazonaws.mobileconnectors.appsync.AWSAppSyncClient)4 AddPostMutation (com.amazonaws.mobileconnectors.appsync.demo.AddPostMutation)4 Test (org.junit.Test)4 CreatePostInput (com.amazonaws.mobileconnectors.appsync.demo.type.CreatePostInput)1 GraphQLCall (com.apollographql.apollo.GraphQLCall)1 ApolloException (com.apollographql.apollo.exception.ApolloException)1 CountDownLatch (java.util.concurrent.CountDownLatch)1