use of com.amplifyframework.datastore.DataStoreException in project amplify-android by aws-amplify.
the class SQLiteStorageAdapterObserveQueryTest method queryWithMaliciousPredicates.
/**
* Test query with SQL injection.
*
* @throws DataStoreException On unexpected failure manipulating items in/out of DataStore
* @throws InterruptedException On unexpected failure manipulating items in/out of DataStore
*/
@Test
public void queryWithMaliciousPredicates() throws DataStoreException, InterruptedException {
final BlogOwner jane = BlogOwner.builder().name("Jane Doe").build();
adapter.save(jane);
QueryPredicate predicate = BlogOwner.NAME.eq("Jane; DROP TABLE Person; --");
CountDownLatch latch = new CountDownLatch(2);
Consumer<Cancelable> observationStarted = NoOpConsumer.create();
Consumer<DataStoreQuerySnapshot<BlogOwner>> onMaliciousQuerySnapshot = resultOfMaliciousQuery -> {
assertTrue(resultOfMaliciousQuery.getItems().isEmpty());
latch.countDown();
};
Consumer<DataStoreException> onObservationError = value -> {
};
Action onObservationComplete = NoOpAction.create();
adapter.observeQuery(BlogOwner.class, new ObserveQueryOptions(predicate, null), observationStarted, onMaliciousQuerySnapshot, onObservationError, onObservationComplete);
Consumer<DataStoreQuerySnapshot<BlogOwner>> onAfterMaliciousQuery = resultAfterMaliciousQuery -> {
assertTrue(resultAfterMaliciousQuery.getItems().contains(jane));
latch.countDown();
};
adapter.observeQuery(BlogOwner.class, new ObserveQueryOptions(null, null), observationStarted, onAfterMaliciousQuery, onObservationError, onObservationComplete);
assertTrue(latch.await(1, TimeUnit.SECONDS));
}
use of com.amplifyframework.datastore.DataStoreException in project amplify-android by aws-amplify.
the class SQLiteStorageAdapterObserveQueryTest method querySavedDataWithMultipleItemsThenItemSaves.
/**
* Test querying the saved item in the SQLite database with observeQuery.
*
* @throws DataStoreException On unexpected failure manipulating items in/out of DataStore
* @throws InterruptedException On unexpected failure manipulating items in/out of DataStore
*/
// @Test
public void querySavedDataWithMultipleItemsThenItemSaves() throws DataStoreException, InterruptedException {
CountDownLatch latch = new CountDownLatch(1);
CountDownLatch changeLatch = new CountDownLatch(1);
Consumer<Cancelable> observationStarted = NoOpConsumer.create();
Consumer<DataStoreException> onObservationError = NoOpConsumer.create();
Action onObservationComplete = NoOpAction.create();
final List<BlogOwner> savedModels = new ArrayList<>();
final int numModels = 10;
AtomicInteger count = new AtomicInteger(0);
for (int counter = 0; counter < numModels; counter++) {
final BlogOwner blogOwner = BlogOwner.builder().name("namePrefix:" + counter).build();
adapter.save(blogOwner);
savedModels.add(blogOwner);
}
Consumer<DataStoreQuerySnapshot<BlogOwner>> onQuerySnapshot = value -> {
if (count.get() == 0) {
for (BlogOwner blogOwner : savedModels) {
assertTrue(value.getItems().contains(blogOwner));
}
latch.countDown();
} else {
assertEquals(12, value.getItems().size());
changeLatch.countDown();
}
count.incrementAndGet();
};
adapter.observeQuery(BlogOwner.class, new ObserveQueryOptions(null, null), observationStarted, onQuerySnapshot, onObservationError, onObservationComplete);
assertTrue(latch.await(30, TimeUnit.SECONDS));
for (int counter = 11; counter < 13; counter++) {
final BlogOwner blogOwner = BlogOwner.builder().name("namePrefix:" + counter).build();
savedModels.add(blogOwner);
adapter.save(blogOwner);
}
assertTrue(changeLatch.await(30, TimeUnit.SECONDS));
}
use of com.amplifyframework.datastore.DataStoreException in project amplify-android by aws-amplify.
the class SQLiteStorageAdapterObserveQueryTest method querySavedDataWithPredicatesOnForeignKey.
/**
* Test querying with predicate condition on connected model.
*
* @throws DataStoreException On unexpected failure manipulating items in/out of DataStore
* @throws InterruptedException On unexpected failure manipulating items in/out of DataStore
*/
@Test
public void querySavedDataWithPredicatesOnForeignKey() throws DataStoreException, InterruptedException {
final BlogOwner blogOwner = BlogOwner.builder().name("Jane Doe").build();
adapter.save(blogOwner);
final Blog blog = Blog.builder().name("Jane's Commercial Real Estate Blog").owner(blogOwner).build();
adapter.save(blog);
CountDownLatch latch = new CountDownLatch(1);
Consumer<Cancelable> observationStarted = NoOpConsumer.create();
Consumer<DataStoreQuerySnapshot<Blog>> onQuerySnapshot = value -> {
assertTrue(value.getItems().contains(blog));
latch.countDown();
};
Consumer<DataStoreException> onObservationError = NoOpConsumer.create();
Action onObservationComplete = NoOpAction.create();
adapter.observeQuery(Blog.class, new ObserveQueryOptions(BlogOwner.NAME.eq("Jane Doe"), null), observationStarted, onQuerySnapshot, onObservationError, onObservationComplete);
assertTrue(latch.await(1, TimeUnit.SECONDS));
}
use of com.amplifyframework.datastore.DataStoreException in project amplify-android by aws-amplify.
the class SQLiteStorageAdapterObserveQueryTest method querySavedDataWithForeignKey.
/**
* Test that querying the saved item with a foreign key with observeQuery
* also populates that instance variable with object.
*
* @throws DataStoreException On unexpected failure manipulating items in/out of DataStore
* @throws InterruptedException On unexpected failure manipulating items in/out of DataStore
*/
@Test
public void querySavedDataWithForeignKey() throws DataStoreException, InterruptedException {
CountDownLatch latch = new CountDownLatch(1);
final BlogOwner blogOwner = BlogOwner.builder().name("Alan Turing").build();
final Blog blog = Blog.builder().name("Alan's Software Blog").owner(blogOwner).build();
adapter.save(blogOwner);
adapter.save(blog);
Consumer<Cancelable> observationStarted = NoOpConsumer.create();
Consumer<DataStoreQuerySnapshot<Blog>> onQuerySnapshot = value -> {
assertTrue(value.getItems().contains(blog));
latch.countDown();
};
Consumer<DataStoreException> onObservationError = NoOpConsumer.create();
Action onObservationComplete = NoOpAction.create();
adapter.observeQuery(Blog.class, new ObserveQueryOptions(null, null), observationStarted, onQuerySnapshot, onObservationError, onObservationComplete);
assertTrue(latch.await(1, TimeUnit.SECONDS));
}
use of com.amplifyframework.datastore.DataStoreException in project amplify-android by aws-amplify.
the class InMemoryStorageAdapter method delete.
// item.getClass() -> Class<?>, but type is T. So cast as Class<T> is OK.
@SuppressWarnings("unchecked")
@Override
public <T extends Model> void delete(@NonNull Class<T> itemClass, @NonNull StorageItemChange.Initiator initiator, @NonNull QueryPredicate predicate, @NonNull Action onSuccess, @NonNull Consumer<DataStoreException> onError) {
final ModelSchema schema;
try {
schema = ModelSchema.fromModelClass(itemClass);
} catch (AmplifyException schemaBuildFailure) {
onError.accept(new DataStoreException("Failed to build model schema.", schemaBuildFailure, "Verify your model."));
return;
}
for (Model savedItem : items) {
if (!itemClass.isInstance(savedItem) || !predicate.evaluate(savedItem)) {
continue;
}
items.remove(savedItem);
StorageItemChange<T> deletion = StorageItemChange.<T>builder().item((T) savedItem).modelSchema(schema).type(StorageItemChange.Type.DELETE).predicate(predicate).initiator(initiator).build();
itemChangeStream.onNext(deletion);
}
onSuccess.call();
}
Aggregations