use of com.amplifyframework.testutils.HubAccumulator in project amplify-android by aws-amplify.
the class AWSDataStorePluginTest method startInLocalMode.
/**
* Starting the plugin in local mode (no API plugin) works without freezing or crashing the calling thread.
* @throws AmplifyException Not expected; on failure to configure of initialize plugin.
*/
@Test
public void startInLocalMode() throws AmplifyException {
// Configure DataStore with an empty config (All defaults)
HubAccumulator dataStoreReadyObserver = HubAccumulator.create(HubChannel.DATASTORE, DataStoreChannelEventName.READY, 1).start();
ApiCategory emptyApiCategory = spy(ApiCategory.class);
AWSDataStorePlugin standAloneDataStorePlugin = AWSDataStorePlugin.builder().modelProvider(modelProvider).apiCategory(emptyApiCategory).build();
SynchronousDataStore synchronousDataStore = SynchronousDataStore.delegatingTo(standAloneDataStorePlugin);
standAloneDataStorePlugin.configure(new JSONObject(), context);
standAloneDataStorePlugin.initialize(context);
// Trick the DataStore since it's not getting initialized as part of the Amplify.initialize call chain
Amplify.Hub.publish(HubChannel.DATASTORE, HubEvent.create(InitializationStatus.SUCCEEDED));
synchronousDataStore.start();
dataStoreReadyObserver.await();
assertSyncProcessorNotStarted(emptyApiCategory);
Person person1 = createPerson("Test", "Dummy I");
synchronousDataStore.save(person1);
assertNotNull(person1.getId());
Person person1FromDb = synchronousDataStore.get(Person.class, person1.getId());
assertEquals(person1, person1FromDb);
}
use of com.amplifyframework.testutils.HubAccumulator in project amplify-android by aws-amplify.
the class AWSDataStorePluginTest method startInApiMode.
/**
* Starting the plugin when in API sync mode succeeds without freezing or crashing the calling thread.
* @throws JSONException on failure to arrange plugin config
* @throws AmplifyException on failure to arrange API plugin via Amplify facade
*/
@Test
public void startInApiMode() throws JSONException, AmplifyException {
HubAccumulator dataStoreReadyObserver = HubAccumulator.create(HubChannel.DATASTORE, DataStoreChannelEventName.READY, 1).start();
HubAccumulator subscriptionsEstablishedObserver = HubAccumulator.create(HubChannel.DATASTORE, DataStoreChannelEventName.SUBSCRIPTIONS_ESTABLISHED, 1).start();
HubAccumulator networkStatusObserver = HubAccumulator.create(HubChannel.DATASTORE, DataStoreChannelEventName.NETWORK_STATUS, 1).start();
ApiCategory mockApiCategory = mockApiCategoryWithGraphQlApi();
JSONObject dataStorePluginJson = new JSONObject().put("syncIntervalInMinutes", 60);
AWSDataStorePlugin awsDataStorePlugin = AWSDataStorePlugin.builder().modelProvider(modelProvider).apiCategory(mockApiCategory).build();
SynchronousDataStore synchronousDataStore = SynchronousDataStore.delegatingTo(awsDataStorePlugin);
awsDataStorePlugin.configure(dataStorePluginJson, context);
awsDataStorePlugin.initialize(context);
// Trick the DataStore since it's not getting initialized as part of the Amplify.initialize call chain
Amplify.Hub.publish(HubChannel.DATASTORE, HubEvent.create(InitializationStatus.SUCCEEDED));
synchronousDataStore.start();
dataStoreReadyObserver.await();
subscriptionsEstablishedObserver.await();
networkStatusObserver.await();
assertRemoteSubscriptionsStarted();
}
use of com.amplifyframework.testutils.HubAccumulator 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);
}
use of com.amplifyframework.testutils.HubAccumulator 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.testutils.HubAccumulator 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);
}
Aggregations