Search in sources :

Example 1 with User

use of com.amplifyframework.testmodels.ratingsblog.User 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 2 with User

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

the class FieldFinderTest method extractsSerializedModelFieldValue.

/**
 * Extracts the field value for a serialized model.
 * @throws AmplifyException On failure to derive ModelSchema or to convert Java model to Map
 * @throws NoSuchFieldException If field name does not exist for model
 */
@Test
public void extractsSerializedModelFieldValue() throws AmplifyException, NoSuchFieldException {
    String username = "foo";
    User user = User.builder().username(username).build();
    ModelSchema modelSchema = ModelSchema.fromModelClass(User.class);
    Map<String, Object> map = ModelConverter.toMap(user, modelSchema);
    SerializedModel serializedModel = SerializedModel.builder().serializedData(map).modelSchema(modelSchema).build();
    Object extractedValue = FieldFinder.extractFieldValue(serializedModel, "username");
    Assert.assertEquals(username, extractedValue);
}
Also used : ModelSchema(com.amplifyframework.core.model.ModelSchema) User(com.amplifyframework.testmodels.ratingsblog.User) SerializedModel(com.amplifyframework.core.model.SerializedModel) Test(org.junit.Test)

Example 3 with User

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

the class FieldFinderTest method extractsModelFieldValue.

/**
 * Extracts the field value for a model.
 * @throws NoSuchFieldException If field name does not exist for model
 */
@Test
public void extractsModelFieldValue() throws NoSuchFieldException {
    String username = "foo";
    User user = User.builder().username(username).build();
    Object extractedValue = FieldFinder.extractFieldValue(user, "username");
    Assert.assertEquals(username, extractedValue);
}
Also used : User(com.amplifyframework.testmodels.ratingsblog.User) Test(org.junit.Test)

Aggregations

User (com.amplifyframework.testmodels.ratingsblog.User)3 Test (org.junit.Test)3 ModelSchema (com.amplifyframework.core.model.ModelSchema)1 SerializedModel (com.amplifyframework.core.model.SerializedModel)1 Blog (com.amplifyframework.testmodels.ratingsblog.Blog)1 Post (com.amplifyframework.testmodels.ratingsblog.Post)1 PostEditor (com.amplifyframework.testmodels.ratingsblog.PostEditor)1