Search in sources :

Example 56 with BlogOwner

use of com.amplifyframework.testmodels.commentsblog.BlogOwner in project amplify-android by aws-amplify.

the class AWSApiPluginTest method headerInterceptorsAreConfigured.

/**
 * Validates that the plugin adds custom headers into the outgoing OkHttp request.
 * @throws ApiException Thrown from the query() call.
 * @throws InterruptedException Possible thrown from takeRequest()
 */
@Test
public void headerInterceptorsAreConfigured() throws ApiException, InterruptedException {
    // Arrange some response. This isn't the point of the test,
    // but it keeps the mock web server from freezing up.
    webServer.enqueue(new MockResponse().setBody(Resources.readAsString("blog-owners-query-results.json")));
    // Fire off a request
    Await.<GraphQLResponse<PaginatedResult<BlogOwner>>, ApiException>result((onResult, onError) -> plugin.query(ModelQuery.list(BlogOwner.class), onResult, onError));
    RecordedRequest recordedRequest = webServer.takeRequest(5, TimeUnit.MILLISECONDS);
    assertNotNull(recordedRequest);
    assertEquals("specialValue", recordedRequest.getHeader("specialKey"));
}
Also used : RecordedRequest(okhttp3.mockwebserver.RecordedRequest) MockResponse(okhttp3.mockwebserver.MockResponse) GraphQLResponse(com.amplifyframework.api.graphql.GraphQLResponse) BlogOwner(com.amplifyframework.testmodels.commentsblog.BlogOwner) ApiException(com.amplifyframework.api.ApiException) Test(org.junit.Test)

Example 57 with BlogOwner

use of com.amplifyframework.testmodels.commentsblog.BlogOwner in project amplify-android by aws-amplify.

the class AWSApiPluginTest method requestUsesIamForAuth.

/**
 * Ensure the auth mode used for the request is AWS_IAM. We verify this by
 * @throws AmplifyException Not expected.
 * @throws InterruptedException Not expected.
 */
@Test
public void requestUsesIamForAuth() throws AmplifyException, InterruptedException {
    webServer.enqueue(new MockResponse().setBody(Resources.readAsString("blog-owners-query-results.json")));
    AppSyncGraphQLRequest<PaginatedResult<BlogOwner>> appSyncGraphQLRequest = createQueryRequestWithAuthMode(BlogOwner.class, AuthorizationType.AWS_IAM);
    GraphQLResponse<PaginatedResult<BlogOwner>> actualResponse = Await.<GraphQLResponse<PaginatedResult<BlogOwner>>, ApiException>result((onResult, onError) -> plugin.query(appSyncGraphQLRequest, onResult, onError));
    RecordedRequest recordedRequest = webServer.takeRequest();
    assertNull(recordedRequest.getHeader("x-api-key"));
    assertNotNull(recordedRequest.getHeader("authorization"));
    assertTrue(recordedRequest.getHeader("authorization").startsWith("AWS4-HMAC-SHA256"));
    assertEquals(Arrays.asList("Curly", "Moe", "Larry"), Observable.fromIterable(actualResponse.getData()).map(BlogOwner::getName).toList().blockingGet());
}
Also used : RecordedRequest(okhttp3.mockwebserver.RecordedRequest) MockResponse(okhttp3.mockwebserver.MockResponse) PaginatedResult(com.amplifyframework.api.graphql.PaginatedResult) GraphQLResponse(com.amplifyframework.api.graphql.GraphQLResponse) BlogOwner(com.amplifyframework.testmodels.commentsblog.BlogOwner) ApiException(com.amplifyframework.api.ApiException) Test(org.junit.Test)

Example 58 with BlogOwner

use of com.amplifyframework.testmodels.commentsblog.BlogOwner in project amplify-android by aws-amplify.

the class AWSApiPluginTest method requestUsesCognitoForAuth.

/**
 * If the auth mode is set for the individual request, ensure that the resulting request
 * to AppSync has the correct auth header.
 * @throws AmplifyException Not expected.
 * @throws InterruptedException Not expected.
 */
@Test
public void requestUsesCognitoForAuth() throws AmplifyException, InterruptedException {
    webServer.enqueue(new MockResponse().setBody(Resources.readAsString("blog-owners-query-results.json")));
    AppSyncGraphQLRequest<PaginatedResult<BlogOwner>> appSyncGraphQLRequest = createQueryRequestWithAuthMode(BlogOwner.class, AuthorizationType.AMAZON_COGNITO_USER_POOLS);
    GraphQLResponse<PaginatedResult<BlogOwner>> actualResponse = Await.<GraphQLResponse<PaginatedResult<BlogOwner>>, ApiException>result((onResult, onError) -> plugin.query(appSyncGraphQLRequest, onResult, onError));
    RecordedRequest recordedRequest = webServer.takeRequest();
    assertNull(recordedRequest.getHeader("x-api-key"));
    assertNotNull(recordedRequest.getHeader("authorization"));
    assertEquals("FAKE_TOKEN", recordedRequest.getHeader("authorization"));
    assertEquals(Arrays.asList("Curly", "Moe", "Larry"), Observable.fromIterable(actualResponse.getData()).map(BlogOwner::getName).toList().blockingGet());
}
Also used : RecordedRequest(okhttp3.mockwebserver.RecordedRequest) MockResponse(okhttp3.mockwebserver.MockResponse) PaginatedResult(com.amplifyframework.api.graphql.PaginatedResult) GraphQLResponse(com.amplifyframework.api.graphql.GraphQLResponse) BlogOwner(com.amplifyframework.testmodels.commentsblog.BlogOwner) ApiException(com.amplifyframework.api.ApiException) Test(org.junit.Test)

Example 59 with BlogOwner

use of com.amplifyframework.testmodels.commentsblog.BlogOwner 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 60 with BlogOwner

use of com.amplifyframework.testmodels.commentsblog.BlogOwner in project amplify-android by aws-amplify.

the class SQLiteStorageAdapterClearTest method clearDeletesAndRecreatesDatabase.

/**
 * Save a record to the database and verify it was saved.
 * Then call clear and verify that the database file is re-created
 * and is writable.
 * @throws DataStoreException bubbles up exceptions thrown from the adapter
 * @throws InterruptedException If interrupted while test observer awaits terminal result
 */
@Test
public void clearDeletesAndRecreatesDatabase() throws DataStoreException, InterruptedException {
    assertDbFileExists();
    assertEquals(0, fileObserver.createFileEventCount);
    assertEquals(0, fileObserver.deleteFileEventCount);
    BlogOwner blogger1 = createBlogger("Dummy Blogger Sr.");
    BlogOwner blogger2 = createBlogger("Dummy Blogger Jr.");
    // Save a record and check if it's there
    adapter.save(blogger1);
    assertRecordIsInDb(blogger1);
    // Verify observer is still alive
    assertFalse(observer.isDisposed());
    assertObserverReceivedRecord(blogger1);
    adapter.clear();
    // Make sure file was deleted and re-created
    assertEquals(1, fileObserver.createFileEventCount);
    assertEquals(1, fileObserver.deleteFileEventCount);
    assertDbFileExists();
    // Verify observer is still alive
    assertFalse(observer.isDisposed());
    // Make sure the new file is writable
    adapter.save(blogger2);
    // Check the new record is in the database
    // and the old record is not.
    assertRecordIsInDb(blogger2);
    assertRecordIsNotInDb(blogger1);
    assertObserverReceivedRecord(blogger2);
    // Terminate the adapter
    adapter.terminate();
    // Verify observer was disposed.
    observer.assertComplete();
    observer.await(TIMEOUT_MS, TimeUnit.MILLISECONDS);
}
Also used : BlogOwner(com.amplifyframework.testmodels.commentsblog.BlogOwner) Test(org.junit.Test)

Aggregations

BlogOwner (com.amplifyframework.testmodels.commentsblog.BlogOwner)150 Test (org.junit.Test)146 DataStoreException (com.amplifyframework.datastore.DataStoreException)35 Blog (com.amplifyframework.testmodels.commentsblog.Blog)32 ModelSchema (com.amplifyframework.core.model.ModelSchema)31 Post (com.amplifyframework.testmodels.commentsblog.Post)31 ArrayList (java.util.ArrayList)29 QueryPredicate (com.amplifyframework.core.model.query.predicate.QueryPredicate)25 Assert.assertEquals (org.junit.Assert.assertEquals)25 ModelMetadata (com.amplifyframework.datastore.appsync.ModelMetadata)24 Collections (java.util.Collections)24 HashSet (java.util.HashSet)23 List (java.util.List)23 PostStatus (com.amplifyframework.testmodels.commentsblog.PostStatus)22 HubAccumulator (com.amplifyframework.testutils.HubAccumulator)22 Arrays (java.util.Arrays)22 TimeUnit (java.util.concurrent.TimeUnit)22 Consumer (com.amplifyframework.core.Consumer)21 ModelWithMetadata (com.amplifyframework.datastore.appsync.ModelWithMetadata)21 Before (org.junit.Before)21