Search in sources :

Example 46 with BlogOwner

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

the class AppSyncConflictUnhandledErrorFactoryTest method createUnhandledConflictError.

/**
 * Validates that the {@link AppSyncConflictUnhandledErrorFactory} is capable
 * of creating {@link AppSyncConflictUnhandledError}s from {@link ModelWithMetadata}.
 */
@Test
public void createUnhandledConflictError() {
    BlogOwner model = BlogOwner.builder().name("Blogger Tony").build();
    Temporal.Timestamp lastChangedAt = new Temporal.Timestamp(1602732606L, TimeUnit.SECONDS);
    ModelMetadata metadata = new ModelMetadata(model.getId(), true, 6, lastChangedAt);
    ModelWithMetadata<BlogOwner> serverData = new ModelWithMetadata<>(model, metadata);
    AppSyncConflictUnhandledError<BlogOwner> error = AppSyncConflictUnhandledErrorFactory.createUnhandledConflictError(serverData);
    assertEquals(serverData, error.getServerVersion());
}
Also used : Temporal(com.amplifyframework.core.model.temporal.Temporal) BlogOwner(com.amplifyframework.testmodels.commentsblog.BlogOwner) Test(org.junit.Test)

Example 47 with BlogOwner

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

the class AppSyncMockingTest method mockFailureForSync.

/**
 * When mockFailure() is called on the SyncConfigurator, the AppSync mock
 * will emit the provided failure.
 * @throws DataStoreException On failure to get a SyncConfigurator via sync()
 */
@Test
public void mockFailureForSync() throws DataStoreException {
    DataStoreException failure = new DataStoreException("Foo", "Bar");
    AppSyncMocking.sync(appSync).mockFailure(failure);
    GraphQLRequest<PaginatedResult<ModelWithMetadata<BlogOwner>>> request = appSync.buildSyncRequest(schema, null, 100, QueryPredicates.all());
    Single.create(emitter -> appSync.sync(request, emitter::onSuccess, emitter::onError)).test().awaitDone(TIMEOUT_SECONDS, TimeUnit.SECONDS).assertError(failure);
}
Also used : Arrays(java.util.Arrays) Single(io.reactivex.rxjava3.core.Single) SyncConfigurator(com.amplifyframework.datastore.appsync.AppSyncMocking.SyncConfigurator) AmplifyException(com.amplifyframework.AmplifyException) QueryPredicates(com.amplifyframework.core.model.query.predicate.QueryPredicates) GraphQLRequest(com.amplifyframework.api.graphql.GraphQLRequest) BlogOwner(com.amplifyframework.testmodels.commentsblog.BlogOwner) Test(org.junit.Test) Completable(io.reactivex.rxjava3.core.Completable) HashSet(java.util.HashSet) TimeUnit(java.util.concurrent.TimeUnit) DataStoreException(com.amplifyframework.datastore.DataStoreException) Consumer(com.amplifyframework.core.Consumer) Observable(io.reactivex.rxjava3.core.Observable) ModelSchema(com.amplifyframework.core.model.ModelSchema) GraphQLResponse(com.amplifyframework.api.graphql.GraphQLResponse) Temporal(com.amplifyframework.core.model.temporal.Temporal) PaginatedResult(com.amplifyframework.api.graphql.PaginatedResult) NoOpAction(com.amplifyframework.core.NoOpAction) NoOpConsumer(com.amplifyframework.core.NoOpConsumer) Collections(java.util.Collections) Before(org.junit.Before) Mockito.mock(org.mockito.Mockito.mock) DataStoreException(com.amplifyframework.datastore.DataStoreException) PaginatedResult(com.amplifyframework.api.graphql.PaginatedResult) BlogOwner(com.amplifyframework.testmodels.commentsblog.BlogOwner) Test(org.junit.Test)

Example 48 with BlogOwner

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

the class AppSyncRequestFactoryTest method validateUpdateWithPredicateGeneration.

/**
 * Checks that we're getting the expected output for a mutation with predicate.
 * @throws DataStoreException If the output does not match.
 * @throws AmplifyException On failure to parse ModelSchema from model class
 * @throws JSONException from JSONAssert.assertEquals.
 */
@Test
public void validateUpdateWithPredicateGeneration() throws AmplifyException, JSONException {
    String blogOwnerId = "926d7ee8-4ea5-40c0-8e62-3fb80b2a2edd";
    BlogOwner owner = BlogOwner.builder().name("John Doe").id(blogOwnerId).build();
    ModelSchema schema = ModelSchema.fromModelClass(BlogOwner.class);
    AppSyncGraphQLRequest<?> request = AppSyncRequestFactory.buildUpdateRequest(schema, owner, 42, BlogOwner.WEA.contains("ther"), DEFAULT_STRATEGY);
    JSONAssert.assertEquals(Resources.readAsString("update-blog-owner-with-predicate.txt"), request.getContent(), true);
}
Also used : ModelSchema(com.amplifyframework.core.model.ModelSchema) BlogOwner(com.amplifyframework.testmodels.commentsblog.BlogOwner) Test(org.junit.Test)

Example 49 with BlogOwner

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

the class AppSyncClientTest method validateBaseSyncQueryGen.

/**
 * Validates the construction of a base-sync query.
 * @throws JSONException On bad request JSON found in API category call
 * @throws DataStoreException If no valid response returned from AppSync endpoint during sync
 * @throws AmplifyException On failure to arrange model schema
 */
@Test
public void validateBaseSyncQueryGen() throws JSONException, AmplifyException {
    ModelSchema schema = ModelSchema.fromModelClass(BlogOwner.class);
    Await.result((Consumer<GraphQLResponse<PaginatedResult<ModelWithMetadata<BlogOwner>>>> onResult, Consumer<DataStoreException> onError) -> {
        try {
            GraphQLRequest<PaginatedResult<ModelWithMetadata<BlogOwner>>> request = endpoint.buildSyncRequest(schema, null, null, QueryPredicates.all());
            endpoint.sync(request, onResult, onError);
        } catch (DataStoreException datastoreException) {
            onError.accept(datastoreException);
        }
    });
    // Now, capture the request argument on API, so we can see what was passed.
    // Recall that we pass a raw doc to API.
    ArgumentCaptor<GraphQLRequest<ModelWithMetadata<BlogOwner>>> requestCaptor = ArgumentCaptor.forClass(GraphQLRequest.class);
    verify(api).query(requestCaptor.capture(), any(Consumer.class), any(Consumer.class));
    GraphQLRequest<ModelWithMetadata<BlogOwner>> capturedRequest = requestCaptor.getValue();
    Type type = TypeMaker.getParameterizedType(PaginatedResult.class, ModelWithMetadata.class, BlogOwner.class);
    assertEquals(type, capturedRequest.getResponseType());
    // The request was sent as JSON. It has a null variables field, and a present query field.
    JSONAssert.assertEquals(Resources.readAsString("base-sync-request-document-for-blog-owner.txt"), capturedRequest.getContent(), true);
}
Also used : ModelSchema(com.amplifyframework.core.model.ModelSchema) GraphQLRequest(com.amplifyframework.api.graphql.GraphQLRequest) DataStoreException(com.amplifyframework.datastore.DataStoreException) Type(java.lang.reflect.Type) Consumer(com.amplifyframework.core.Consumer) PaginatedResult(com.amplifyframework.api.graphql.PaginatedResult) BlogOwner(com.amplifyframework.testmodels.commentsblog.BlogOwner) Test(org.junit.Test)

Example 50 with BlogOwner

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

the class BasicCloudSyncInstrumentationTest method updateAfterCreate.

/**
 * Verify that updating an item shortly after creating it succeeds.  This can be tricky because the _version
 * returned in the response from the create request must be included in the input for the subsequent update request.
 * @throws DataStoreException On failure to save or query items from DataStore.
 * @throws ApiException On failure to query the API.
 */
@Test
public void updateAfterCreate() throws DataStoreException, ApiException {
    // Setup
    BlogOwner richard = BlogOwner.builder().name("Richard").build();
    BlogOwner updatedRichard = richard.copyOfBuilder().name("Richard McClellan").build();
    String modelName = BlogOwner.class.getSimpleName();
    // Expect two mutations to be published to AppSync.
    HubAccumulator richardAccumulator = HubAccumulator.create(HubChannel.DATASTORE, publicationOf(modelName, richard.getId()), 2).start();
    // Create an item, then update it and save it again.
    dataStore.save(richard);
    dataStore.save(updatedRichard);
    // Verify that 2 mutations were published.
    richardAccumulator.await(30, TimeUnit.SECONDS);
    // Verify that the updatedRichard is saved in the DataStore.
    BlogOwner localRichard = dataStore.get(BlogOwner.class, richard.getId());
    ModelAssert.assertEqualsIgnoringTimestamps(updatedRichard, localRichard);
    // Verify that the updatedRichard is saved on the backend.
    BlogOwner remoteRichard = api.get(BlogOwner.class, richard.getId());
    ModelAssert.assertEqualsIgnoringTimestamps(updatedRichard, remoteRichard);
}
Also used : BlogOwner(com.amplifyframework.testmodels.commentsblog.BlogOwner) HubAccumulator(com.amplifyframework.testutils.HubAccumulator) 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