Search in sources :

Example 11 with Blog

use of com.amplifyframework.testmodels.commentsblog.Blog 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 12 with Blog

use of com.amplifyframework.testmodels.commentsblog.Blog in project amplify-android by aws-amplify.

the class SQLiteStorageAdapterQueryTest method queryWithOrderByRelatedModel.

/**
 * Test query with order by.  Validate that a list of Blog can be sorted by the names of BlogOwners.
 * @throws DataStoreException On failure to arrange items into store, or from the query action itself
 */
@Test
public void queryWithOrderByRelatedModel() throws DataStoreException {
    // Expect: Create BlogOwners and their respective blogs
    List<String> names = Arrays.asList("Joe", "Bob", "Dan", "Jane");
    List<Blog> blogs = new ArrayList<>();
    for (String name : names) {
        BlogOwner owner = BlogOwner.builder().name(name).build();
        adapter.save(owner);
        Blog blog = Blog.builder().name("").owner(owner).build();
        adapter.save(blog);
        blogs.add(blog);
    }
    // Act: Query Blogs sorted by owner's name
    List<Blog> result = adapter.query(Blog.class, Where.sorted(BlogOwner.NAME.ascending()));
    // Verify: Query result is sorted by owner's name
    List<Blog> sorted = new ArrayList<>(blogs);
    Collections.sort(sorted, Comparator.comparing(blog -> blog.getOwner().getName()));
    assertEquals(sorted, result);
}
Also used : Arrays(java.util.Arrays) BeforeClass(org.junit.BeforeClass) Blog(com.amplifyframework.testmodels.commentsblog.Blog) ArrayList(java.util.ArrayList) SynchronousStorageAdapter(com.amplifyframework.datastore.storage.SynchronousStorageAdapter) HashSet(java.util.HashSet) Observable(io.reactivex.rxjava3.core.Observable) After(org.junit.After) QueryPredicate(com.amplifyframework.core.model.query.predicate.QueryPredicate) PostStatus(com.amplifyframework.testmodels.commentsblog.PostStatus) Before(org.junit.Before) Page(com.amplifyframework.core.model.query.Page) Phone(com.amplifyframework.testmodels.phonecall.Phone) Assert.assertNotNull(org.junit.Assert.assertNotNull) BlogOwner(com.amplifyframework.testmodels.commentsblog.BlogOwner) Comment(com.amplifyframework.testmodels.commentsblog.Comment) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) Where(com.amplifyframework.core.model.query.Where) StrictMode(com.amplifyframework.datastore.StrictMode) DataStoreException(com.amplifyframework.datastore.DataStoreException) List(java.util.List) Person(com.amplifyframework.testmodels.phonecall.Person) Temporal(com.amplifyframework.core.model.temporal.Temporal) Comparator(java.util.Comparator) AmplifyModelProvider(com.amplifyframework.testmodels.commentsblog.AmplifyModelProvider) Post(com.amplifyframework.testmodels.commentsblog.Post) Collections(java.util.Collections) QueryField.field(com.amplifyframework.core.model.query.predicate.QueryField.field) QueryPredicate.not(com.amplifyframework.core.model.query.predicate.QueryPredicate.not) Assert.assertEquals(org.junit.Assert.assertEquals) Call(com.amplifyframework.testmodels.phonecall.Call) ArrayList(java.util.ArrayList) BlogOwner(com.amplifyframework.testmodels.commentsblog.BlogOwner) Blog(com.amplifyframework.testmodels.commentsblog.Blog) Test(org.junit.Test)

Example 13 with Blog

use of com.amplifyframework.testmodels.commentsblog.Blog in project amplify-android by aws-amplify.

the class HybridAssociationSyncInstrumentationTest method associatedModelAreSyncedDownFromCloud.

/**
 * When the cloud sees an update to its data, the new data should be reflected in the
 * local store. What's more, we should be able to query for the updated data by its model names,
 * and expect to see the result, that way. This should hold for associated models, too.
 * @throws AmplifyException For a variety of reasons, including failure to build schema,
 *                          or bad interaction with API or DataStore
 */
@Ignore("It passes. Not automating due to operational concerns as noted in class-level @Ignore.")
@Test
public void associatedModelAreSyncedDownFromCloud() throws AmplifyException {
    // Create a BlogOwner on the remote system,
    // and wait for it to trickle back to the client.
    BlogOwner owner = BlogOwner.builder().name("Agent Texas").build();
    String ownerModelName = BlogOwner.class.getSimpleName();
    ModelSchema ownerSchema = schemaProvider.modelSchemas().get(ownerModelName);
    assertNotNull(ownerSchema);
    HubAccumulator ownerAccumulator = HubAccumulator.create(HubChannel.DATASTORE, receiptOf(owner.getId()), 1).start();
    appSync.create(owner, ownerSchema);
    ownerAccumulator.await(TIMEOUT_SECONDS, TimeUnit.SECONDS);
    // Now, validate that we see the data locally, when we query for serialized models
    // and by Java BlogOwners.
    Map<String, Object> expectedOwnerData = new HashMap<>();
    expectedOwnerData.put("id", owner.getId());
    expectedOwnerData.put("name", owner.getName());
    List<SerializedModel> actualSerializedOwners = hybridBehaviors.list(ownerSchema.getName());
    assertTrue(actualSerializedOwners.contains(SerializedModel.builder().serializedData(expectedOwnerData).modelSchema(ownerSchema).build()));
    assertTrue(normalBehaviors.list(BlogOwner.class).contains(owner));
    // Now, remotely save a model that has an association to the owner above.
    Blog blog = Blog.builder().name("Blog about Texas").owner(owner).build();
    String blogModelName = Blog.class.getSimpleName();
    ModelSchema blogSchema = schemaProvider.modelSchemas().get(blogModelName);
    assertNotNull(blogSchema);
    HubAccumulator blogAccumulator = HubAccumulator.create(HubChannel.DATASTORE, receiptOf(blog.getId()), 1).start();
    appSync.create(blog, blogSchema);
    blogAccumulator.await(TIMEOUT_SECONDS, TimeUnit.SECONDS);
    // Validate that we can find the newly associated model locally, now.
    Map<String, Object> expectedBlogData = new HashMap<>();
    expectedBlogData.put("id", blog.getId());
    expectedBlogData.put("name", blog.getName());
    expectedBlogData.put("owner", SerializedModel.builder().serializedData(Collections.singletonMap("id", owner.getId())).modelSchema(null).build());
    List<SerializedModel> expectedSerializedBlogs = hybridBehaviors.list(blogSchema.getName());
    assertTrue(expectedSerializedBlogs.contains(SerializedModel.builder().serializedData(expectedBlogData).modelSchema(blogSchema).build()));
    assertTrue(normalBehaviors.list(Blog.class).contains(blog));
}
Also used : ModelSchema(com.amplifyframework.core.model.ModelSchema) HashMap(java.util.HashMap) BlogOwner(com.amplifyframework.testmodels.commentsblog.BlogOwner) HubAccumulator(com.amplifyframework.testutils.HubAccumulator) SerializedModel(com.amplifyframework.core.model.SerializedModel) Blog(com.amplifyframework.testmodels.commentsblog.Blog) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 14 with Blog

use of com.amplifyframework.testmodels.commentsblog.Blog in project amplify-android by aws-amplify.

the class HybridAssociationSyncInstrumentationTest method associatedModelsAreSyncedUpToCloud.

/**
 * When we save {@link SerializedModel}s, we should find them in the cloud,
 * shortly there-after. Saving associated serialized models will work.
 * @throws AmplifyException For a variety of reasons, including failure to build schema,
 *                          or bad interaction with API or DataStore
 */
@Ignore("It passes. Not automating due to operational concerns as noted in class-level @Ignore.")
@Test
public void associatedModelsAreSyncedUpToCloud() throws AmplifyException {
    // First up, we're going to save a "leaf" model, a BlogOwner.
    String ownerModelName = BlogOwner.class.getSimpleName();
    ModelSchema ownerSchema = schemaProvider.modelSchemas().get(ownerModelName);
    assertNotNull(ownerSchema);
    BlogOwner owner = BlogOwner.builder().name("Guillermo Esteban").build();
    Map<String, Object> ownerData = new HashMap<>();
    ownerData.put("id", owner.getId());
    ownerData.put("name", owner.getName());
    SerializedModel serializedOwner = SerializedModel.builder().serializedData(ownerData).modelSchema(ownerSchema).build();
    // Setup an accumulator so we know when there has been a publication.
    HubAccumulator ownerAccumulator = HubAccumulator.create(HubChannel.DATASTORE, publicationOf(ownerModelName, owner.getId()), 1).start();
    hybridBehaviors.save(serializedOwner);
    ownerAccumulator.await(TIMEOUT_SECONDS, TimeUnit.SECONDS);
    // Validate that the Blog Owner was saved locally, and in the cloud.
    List<SerializedModel> allCurrentSerializedOwners = hybridBehaviors.list(ownerSchema.getName());
    assertTrue(allCurrentSerializedOwners.contains(serializedOwner));
    List<BlogOwner> allCurrentBlogOwners = normalBehaviors.list(BlogOwner.class);
    assertTrue(allCurrentBlogOwners.contains(owner));
    assertEquals(owner, api.get(BlogOwner.class, owner.getId()));
    // Now, we're going to save a type with a connection.
    // Build a blog, and its serialized form. Blog has association to a BlogOwner.
    Blog blog = Blog.builder().name("A wonderful blog").owner(owner).build();
    Map<String, Object> blogData = new HashMap<>();
    blogData.put("id", blog.getId());
    blogData.put("name", blog.getName());
    blogData.put("owner", SerializedModel.builder().serializedData(Collections.singletonMap("id", owner.getId())).modelSchema(null).build());
    String blogSchemaName = Blog.class.getSimpleName();
    ModelSchema blogSchema = schemaProvider.modelSchemas().get(blogSchemaName);
    assertNotNull(blogSchema);
    SerializedModel serializedBlog = SerializedModel.builder().serializedData(blogData).modelSchema(blogSchema).build();
    // Save the blog
    HubAccumulator blogAccumulator = HubAccumulator.create(HubChannel.DATASTORE, publicationOf(blogSchemaName, blog.getId()), 1).start();
    hybridBehaviors.save(serializedBlog);
    blogAccumulator.await(TIMEOUT_SECONDS, TimeUnit.SECONDS);
    // Validate that we find the blog locally, and on the remote system.
    List<SerializedModel> allCurrentSerializedBlogs = hybridBehaviors.list(blogSchema.getName());
    assertTrue(allCurrentSerializedBlogs.contains(serializedBlog));
    List<Blog> allCurrentBlogs = normalBehaviors.list(Blog.class);
    assertTrue(allCurrentBlogs.contains(blog));
    Blog foundBlog = api.get(Blog.class, blog.getId());
    assertEquals(blog, foundBlog);
    assertEquals(owner.getId(), foundBlog.getOwner().getId());
}
Also used : ModelSchema(com.amplifyframework.core.model.ModelSchema) HashMap(java.util.HashMap) BlogOwner(com.amplifyframework.testmodels.commentsblog.BlogOwner) HubAccumulator(com.amplifyframework.testutils.HubAccumulator) SerializedModel(com.amplifyframework.core.model.SerializedModel) Blog(com.amplifyframework.testmodels.commentsblog.Blog) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 15 with Blog

use of com.amplifyframework.testmodels.commentsblog.Blog in project amplify-android by aws-amplify.

the class HybridOfflineInstrumentationTest method queryModelsInSerializedForm.

/**
 * Save some ordinary Java language models, then try to query
 * them in serialized form.
 * @throws DataStoreException on failure interacting with DataStore
 */
@Test
public void queryModelsInSerializedForm() throws DataStoreException {
    BlogOwner blogOwner = BlogOwner.builder().name("Johnny Stewart, Blogger Extraordinaire").id("b01ab515-dc82-4e65-99c9-a4ed8d799bed").build();
    normalBehaviors.save(blogOwner);
    Blog blog = Blog.builder().name("Johnny's Exquisite Blog").owner(blogOwner).build();
    normalBehaviors.save(blog);
    Map<String, Object> serializedBlogOwnerData = new HashMap<>();
    serializedBlogOwnerData.put("id", blogOwner.getId());
    serializedBlogOwnerData.put("name", blogOwner.getName());
    assertEquals(Collections.singletonList(SerializedModel.builder().serializedData(serializedBlogOwnerData).modelSchema(blogOwnerSchema).build()), hybridBehaviors.list("BlogOwner"));
    Map<String, Object> serializedBlogData = new HashMap<>();
    serializedBlogData.put("id", blog.getId());
    serializedBlogData.put("name", blog.getName());
    serializedBlogData.put("owner", SerializedModel.builder().serializedData(serializedBlogOwnerData).modelSchema(blogOwnerSchema).build());
    assertEquals(Collections.singletonList(SerializedModel.builder().serializedData(serializedBlogData).modelSchema(blogSchema).build()), hybridBehaviors.list("Blog"));
}
Also used : HashMap(java.util.HashMap) BlogOwner(com.amplifyframework.testmodels.commentsblog.BlogOwner) Blog(com.amplifyframework.testmodels.commentsblog.Blog) Test(org.junit.Test)

Aggregations

Blog (com.amplifyframework.testmodels.commentsblog.Blog)30 Test (org.junit.Test)30 BlogOwner (com.amplifyframework.testmodels.commentsblog.BlogOwner)26 Post (com.amplifyframework.testmodels.commentsblog.Post)16 HashSet (java.util.HashSet)12 QueryPredicate (com.amplifyframework.core.model.query.predicate.QueryPredicate)11 DataStoreException (com.amplifyframework.datastore.DataStoreException)10 StrictMode (com.amplifyframework.datastore.StrictMode)10 SynchronousStorageAdapter (com.amplifyframework.datastore.storage.SynchronousStorageAdapter)10 AmplifyModelProvider (com.amplifyframework.testmodels.commentsblog.AmplifyModelProvider)10 PostStatus (com.amplifyframework.testmodels.commentsblog.PostStatus)10 Collections (java.util.Collections)10 List (java.util.List)10 After (org.junit.After)10 Assert.assertEquals (org.junit.Assert.assertEquals)10 Assert.assertTrue (org.junit.Assert.assertTrue)10 Before (org.junit.Before)10 BeforeClass (org.junit.BeforeClass)10 Comment (com.amplifyframework.testmodels.commentsblog.Comment)9 ArrayList (java.util.ArrayList)9