Search in sources :

Example 11 with AWSAppSyncClient

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

the class SubscriptionInstrumentationTest method testAddSubscriptionWithApiKeyAuthModel.

private static void testAddSubscriptionWithApiKeyAuthModel(SubscriptionReconnectMode subscriptionReconnectMode) {
    boolean shouldAutomaticallyReconnect = SubscriptionReconnectMode.AUTOMATICALLY_RECONNECT.equals(subscriptionReconnectMode);
    AWSAppSyncClient awsAppSyncClient = AWSAppSyncClients.withAPIKEYFromAWSConfiguration(shouldAutomaticallyReconnect, 0);
    final String title = "Alabama Song [Whisky Bar]";
    final String author = "Doors @ " + System.currentTimeMillis();
    final String url = "The Doors";
    final String content = "Well, show me the way, to the next whisky bar @" + System.currentTimeMillis();
    // Subscribe to creations of Post.
    AppSyncSubscriptionCall<OnCreatePostSubscription.Data> onCreatePostSubscriptionCall = awsAppSyncClient.subscribe(OnCreatePostSubscription.builder().build());
    LatchedSubscriptionCallback<OnCreatePostSubscription.Data> onCreatePostCallback = LatchedSubscriptionCallback.instance();
    onCreatePostSubscriptionCall.execute(onCreatePostCallback);
    Log.d(TAG, "Subscribed and setup callback handler.");
    // Sleep for a while to make sure the subscription goes through
    Sleep.milliseconds(REASONABLE_WAIT_TIME_MS);
    Posts.add(awsAppSyncClient, title, author, url, content);
    Log.d(TAG, "Added Post");
    // Did the post show up on the subscription?
    onCreatePostCallback.awaitNextSuccessfulResponse();
    // Cancel the subscription call, and expect a completion callback.
    onCreatePostSubscriptionCall.cancel();
    onCreatePostCallback.awaitCompletion();
}
Also used : AWSAppSyncClient(com.amazonaws.mobileconnectors.appsync.AWSAppSyncClient)

Example 12 with AWSAppSyncClient

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

the class ComplexObjectsInstrumentationTests method listArticles.

Map<String, Response<AllArticlesQuery.Data>> listArticles(AWSAppSyncClient awsAppSyncClient, ResponseFetcher responseFetcher) {
    final CountDownLatch cacheLatch = new CountDownLatch(1);
    final CountDownLatch networkLatch = new CountDownLatch(1);
    final Map<String, Response<AllArticlesQuery.Data>> responses = new HashMap<>();
    awsAppSyncClient.query(AllArticlesQuery.builder().build()).responseFetcher(responseFetcher).enqueue(DelegatingGraphQLCallback.to(response -> {
        if (response.fromCache()) {
            responses.put("CACHE", response);
            cacheLatch.countDown();
        } else {
            responses.put("NETWORK", response);
            networkLatch.countDown();
        }
    }, failure -> {
        cacheLatch.countDown();
        networkLatch.countDown();
    }));
    if (AppSyncResponseFetchers.NETWORK_ONLY.equals(responseFetcher) || AppSyncResponseFetchers.CACHE_AND_NETWORK.equals(responseFetcher)) {
        Await.latch(networkLatch);
        assertNotNull(responses.get("NETWORK"));
    }
    if (AppSyncResponseFetchers.CACHE_ONLY.equals(responseFetcher) || AppSyncResponseFetchers.CACHE_AND_NETWORK.equals(responseFetcher)) {
        Await.latch(cacheLatch);
        assertNotNull(responses.get("CACHE"));
    }
    return responses;
}
Also used : Response(com.apollographql.apollo.api.Response) AndroidJUnit4(androidx.test.runner.AndroidJUnit4) BeforeClass(org.junit.BeforeClass) RunWith(org.junit.runner.RunWith) AppSyncResponseFetchers(com.amazonaws.mobileconnectors.appsync.fetcher.AppSyncResponseFetchers) CustomCognitoUserPool(com.amazonaws.mobileconnectors.appsync.identity.CustomCognitoUserPool) HashMap(java.util.HashMap) CreateArticleMutation(com.amazonaws.mobileconnectors.appsync.demo.CreateArticleMutation) AllArticlesQuery(com.amazonaws.mobileconnectors.appsync.demo.AllArticlesQuery) UpdateArticleInput(com.amazonaws.mobileconnectors.appsync.demo.type.UpdateArticleInput) DelegatingGraphQLCallback(com.amazonaws.mobileconnectors.appsync.client.DelegatingGraphQLCallback) Map(java.util.Map) DataFile(com.amazonaws.mobileconnectors.appsync.util.DataFile) AWSAppSyncClients(com.amazonaws.mobileconnectors.appsync.client.AWSAppSyncClients) Assert.assertNotNull(org.junit.Assert.assertNotNull) Test(org.junit.Test) Await(com.amazonaws.mobileconnectors.appsync.util.Await) LatchedGraphQLCallback(com.amazonaws.mobileconnectors.appsync.client.LatchedGraphQLCallback) CountDownLatch(java.util.concurrent.CountDownLatch) UpdateArticleMutation(com.amazonaws.mobileconnectors.appsync.demo.UpdateArticleMutation) Response(com.apollographql.apollo.api.Response) ResponseFetcher(com.apollographql.apollo.fetcher.ResponseFetcher) S3ObjectInput(com.amazonaws.mobileconnectors.appsync.demo.type.S3ObjectInput) AWSAppSyncClient(com.amazonaws.mobileconnectors.appsync.AWSAppSyncClient) CreateArticleInput(com.amazonaws.mobileconnectors.appsync.demo.type.CreateArticleInput) InternetConnectivity.goOnline(com.amazonaws.mobileconnectors.appsync.util.InternetConnectivity.goOnline) Assert.assertEquals(org.junit.Assert.assertEquals) HashMap(java.util.HashMap) AllArticlesQuery(com.amazonaws.mobileconnectors.appsync.demo.AllArticlesQuery) CountDownLatch(java.util.concurrent.CountDownLatch)

Example 13 with AWSAppSyncClient

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

the class ConflictManagementInstrumentationTest method beforeAnyTests.

/**
 * We will do one add and 5 updates that try out the various paths of conflict
 * management. This function will exit once the add is completed and the
 * updates are queued, but not executed. This has the effect of populating
 * the persistent queue and exercising the persistent mutation execution flow
 * when one of the tests in this suite starts.
 */
@BeforeClass
public static void beforeAnyTests() {
    goOnline();
    String title = "Minstrel in the Gallery";
    String author = "Tull";
    CustomCognitoUserPool.setup();
    AWSAppSyncClient awsAppSyncClient = AWSAppSyncClients.withAPIKEYFromAWSConfiguration();
    LatchedGraphQLCallback<CreateArticleMutation.Data> createArticleCallback = LatchedGraphQLCallback.instance();
    awsAppSyncClient.mutate(CreateArticleMutation.builder().input(CreateArticleInput.builder().title(title).author(author).version(100).build()).build(), new CreateArticleMutation.Data(new CreateArticleMutation.CreateArticle("Article", "", "", "", 100, null, null))).enqueue(createArticleCallback);
    Response<CreateArticleMutation.Data> response = createArticleCallback.awaitSuccessfulResponse();
    assertNotNull(response);
    assertNotNull(response.data());
    CreateArticleMutation.CreateArticle createArticle = response.data().createArticle();
    assertNotNull(createArticle);
    assertNotNull(createArticle.id());
    String[] titles = { title + System.currentTimeMillis(), "RESOLVE_CONFLICT_INCORRECTLY", title + System.currentTimeMillis(), "ALWAYS DISCARD", title + System.currentTimeMillis() };
    for (String string : titles) {
        awsAppSyncClient.mutate(UpdateArticleMutation.builder().input(UpdateArticleInput.builder().id(createArticle.id()).title(string).author(author).expectedVersion(1).build()).build(), new UpdateArticleMutation.Data(new UpdateArticleMutation.UpdateArticle("Article", "", "", "", 1, null, null))).enqueue(NoOpGraphQLCallback.instance());
    }
}
Also used : CreateArticleMutation(com.amazonaws.mobileconnectors.appsync.demo.CreateArticleMutation) UpdateArticleMutation(com.amazonaws.mobileconnectors.appsync.demo.UpdateArticleMutation) AWSAppSyncClient(com.amazonaws.mobileconnectors.appsync.AWSAppSyncClient) BeforeClass(org.junit.BeforeClass)

Example 14 with AWSAppSyncClient

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

the class ConflictManagementInstrumentationTest method testAddUpdateArticleNoConflict.

@Test
public void testAddUpdateArticleNoConflict() {
    final AWSAppSyncClient awsAppSyncClient = AWSAppSyncClients.withAPIKEYFromAWSConfiguration();
    String title = "Thick as a brick";
    String author = "Tull" + System.currentTimeMillis();
    String articleID = addArticle(awsAppSyncClient, title, author, 1);
    updateArticle(awsAppSyncClient, articleID, title, author + System.currentTimeMillis(), 1, 2);
}
Also used : AWSAppSyncClient(com.amazonaws.mobileconnectors.appsync.AWSAppSyncClient) Test(org.junit.Test)

Example 15 with AWSAppSyncClient

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

the class MultiClientInstrumentationTest method testSyncOnlyBaseQuery.

@Test
public void testSyncOnlyBaseQuery() {
    List<AWSAppSyncClient> clients = Arrays.asList(AWSAppSyncClients.withAPIKEYFromAWSConfiguration(), AWSAppSyncClients.withIAMFromAWSConfiguration(), AWSAppSyncClients.withUserPoolsFromAWSConfiguration());
    for (AWSAppSyncClient client : clients) {
        Query<AllPostsQuery.Data, AllPostsQuery.Data, Variables> baseQuery = AllPostsQuery.builder().build();
        LatchedGraphQLCallback<Query.Data> baseQueryCallback = LatchedGraphQLCallback.instance();
        Cancelable handle = client.sync(baseQuery, baseQueryCallback, 0);
        assertFalse(handle.isCanceled());
        baseQueryCallback.awaitSuccessfulResponse();
        handle.cancel();
        assertTrue(handle.isCanceled());
        // This should be a No-op. Test to make sure.
        handle.cancel();
        assertTrue(handle.isCanceled());
    }
}
Also used : Variables(com.apollographql.apollo.api.Operation.Variables) 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