Search in sources :

Example 1 with AppSync

use of com.amplifyframework.datastore.appsync.AppSync in project amplify-android by aws-amplify.

the class SubscriptionProcessorTest method setup.

/**
 * Sets up an {@link SubscriptionProcessor} and associated test dependencies.
 * @throws DataStoreException on error building the {@link DataStoreConfiguration}
 */
@Before
public void setup() throws DataStoreException {
    ModelProvider modelProvider = AmplifyModelProvider.getInstance();
    schemaRegistry = SchemaRegistry.instance();
    schemaRegistry.register(modelProvider.modelSchemas());
    this.modelSchemas = sortedModels(modelProvider);
    this.appSync = mock(AppSync.class);
    this.merger = mock(Merger.class);
    DataStoreConfiguration dataStoreConfiguration = DataStoreConfiguration.builder().syncExpression(BlogOwner.class, () -> BlogOwner.NAME.beginsWith("John")).build();
    QueryPredicateProvider queryPredicateProvider = new QueryPredicateProvider(() -> dataStoreConfiguration);
    queryPredicateProvider.resolvePredicates();
    this.subscriptionProcessor = SubscriptionProcessor.builder().appSync(appSync).modelProvider(modelProvider).schemaRegistry(schemaRegistry).merger(merger).queryPredicateProvider(queryPredicateProvider).onFailure(throwable -> {
    }).build();
}
Also used : DataStoreConfiguration(com.amplifyframework.datastore.DataStoreConfiguration) ModelProvider(com.amplifyframework.core.model.ModelProvider) AmplifyModelProvider(com.amplifyframework.testmodels.commentsblog.AmplifyModelProvider) AppSync(com.amplifyframework.datastore.appsync.AppSync) BlogOwner(com.amplifyframework.testmodels.commentsblog.BlogOwner) Before(org.junit.Before)

Example 2 with AppSync

use of com.amplifyframework.datastore.appsync.AppSync in project amplify-android by aws-amplify.

the class SubscriptionProcessorTest method arrangeStartedSubscriptions.

private static void arrangeStartedSubscriptions(AppSync appSync, List<ModelSchema> modelSchemas, SubscriptionType[] subscriptionTypes) {
    Answer<Cancelable> answer = invocation -> {
        final int startConsumerIndex = 1;
        Consumer<String> onStart = invocation.getArgument(startConsumerIndex);
        onStart.accept(RandomString.string());
        return new NoOpCancelable();
    };
    arrangeSubscriptions(appSync, answer, modelSchemas, subscriptionTypes);
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) ModelWithMetadata(com.amplifyframework.datastore.appsync.ModelWithMetadata) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) ModelProvider(com.amplifyframework.core.model.ModelProvider) Pair(android.util.Pair) RunWith(org.junit.runner.RunWith) AppSync(com.amplifyframework.datastore.appsync.AppSync) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) SchemaRegistry(com.amplifyframework.core.model.SchemaRegistry) Answer(org.mockito.stubbing.Answer) Consumer(com.amplifyframework.core.Consumer) SubscriptionType(com.amplifyframework.api.graphql.SubscriptionType) Observable(io.reactivex.rxjava3.core.Observable) Map(java.util.Map) Mockito.doAnswer(org.mockito.Mockito.doAnswer) NoOpCancelable(com.amplifyframework.core.async.NoOpCancelable) ModelSchema(com.amplifyframework.core.model.ModelSchema) ModelMetadata(com.amplifyframework.datastore.appsync.ModelMetadata) GraphQLResponse(com.amplifyframework.api.graphql.GraphQLResponse) Before(org.junit.Before) DataStoreConfiguration(com.amplifyframework.datastore.DataStoreConfiguration) BlogOwner(com.amplifyframework.testmodels.commentsblog.BlogOwner) Model(com.amplifyframework.core.model.Model) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) Completable(io.reactivex.rxjava3.core.Completable) Action(com.amplifyframework.core.Action) RobolectricTestRunner(org.robolectric.RobolectricTestRunner) TimeUnit(java.util.concurrent.TimeUnit) DataStoreException(com.amplifyframework.datastore.DataStoreException) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) Cancelable(com.amplifyframework.core.async.Cancelable) Assert.assertFalse(org.junit.Assert.assertFalse) RandomString(com.amplifyframework.testutils.random.RandomString) Temporal(com.amplifyframework.core.model.temporal.Temporal) AmplifyModelProvider(com.amplifyframework.testmodels.commentsblog.AmplifyModelProvider) Collections(java.util.Collections) Mockito.mock(org.mockito.Mockito.mock) Consumer(com.amplifyframework.core.Consumer) NoOpCancelable(com.amplifyframework.core.async.NoOpCancelable) NoOpCancelable(com.amplifyframework.core.async.NoOpCancelable) Cancelable(com.amplifyframework.core.async.Cancelable)

Example 3 with AppSync

use of com.amplifyframework.datastore.appsync.AppSync in project amplify-android by aws-amplify.

the class SubscriptionProcessorTest method appSyncInvokedWhenSubscriptionsStarted.

/**
 * When {@link SubscriptionProcessor#startSubscriptions()} is invoked,
 * the {@link AppSync} client receives subscription requests.
 */
@Test
public void appSyncInvokedWhenSubscriptionsStarted() {
    // For every Class-SubscriptionType pairing, use a CountDownLatch
    // to tell whether or not we've "seen" a subscription event for it.
    Map<Pair<ModelSchema, SubscriptionType>, CountDownLatch> seen = new HashMap<>();
    // Build a stream of such pairs.
    Observable.fromIterable(modelSchemas).flatMap(modelSchema -> Observable.fromArray(SubscriptionType.values()).map(value -> Pair.create(modelSchema, value))).blockingForEach(pair -> {
        // For each one, store a latch. Add a mocking behavior to count down
        // the latch when the subscription API is hit, for that class and subscription type.
        CountDownLatch latch = new CountDownLatch(1);
        seen.put(Pair.create(pair.first, pair.second), latch);
        Answer<Cancelable> answer = invocation -> {
            latch.countDown();
            return new NoOpCancelable();
        };
        arrangeSubscription(appSync, answer, pair.first, pair.second);
    });
    // Act: start some subscriptions.
    try {
        subscriptionProcessor.startSubscriptions();
    } catch (DataStoreException exception) {
    // startSubscriptions throws this exception if it doesn't receive the start_ack messages after a time out.
    // This test doesn't mock those start_ack messages, so this expection is expected.  That's okay though -
    // we just want to verify that the subscriptions were requested.
    }
    // Make sure that all of the subscriptions have been
    Observable.fromIterable(seen.entrySet()).blockingForEach(entry -> {
        CountDownLatch latch = entry.getValue();
        assertTrue(latch.await(OPERATION_TIMEOUT_MS, TimeUnit.MILLISECONDS));
    });
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) ModelWithMetadata(com.amplifyframework.datastore.appsync.ModelWithMetadata) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) ModelProvider(com.amplifyframework.core.model.ModelProvider) Pair(android.util.Pair) RunWith(org.junit.runner.RunWith) AppSync(com.amplifyframework.datastore.appsync.AppSync) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) SchemaRegistry(com.amplifyframework.core.model.SchemaRegistry) Answer(org.mockito.stubbing.Answer) Consumer(com.amplifyframework.core.Consumer) SubscriptionType(com.amplifyframework.api.graphql.SubscriptionType) Observable(io.reactivex.rxjava3.core.Observable) Map(java.util.Map) Mockito.doAnswer(org.mockito.Mockito.doAnswer) NoOpCancelable(com.amplifyframework.core.async.NoOpCancelable) ModelSchema(com.amplifyframework.core.model.ModelSchema) ModelMetadata(com.amplifyframework.datastore.appsync.ModelMetadata) GraphQLResponse(com.amplifyframework.api.graphql.GraphQLResponse) Before(org.junit.Before) DataStoreConfiguration(com.amplifyframework.datastore.DataStoreConfiguration) BlogOwner(com.amplifyframework.testmodels.commentsblog.BlogOwner) Model(com.amplifyframework.core.model.Model) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) Completable(io.reactivex.rxjava3.core.Completable) Action(com.amplifyframework.core.Action) RobolectricTestRunner(org.robolectric.RobolectricTestRunner) TimeUnit(java.util.concurrent.TimeUnit) DataStoreException(com.amplifyframework.datastore.DataStoreException) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) Cancelable(com.amplifyframework.core.async.Cancelable) Assert.assertFalse(org.junit.Assert.assertFalse) RandomString(com.amplifyframework.testutils.random.RandomString) Temporal(com.amplifyframework.core.model.temporal.Temporal) AmplifyModelProvider(com.amplifyframework.testmodels.commentsblog.AmplifyModelProvider) Collections(java.util.Collections) Mockito.mock(org.mockito.Mockito.mock) DataStoreException(com.amplifyframework.datastore.DataStoreException) HashMap(java.util.HashMap) NoOpCancelable(com.amplifyframework.core.async.NoOpCancelable) CountDownLatch(java.util.concurrent.CountDownLatch) NoOpCancelable(com.amplifyframework.core.async.NoOpCancelable) Cancelable(com.amplifyframework.core.async.Cancelable) Pair(android.util.Pair) Test(org.junit.Test)

Example 4 with AppSync

use of com.amplifyframework.datastore.appsync.AppSync in project amplify-android by aws-amplify.

the class MutationProcessorTest method setup.

/**
 * A {@link MutationProcessor} is being tested. To do so, we arrange mutations into
 * an {@link MutationOutbox}. Fake responses are returned from a mock {@link AppSync}.
 * @throws AmplifyException When loading SchemaRegistry
 */
@Before
public void setup() throws AmplifyException {
    ShadowLog.stream = System.out;
    LocalStorageAdapter localStorageAdapter = InMemoryStorageAdapter.create();
    this.synchronousStorageAdapter = SynchronousStorageAdapter.delegatingTo(localStorageAdapter);
    this.mutationOutbox = new PersistentMutationOutbox(localStorageAdapter);
    VersionRepository versionRepository = new VersionRepository(localStorageAdapter);
    Merger merger = new Merger(mutationOutbox, versionRepository, localStorageAdapter);
    this.appSync = mock(AppSync.class);
    this.configurationProvider = mock(DataStoreConfigurationProvider.class);
    ConflictResolver conflictResolver = new ConflictResolver(configurationProvider, appSync);
    schemaRegistry = SchemaRegistry.instance();
    schemaRegistry.register(Collections.singleton(BlogOwner.class));
    this.mutationProcessor = MutationProcessor.builder().merger(merger).versionRepository(versionRepository).schemaRegistry(schemaRegistry).mutationOutbox(mutationOutbox).appSync(appSync).conflictResolver(conflictResolver).build();
}
Also used : DataStoreConfigurationProvider(com.amplifyframework.datastore.DataStoreConfigurationProvider) LocalStorageAdapter(com.amplifyframework.datastore.storage.LocalStorageAdapter) AppSync(com.amplifyframework.datastore.appsync.AppSync) BlogOwner(com.amplifyframework.testmodels.commentsblog.BlogOwner) Before(org.junit.Before)

Example 5 with AppSync

use of com.amplifyframework.datastore.appsync.AppSync in project amplify-android by aws-amplify.

the class SyncProcessor method syncModel.

/**
 * Sync models for a given model class.
 * This involves three steps:
 *  1. Lookup the last time the model class was synced;
 *  2. Make a request to the AppSync endpoint. If the last sync time is within a recent window
 *     of time, then request a *delta* sync. If the last sync time is outside a recent window of time,
 *     perform a *base* sync. A base sync is preformed by passing null.
 *  3. Continue fetching paged results until !hasNextResult() or we have synced the max records.
 *
 * @param schema The schema of the model to sync
 * @param syncTime The time of a last successful sync.
 * @param <T> The type of model to sync.
 * @return a stream of all ModelWithMetadata&lt;T&gt; objects from all pages for the provided model.
 * @throws DataStoreException if dataStoreConfigurationProvider.getConfiguration() fails
 */
private <T extends Model> Flowable<List<ModelWithMetadata<T>>> syncModel(ModelSchema schema, SyncTime syncTime) throws DataStoreException {
    final Long lastSyncTimeAsLong = syncTime.exists() ? syncTime.toLong() : null;
    final Integer syncPageSize = dataStoreConfigurationProvider.getConfiguration().getSyncPageSize();
    final Integer syncMaxRecords = dataStoreConfigurationProvider.getConfiguration().getSyncMaxRecords();
    AtomicReference<Integer> recordsFetched = new AtomicReference<>(0);
    QueryPredicate predicate = queryPredicateProvider.getPredicate(schema.getName());
    // Create a BehaviorProcessor, and set the default value to a GraphQLRequest that fetches the first page.
    BehaviorProcessor<GraphQLRequest<PaginatedResult<ModelWithMetadata<T>>>> processor = BehaviorProcessor.createDefault(appSync.buildSyncRequest(schema, lastSyncTimeAsLong, syncPageSize, predicate));
    return processor.concatMap(request -> {
        if (isSyncRetryEnabled) {
            return syncPageWithRetry(request).toFlowable();
        } else {
            return syncPage(request).toFlowable();
        }
    }).doOnNext(paginatedResult -> {
        if (paginatedResult.hasNextResult()) {
            processor.onNext(paginatedResult.getRequestForNextResult());
        } else {
            processor.onComplete();
        }
    }).map(paginatedResult -> Flowable.fromIterable(paginatedResult).map(modelWithMetadata -> hydrateSchemaIfNeeded(modelWithMetadata, schema)).toList().blockingGet()).takeUntil(items -> recordsFetched.accumulateAndGet(items.size(), Integer::sum) >= syncMaxRecords);
}
Also used : DataStoreConfigurationProvider(com.amplifyframework.datastore.DataStoreConfigurationProvider) Single(io.reactivex.rxjava3.core.Single) DataStoreErrorHandler(com.amplifyframework.datastore.DataStoreErrorHandler) BehaviorProcessor(io.reactivex.rxjava3.processors.BehaviorProcessor) NonNull(androidx.annotation.NonNull) ModelWithMetadata(com.amplifyframework.datastore.appsync.ModelWithMetadata) GraphQLRequest(com.amplifyframework.api.graphql.GraphQLRequest) ModelProvider(com.amplifyframework.core.model.ModelProvider) DataStoreChannelEventName(com.amplifyframework.datastore.DataStoreChannelEventName) AppSync(com.amplifyframework.datastore.appsync.AppSync) SyncQueriesStartedEvent(com.amplifyframework.datastore.events.SyncQueriesStartedEvent) AtomicReference(java.util.concurrent.atomic.AtomicReference) ApiException(com.amplifyframework.api.ApiException) ArrayList(java.util.ArrayList) SchemaRegistry(com.amplifyframework.core.model.SchemaRegistry) Time(com.amplifyframework.util.Time) Schedulers(io.reactivex.rxjava3.schedulers.Schedulers) Consumer(com.amplifyframework.core.Consumer) ModelSchema(com.amplifyframework.core.model.ModelSchema) PaginatedResult(com.amplifyframework.api.graphql.PaginatedResult) QueryPredicate(com.amplifyframework.core.model.query.predicate.QueryPredicate) HubEvent(com.amplifyframework.hub.HubEvent) Amplify(com.amplifyframework.core.Amplify) Flowable(io.reactivex.rxjava3.core.Flowable) HubChannel(com.amplifyframework.hub.HubChannel) SerializedModel(com.amplifyframework.core.model.SerializedModel) Model(com.amplifyframework.core.model.Model) Completable(io.reactivex.rxjava3.core.Completable) Logger(com.amplifyframework.logging.Logger) Objects(java.util.Objects) DataStoreException(com.amplifyframework.datastore.DataStoreException) List(java.util.List) Cancelable(com.amplifyframework.core.async.Cancelable) AmplifyDisposables(com.amplifyframework.datastore.AmplifyDisposables) ForEach(com.amplifyframework.util.ForEach) Collections(java.util.Collections) GraphQLRequest(com.amplifyframework.api.graphql.GraphQLRequest) ModelWithMetadata(com.amplifyframework.datastore.appsync.ModelWithMetadata) QueryPredicate(com.amplifyframework.core.model.query.predicate.QueryPredicate) AtomicReference(java.util.concurrent.atomic.AtomicReference)

Aggregations

AppSync (com.amplifyframework.datastore.appsync.AppSync)10 ModelProvider (com.amplifyframework.core.model.ModelProvider)8 Model (com.amplifyframework.core.model.Model)7 ModelSchema (com.amplifyframework.core.model.ModelSchema)7 SchemaRegistry (com.amplifyframework.core.model.SchemaRegistry)7 DataStoreException (com.amplifyframework.datastore.DataStoreException)7 ModelWithMetadata (com.amplifyframework.datastore.appsync.ModelWithMetadata)7 Completable (io.reactivex.rxjava3.core.Completable)7 List (java.util.List)7 GraphQLResponse (com.amplifyframework.api.graphql.GraphQLResponse)6 Consumer (com.amplifyframework.core.Consumer)6 Cancelable (com.amplifyframework.core.async.Cancelable)6 BlogOwner (com.amplifyframework.testmodels.commentsblog.BlogOwner)6 Observable (io.reactivex.rxjava3.core.Observable)6 TimeUnit (java.util.concurrent.TimeUnit)6 SubscriptionType (com.amplifyframework.api.graphql.SubscriptionType)5 Action (com.amplifyframework.core.Action)5 DataStoreConfiguration (com.amplifyframework.datastore.DataStoreConfiguration)5 AmplifyModelProvider (com.amplifyframework.testmodels.commentsblog.AmplifyModelProvider)5 Temporal (com.amplifyframework.core.model.temporal.Temporal)4