use of com.amazonaws.mobileconnectors.appsync.AWSAppSyncClient in project aws-mobile-appsync-sdk-android by awslabs.
the class QueryInstrumentationTest method testBaseAndDeltaSyncQuery.
@Test
public void testBaseAndDeltaSyncQuery() {
AWSAppSyncClient awsAppSyncClient = AWSAppSyncClients.withIAMFromAWSConfiguration();
AllPostsQuery baseQuery = AllPostsQuery.builder().build();
LatchedGraphQLCallback<Operation.Data> baseQueryCallback = LatchedGraphQLCallback.instance();
AllPostsQuery deltaQuery = AllPostsQuery.builder().build();
LatchedGraphQLCallback<Operation.Data> deltaQueryCallback = LatchedGraphQLCallback.instance();
// First, a base sync happens
Cancelable cancelable = awsAppSyncClient.sync(baseQuery, baseQueryCallback, deltaQuery, deltaQueryCallback, TimeUnit.HOURS.toSeconds(1));
assertFalse(cancelable.isCanceled());
baseQueryCallback.awaitSuccessfulResponse();
// Next, a delta sync, since the refresh interval isn't over.
// deltaQueryCallback.awaitResponse(); This does not appear to be working.
cancelable.cancel();
assertTrue(cancelable.isCanceled());
// This should be a No op. Test to make sure that there are no unintended side effects
cancelable.cancel();
assertTrue(cancelable.isCanceled());
}
use of com.amazonaws.mobileconnectors.appsync.AWSAppSyncClient 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());
}
use of com.amazonaws.mobileconnectors.appsync.AWSAppSyncClient in project aws-mobile-appsync-sdk-android by awslabs.
the class QueryInstrumentationTest method testUpdateWithInvalidID.
@Test
public void testUpdateWithInvalidID() {
AWSAppSyncClient awsAppSyncClient = AWSAppSyncClients.withIAMFromAWSConfiguration();
// Try to update a Post with a Fake ID
final String updatedContent = "New content coming up @" + System.currentTimeMillis();
final String randomID = UUID.randomUUID().toString();
Response<UpdatePostMutation.Data> updatePostMutationResponse = Posts.update(awsAppSyncClient, randomID, updatedContent);
assertNotNull(updatePostMutationResponse);
UpdatePostMutation.Data data = updatePostMutationResponse.data();
assertNotNull(data);
assertNull(data.updatePost());
assertNotNull(updatePostMutationResponse.errors());
Error error = updatePostMutationResponse.errors().get(0);
assertNotNull(error);
assertNotNull(error.message());
assertTrue(error.message().contains("The conditional request failed"));
}
use of com.amazonaws.mobileconnectors.appsync.AWSAppSyncClient in project aws-mobile-appsync-sdk-android by awslabs.
the class SubscriptionInstrumentationTest method testAddSubscriptionWithIAMAuthModel.
private static void testAddSubscriptionWithIAMAuthModel(boolean subscriptionAutoReconnect) {
AWSAppSyncClient awsAppSyncClient = AWSAppSyncClients.withIAMFromAWSConfiguration(subscriptionAutoReconnect, 0);
final String title = "Alabama Song [Whisky Bar]";
final String author = "Doors @ " + System.currentTimeMillis();
final String url = "The Doors";
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);
// Create a post.
String firstPostContent = "Well, show me the way, to the next whisky bar @" + System.currentTimeMillis();
Posts.add(awsAppSyncClient, title, author, url, firstPostContent);
Log.d(TAG, "Added Post");
// Did it show up on the subscription?
onCreatePostCallback.awaitNextSuccessfulResponse();
// Close the subscription and expect a completion callback.
Log.d(TAG, "Going to cancel subscription");
onCreatePostSubscriptionCall.cancel();
onCreatePostCallback.awaitCompletion();
// Add another post. The expectation is that we will NOT get a message
// on the subscription, since we just closed it.
String secondPostContent = "Well, show me the way, to the next whisky bar @" + System.currentTimeMillis();
Posts.add(awsAppSyncClient, title, author, url, secondPostContent);
onCreatePostCallback.expectNoResponse();
}
use of com.amazonaws.mobileconnectors.appsync.AWSAppSyncClient 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();
}
}
Aggregations