Search in sources :

Example 1 with PostEditor

use of com.amplifyframework.testmodels.ratingsblog.PostEditor in project amplify-android by aws-amplify.

the class CodeGenerationInstrumentationTest method manyToManyRelationship.

/**
 * Tests the code generation for a Many to Many relationship simulated through two HasMany relationships.
 * @throws ApiException On failure to obtain valid response from endpoint
 */
@Test
public void manyToManyRelationship() throws ApiException {
    // Create a blog.
    Blog blog = Blog.builder().name("Necessary blog for post").build();
    api.create(BLOG_API_NAME, blog);
    // Which contains a post
    Post post = Post.builder().title("Test post").blog(blog).build();
    api.create(BLOG_API_NAME, post);
    // Create a user
    User user = User.builder().username("Patches46").build();
    api.create(BLOG_API_NAME, user);
    // The user is an editor of the created post
    PostEditor editor = PostEditor.builder().post(post).editor(user).build();
    api.create(BLOG_API_NAME, editor);
    // Now, see what was actually setup on the endpoint, by querying for post and user
    Post queriedPost = api.get(BLOG_API_NAME, Post.class, post.getId());
    User queriedUser = api.get(BLOG_API_NAME, User.class, user.getId());
    // Validate that associations are setup correctly on the objects returned from endpoint
    // The post should refer to the user, and the user should refer to the post.
    assertEquals(1, queriedPost.getEditors().size());
    assertEquals(user, queriedPost.getEditors().get(0).getEditor());
    assertEquals(1, queriedUser.getPosts().size());
    assertEquals(post.getTitle(), queriedUser.getPosts().get(0).getPost().getTitle());
    assertEquals(post.getId(), queriedUser.getPosts().get(0).getPost().getId());
}
Also used : User(com.amplifyframework.testmodels.ratingsblog.User) Post(com.amplifyframework.testmodels.ratingsblog.Post) PostEditor(com.amplifyframework.testmodels.ratingsblog.PostEditor) Blog(com.amplifyframework.testmodels.ratingsblog.Blog) Test(org.junit.Test)

Aggregations

Blog (com.amplifyframework.testmodels.ratingsblog.Blog)1 Post (com.amplifyframework.testmodels.ratingsblog.Post)1 PostEditor (com.amplifyframework.testmodels.ratingsblog.PostEditor)1 User (com.amplifyframework.testmodels.ratingsblog.User)1 Test (org.junit.Test)1