use of com.amplifyframework.testutils.Await in project amplify-android by aws-amplify.
the class AWSApiPluginTest method graphQlMutationGetsResponse.
/**
* It should be possible to perform a successful call to
* {@link AWSApiPlugin#mutate(GraphQLRequest, Consumer, Consumer)}.
* When the server returns a valid response, then the mutate methods should
* emit content via their value consumer.
* @throws ApiException If call to mutate(...) itself emits such an exception
* @throws JSONException On failure to arrange response JSON
*/
@Test
public void graphQlMutationGetsResponse() throws JSONException, ApiException {
HubAccumulator networkStatusObserver = HubAccumulator.create(HubChannel.API, ApiChannelEventName.API_ENDPOINT_STATUS_CHANGED, 1).start();
// Arrange a response from the "server"
String expectedName = RandomString.string();
webServer.enqueue(new MockResponse().setBody(new JSONObject().put("data", new JSONObject().put("createBlogOwner", new JSONObject().put("name", expectedName))).toString()));
// Try to perform a mutation.
BlogOwner tony = BlogOwner.builder().name(expectedName).build();
GraphQLResponse<BlogOwner> actualResponse = Await.<GraphQLResponse<BlogOwner>, ApiException>result(((onResult, onError) -> plugin.mutate(ModelMutation.create(tony), onResult, onError)));
// Assert that the expected response was received
assertEquals(expectedName, actualResponse.getData().getName());
// Verify that the expected hub event fired.
HubEvent<?> event = networkStatusObserver.awaitFirst();
assertNotNull(event);
assertTrue(event.getData() instanceof ApiEndpointStatusChangeEvent);
ApiEndpointStatusChangeEvent eventData = (ApiEndpointStatusChangeEvent) event.getData();
assertEquals(ApiEndpointStatusChangeEvent.ApiEndpointStatus.REACHABLE, eventData.getCurrentStatus());
}
use of com.amplifyframework.testutils.Await in project amplify-android by aws-amplify.
the class AWSRestOperationTest method noErrorEmittedIfOperationIsCancelled.
/**
* If the user calls {@link AWSRestOperation#cancel()}, then the operation
* will not fire any callback. This behavior is consistent with iOS's REST operation.
*/
@Test
public void noErrorEmittedIfOperationIsCancelled() {
long timeToWaitForResponse = 300L;
RestOperationRequest request = new RestOperationRequest(HttpMethod.GET, baseUrl.uri().getPath(), emptyMap(), emptyMap());
assertTimedOut(() -> Await.<RestResponse, ApiException>result(timeToWaitForResponse, (onResult, onError) -> {
AWSRestOperation operation = new AWSRestOperation(request, baseUrl.url().toString(), client, onResult, onError);
operation.start();
operation.cancel();
}));
}
use of com.amplifyframework.testutils.Await in project amplify-android by aws-amplify.
the class SynchronousMobileClient method initialize.
/**
* Initialize the client for use, in a synchronous way, by delegating to
* {@link AWSMobileClient#initialize(Context, Callback)}.
* @param context An Android Context
* @param awsConfiguration custom AWS configuration to use for initializing Mobile Client
* @return The result received by {@link AWSMobileClient#initialize(Context, Callback)}, if successful
* @throws MobileClientException A wrapped form of the error received by the async callback.
*/
@NonNull
public UserStateDetails initialize(@NonNull Context context, @NonNull AWSConfiguration awsConfiguration) throws MobileClientException {
Objects.requireNonNull(context);
Objects.requireNonNull(awsConfiguration);
final UserStateDetails userStateDetails;
try {
userStateDetails = Await.<UserStateDetails, Exception>result((onResult, onError) -> {
Callback<UserStateDetails> callback = DelegatingCallback.with(onResult, onError);
awsMobileClient.initialize(context, awsConfiguration, callback);
});
} catch (Exception initializationError) {
throw new MobileClientException("Failed to initialize Mobile Client", initializationError);
}
return Objects.requireNonNull(userStateDetails);
}
Aggregations