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());
}
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);
}
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());
}
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);
}
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);
}
Aggregations