Search in sources :

Example 1 with SynchronousDataStore

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);
}
Also used : SynchronousDataStore(com.amplifyframework.testutils.sync.SynchronousDataStore) JSONObject(org.json.JSONObject) ApiCategory(com.amplifyframework.api.ApiCategory) HubAccumulator(com.amplifyframework.testutils.HubAccumulator) Person(com.amplifyframework.testmodels.personcar.Person) Test(org.junit.Test)

Example 2 with SynchronousDataStore

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());
}
Also used : JSONObject(org.json.JSONObject) SynchronousDataStore(com.amplifyframework.testutils.sync.SynchronousDataStore) Person(com.amplifyframework.testmodels.personcar.Person) Test(org.junit.Test)

Example 3 with SynchronousDataStore

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();
}
Also used : JSONObject(org.json.JSONObject) SynchronousDataStore(com.amplifyframework.testutils.sync.SynchronousDataStore) ApiCategory(com.amplifyframework.api.ApiCategory) HubAccumulator(com.amplifyframework.testutils.HubAccumulator) Test(org.junit.Test)

Example 4 with SynchronousDataStore

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);
}
Also used : JSONObject(org.json.JSONObject) SynchronousDataStore(com.amplifyframework.testutils.sync.SynchronousDataStore) CountDownLatch(java.util.concurrent.CountDownLatch) Person(com.amplifyframework.testmodels.personcar.Person) Test(org.junit.Test)

Example 5 with SynchronousDataStore

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);
}
Also used : JSONObject(org.json.JSONObject) SynchronousDataStore(com.amplifyframework.testutils.sync.SynchronousDataStore) ApiCategory(com.amplifyframework.api.ApiCategory) CountDownLatch(java.util.concurrent.CountDownLatch) Person(com.amplifyframework.testmodels.personcar.Person) Test(org.junit.Test)

Aggregations

SynchronousDataStore (com.amplifyframework.testutils.sync.SynchronousDataStore)8 JSONObject (org.json.JSONObject)8 Test (org.junit.Test)8 Person (com.amplifyframework.testmodels.personcar.Person)7 ApiCategory (com.amplifyframework.api.ApiCategory)6 HubAccumulator (com.amplifyframework.testutils.HubAccumulator)4 GraphQLResponse (com.amplifyframework.api.graphql.GraphQLResponse)2 ModelMetadata (com.amplifyframework.datastore.appsync.ModelMetadata)2 ModelWithMetadata (com.amplifyframework.datastore.appsync.ModelWithMetadata)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 Ignore (org.junit.Ignore)1