Search in sources :

Example 76 with BlogOwner

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

use of com.amplifyframework.testmodels.commentsblog.BlogOwner 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)

Example 78 with BlogOwner

use of com.amplifyframework.testmodels.commentsblog.BlogOwner 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)

Example 79 with BlogOwner

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

the class SQLiteStorageAdapterSaveTest method saveModelWithPredicateUpdatesForNestedModels.

/**
 * Test save with predicate. Confirms that conditionally updating a nested model also works.
 * @throws DataStoreException On unexpected failure manipulating items in/out of DataStore
 */
@Test
public void saveModelWithPredicateUpdatesForNestedModels() throws DataStoreException {
    // Save a model
    final BlogOwner mark = BlogOwner.builder().name("Mark").build();
    adapter.save(mark);
    // Save a model that belongs to another model
    final Blog marksBlog = Blog.builder().name("Mark's very first blog.").owner(mark).build();
    adapter.save(marksBlog);
    // Update a model that belongs to another model
    final Blog marksBlogEdit = marksBlog.copyOfBuilder().name("Mark's edited blog.").build();
    adapter.save(marksBlogEdit);
    // Assert that update went through successfully
    assertEquals(Collections.singletonList(marksBlogEdit), adapter.query(Blog.class));
}
Also used : BlogOwner(com.amplifyframework.testmodels.commentsblog.BlogOwner) Blog(com.amplifyframework.testmodels.commentsblog.Blog) Test(org.junit.Test)

Example 80 with BlogOwner

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

the class SQLiteStorageAdapterSaveTest method saveModelInsertsData.

/**
 * Assert that save stores data in the SQLite database correctly.
 * @throws DataStoreException from possible underlying DataStore exceptions
 */
@Test
public void saveModelInsertsData() throws DataStoreException {
    final BlogOwner alan = BlogOwner.builder().name("Alan Turing").build();
    adapter.save(alan);
    // Get the BlogOwner from the database
    final List<BlogOwner> blogOwners = adapter.query(BlogOwner.class);
    assertEquals(1, blogOwners.size());
    assertTrue(blogOwners.contains(alan));
}
Also used : BlogOwner(com.amplifyframework.testmodels.commentsblog.BlogOwner) Test(org.junit.Test)

Aggregations

BlogOwner (com.amplifyframework.testmodels.commentsblog.BlogOwner)150 Test (org.junit.Test)146 DataStoreException (com.amplifyframework.datastore.DataStoreException)35 Blog (com.amplifyframework.testmodels.commentsblog.Blog)32 ModelSchema (com.amplifyframework.core.model.ModelSchema)31 Post (com.amplifyframework.testmodels.commentsblog.Post)31 ArrayList (java.util.ArrayList)29 QueryPredicate (com.amplifyframework.core.model.query.predicate.QueryPredicate)25 Assert.assertEquals (org.junit.Assert.assertEquals)25 ModelMetadata (com.amplifyframework.datastore.appsync.ModelMetadata)24 Collections (java.util.Collections)24 HashSet (java.util.HashSet)23 List (java.util.List)23 PostStatus (com.amplifyframework.testmodels.commentsblog.PostStatus)22 HubAccumulator (com.amplifyframework.testutils.HubAccumulator)22 Arrays (java.util.Arrays)22 TimeUnit (java.util.concurrent.TimeUnit)22 Consumer (com.amplifyframework.core.Consumer)21 ModelWithMetadata (com.amplifyframework.datastore.appsync.ModelWithMetadata)21 Before (org.junit.Before)21