use of com.amplifyframework.core.model.ModelSchema in project amplify-android by aws-amplify.
the class MutationPersistenceInstrumentationTest method obtainLocalStorageAndValidateModelSchema.
/**
* Prepare an instance of {@link LocalStorageAdapter}. Evaluate its
* emitted collection of ModelSchema, to ensure that
* {@link PendingMutation.PersistentRecord} is among them.
*
* TODO: later, consider hiding system schema, such as the
* {@link PendingMutation.PersistentRecord}, from the callback. This schema might be
* an implementation detail, that is working as a leaky abstraction.
*
* @throws AmplifyException On failure to initialize the storage adapter,
* or on failure to load model schema into registry
*/
@Before
public void obtainLocalStorageAndValidateModelSchema() throws AmplifyException {
this.converter = new GsonPendingMutationConverter();
getApplicationContext().deleteDatabase(DATABASE_NAME);
ModelProvider modelProvider = SimpleModelProvider.withRandomVersion(BlogOwner.class);
schemaRegistry = SchemaRegistry.instance();
schemaRegistry.clear();
schemaRegistry.register(modelProvider.models());
LocalStorageAdapter localStorageAdapter = SQLiteStorageAdapter.forModels(schemaRegistry, modelProvider);
this.storage = SynchronousStorageAdapter.delegatingTo(localStorageAdapter);
List<ModelSchema> initializationResults = storage.initialize(getApplicationContext());
// Evaluate the returned set of ModelSchema. Ensure that there is one
// for the PendingMutation.PersistentRecord system class.
assertTrue(Observable.fromIterable(initializationResults).map(ModelSchema::getName).toList().blockingGet().contains(PendingMutation.PersistentRecord.class.getSimpleName()));
}
use of com.amplifyframework.core.model.ModelSchema in project amplify-android by aws-amplify.
the class SubscriptionProcessor method subscriptionObservable.
private <T extends Model> Observable<SubscriptionEvent<? extends Model>> subscriptionObservable(AppSync appSync, SubscriptionType subscriptionType, AbortableCountDownLatch<DataStoreException> latch, ModelSchema modelSchema) {
return Observable.<GraphQLResponse<ModelWithMetadata<T>>>create(emitter -> {
SubscriptionMethod method = subscriptionMethodFor(appSync, subscriptionType);
AtomicReference<String> subscriptionId = new AtomicReference<>();
Cancelable cancelable = method.subscribe(modelSchema, token -> {
LOG.debug("Subscription started for " + subscriptionType.name() + " " + modelSchema.getName() + " subscriptionId: " + token);
subscriptionId.set(token);
latch.countDown();
}, emitter::onNext, dataStoreException -> {
if (isExceptionType(dataStoreException, AppSyncErrorType.UNAUTHORIZED)) {
// Ignore Unauthorized errors, so that DataStore can still be used even if the user is only
// authorized to read a subset of the models.
latch.countDown();
LOG.warn("Unauthorized failure:" + subscriptionType.name() + " " + modelSchema.getName());
} else if (isExceptionType(dataStoreException, AppSyncErrorType.OPERATION_DISABLED)) {
// Ignore OperationDisabled errors, so that DataStore can be used even without subscriptions.
// This logic is only in place to address a specific use case, and should not be used without
// unless you have consulted with AWS. It is subject to be deprecated/removed in the future.
latch.countDown();
LOG.warn("Operation disabled:" + subscriptionType.name() + " " + modelSchema.getName());
} else {
if (latch.getCount() > 0) {
// An error occurred during startup. Abort and notify the Orchestrator by throwing the
// exception from startSubscriptions.
latch.abort(dataStoreException);
} else {
// An error occurred after startup. Notify the Orchestrator via the onFailure action.
onFailure.accept(dataStoreException);
}
}
}, () -> {
LOG.debug("Subscription completed:" + subscriptionId.get());
emitter.onComplete();
});
// When the observable is disposed, we need to call cancel() on the subscription
// so it can properly dispose of resources if necessary. For the AWS API plugin,
// this means closing the underlying network connection.
emitter.setDisposable(AmplifyDisposables.fromCancelable(cancelable));
}).doOnError(subscriptionError -> LOG.warn("An error occurred on the remote " + subscriptionType.name() + " subscription for model " + modelSchema.getName(), subscriptionError)).subscribeOn(Schedulers.io()).observeOn(Schedulers.io()).map(SubscriptionProcessor::unwrapResponse).filter(modelWithMetadata -> {
QueryPredicate predicate = queryPredicateProvider.getPredicate(modelSchema.getName());
return predicate.evaluate(modelWithMetadata.getModel());
}).map(modelWithMetadata -> SubscriptionEvent.<T>builder().type(fromSubscriptionType(subscriptionType)).modelWithMetadata(modelWithMetadata).modelSchema(modelSchema).build());
}
use of com.amplifyframework.core.model.ModelSchema in project amplify-android by aws-amplify.
the class SubscriptionProcessor method startSubscriptions.
/**
* Start subscribing to model mutations.
*/
synchronized void startSubscriptions() throws DataStoreException {
int subscriptionCount = modelProvider.modelNames().size() * SubscriptionType.values().length;
// Create a latch with the number of subscriptions are requesting. Each of these will be
// counted down when each subscription's onStarted event is called.
AbortableCountDownLatch<DataStoreException> latch = new AbortableCountDownLatch<>(subscriptionCount);
// Need to create a new buffer so we can properly handle retries and stop/start scenarios.
// Re-using the same buffer has some unexpected results due to the replay aspect of the subject.
buffer = ReplaySubject.create();
Set<Observable<SubscriptionEvent<? extends Model>>> subscriptions = new HashSet<>();
for (ModelSchema modelSchema : modelProvider.modelSchemas().values()) {
for (SubscriptionType subscriptionType : SubscriptionType.values()) {
subscriptions.add(subscriptionObservable(appSync, subscriptionType, latch, modelSchema));
}
}
ongoingOperationsDisposable.add(Observable.merge(subscriptions).subscribeOn(Schedulers.io()).observeOn(Schedulers.io()).doOnSubscribe(disposable -> LOG.info("Starting processing subscription events.")).doOnError(failure -> LOG.warn("Reading subscription events has failed.", failure)).doOnComplete(() -> LOG.warn("Reading subscription events is completed.")).subscribe(buffer::onNext, buffer::onError, buffer::onComplete));
boolean subscriptionsStarted;
try {
LOG.debug("Waiting for subscriptions to start.");
subscriptionsStarted = latch.abortableAwait(adjustedTimeoutSeconds, TimeUnit.SECONDS);
} catch (InterruptedException exception) {
LOG.warn("Subscription operations were interrupted during setup.");
return;
}
if (subscriptionsStarted) {
Amplify.Hub.publish(HubChannel.DATASTORE, HubEvent.create(DataStoreChannelEventName.SUBSCRIPTIONS_ESTABLISHED));
LOG.info(String.format(Locale.US, "Started subscription processor for models: %s of types %s.", modelProvider.modelNames(), Arrays.toString(SubscriptionType.values())));
} else {
throw new DataStoreException("Timed out waiting for subscription processor to start.", "Retry");
}
}
use of com.amplifyframework.core.model.ModelSchema in project amplify-android by aws-amplify.
the class AppSyncRequestFactoryTest method validateMatchNonePredicateForSyncExpressionIsWrappedWithAnd.
/**
* If a MatchNoneQueryPredicate is provided, it should be wrapped in an AND group.
* This enables AppSync to optimize by performing an DDB query instead of scan.
* @throws AmplifyException On failure to parse ModelSchema from model class
* @throws JSONException from JSONAssert.assertEquals.
*/
@Test
public void validateMatchNonePredicateForSyncExpressionIsWrappedWithAnd() throws AmplifyException, JSONException {
ModelSchema schema = ModelSchema.fromModelClass(BlogOwner.class);
final GraphQLRequest<Iterable<Post>> request = AppSyncRequestFactory.buildSyncRequest(schema, null, null, QueryPredicates.none());
JSONAssert.assertEquals(Resources.readAsString("base-sync-request-with-predicate-match-none.txt"), request.getContent(), true);
}
use of com.amplifyframework.core.model.ModelSchema in project amplify-android by aws-amplify.
the class AppSyncRequestFactoryTest method ownerFieldIsNotRemovedIfSet.
/**
* Verify that the owner field is NOT removed if the value is set.
* @throws AmplifyException On failure to build schema
*/
@Test
public void ownerFieldIsNotRemovedIfSet() throws AmplifyException {
// Expect
Map<String, Object> expected = new HashMap<>();
expected.put("id", "111");
expected.put("description", "Mop the floor");
expected.put("owner", "johndoe");
// Act
Todo todo = new Todo("111", "Mop the floor", "johndoe");
ModelSchema schema = ModelSchema.fromModelClass(Todo.class);
@SuppressWarnings("unchecked") Map<String, Object> actual = (Map<String, Object>) AppSyncRequestFactory.buildCreationRequest(schema, todo, DEFAULT_STRATEGY).getVariables().get("input");
// Assert
assertEquals(expected, actual);
}
Aggregations