Search in sources :

Example 1 with Post

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

the class CodeGenerationInstrumentationTest method hasManyRelationship.

/**
 * Tests the code generation for HAS_MANY relationship.
 * @throws ApiException On failure to obtain valid response from endpoint
 */
@Test
public void hasManyRelationship() throws ApiException {
    // Create a blog.
    Blog blog = Blog.builder().name("All Things Amplify").tags(Arrays.asList("amazon", "amplify", "framework", "software")).build();
    assertEquals(blog, api.create(BLOG_API_NAME, blog));
    // Create a post, associated to that blog
    Post post = Post.builder().title("Test 1").blog(blog).build();
    Post createdPost = api.create(BLOG_API_NAME, post);
    // Validate that created post has same fields
    assertEquals(post.getId(), createdPost.getId());
    assertEquals(post.getTitle(), createdPost.getTitle());
    assertEquals(post.getBlog().getId(), createdPost.getBlog().getId());
    assertEquals(post.getBlog().getName(), createdPost.getBlog().getName());
    assertEquals(post.getBlog().getTags(), createdPost.getBlog().getTags());
    // Get the blog, and ensure that posts are associated to it on the endpoint
    Blog queriedBlog = api.get(BLOG_API_NAME, Blog.class, blog.getId());
    Post firstPostInQueriedBlog = queriedBlog.getPosts().get(0);
    assertEquals(post.getId(), firstPostInQueriedBlog.getId());
    assertEquals(post.getTitle(), firstPostInQueriedBlog.getTitle());
}
Also used : Post(com.amplifyframework.testmodels.ratingsblog.Post) Blog(com.amplifyframework.testmodels.ratingsblog.Blog) Test(org.junit.Test)

Example 2 with Post

use of com.amplifyframework.testmodels.ratingsblog.Post 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)

Example 3 with Post

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

the class CodeGenerationInstrumentationTest method hasOneRelationship.

/**
 * Tests the code generation for HAS_ONE relationship.
 * @throws ApiException On failure to obtain valid response from endpoint
 */
@Test
public void hasOneRelationship() throws ApiException {
    // Create a blog
    Blog blog = Blog.builder().name("Necessary blog for post").build();
    api.create(BLOG_API_NAME, blog);
    // Associate a post to that blog
    Post post = Post.builder().title("Test post").blog(blog).build();
    api.create(BLOG_API_NAME, post);
    // Make a ating for the post
    Rating rating = Rating.builder().stars(5).post(post).build();
    Rating createdRating = api.create(BLOG_API_NAME, rating);
    // Validate that rating that exists on the endpoint refers to the original post
    assertEquals(post, createdRating.getPost());
/*
        TODO: This condition should work. However there is a bug on the AppSync transformer side which
            sets up the HasOne / BelongsTo relationship as two independent BelongsTo relationships so it fails.
            Once the AppSync transformer bug is fixed, we can uncomment this part of the test.

        assertEquals(
            rating,
            api.get(BLOG_API_NAME, Post.class, post.getId())
                .getRating()
        );
        */
}
Also used : Post(com.amplifyframework.testmodels.ratingsblog.Post) Rating(com.amplifyframework.testmodels.ratingsblog.Rating) Blog(com.amplifyframework.testmodels.ratingsblog.Blog) Test(org.junit.Test)

Aggregations

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