use of com.amplifyframework.testutils.sync.SynchronousDataStore 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.sync.SynchronousDataStore in project amplify-android by aws-amplify.
the class AWSDataStorePluginTest method observeWithoutMatchingPredicate.
/**
* Verify that the observe api is not invoke when the item changed does not match the predicate.
*
* @throws JSONException on failure to arrange plugin config.
* @throws AmplifyException on failure to arrange API plugin via Amplify facade.
* @throws InterruptedException If interrupted while test observer awaits terminal result.
*/
@Test
public void observeWithoutMatchingPredicate() throws InterruptedException, AmplifyException, JSONException {
AWSDataStorePlugin awsDataStorePlugin = AWSDataStorePlugin.builder().modelProvider(modelProvider).build();
JSONObject dataStorePluginJson = new JSONObject().put("syncIntervalInMinutes", 60);
awsDataStorePlugin.configure(dataStorePluginJson, context);
awsDataStorePlugin.initialize(context);
Amplify.Hub.publish(HubChannel.DATASTORE, HubEvent.create(InitializationStatus.SUCCEEDED));
SynchronousDataStore synchronousDataStore = SynchronousDataStore.delegatingTo(awsDataStorePlugin);
Person expectedResult = createPerson("Test", "Dummy I");
final Person[] actualResult = { null };
Consumer<DataStoreItemChange<Person>> onObserveResult = spy(new Consumer<DataStoreItemChange<Person>>() {
@Override
public void accept(@NonNull DataStoreItemChange<Person> value) {
actualResult[0] = value.item();
}
});
awsDataStorePlugin.observe(Person.class, Person.FIRST_NAME.eq("NO MATCH"), value -> {
}, onObserveResult, error -> {
LOG.error("Error: " + error);
}, () -> {
});
synchronousDataStore.save(expectedResult);
Thread.sleep(1000L);
verify(onObserveResult, never()).accept(any());
}
use of com.amplifyframework.testutils.sync.SynchronousDataStore 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.sync.SynchronousDataStore in project amplify-android by aws-amplify.
the class AWSDataStorePluginTest method observeWithMatchingPredicate.
/**
* Verify that the observe api returns itemChanged which matches the predicate.
*
* @throws JSONException on failure to arrange plugin config.
* @throws AmplifyException on failure to arrange API plugin via Amplify facade.
* @throws InterruptedException If interrupted while test observer awaits terminal result.
*/
@Test
public void observeWithMatchingPredicate() throws InterruptedException, AmplifyException, JSONException {
AWSDataStorePlugin awsDataStorePlugin = AWSDataStorePlugin.builder().modelProvider(modelProvider).build();
JSONObject dataStorePluginJson = new JSONObject().put("syncIntervalInMinutes", 60);
awsDataStorePlugin.configure(dataStorePluginJson, context);
awsDataStorePlugin.initialize(context);
Amplify.Hub.publish(HubChannel.DATASTORE, HubEvent.create(InitializationStatus.SUCCEEDED));
SynchronousDataStore synchronousDataStore = SynchronousDataStore.delegatingTo(awsDataStorePlugin);
final CountDownLatch latch = new CountDownLatch(1);
Person expectedResult = createPerson("Test", "Dummy I");
final Person[] actualResult = { null };
Consumer<DataStoreItemChange<Person>> onObserveResult = spy(new Consumer<DataStoreItemChange<Person>>() {
@Override
public void accept(@NonNull DataStoreItemChange<Person> value) {
latch.countDown();
actualResult[0] = value.item();
}
});
awsDataStorePlugin.observe(Person.class, Person.FIRST_NAME.eq("Test"), value -> {
}, onObserveResult, error -> {
LOG.error("Error: " + error);
}, () -> {
});
synchronousDataStore.save(expectedResult);
latch.await(TIMEOUT_MS, TimeUnit.SECONDS);
verify(onObserveResult).accept(any());
assertEquals(actualResult[0], expectedResult);
}
use of com.amplifyframework.testutils.sync.SynchronousDataStore in project amplify-android by aws-amplify.
the class ConflictResolverIntegrationTest method conflictIsResolvedByRetryingLocalData.
/**
* When {@link Completable} completes,
* then the local storage adapter should have all of the remote model state.
* @throws AmplifyException On failure interacting with storage adapter
* @throws InterruptedException If interrupted while awaiting terminal result in test observer
* @throws JSONException If unable to parse the JSON.
*/
// Varied types in Observable.fromArray(...).
@SuppressWarnings("unchecked")
@Test
public void conflictIsResolvedByRetryingLocalData() throws AmplifyException, JSONException, InterruptedException {
CountDownLatch latch = new CountDownLatch(4);
// Arrange for the user-provided conflict handler to always request local retry.
ApiCategory mockApiCategory = mockApiCategoryWithGraphQlApi();
Person person1 = setupApiMock(latch, mockApiCategory);
JSONObject dataStorePluginJson = new JSONObject().put("syncIntervalInMinutes", 60);
AWSDataStorePlugin awsDataStorePlugin = AWSDataStorePlugin.builder().modelProvider(modelProvider).apiCategory(mockApiCategory).dataStoreConfiguration(DataStoreConfiguration.builder().conflictHandler(DataStoreConflictHandler.alwaysRetryLocal()).build()).build();
SynchronousDataStore synchronousDataStore = SynchronousDataStore.delegatingTo(awsDataStorePlugin);
awsDataStorePlugin.configure(dataStorePluginJson, context);
awsDataStorePlugin.initialize(context);
awsDataStorePlugin.start(() -> {
}, (onError) -> {
});
// 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));
// Save person 1
synchronousDataStore.save(person1);
Person result1 = synchronousDataStore.get(Person.class, person1.getId());
assertTrue(latch.await(2, TimeUnit.SECONDS));
assertEquals(person1, result1);
}
Aggregations