Search in sources :

Example 1 with AWSAppSyncClientException

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

the class MultiClientInstrumentationTest method testQueryPostsWithAllResponseFetchersAndMultipleClients.

@Test
public void testQueryPostsWithAllResponseFetchersAndMultipleClients() {
    final ResponseFetcher[] appSyncResponseFetchers = new ResponseFetcher[] { AppSyncResponseFetchers.NETWORK_FIRST, AppSyncResponseFetchers.NETWORK_ONLY, AppSyncResponseFetchers.CACHE_FIRST };
    for (ResponseFetcher appSyncResponseFetcher : appSyncResponseFetchers) {
        Log.d(TAG, "Response Fetcher: " + appSyncResponseFetcher.toString());
        Map<String, AWSAppSyncClient> appSyncClientMap = new HashMap<>();
        appSyncClientMap.put("AMAZON_COGNITO_USER_POOLS", AWSAppSyncClients.withUserPoolsFromAWSConfiguration(appSyncResponseFetcher));
        appSyncClientMap.put("AMAZON_COGNITO_USER_POOLS_2", AWSAppSyncClients.withUserPools2FromAWSConfiguration(idToken, appSyncResponseFetcher));
        for (final String clientName : appSyncClientMap.keySet()) {
            // List Posts
            final AWSAppSyncClient awsAppSyncClient = appSyncClientMap.get(clientName);
            Log.d(TAG, "AWSAppSyncClient for clientName: " + clientName + "; client: " + awsAppSyncClient);
            assertNotNull(awsAppSyncClient);
            Map<String, Response<AllPostsQuery.Data>> listPostsResponses = Posts.list(awsAppSyncClient, appSyncResponseFetcher);
            Response<AllPostsQuery.Data> allPostsResponse = listPostsResponses.get("CACHE") != null ? listPostsResponses.get("CACHE") : listPostsResponses.get("NETWORK");
            assertNotNull(allPostsResponse);
            assertNotNull(allPostsResponse.data());
            AllPostsQuery.ListPosts listPosts = allPostsResponse.data().listPosts();
            assertNotNull(listPosts);
            List<AllPostsQuery.Item> items = listPosts.items();
            assertNotNull(items);
            // Loop over the list and query each post
            String postID;
            for (AllPostsQuery.Item item : items) {
                postID = item.id();
                Map<String, Response<GetPostQuery.Data>> getPostResponses = Posts.query(awsAppSyncClient, appSyncResponseFetcher, postID);
                if (clientName.equals("API_KEY") || clientName.equals("AWS_IAM")) {
                    assertResponseForQueryPost(appSyncResponseFetcher, getPostResponses, postID);
                } else if (clientName.startsWith("AMAZON_COGNITO_USER_POOLS")) {
                    assertResponseForQueryPostWithUserPools(appSyncResponseFetcher, getPostResponses, postID);
                }
            }
        }
        // Clear the cache
        for (final String clientName : appSyncClientMap.keySet()) {
            final AWSAppSyncClient awsAppSyncClient = appSyncClientMap.get(clientName);
            assertNotNull(awsAppSyncClient);
            try {
                awsAppSyncClient.clearCaches();
            } catch (AWSAppSyncClientException e) {
                fail("Error in clearing the cache." + e);
            }
        }
    }
}
Also used : AWSAppSyncClientException(com.amazonaws.mobileconnectors.appsync.AWSAppSyncClientException) AllPostsQuery(com.amazonaws.mobileconnectors.appsync.demo.AllPostsQuery) GetPostQuery(com.amazonaws.mobileconnectors.appsync.demo.GetPostQuery) HashMap(java.util.HashMap) ResponseFetcher(com.apollographql.apollo.fetcher.ResponseFetcher) Response(com.apollographql.apollo.api.Response) AWSAppSyncClient(com.amazonaws.mobileconnectors.appsync.AWSAppSyncClient) Test(org.junit.Test)

Aggregations

AWSAppSyncClient (com.amazonaws.mobileconnectors.appsync.AWSAppSyncClient)1 AWSAppSyncClientException (com.amazonaws.mobileconnectors.appsync.AWSAppSyncClientException)1 AllPostsQuery (com.amazonaws.mobileconnectors.appsync.demo.AllPostsQuery)1 GetPostQuery (com.amazonaws.mobileconnectors.appsync.demo.GetPostQuery)1 Response (com.apollographql.apollo.api.Response)1 ResponseFetcher (com.apollographql.apollo.fetcher.ResponseFetcher)1 HashMap (java.util.HashMap)1 Test (org.junit.Test)1