Search in sources :

Example 1 with OnDeleteArticleSubscription

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

the class SubscriptionInstrumentationTest method testMultipleSubscriptionsWithIAM.

private static void testMultipleSubscriptionsWithIAM(SubscriptionReconnectMode subscriptionReconnectMode) {
    final boolean shouldAutomaticallyReconnect = SubscriptionReconnectMode.AUTOMATICALLY_RECONNECT.equals(subscriptionReconnectMode);
    AWSAppSyncClient awsAppSyncClient = AWSAppSyncClients.withIAMFromAWSConfiguration(shouldAutomaticallyReconnect, 0);
    // TODO: why is this looped over 3 times?
    for (int iteration = 0; iteration < 3; iteration++) {
        final String title = "Pull Me Under";
        final String author = "Dream Theater @ " + System.currentTimeMillis();
        final String url = "Dream Theater";
        final String content = "Lost in the sky @" + System.currentTimeMillis();
        // Form subscription to creations of Post models.
        AppSyncSubscriptionCall<OnCreatePostSubscription.Data> onCreatePostSubscriptionCall = awsAppSyncClient.subscribe(OnCreatePostSubscription.builder().build());
        LatchedSubscriptionCallback<OnCreatePostSubscription.Data> onCreatePostCallback = LatchedSubscriptionCallback.instance();
        onCreatePostSubscriptionCall.execute(onCreatePostCallback);
        // Form a subscription to updates of Post models.
        AppSyncSubscriptionCall<OnUpdatePostSubscription.Data> onUpdatePostSubscriptionCall = awsAppSyncClient.subscribe(OnUpdatePostSubscription.builder().build());
        LatchedSubscriptionCallback<OnUpdatePostSubscription.Data> onUpdatePostCallback = LatchedSubscriptionCallback.instance();
        onUpdatePostSubscriptionCall.execute(onUpdatePostCallback);
        // Form a subscription to deletions of Post models.
        AppSyncSubscriptionCall<OnDeletePostSubscription.Data> onDeletePostSubscriptionCall = awsAppSyncClient.subscribe(OnDeletePostSubscription.builder().build());
        LatchedSubscriptionCallback<OnDeletePostSubscription.Data> onDeletePostCallback = LatchedSubscriptionCallback.instance();
        onDeletePostSubscriptionCall.execute(onDeletePostCallback);
        // Form a subscription to creations of Articles.
        AppSyncSubscriptionCall<OnCreateArticleSubscription.Data> onCreateArticleSubscriptionCall = awsAppSyncClient.subscribe(OnCreateArticleSubscription.builder().build());
        LatchedSubscriptionCallback<OnCreateArticleSubscription.Data> onCreateArticleCallback = LatchedSubscriptionCallback.instance();
        onCreateArticleSubscriptionCall.execute(onCreateArticleCallback);
        // Form a subscription to update to Articles.
        AppSyncSubscriptionCall<OnUpdateArticleSubscription.Data> onUpdateArticleSubscriptionCall = awsAppSyncClient.subscribe(OnUpdateArticleSubscription.builder().build());
        LatchedSubscriptionCallback<OnUpdateArticleSubscription.Data> onUpdateArticleCallback = LatchedSubscriptionCallback.instance();
        onUpdateArticleSubscriptionCall.execute(onUpdateArticleCallback);
        // Form a subscription to deletions of Articles.
        OnDeleteArticleSubscription onDeleteArticleSubscription = OnDeleteArticleSubscription.builder().build();
        AppSyncSubscriptionCall<OnDeleteArticleSubscription.Data> onDeleteArticleSubscriptionCall = awsAppSyncClient.subscribe(onDeleteArticleSubscription);
        LatchedSubscriptionCallback<OnDeleteArticleSubscription.Data> onDeleteArticleCallback = LatchedSubscriptionCallback.instance();
        onDeleteArticleSubscriptionCall.execute(onDeleteArticleCallback);
        // *Assume* that all those subscriptions will be active after waiting a while ...
        Sleep.milliseconds(REASONABLE_WAIT_TIME_MS);
        Log.d(TAG, "Subscribed and setup callback handlers.");
        // Create a Post.
        Response<AddPostMutation.Data> addPostMutationResponse = Posts.add(awsAppSyncClient, title, author, url, content);
        assertNotNull(addPostMutationResponse.data());
        AddPostMutation.CreatePost createPost = addPostMutationResponse.data().createPost();
        assertNotNull(createPost);
        String postId = createPost.id();
        Log.d(TAG, "Added Post with ID = " + postId);
        // Update that same post, 5 times.
        for (int i = 0; i < 5; i++) {
            Response<UpdatePostMutation.Data> updatePostMutationResponse = Posts.update(awsAppSyncClient, postId, "Lost in the sky @" + System.currentTimeMillis());
            assertNotNull(updatePostMutationResponse);
        }
        Log.d(TAG, "Updated post five times");
        // Okay, now delete the post.
        Posts.delete(awsAppSyncClient, postId);
        Log.d(TAG, "Deleted post");
        // Validate that the mutations "worked".
        onCreatePostCallback.awaitNextSuccessfulResponse();
        onUpdatePostCallback.awaitSuccessfulResponses(5);
        onDeletePostCallback.awaitNextSuccessfulResponse();
        // Cancel all ongoing subscription calls.
        onCreatePostSubscriptionCall.cancel();
        onUpdatePostSubscriptionCall.cancel();
        onDeletePostSubscriptionCall.cancel();
        onCreateArticleSubscriptionCall.cancel();
        onUpdateArticleSubscriptionCall.cancel();
        onDeleteArticleSubscriptionCall.cancel();
        // In response to canceling the calls, the callbacks should receive
        // completion callbacks.
        onCreatePostCallback.awaitCompletion();
        onUpdatePostCallback.awaitCompletion();
        onDeletePostCallback.awaitCompletion();
        onCreateArticleCallback.awaitCompletion();
        onUpdateArticleCallback.awaitCompletion();
        onDeleteArticleCallback.awaitCompletion();
    }
}
Also used : AddPostMutation(com.amazonaws.mobileconnectors.appsync.demo.AddPostMutation) AWSAppSyncClient(com.amazonaws.mobileconnectors.appsync.AWSAppSyncClient) OnDeleteArticleSubscription(com.amazonaws.mobileconnectors.appsync.demo.OnDeleteArticleSubscription)

Aggregations

AWSAppSyncClient (com.amazonaws.mobileconnectors.appsync.AWSAppSyncClient)1 AddPostMutation (com.amazonaws.mobileconnectors.appsync.demo.AddPostMutation)1 OnDeleteArticleSubscription (com.amazonaws.mobileconnectors.appsync.demo.OnDeleteArticleSubscription)1