use of com.amplifyframework.core.model.query.predicate.QueryPredicate in project amplify-android by aws-amplify.
the class BasicCloudSyncInstrumentationTest method createWaitThenUpdate10TimesWithPredicate.
/**
* The test is to test consecutive updates with predicate.
* @throws DataStoreException On failure to save or query items from DataStore.
* @throws ApiException On failure to query the API.
*/
@Test
public void createWaitThenUpdate10TimesWithPredicate() throws DataStoreException, ApiException {
// Setup
BlogOwner owner = BlogOwner.builder().name("Blogger").wea("ryt").build();
String modelName = BlogOwner.class.getSimpleName();
QueryPredicate predicate = BlogOwner.WEA.beginsWith("r");
// Setup 10 updates
List<String> weas = Arrays.asList("ron", "rth", "rer", "rly", "ren", "rel", "ral", "rec", "rin", "reh");
List<BlogOwner> owners = new ArrayList<>();
for (int i = 0; i < weas.size(); i++) {
BlogOwner updatedOwner = owner.copyOfBuilder().wea(weas.get(i)).build();
owners.add(updatedOwner);
}
HubAccumulator accumulator = HubAccumulator.create(HubChannel.DATASTORE, publicationOf(modelName, owner.getId()), 1).start();
// Create an item.
dataStore.save(owner);
// Wait for the sync.
accumulator.await(TIMEOUT_SECONDS, TimeUnit.SECONDS);
// Make 10 consecutive updates with predicate
HubAccumulator updateAccumulator = HubAccumulator.create(HubChannel.DATASTORE, publicationOf(modelName, owner.getId()), 10).start();
for (int i = 0; i < weas.size(); i++) {
dataStore.save(owners.get(i), predicate);
}
updateAccumulator.await(TIMEOUT_SECONDS, TimeUnit.SECONDS);
BlogOwner lastUpdate = owners.get(owners.size() - 1);
BlogOwner localOwner = dataStore.get(BlogOwner.class, lastUpdate.getId());
ModelAssert.assertEqualsIgnoringTimestamps(lastUpdate, localOwner);
BlogOwner remoteOwner = api.get(BlogOwner.class, lastUpdate.getId());
ModelAssert.assertEqualsIgnoringTimestamps(lastUpdate, remoteOwner);
}
use of com.amplifyframework.core.model.query.predicate.QueryPredicate in project amplify-android by aws-amplify.
the class SQLiteStorageAdapterObserveQueryTest method querySavedDataWithNumericalPredicates.
/**
* Test querying the saved item in the SQLite database with
* predicate conditions.
*
* @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 querySavedDataWithNumericalPredicates() throws DataStoreException, InterruptedException {
CountDownLatch latch = new CountDownLatch(1);
CountDownLatch changeLatch = new CountDownLatch(1);
final List<Post> savedModels = new ArrayList<>();
final int numModels = 10;
BlogOwner blogOwner = BlogOwner.builder().name("Test Dummy").build();
adapter.save(blogOwner);
Blog blog = Blog.builder().name("Blogging for Dummies").owner(blogOwner).build();
adapter.save(blog);
for (int counter = 0; counter < numModels; counter++) {
final Post post = Post.builder().title("titlePrefix:" + counter).status(PostStatus.INACTIVE).rating(counter).blog(blog).build();
adapter.save(post);
savedModels.add(post);
}
// 1, 4, 5, 6
QueryPredicate predicate = Post.RATING.ge(4).and(Post.RATING.lt(7)).or(Post.RATING.eq(1).and(Post.RATING.ne(7)));
Consumer<Cancelable> observationStarted = NoOpConsumer.create();
AtomicInteger count = new AtomicInteger(0);
Consumer<DataStoreQuerySnapshot<Post>> onQuerySnapshot = value -> {
if (count.get() == 0) {
List<Post> expected = Arrays.asList(savedModels.get(1), savedModels.get(4), savedModels.get(5), savedModels.get(6));
assertEquals(new HashSet<>(expected), new HashSet<>(value.getItems()));
latch.countDown();
} else if (count.get() == 2) {
assertEquals(5, value.getItems().size());
assertTrue(value.getItems().contains(savedModels.get(1)));
assertTrue(value.getItems().contains(savedModels.get(4)));
assertTrue(value.getItems().contains(savedModels.get(5)));
assertTrue(value.getItems().contains(savedModels.get(6)));
assertTrue(value.getItems().contains(savedModels.get(11)));
changeLatch.countDown();
}
count.incrementAndGet();
};
Consumer<DataStoreException> onObservationError = NoOpConsumer.create();
Action onObservationComplete = NoOpAction.create();
adapter.observeQuery(Post.class, new ObserveQueryOptions(predicate, null), observationStarted, onQuerySnapshot, onObservationError, onObservationComplete);
assertTrue(latch.await(15, TimeUnit.SECONDS));
for (int counter = 3; counter < 5; counter++) {
final Post post = Post.builder().title("titlePrefix:" + counter + "change").status(PostStatus.INACTIVE).rating(counter).blog(blog).build();
adapter.save(post);
savedModels.add(post);
}
assertTrue(changeLatch.await(30, TimeUnit.SECONDS));
}
use of com.amplifyframework.core.model.query.predicate.QueryPredicate in project amplify-android by aws-amplify.
the class SQLiteStorageAdapterQueryTest method queryWithMaliciousPredicates.
/**
* Test query with SQL injection.
* @throws DataStoreException On unexpected failure manipulating items in/out of DataStore
*/
@Test
public void queryWithMaliciousPredicates() throws DataStoreException {
final BlogOwner jane = BlogOwner.builder().name("Jane Doe").build();
adapter.save(jane);
QueryPredicate predicate = BlogOwner.NAME.eq("Jane; DROP TABLE Person; --");
final List<BlogOwner> resultOfMaliciousQuery = adapter.query(BlogOwner.class, Where.matches(predicate));
assertTrue(resultOfMaliciousQuery.isEmpty());
final List<BlogOwner> resultAfterMaliciousQuery = adapter.query(BlogOwner.class);
assertTrue(resultAfterMaliciousQuery.contains(jane));
}
use of com.amplifyframework.core.model.query.predicate.QueryPredicate in project amplify-android by aws-amplify.
the class SQLiteStorageAdapterQueryTest method querySavedDataWithNumericalPredicates.
/**
* Test querying the saved item in the SQLite database with
* predicate conditions.
* @throws DataStoreException On unexpected failure manipulating items in/out of DataStore
*/
@Test
public void querySavedDataWithNumericalPredicates() throws DataStoreException {
final List<Post> savedModels = new ArrayList<>();
final int numModels = 10;
BlogOwner blogOwner = BlogOwner.builder().name("Test Dummy").build();
adapter.save(blogOwner);
Blog blog = Blog.builder().name("Blogging for Dummies").owner(blogOwner).build();
adapter.save(blog);
for (int counter = 0; counter < numModels; counter++) {
final Post post = Post.builder().title("titlePrefix:" + counter).status(PostStatus.INACTIVE).rating(counter).blog(blog).build();
adapter.save(post);
savedModels.add(post);
}
// 1, 4, 5, 6
QueryPredicate predicate = Post.RATING.ge(4).and(Post.RATING.lt(7)).or(Post.RATING.eq(1).and(Post.RATING.ne(7)));
assertEquals(Observable.fromArray(1, 4, 5, 6).map(savedModels::get).toList().map(HashSet::new).blockingGet(), Observable.fromIterable(adapter.query(Post.class, Where.matches(predicate))).toList().map(HashSet::new).blockingGet());
}
use of com.amplifyframework.core.model.query.predicate.QueryPredicate in project amplify-android by aws-amplify.
the class SQLiteStorageAdapterQueryTest method querySavedDataWithDateTimePredicates.
/**
* Test querying the saved item in the SQLite database with DateTime
* predicate conditions.
* @throws DataStoreException On unexpected failure manipulating items in/out of DataStore
*/
@Test
public void querySavedDataWithDateTimePredicates() throws DataStoreException {
final List<BlogOwner> savedModels = new ArrayList<>();
final int numModels = 8;
final List<Temporal.DateTime> createdAtTimes = Arrays.asList(new Temporal.DateTime("2020-01-01T19:30:45.000000000Z"), new Temporal.DateTime("2020-01-01T19:30:45.100000000Z"), new Temporal.DateTime("2020-01-01T19:30:45.100250000Z"), new Temporal.DateTime("2020-01-01T19:30:45.1000Z"), new Temporal.DateTime("2020-01-01T20:30:45.111Z"), new Temporal.DateTime("2020-01-01T19:30:45.111+00:00"), new Temporal.DateTime("2020-01-01T19:30:45.111+01:00"), new Temporal.DateTime("2020-01-01T19:30:45.111222333Z"));
for (int counter = 0; counter < numModels; counter++) {
BlogOwner blogOwner = BlogOwner.builder().name("Test Blogger " + counter).createdAt(createdAtTimes.get(counter)).build();
adapter.save(blogOwner);
savedModels.add(blogOwner);
}
// 0, 1, 3, 6
QueryPredicate predicate = BlogOwner.CREATED_AT.le(new Temporal.DateTime("2020-01-01T19:30:45.100000000Z"));
assertEquals(Observable.fromArray(0, 1, 3, 6).map(savedModels::get).map(BlogOwner::getId).toList().map(HashSet::new).blockingGet(), Observable.fromIterable(adapter.query(BlogOwner.class, Where.matches(predicate))).map(BlogOwner::getId).toList().map(HashSet::new).blockingGet());
}
Aggregations