use of com.amplifyframework.core.model.ModelSchema 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);
}
use of com.amplifyframework.core.model.ModelSchema 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));
});
}
use of com.amplifyframework.core.model.ModelSchema in project amplify-android by aws-amplify.
the class SqlCommandTest method modelWithIndexReturnsExpectedCreateIndexCommand.
/**
* Test if {@link ModelSchema} with index returns an expected
* CREATE INDEX SQL command.
*/
@Test
public void modelWithIndexReturnsExpectedCreateIndexCommand() {
final ModelIndex index = ModelIndex.builder().indexName("idBasedIndex").indexFieldNames(Collections.singletonList("id")).build();
final ModelSchema modelSchema = ModelSchema.builder().name("Person").indexes(Collections.singletonMap("idBasedIndex", index)).build();
final Iterator<SqlCommand> sqlCommandIterator = sqlCommandFactory.createIndexesFor(modelSchema).iterator();
assertTrue(sqlCommandIterator.hasNext());
final SqlCommand createIndexSqlCommand = sqlCommandIterator.next();
assertEquals("Person", createIndexSqlCommand.tableName());
assertEquals("CREATE INDEX IF NOT EXISTS `idBasedIndex` ON `Person` (`id`);", createIndexSqlCommand.sqlStatement());
}
use of com.amplifyframework.core.model.ModelSchema in project amplify-android by aws-amplify.
the class SqlCommandTest method queryWithFirstResultPaginationInput.
/**
* Validates that the correct bindings are generated when usign the {@link Page#firstResult()}
* pagination option.
* @throws DataStoreException From {@link SQLCommandFactory#queryFor(ModelSchema, QueryOptions)}
*/
@Test
public void queryWithFirstResultPaginationInput() throws DataStoreException {
final ModelSchema personSchema = getPersonModelSchema();
final SqlCommand sqlCommand = sqlCommandFactory.queryFor(personSchema, Where.matchesAll().paginated(Page.firstResult()));
assertNotNull(sqlCommand);
assertEquals(PERSON_BASE_QUERY + " LIMIT ? OFFSET ?;", sqlCommand.sqlStatement());
final List<Object> bindings = sqlCommand.getBindings();
assertEquals(2, bindings.size());
assertEquals(1, bindings.get(0));
assertEquals(0, bindings.get(1));
}
use of com.amplifyframework.core.model.ModelSchema in project amplify-android by aws-amplify.
the class SqlCommandTest method existsFor.
/**
* Verify the SqlCommand generated to check if a model exists is as expected.
* @throws DataStoreException From {@link SQLCommandFactory#existsFor(ModelSchema, QueryPredicate)}
*/
@Test
public void existsFor() throws DataStoreException {
final ModelSchema personSchema = getPersonModelSchema();
String personId = RandomString.string();
final SqlCommand sqlCommand = sqlCommandFactory.existsFor(personSchema, Person.ID.eq(personId));
assertEquals("SELECT EXISTS(SELECT 1 FROM `Person` WHERE id = ?);", sqlCommand.sqlStatement());
assertEquals(Collections.singletonList(personId), sqlCommand.getBindings());
}
Aggregations