Search in sources :

Example 16 with Post

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

the class BasicCloudSyncInstrumentationTest method createPost1WithCommentThenReassignCommentToPost2.

/**
 * The test is to create a new Post with Comment, reassign Comment to a different Post.
 * @throws DataStoreException On failure to save or query items from DataStore.
 * @throws ApiException On failure to query the API.
 */
@Test
public void createPost1WithCommentThenReassignCommentToPost2() throws DataStoreException, ApiException {
    // Setup
    BlogOwner owner = BlogOwner.builder().name("Owner").build();
    Blog blog = Blog.builder().name("MyBlog").owner(owner).build();
    Post firstPost = Post.builder().title("First Post").status(PostStatus.ACTIVE).rating(3).blog(blog).build();
    Post secondPost = Post.builder().title("Second Post").status(PostStatus.ACTIVE).rating(5).blog(blog).build();
    Comment comment = Comment.builder().content("Some comment").post(firstPost).build();
    String modelName = Comment.class.getSimpleName();
    // Save first post and comment. Then verify that first post and comment were saved.
    HubAccumulator accumulator = HubAccumulator.create(HubChannel.DATASTORE, publicationOf(modelName, comment.getId()), 1).start();
    dataStore.save(owner);
    dataStore.save(blog);
    dataStore.save(firstPost);
    dataStore.save(comment);
    accumulator.await(TIMEOUT_SECONDS, TimeUnit.SECONDS);
    Comment localComment = dataStore.get(Comment.class, comment.getId());
    Assert.assertEquals(comment.getPost().getId(), localComment.getPost().getId());
    Comment remoteComment = api.get(Comment.class, comment.getId());
    Assert.assertEquals(comment.getPost().getId(), remoteComment.getPost().getId());
    // Reassign comment to second post, save and sync
    Comment commentCopy = comment.copyOfBuilder().post(secondPost).build();
    accumulator = HubAccumulator.create(HubChannel.DATASTORE, publicationOf(modelName, commentCopy.getId()), 1).start();
    dataStore.save(secondPost);
    dataStore.save(commentCopy);
    accumulator.await(TIMEOUT_SECONDS, TimeUnit.SECONDS);
    // Verify that comment was reassigned
    Comment currentLocalComment = dataStore.get(Comment.class, comment.getId());
    Assert.assertEquals(secondPost.getId(), currentLocalComment.getPost().getId());
    Comment currentRemoteComment = api.get(Comment.class, comment.getId());
    Assert.assertEquals(secondPost.getId(), currentRemoteComment.getPost().getId());
}
Also used : Comment(com.amplifyframework.testmodels.commentsblog.Comment) Post(com.amplifyframework.testmodels.commentsblog.Post) BlogOwner(com.amplifyframework.testmodels.commentsblog.BlogOwner) HubAccumulator(com.amplifyframework.testutils.HubAccumulator) Blog(com.amplifyframework.testmodels.commentsblog.Blog) Test(org.junit.Test)

Aggregations

Post (com.amplifyframework.testmodels.commentsblog.Post)16 Test (org.junit.Test)16 BlogOwner (com.amplifyframework.testmodels.commentsblog.BlogOwner)15 Blog (com.amplifyframework.testmodels.commentsblog.Blog)11 ArrayList (java.util.ArrayList)7 QueryPredicate (com.amplifyframework.core.model.query.predicate.QueryPredicate)6 DataStoreException (com.amplifyframework.datastore.DataStoreException)6 Comment (com.amplifyframework.testmodels.commentsblog.Comment)6 PostStatus (com.amplifyframework.testmodels.commentsblog.PostStatus)6 Collections (java.util.Collections)6 HashSet (java.util.HashSet)6 List (java.util.List)6 Assert.assertEquals (org.junit.Assert.assertEquals)6 Model (com.amplifyframework.core.model.Model)5 StrictMode (com.amplifyframework.datastore.StrictMode)5 SynchronousStorageAdapter (com.amplifyframework.datastore.storage.SynchronousStorageAdapter)5 AmplifyModelProvider (com.amplifyframework.testmodels.commentsblog.AmplifyModelProvider)5 Action (com.amplifyframework.core.Action)4 Consumer (com.amplifyframework.core.Consumer)4 NoOpAction (com.amplifyframework.core.NoOpAction)4