Search in sources :

Example 1 with AWSAppSyncClient

use of com.amazonaws.mobileconnectors.appsync.AWSAppSyncClient 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)

Example 2 with AWSAppSyncClient

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

the class MultiClientInstrumentationTest method testCache.

@Test
public void testCache() {
    List<AWSAppSyncClient> clients = Arrays.asList(AWSAppSyncClients.withAPIKEYFromAWSConfiguration(), AWSAppSyncClients.withIAMFromAWSConfiguration(), AWSAppSyncClients.withUserPoolsFromAWSConfiguration());
    for (AWSAppSyncClient client : clients) {
        Response<AllPostsQuery.Data> allPostsResponse = Posts.list(client, AppSyncResponseFetchers.NETWORK_ONLY).get("NETWORK");
        assertNotNull(allPostsResponse);
        assertNotNull(allPostsResponse.data());
        assertNotNull(allPostsResponse.data().listPosts());
        AllPostsQuery.ListPosts listPosts = allPostsResponse.data().listPosts();
        assertNotNull(listPosts);
        List<AllPostsQuery.Item> items = listPosts.items();
        assertNotNull(items);
        String postID;
        long numPostsFromNetwork = 0;
        for (AllPostsQuery.Item item : items) {
            postID = item.id();
            Response<GetPostQuery.Data> getPostQueryResponse = Posts.query(client, AppSyncResponseFetchers.NETWORK_ONLY, postID).get("NETWORK");
            assertNotNull(getPostQueryResponse);
            assertNotNull(getPostQueryResponse.data());
            assertNotNull(getPostQueryResponse.data().getPost());
            assertEquals(postID, getPostQueryResponse.data().getPost().id());
            numPostsFromNetwork++;
        }
        long numPostsFromCache = 0;
        for (AllPostsQuery.Item item : items) {
            postID = item.id();
            Response<GetPostQuery.Data> getPostQueryResponse = Posts.query(client, AppSyncResponseFetchers.CACHE_ONLY, postID).get("CACHE");
            assertNotNull(getPostQueryResponse);
            assertNotNull(getPostQueryResponse.data());
            assertNotNull(getPostQueryResponse.data().getPost());
            assertEquals(postID, getPostQueryResponse.data().getPost().id());
            numPostsFromCache++;
        }
        assertEquals(numPostsFromNetwork, numPostsFromCache);
    }
}
Also used : AWSAppSyncClient(com.amazonaws.mobileconnectors.appsync.AWSAppSyncClient) AllPostsQuery(com.amazonaws.mobileconnectors.appsync.demo.AllPostsQuery) Test(org.junit.Test)

Example 3 with AWSAppSyncClient

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

the class MultiClientInstrumentationTest method testCodeHasClientDatabasePrefixAndUseClientDatabasePrefixTrue.

@Test
public void testCodeHasClientDatabasePrefixAndUseClientDatabasePrefixTrue() throws JSONException {
    AWSConfiguration awsConfiguration = new AWSConfiguration(getTargetContext());
    JSONObject appSyncConfig = awsConfiguration.optJsonObject("AppSync");
    AWSAppSyncClient.Builder awsAppSyncClientBuilder = AWSAppSyncClient.builder().context(getTargetContext()).apiKey(new BasicAPIKeyAuthProvider(appSyncConfig.getString("ApiKey"))).serverUrl(appSyncConfig.getString("ApiUrl")).region(Regions.fromName(appSyncConfig.getString("Region"))).useClientDatabasePrefix(true).clientDatabasePrefix(appSyncConfig.getString("ClientDatabasePrefix"));
    AWSAppSyncClient awsAppSyncClient = awsAppSyncClientBuilder.build();
    AWSAppSyncClients.validateAppSyncClient(awsAppSyncClient, appSyncConfig.getString("ClientDatabasePrefix"), appSyncConfig.getString("AuthMode"));
}
Also used : AWSAppSyncClient(com.amazonaws.mobileconnectors.appsync.AWSAppSyncClient) JSONObject(org.json.JSONObject) AWSConfiguration(com.amazonaws.mobile.config.AWSConfiguration) BasicAPIKeyAuthProvider(com.amazonaws.mobileconnectors.appsync.sigv4.BasicAPIKeyAuthProvider) Test(org.junit.Test)

Example 4 with AWSAppSyncClient

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

the class MultiClientInstrumentationTest method testCodeHasClientDatabasePrefixAndUseClientDatabasePrefixFalse.

@Test
public void testCodeHasClientDatabasePrefixAndUseClientDatabasePrefixFalse() throws JSONException {
    AWSConfiguration awsConfiguration = new AWSConfiguration(getTargetContext());
    JSONObject appSyncConfig = awsConfiguration.optJsonObject("AppSync");
    AWSAppSyncClient awsAppSyncClient = AWSAppSyncClient.builder().context(getTargetContext()).apiKey(new BasicAPIKeyAuthProvider(appSyncConfig.getString("ApiKey"))).serverUrl(appSyncConfig.getString("ApiUrl")).region(Regions.fromName(appSyncConfig.getString("Region"))).useClientDatabasePrefix(false).clientDatabasePrefix(appSyncConfig.getString("ClientDatabasePrefix")).build();
    AWSAppSyncClients.validateAppSyncClient(awsAppSyncClient, null, null);
}
Also used : AWSAppSyncClient(com.amazonaws.mobileconnectors.appsync.AWSAppSyncClient) JSONObject(org.json.JSONObject) AWSConfiguration(com.amazonaws.mobile.config.AWSConfiguration) BasicAPIKeyAuthProvider(com.amazonaws.mobileconnectors.appsync.sigv4.BasicAPIKeyAuthProvider) Test(org.junit.Test)

Example 5 with AWSAppSyncClient

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

the class QueryInstrumentationTest method testBaseSyncQuery.

@Test
public void testBaseSyncQuery() {
    AWSAppSyncClient awsAppSyncClient = AWSAppSyncClients.withIAMFromAWSConfiguration();
    // Perform a base query and await success.
    LatchedGraphQLCallback<Operation.Data> baseQueryCallback = LatchedGraphQLCallback.instance();
    Cancelable syncCancelable = awsAppSyncClient.sync(AllPostsQuery.builder().build(), baseQueryCallback, 0);
    assertFalse(syncCancelable.isCanceled());
    baseQueryCallback.awaitResponse();
    // Cancel the query. TODO: it just completed with success, so this probably does nothing.
    syncCancelable.cancel();
    assertTrue(syncCancelable.isCanceled());
    // Canceling twice should land things in the same place.
    syncCancelable.cancel();
    assertTrue(syncCancelable.isCanceled());
}
Also used : AWSAppSyncClient(com.amazonaws.mobileconnectors.appsync.AWSAppSyncClient) Cancelable(com.apollographql.apollo.internal.util.Cancelable) Test(org.junit.Test)

Aggregations

AWSAppSyncClient (com.amazonaws.mobileconnectors.appsync.AWSAppSyncClient)28 Test (org.junit.Test)19 AddPostMutation (com.amazonaws.mobileconnectors.appsync.demo.AddPostMutation)8 AWSConfiguration (com.amazonaws.mobile.config.AWSConfiguration)6 AllPostsQuery (com.amazonaws.mobileconnectors.appsync.demo.AllPostsQuery)6 Response (com.apollographql.apollo.api.Response)5 GetPostQuery (com.amazonaws.mobileconnectors.appsync.demo.GetPostQuery)4 ResponseFetcher (com.apollographql.apollo.fetcher.ResponseFetcher)4 HashMap (java.util.HashMap)4 CountDownLatch (java.util.concurrent.CountDownLatch)4 JSONObject (org.json.JSONObject)4 DelegatingGraphQLCallback (com.amazonaws.mobileconnectors.appsync.client.DelegatingGraphQLCallback)3 LatchedGraphQLCallback (com.amazonaws.mobileconnectors.appsync.client.LatchedGraphQLCallback)3 UpdatePostMutation (com.amazonaws.mobileconnectors.appsync.demo.UpdatePostMutation)3 CreatePostInput (com.amazonaws.mobileconnectors.appsync.demo.type.CreatePostInput)3 AppSyncResponseFetchers (com.amazonaws.mobileconnectors.appsync.fetcher.AppSyncResponseFetchers)3 BasicAPIKeyAuthProvider (com.amazonaws.mobileconnectors.appsync.sigv4.BasicAPIKeyAuthProvider)3 Await (com.amazonaws.mobileconnectors.appsync.util.Await)3 Cancelable (com.apollographql.apollo.internal.util.Cancelable)3 Map (java.util.Map)3