Search in sources :

Example 1 with ApiCategory

use of com.amplifyframework.api.ApiCategory in project amplify-android by aws-amplify.

the class GraphQLInstrumentationTest method setUp.

/**
 * Configure the Amplify framework, if that hasn't already happened in this process instance.
 * @throws AmplifyException From Amplify configuration
 * @throws SynchronousMobileClient.MobileClientException From failure to initialize auth
 */
@Before
public void setUp() throws AmplifyException, SynchronousMobileClient.MobileClientException {
    // Set up and configure API category
    ApiCategory asyncDelegate = TestApiCategory.fromConfiguration(R.raw.amplifyconfiguration);
    api = SynchronousApi.delegatingTo(asyncDelegate);
    // Set up Auth
    mobileClient = SynchronousMobileClient.instance();
    mobileClient.initialize();
    mobileClient.signOut();
}
Also used : ApiCategory(com.amplifyframework.api.ApiCategory) Before(org.junit.Before)

Example 2 with ApiCategory

use of com.amplifyframework.api.ApiCategory 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 3 with ApiCategory

use of com.amplifyframework.api.ApiCategory 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 ApiCategory

use of com.amplifyframework.api.ApiCategory in project amplify-android by aws-amplify.

the class AWSDataStorePluginTest method configureAndInitialize.

/**
 * Configuring and initializing the plugin succeeds without freezing or crashing the calling thread. Basic. 🙄
 * @throws AmplifyException On failure to configure or initialize plugin.
 */
@Test
public void configureAndInitialize() throws AmplifyException {
    // Configure DataStore with an empty config (All defaults)
    ApiCategory emptyApiCategory = spy(ApiCategory.class);
    AWSDataStorePlugin standAloneDataStorePlugin = AWSDataStorePlugin.builder().modelProvider(modelProvider).apiCategory(emptyApiCategory).build();
    standAloneDataStorePlugin.configure(new JSONObject(), context);
    standAloneDataStorePlugin.initialize(context);
}
Also used : JSONObject(org.json.JSONObject) ApiCategory(com.amplifyframework.api.ApiCategory) Test(org.junit.Test)

Example 5 with ApiCategory

use of com.amplifyframework.api.ApiCategory 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

ApiCategory (com.amplifyframework.api.ApiCategory)21 JSONObject (org.json.JSONObject)9 Test (org.junit.Test)9 SynchronousDataStore (com.amplifyframework.testutils.sync.SynchronousDataStore)8 Context (android.content.Context)7 ApplicationProvider.getApplicationContext (androidx.test.core.app.ApplicationProvider.getApplicationContext)6 Person (com.amplifyframework.testmodels.personcar.Person)6 Before (org.junit.Before)6 RawRes (androidx.annotation.RawRes)5 AWSApiPlugin (com.amplifyframework.api.aws.AWSApiPlugin)5 GraphQLResponse (com.amplifyframework.api.graphql.GraphQLResponse)5 CategoryConfiguration (com.amplifyframework.core.category.CategoryConfiguration)5 ModelWithMetadata (com.amplifyframework.datastore.appsync.ModelWithMetadata)5 HubAccumulator (com.amplifyframework.testutils.HubAccumulator)5 ApiCategoryConfiguration (com.amplifyframework.api.ApiCategoryConfiguration)4 GraphQLRequest (com.amplifyframework.api.graphql.GraphQLRequest)4 Consumer (com.amplifyframework.core.Consumer)4 AndroidLoggingPlugin (com.amplifyframework.logging.AndroidLoggingPlugin)4 ApiEndpointStatusChangeEvent (com.amplifyframework.api.events.ApiEndpointStatusChangeEvent)3 PaginatedResult (com.amplifyframework.api.graphql.PaginatedResult)3