Search in sources :

Example 6 with HubAccumulator

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);
}
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 7 with HubAccumulator

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();
}
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 8 with HubAccumulator

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);
}
Also used : BlogOwner(com.amplifyframework.testmodels.commentsblog.BlogOwner) HubAccumulator(com.amplifyframework.testutils.HubAccumulator) Test(org.junit.Test)

Example 9 with HubAccumulator

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());
}
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 10 with HubAccumulator

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);
}
Also used : BlogOwner(com.amplifyframework.testmodels.commentsblog.BlogOwner) HubAccumulator(com.amplifyframework.testutils.HubAccumulator) Test(org.junit.Test)

Aggregations

HubAccumulator (com.amplifyframework.testutils.HubAccumulator)30 Test (org.junit.Test)27 BlogOwner (com.amplifyframework.testmodels.commentsblog.BlogOwner)21 ModelSchema (com.amplifyframework.core.model.ModelSchema)7 SerializedModel (com.amplifyframework.core.model.SerializedModel)5 ModelMetadata (com.amplifyframework.datastore.appsync.ModelMetadata)5 ModelWithMetadata (com.amplifyframework.datastore.appsync.ModelWithMetadata)5 JSONObject (org.json.JSONObject)5 Ignore (org.junit.Ignore)5 ApiCategory (com.amplifyframework.api.ApiCategory)4 SynchronousDataStore (com.amplifyframework.testutils.sync.SynchronousDataStore)4 GraphQLResponse (com.amplifyframework.api.graphql.GraphQLResponse)3 HubEvent (com.amplifyframework.hub.HubEvent)3 Blog (com.amplifyframework.testmodels.commentsblog.Blog)3 RandomString (com.amplifyframework.testutils.random.RandomString)3 Before (org.junit.Before)3 AmplifyException (com.amplifyframework.AmplifyException)2 Model (com.amplifyframework.core.model.Model)2 HubChannel (com.amplifyframework.hub.HubChannel)2 Person (com.amplifyframework.testmodels.personcar.Person)2