Search in sources :

Example 16 with QueryPredicate

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);
}
Also used : QueryPredicate(com.amplifyframework.core.model.query.predicate.QueryPredicate) ArrayList(java.util.ArrayList) BlogOwner(com.amplifyframework.testmodels.commentsblog.BlogOwner) HubAccumulator(com.amplifyframework.testutils.HubAccumulator) Test(org.junit.Test)

Example 17 with QueryPredicate

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));
}
Also used : Arrays(java.util.Arrays) BeforeClass(org.junit.BeforeClass) ObserveQueryOptions(com.amplifyframework.core.model.query.ObserveQueryOptions) Blog(com.amplifyframework.testmodels.commentsblog.Blog) ArrayList(java.util.ArrayList) SynchronousStorageAdapter(com.amplifyframework.datastore.storage.SynchronousStorageAdapter) HashSet(java.util.HashSet) Consumer(com.amplifyframework.core.Consumer) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) After(org.junit.After) QueryPredicate(com.amplifyframework.core.model.query.predicate.QueryPredicate) NoOpConsumer(com.amplifyframework.core.NoOpConsumer) PostStatus(com.amplifyframework.testmodels.commentsblog.PostStatus) DataStoreQuerySnapshot(com.amplifyframework.datastore.DataStoreQuerySnapshot) Before(org.junit.Before) BlogOwner(com.amplifyframework.testmodels.commentsblog.BlogOwner) Comment(com.amplifyframework.testmodels.commentsblog.Comment) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) Action(com.amplifyframework.core.Action) StrictMode(com.amplifyframework.datastore.StrictMode) 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) QuerySortBy(com.amplifyframework.core.model.query.QuerySortBy) Comparator(java.util.Comparator) NoOpAction(com.amplifyframework.core.NoOpAction) AmplifyModelProvider(com.amplifyframework.testmodels.commentsblog.AmplifyModelProvider) Post(com.amplifyframework.testmodels.commentsblog.Post) Collections(java.util.Collections) QueryPredicate.not(com.amplifyframework.core.model.query.predicate.QueryPredicate.not) Assert.assertEquals(org.junit.Assert.assertEquals) DataStoreException(com.amplifyframework.datastore.DataStoreException) Action(com.amplifyframework.core.Action) NoOpAction(com.amplifyframework.core.NoOpAction) QueryPredicate(com.amplifyframework.core.model.query.predicate.QueryPredicate) Post(com.amplifyframework.testmodels.commentsblog.Post) ArrayList(java.util.ArrayList) CountDownLatch(java.util.concurrent.CountDownLatch) DataStoreQuerySnapshot(com.amplifyframework.datastore.DataStoreQuerySnapshot) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ObserveQueryOptions(com.amplifyframework.core.model.query.ObserveQueryOptions) BlogOwner(com.amplifyframework.testmodels.commentsblog.BlogOwner) ArrayList(java.util.ArrayList) List(java.util.List) Blog(com.amplifyframework.testmodels.commentsblog.Blog) Cancelable(com.amplifyframework.core.async.Cancelable) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 18 with QueryPredicate

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));
}
Also used : QueryPredicate(com.amplifyframework.core.model.query.predicate.QueryPredicate) BlogOwner(com.amplifyframework.testmodels.commentsblog.BlogOwner) Test(org.junit.Test)

Example 19 with QueryPredicate

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());
}
Also used : QueryPredicate(com.amplifyframework.core.model.query.predicate.QueryPredicate) Post(com.amplifyframework.testmodels.commentsblog.Post) ArrayList(java.util.ArrayList) BlogOwner(com.amplifyframework.testmodels.commentsblog.BlogOwner) Blog(com.amplifyframework.testmodels.commentsblog.Blog) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 20 with QueryPredicate

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());
}
Also used : Temporal(com.amplifyframework.core.model.temporal.Temporal) QueryPredicate(com.amplifyframework.core.model.query.predicate.QueryPredicate) ArrayList(java.util.ArrayList) BlogOwner(com.amplifyframework.testmodels.commentsblog.BlogOwner) HashSet(java.util.HashSet) Test(org.junit.Test)

Aggregations

QueryPredicate (com.amplifyframework.core.model.query.predicate.QueryPredicate)31 Test (org.junit.Test)17 ArrayList (java.util.ArrayList)12 BlogOwner (com.amplifyframework.testmodels.commentsblog.BlogOwner)11 ModelSchema (com.amplifyframework.core.model.ModelSchema)9 DataStoreException (com.amplifyframework.datastore.DataStoreException)9 HashSet (java.util.HashSet)8 List (java.util.List)7 NonNull (androidx.annotation.NonNull)6 SQLPredicate (com.amplifyframework.datastore.storage.sqlite.adapter.SQLPredicate)6 Consumer (com.amplifyframework.core.Consumer)5 Cancelable (com.amplifyframework.core.async.Cancelable)5 Model (com.amplifyframework.core.model.Model)5 SerializedModel (com.amplifyframework.core.model.SerializedModel)5 SQLiteTable (com.amplifyframework.datastore.storage.sqlite.adapter.SQLiteTable)5 Action (com.amplifyframework.core.Action)4 TimeUnit (java.util.concurrent.TimeUnit)4 AmplifyException (com.amplifyframework.AmplifyException)3 Amplify (com.amplifyframework.core.Amplify)3 ModelProvider (com.amplifyframework.core.model.ModelProvider)3