Search in sources :

Example 1 with Comment

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

the class AppSyncRequestFactoryTest method validateMutationGenerationOnCreateComment.

/**
 * Validates creation of a "create a model" request.
 * @throws DataStoreException On failure to interrogate the model fields.
 * @throws AmplifyException On failure to parse ModelSchema from model class
 * @throws JSONException from JSONAssert.assertEquals.
 */
@Test
public void validateMutationGenerationOnCreateComment() throws AmplifyException, JSONException {
    Post post = Post.justId("9a4295d6-8225-495a-a531-beffc8b7ae7d");
    Comment comment = Comment.builder().id("426f8e8d-ea0f-4839-a73f-6a2a38565ba1").content("toast").post(post).build();
    ModelSchema schema = ModelSchema.fromModelClass(Comment.class);
    JSONAssert.assertEquals(Resources.readAsString("create-comment-request.txt"), AppSyncRequestFactory.buildCreationRequest(schema, comment, DEFAULT_STRATEGY).getContent(), true);
}
Also used : Comment(com.amplifyframework.testmodels.commentsblog.Comment) ModelSchema(com.amplifyframework.core.model.ModelSchema) Post(com.amplifyframework.testmodels.commentsblog.Post) Test(org.junit.Test)

Example 2 with Comment

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

the class SQLiteStorageAdapterObserveQueryTest method querySavedDataWithMultiLevelJoins.

/**
 * Test that querying the saved item with a foreign key
 * also populates that instance variable with object.
 *
 * @throws DataStoreException   On unexpected failure manipulating items in/out of DataStore
 * @throws InterruptedException On unexpected failure manipulating items in/out of DataStore
 */
// @Test
public void querySavedDataWithMultiLevelJoins() throws DataStoreException, InterruptedException {
    CountDownLatch latch = new CountDownLatch(1);
    final BlogOwner blogOwner = BlogOwner.builder().name("Alan Turing").build();
    final Blog blog = Blog.builder().name("Alan's Software Blog").owner(blogOwner).build();
    final Post post = Post.builder().title("Alan's first post").status(PostStatus.ACTIVE).rating(2).blog(blog).build();
    final Comment comment = Comment.builder().content("Alan's first comment").post(post).build();
    adapter.save(blogOwner);
    adapter.save(blog);
    adapter.save(post);
    adapter.save(comment);
    Consumer<Cancelable> observationStarted = NoOpConsumer.create();
    Consumer<DataStoreQuerySnapshot<Comment>> onQuerySnapshot = value -> {
        assertTrue(value.getItems().contains(comment));
        assertEquals(value.getItems().get(0).getPost(), post);
        assertEquals(value.getItems().get(0).getPost().getBlog(), blog);
        assertEquals(value.getItems().get(0).getPost().getBlog().getOwner(), blogOwner);
        latch.countDown();
    };
    Consumer<DataStoreException> onObservationError = NoOpConsumer.create();
    Action onObservationComplete = NoOpAction.create();
    adapter.observeQuery(Comment.class, new ObserveQueryOptions(null, null), observationStarted, onQuerySnapshot, onObservationError, onObservationComplete);
    assertTrue(latch.await(1, TimeUnit.SECONDS));
}
Also used : Arrays(java.util.Arrays) BeforeClass(org.junit.BeforeClass) ObserveQueryOptions(com.amplifyframework.core.model.query.ObserveQueryOptions) Blog(com.amplifyframework.testmodels.commentsblog.Blog) ArrayList(java.util.ArrayList) SynchronousStorageAdapter(com.amplifyframework.datastore.storage.SynchronousStorageAdapter) HashSet(java.util.HashSet) Consumer(com.amplifyframework.core.Consumer) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) After(org.junit.After) QueryPredicate(com.amplifyframework.core.model.query.predicate.QueryPredicate) NoOpConsumer(com.amplifyframework.core.NoOpConsumer) PostStatus(com.amplifyframework.testmodels.commentsblog.PostStatus) DataStoreQuerySnapshot(com.amplifyframework.datastore.DataStoreQuerySnapshot) Before(org.junit.Before) BlogOwner(com.amplifyframework.testmodels.commentsblog.BlogOwner) Comment(com.amplifyframework.testmodels.commentsblog.Comment) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) Action(com.amplifyframework.core.Action) StrictMode(com.amplifyframework.datastore.StrictMode) TimeUnit(java.util.concurrent.TimeUnit) DataStoreException(com.amplifyframework.datastore.DataStoreException) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) Cancelable(com.amplifyframework.core.async.Cancelable) Assert.assertFalse(org.junit.Assert.assertFalse) QuerySortBy(com.amplifyframework.core.model.query.QuerySortBy) Comparator(java.util.Comparator) NoOpAction(com.amplifyframework.core.NoOpAction) AmplifyModelProvider(com.amplifyframework.testmodels.commentsblog.AmplifyModelProvider) Post(com.amplifyframework.testmodels.commentsblog.Post) Collections(java.util.Collections) QueryPredicate.not(com.amplifyframework.core.model.query.predicate.QueryPredicate.not) Assert.assertEquals(org.junit.Assert.assertEquals) Comment(com.amplifyframework.testmodels.commentsblog.Comment) DataStoreException(com.amplifyframework.datastore.DataStoreException) Action(com.amplifyframework.core.Action) NoOpAction(com.amplifyframework.core.NoOpAction) Post(com.amplifyframework.testmodels.commentsblog.Post) CountDownLatch(java.util.concurrent.CountDownLatch) DataStoreQuerySnapshot(com.amplifyframework.datastore.DataStoreQuerySnapshot) ObserveQueryOptions(com.amplifyframework.core.model.query.ObserveQueryOptions) BlogOwner(com.amplifyframework.testmodels.commentsblog.BlogOwner) Blog(com.amplifyframework.testmodels.commentsblog.Blog) Cancelable(com.amplifyframework.core.async.Cancelable)

Example 3 with Comment

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

the class SQLiteStorageAdapterQueryTest method querySavedDataWithMultiLevelJoins.

/**
 * Test that querying the saved item with a foreign key
 * also populates that instance variable with object.
 * @throws DataStoreException On unexpected failure manipulating items in/out of DataStore
 */
@Test
public void querySavedDataWithMultiLevelJoins() throws DataStoreException {
    final BlogOwner blogOwner = BlogOwner.builder().name("Alan Turing").build();
    final Blog blog = Blog.builder().name("Alan's Software Blog").owner(blogOwner).build();
    final Post post = Post.builder().title("Alan's first post").status(PostStatus.ACTIVE).rating(2).blog(blog).build();
    final Comment comment = Comment.builder().content("Alan's first comment").post(post).build();
    adapter.save(blogOwner);
    adapter.save(blog);
    adapter.save(post);
    adapter.save(comment);
    final List<Comment> comments = adapter.query(Comment.class);
    assertTrue(comments.contains(comment));
    assertEquals(comments.get(0).getPost(), post);
    assertEquals(comments.get(0).getPost().getBlog(), blog);
    assertEquals(comments.get(0).getPost().getBlog().getOwner(), blogOwner);
}
Also used : Comment(com.amplifyframework.testmodels.commentsblog.Comment) Post(com.amplifyframework.testmodels.commentsblog.Post) BlogOwner(com.amplifyframework.testmodels.commentsblog.BlogOwner) Blog(com.amplifyframework.testmodels.commentsblog.Blog) Test(org.junit.Test)

Example 4 with Comment

use of com.amplifyframework.testmodels.commentsblog.Comment 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

Comment (com.amplifyframework.testmodels.commentsblog.Comment)4 Post (com.amplifyframework.testmodels.commentsblog.Post)4 Test (org.junit.Test)4 Blog (com.amplifyframework.testmodels.commentsblog.Blog)3 BlogOwner (com.amplifyframework.testmodels.commentsblog.BlogOwner)3 Action (com.amplifyframework.core.Action)1 Consumer (com.amplifyframework.core.Consumer)1 NoOpAction (com.amplifyframework.core.NoOpAction)1 NoOpConsumer (com.amplifyframework.core.NoOpConsumer)1 Cancelable (com.amplifyframework.core.async.Cancelable)1 ModelSchema (com.amplifyframework.core.model.ModelSchema)1 ObserveQueryOptions (com.amplifyframework.core.model.query.ObserveQueryOptions)1 QuerySortBy (com.amplifyframework.core.model.query.QuerySortBy)1 QueryPredicate (com.amplifyframework.core.model.query.predicate.QueryPredicate)1 QueryPredicate.not (com.amplifyframework.core.model.query.predicate.QueryPredicate.not)1 DataStoreException (com.amplifyframework.datastore.DataStoreException)1 DataStoreQuerySnapshot (com.amplifyframework.datastore.DataStoreQuerySnapshot)1 StrictMode (com.amplifyframework.datastore.StrictMode)1 SynchronousStorageAdapter (com.amplifyframework.datastore.storage.SynchronousStorageAdapter)1 AmplifyModelProvider (com.amplifyframework.testmodels.commentsblog.AmplifyModelProvider)1