Search in sources :

Example 51 with BlogOwner

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

the class BasicCloudSyncInstrumentationTest method syncDownFromCloudIsWorking.

/**
 * The sync engine should receive mutations for its managed models, through its
 * subscriptions. When we create a model remotely, the sync engine should respond
 * by processing the subscription event and saving the model locally.
 * @throws DataStoreException On failure to query the local data store for
 *                            local presence of arranged data (second step)
 * @throws AmplifyException On failure to arrange a {@link DataStoreCategory} via the
 *                          {@link DataStoreCategoryConfigurator}
 */
@Test
public void syncDownFromCloudIsWorking() throws AmplifyException {
    // This model will get saved to the cloud.
    BlogOwner jameson = BlogOwner.builder().name("Jameson Williams").build();
    // Start watching locally, to see if it shows up on the client.
    HubAccumulator receiptAccumulator = HubAccumulator.create(HubChannel.DATASTORE, receiptOf(jameson.getId()), 1).start();
    // Act: create the model in the cloud
    ModelSchema schema = ModelSchema.fromModelClass(BlogOwner.class);
    GraphQLResponse<ModelWithMetadata<BlogOwner>> createResponse = appSync.create(jameson, schema);
    ModelMetadata metadata = createResponse.getData().getSyncMetadata();
    assertEquals(Integer.valueOf(1), metadata.getVersion());
    // Wait for the events to show up on Hub.
    receiptAccumulator.awaitFirst(TIMEOUT_SECONDS, TimeUnit.SECONDS);
    // Jameson should be in the local DataStore.
    BlogOwner owner = dataStore.get(BlogOwner.class, jameson.getId());
    assertEquals("Jameson Williams", owner.getName());
    assertEquals(jameson.getId(), owner.getId());
}
Also used : ModelSchema(com.amplifyframework.core.model.ModelSchema) ModelWithMetadata(com.amplifyframework.datastore.appsync.ModelWithMetadata) BlogOwner(com.amplifyframework.testmodels.commentsblog.BlogOwner) HubAccumulator(com.amplifyframework.testutils.HubAccumulator) ModelMetadata(com.amplifyframework.datastore.appsync.ModelMetadata) Test(org.junit.Test)

Example 52 with BlogOwner

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

the class BasicCloudSyncInstrumentationTest method createThenUpdateDifferentField.

/**
 * Verify that updating a different field of an item shortly after creating it succeeds.
 * @throws DataStoreException On failure to save or query items from DataStore.
 * @throws ApiException On failure to query the API.
 */
@Test
public void createThenUpdateDifferentField() throws DataStoreException, ApiException {
    // Setup
    BlogOwner owner = BlogOwner.builder().name("Richard").build();
    BlogOwner updatedOwner = owner.copyOfBuilder().wea("pon").build();
    String modelName = BlogOwner.class.getSimpleName();
    // Expect two mutations to be published to AppSync.
    HubAccumulator accumulator = HubAccumulator.create(HubChannel.DATASTORE, publicationOf(modelName, owner.getId()), 2).start();
    // Create an item, then update it with different field and save it again.
    dataStore.save(owner);
    dataStore.save(updatedOwner);
    // Verify that 2 mutations were published.
    accumulator.await(30, TimeUnit.SECONDS);
    // Verify that the updatedOwner is saved in the DataStore.
    BlogOwner localOwner = dataStore.get(BlogOwner.class, owner.getId());
    ModelAssert.assertEqualsIgnoringTimestamps(updatedOwner, localOwner);
    // Verify that the updatedOwner is saved on the backend.
    BlogOwner remoteOwner = api.get(BlogOwner.class, owner.getId());
    ModelAssert.assertEqualsIgnoringTimestamps(updatedOwner, remoteOwner);
}
Also used : BlogOwner(com.amplifyframework.testmodels.commentsblog.BlogOwner) HubAccumulator(com.amplifyframework.testutils.HubAccumulator) Test(org.junit.Test)

Example 53 with BlogOwner

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

the class BasicCloudSyncInstrumentationTest method syncUpToCloudIsWorking.

/**
 * Save a BlogOwner via DataStore, wait a bit, check API to see if the BlogOwner is there, remotely.
 * @throws DataStoreException On failure to save item into DataStore (first step)
 * @throws ApiException On failure to retrieve a valid response from API when checking
 *                      for remote presence of saved item
 * @throws AmplifyException On failure to arrange a {@link DataStoreCategory} via the
 *                          {@link DataStoreCategoryConfigurator}
 */
@Test
public void syncUpToCloudIsWorking() throws AmplifyException {
    // Start listening for model publication events on the Hub.
    BlogOwner localCharley = BlogOwner.builder().name("Charley Crockett").build();
    String modelName = BlogOwner.class.getSimpleName();
    HubAccumulator publishedMutationsAccumulator = HubAccumulator.create(HubChannel.DATASTORE, publicationOf(modelName, localCharley.getId()), 1).start();
    // Save Charley Crockett, a guy who has a blog, into the DataStore.
    dataStore.save(localCharley);
    // Wait for a Hub event telling us that our Charley model got published to the cloud.
    publishedMutationsAccumulator.await(TIMEOUT_SECONDS, TimeUnit.SECONDS);
    // Try to get Charley from the backend.
    BlogOwner remoteCharley = api.get(BlogOwner.class, localCharley.getId());
    // A Charley is a Charley is a Charley, right?
    assertEquals(localCharley.getId(), remoteCharley.getId());
    assertEquals(localCharley.getName(), remoteCharley.getName());
}
Also used : BlogOwner(com.amplifyframework.testmodels.commentsblog.BlogOwner) HubAccumulator(com.amplifyframework.testutils.HubAccumulator) Test(org.junit.Test)

Example 54 with BlogOwner

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

the class BasicCloudSyncInstrumentationTest method createItemThenUpdateThenWaitThenUpdate.

/**
 * Create new item, then immediately update a different field.
 * Wait for sync round trip. Then update the first field.
 * @throws DataStoreException On failure to save or query items from DataStore.
 * @throws ApiException On failure to query the API.
 */
@Test
public void createItemThenUpdateThenWaitThenUpdate() throws DataStoreException, ApiException {
    // Setup
    BlogOwner owner = BlogOwner.builder().name("ownerName").build();
    BlogOwner updatedOwner = owner.copyOfBuilder().wea("pon").build();
    String modelName = BlogOwner.class.getSimpleName();
    HubAccumulator accumulator = HubAccumulator.create(HubChannel.DATASTORE, publicationOf(modelName, owner.getId()), 2).start();
    // Create new and then immediately update
    dataStore.save(owner);
    dataStore.save(updatedOwner);
    accumulator.await(TIMEOUT_SECONDS, TimeUnit.SECONDS);
    // Update the field
    BlogOwner diffFieldUpdated = updatedOwner.copyOfBuilder().name("ownerUpdatedName").build();
    accumulator = HubAccumulator.create(HubChannel.DATASTORE, publicationOf(modelName, diffFieldUpdated.getId()), 1).start();
    dataStore.save(diffFieldUpdated);
    accumulator.await(TIMEOUT_SECONDS, TimeUnit.SECONDS);
    BlogOwner localOwner = dataStore.get(BlogOwner.class, diffFieldUpdated.getId());
    ModelAssert.assertEqualsIgnoringTimestamps(diffFieldUpdated, localOwner);
    BlogOwner remoteOwner = api.get(BlogOwner.class, diffFieldUpdated.getId());
    ModelAssert.assertEqualsIgnoringTimestamps(diffFieldUpdated, remoteOwner);
}
Also used : BlogOwner(com.amplifyframework.testmodels.commentsblog.BlogOwner) HubAccumulator(com.amplifyframework.testutils.HubAccumulator) Test(org.junit.Test)

Example 55 with BlogOwner

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

the class BasicCloudSyncInstrumentationTest method createWaitThenUpdate10TimesWithPredicate.

/**
 * The test is to test consecutive updates with predicate.
 * @throws DataStoreException On failure to save or query items from DataStore.
 * @throws ApiException On failure to query the API.
 */
@Test
public void createWaitThenUpdate10TimesWithPredicate() throws DataStoreException, ApiException {
    // Setup
    BlogOwner owner = BlogOwner.builder().name("Blogger").wea("ryt").build();
    String modelName = BlogOwner.class.getSimpleName();
    QueryPredicate predicate = BlogOwner.WEA.beginsWith("r");
    // Setup 10 updates
    List<String> weas = Arrays.asList("ron", "rth", "rer", "rly", "ren", "rel", "ral", "rec", "rin", "reh");
    List<BlogOwner> owners = new ArrayList<>();
    for (int i = 0; i < weas.size(); i++) {
        BlogOwner updatedOwner = owner.copyOfBuilder().wea(weas.get(i)).build();
        owners.add(updatedOwner);
    }
    HubAccumulator accumulator = HubAccumulator.create(HubChannel.DATASTORE, publicationOf(modelName, owner.getId()), 1).start();
    // Create an item.
    dataStore.save(owner);
    // Wait for the sync.
    accumulator.await(TIMEOUT_SECONDS, TimeUnit.SECONDS);
    // Make 10 consecutive updates with predicate
    HubAccumulator updateAccumulator = HubAccumulator.create(HubChannel.DATASTORE, publicationOf(modelName, owner.getId()), 10).start();
    for (int i = 0; i < weas.size(); i++) {
        dataStore.save(owners.get(i), predicate);
    }
    updateAccumulator.await(TIMEOUT_SECONDS, TimeUnit.SECONDS);
    BlogOwner lastUpdate = owners.get(owners.size() - 1);
    BlogOwner localOwner = dataStore.get(BlogOwner.class, lastUpdate.getId());
    ModelAssert.assertEqualsIgnoringTimestamps(lastUpdate, localOwner);
    BlogOwner remoteOwner = api.get(BlogOwner.class, lastUpdate.getId());
    ModelAssert.assertEqualsIgnoringTimestamps(lastUpdate, remoteOwner);
}
Also used : QueryPredicate(com.amplifyframework.core.model.query.predicate.QueryPredicate) ArrayList(java.util.ArrayList) 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