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());
}
Aggregations