Search in sources :

Example 1 with Await

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());
}
Also used : Arrays(java.util.Arrays) AmplifyException(com.amplifyframework.AmplifyException) ApplicationProvider(androidx.test.core.app.ApplicationProvider) ApiChannelEventName(com.amplifyframework.api.events.ApiChannelEventName) JSONException(org.json.JSONException) JSONObject(org.json.JSONObject) After(org.junit.After) Map(java.util.Map) MockWebServer(okhttp3.mockwebserver.MockWebServer) GraphQLResponse(com.amplifyframework.api.graphql.GraphQLResponse) AWSCredentials(com.amazonaws.auth.AWSCredentials) ResponseBody(okhttp3.ResponseBody) HubEvent(com.amplifyframework.hub.HubEvent) Request(okhttp3.Request) HubChannel(com.amplifyframework.hub.HubChannel) RecordedRequest(okhttp3.mockwebserver.RecordedRequest) ModelPagination(com.amplifyframework.api.graphql.model.ModelPagination) RobolectricTestRunner(org.robolectric.RobolectricTestRunner) Type(java.lang.reflect.Type) Await(com.amplifyframework.testutils.Await) RandomString(com.amplifyframework.testutils.random.RandomString) ModelQuery(com.amplifyframework.api.graphql.model.ModelQuery) HttpUrl(okhttp3.HttpUrl) MockResponse(okhttp3.mockwebserver.MockResponse) GraphQLRequest(com.amplifyframework.api.graphql.GraphQLRequest) RunWith(org.junit.runner.RunWith) Resources(com.amplifyframework.testutils.Resources) HubAccumulator(com.amplifyframework.testutils.HubAccumulator) ApiException(com.amplifyframework.api.ApiException) Consumer(com.amplifyframework.core.Consumer) TypeMaker(com.amplifyframework.util.TypeMaker) Observable(io.reactivex.rxjava3.core.Observable) ApiEndpointStatusChangeEvent(com.amplifyframework.api.events.ApiEndpointStatusChangeEvent) AWSCredentialsProvider(com.amazonaws.auth.AWSCredentialsProvider) Response(okhttp3.Response) CognitoUserPoolsAuthProvider(com.amplifyframework.api.aws.sigv4.CognitoUserPoolsAuthProvider) PaginatedResult(com.amplifyframework.api.graphql.PaginatedResult) Before(org.junit.Before) BasicAWSCredentials(com.amazonaws.auth.BasicAWSCredentials) Assert.assertNotNull(org.junit.Assert.assertNotNull) BlogOwner(com.amplifyframework.testmodels.commentsblog.BlogOwner) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) IOException(java.io.IOException) QueryType(com.amplifyframework.api.graphql.QueryType) TimeUnit(java.util.concurrent.TimeUnit) OkHttpClient(okhttp3.OkHttpClient) Assert.assertNull(org.junit.Assert.assertNull) ModelMutation(com.amplifyframework.api.graphql.model.ModelMutation) Assert.assertEquals(org.junit.Assert.assertEquals) MockResponse(okhttp3.mockwebserver.MockResponse) JSONObject(org.json.JSONObject) ApiEndpointStatusChangeEvent(com.amplifyframework.api.events.ApiEndpointStatusChangeEvent) GraphQLResponse(com.amplifyframework.api.graphql.GraphQLResponse) BlogOwner(com.amplifyframework.testmodels.commentsblog.BlogOwner) HubAccumulator(com.amplifyframework.testutils.HubAccumulator) RandomString(com.amplifyframework.testutils.random.RandomString) ApiException(com.amplifyframework.api.ApiException) Test(org.junit.Test)

Example 2 with Await

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();
    }));
}
Also used : Collections.emptyMap(java.util.Collections.emptyMap) Assert.assertThrows(org.junit.Assert.assertThrows) RunWith(org.junit.runner.RunWith) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) IOException(java.io.IOException) HashMap(java.util.HashMap) Callable(java.util.concurrent.Callable) ApiException(com.amplifyframework.api.ApiException) RestOperationRequest(com.amplifyframework.api.rest.RestOperationRequest) RestResponse(com.amplifyframework.api.rest.RestResponse) RobolectricTestRunner(org.robolectric.RobolectricTestRunner) HttpMethod(com.amplifyframework.api.rest.HttpMethod) JSONException(org.json.JSONException) JSONObject(org.json.JSONObject) OkHttpClient(okhttp3.OkHttpClient) Await(com.amplifyframework.testutils.Await) After(org.junit.After) Map(java.util.Map) MockWebServer(okhttp3.mockwebserver.MockWebServer) HttpUrl(okhttp3.HttpUrl) MockResponse(okhttp3.mockwebserver.MockResponse) Assert.assertEquals(org.junit.Assert.assertEquals) Before(org.junit.Before) RestOperationRequest(com.amplifyframework.api.rest.RestOperationRequest) Test(org.junit.Test)

Example 3 with Await

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);
}
Also used : Objects(java.util.Objects) Context(android.content.Context) Consumer(com.amplifyframework.core.Consumer) Nullable(androidx.annotation.Nullable) Callback(com.amazonaws.mobile.client.Callback) NonNull(androidx.annotation.NonNull) Await(com.amplifyframework.testutils.Await) UserStateDetails(com.amazonaws.mobile.client.UserStateDetails) SignInResult(com.amazonaws.mobile.client.results.SignInResult) AWSMobileClient(com.amazonaws.mobile.client.AWSMobileClient) ApplicationProvider(androidx.test.core.app.ApplicationProvider) AWSConfiguration(com.amazonaws.mobile.config.AWSConfiguration) Callback(com.amazonaws.mobile.client.Callback) UserStateDetails(com.amazonaws.mobile.client.UserStateDetails) NonNull(androidx.annotation.NonNull)

Aggregations

Await (com.amplifyframework.testutils.Await)3 ApplicationProvider (androidx.test.core.app.ApplicationProvider)2 ApiException (com.amplifyframework.api.ApiException)2 Consumer (com.amplifyframework.core.Consumer)2 IOException (java.io.IOException)2 Map (java.util.Map)2 HttpUrl (okhttp3.HttpUrl)2 OkHttpClient (okhttp3.OkHttpClient)2 MockResponse (okhttp3.mockwebserver.MockResponse)2 MockWebServer (okhttp3.mockwebserver.MockWebServer)2 JSONException (org.json.JSONException)2 JSONObject (org.json.JSONObject)2 After (org.junit.After)2 Assert.assertEquals (org.junit.Assert.assertEquals)2 Assert.assertTrue (org.junit.Assert.assertTrue)2 Before (org.junit.Before)2 Test (org.junit.Test)2 RunWith (org.junit.runner.RunWith)2 RobolectricTestRunner (org.robolectric.RobolectricTestRunner)2 Context (android.content.Context)1