Search in sources :

Example 86 with BlogOwner

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

the class ModelConverterTest method toMapForModelReturnsExpectedMap.

/**
 * Verify that a Java model converted to a Map returns the expected value.
 * @throws AmplifyException On failure to derive ModelSchema
 */
@Test
public void toMapForModelReturnsExpectedMap() throws AmplifyException {
    BlogOwner blogOwner = BlogOwner.builder().name("Joe Swanson").build();
    ModelSchema schema = ModelSchema.fromModelClass(BlogOwner.class);
    Map<String, Object> actual = ModelConverter.toMap(blogOwner, schema);
    Map<String, Object> expected = new HashMap<>();
    expected.put("id", blogOwner.getId());
    expected.put("createdAt", null);
    expected.put("name", "Joe Swanson");
    expected.put("updatedAt", null);
    expected.put("wea", null);
    assertEquals(expected, actual);
}
Also used : HashMap(java.util.HashMap) BlogOwner(com.amplifyframework.testmodels.commentsblog.BlogOwner) Test(org.junit.Test)

Example 87 with BlogOwner

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

the class PersistentMutationOutboxTest method existingSerializedModelCreateIncomingUpdateMergesWithExistingMutation.

/**
 * When there is an existing SerializedModel create mutation, and a new SerializedModel update mutation comes in,
 * then we need to merge any existing mutations for that modelId and create the new one of type Create.
 * @throws AmplifyException On failure to find the serializedModel difference.
 * @throws InterruptedException If interrupted while awaiting terminal result in test observer
 */
@Test
public void existingSerializedModelCreateIncomingUpdateMergesWithExistingMutation() throws AmplifyException, InterruptedException {
    // Arrange an existing update mutation
    BlogOwner modelInSqlLite = BlogOwner.builder().name("Papa Tony").wea("Something").build();
    BlogOwner initialUpdate = BlogOwner.builder().name("Tony Jr").id(modelInSqlLite.getId()).build();
    PendingMutation<SerializedModel> initialUpdatePendingMutation = PendingMutation.creation(SerializedModel.difference(initialUpdate, modelInSqlLite, schema), schema);
    String existingUpdateId = initialUpdatePendingMutation.getMutationId().toString();
    mutationOutbox.enqueue(initialUpdatePendingMutation).blockingAwait();
    // Act: try to enqueue a new update mutation when there already is one
    BlogOwner incomingUpdatedModel = BlogOwner.builder().name("Papa Tony").wea("something else").id(modelInSqlLite.getId()).build();
    PendingMutation<SerializedModel> incomingUpdate = PendingMutation.update(SerializedModel.difference(incomingUpdatedModel, modelInSqlLite, schema), schema);
    String incomingUpdateId = incomingUpdate.getMutationId().toString();
    TestObserver<Void> enqueueObserver = mutationOutbox.enqueue(incomingUpdate).test();
    // Assert: OK. The new mutation is accepted
    enqueueObserver.await(TIMEOUT_MS, TimeUnit.MILLISECONDS);
    enqueueObserver.assertComplete();
    // Assert: the existing mutation has been removed
    assertRecordCountForMutationId(existingUpdateId, 0);
    // And the new one has been added to the queue
    assertRecordCountForMutationId(incomingUpdateId, 0);
    List<PersistentRecord> pendingMutationsFromStorage = getAllPendingMutationRecordFromStorage();
    for (PersistentRecord record : pendingMutationsFromStorage) {
        if (!record.getContainedModelId().equals(incomingUpdate.getMutatedItem().getId())) {
            pendingMutationsFromStorage.remove(record);
        }
    }
    // Ensure the new one is in storage.
    PendingMutation<SerializedModel> storedMutation = converter.fromRecord(pendingMutationsFromStorage.get(0));
    // This is the name from the second model, not the first!!
    assertEquals(initialUpdate.getName(), storedMutation.getMutatedItem().getSerializedData().get("name"));
    // wea got merged from existing model!!
    assertEquals(incomingUpdatedModel.getWea(), storedMutation.getMutatedItem().getSerializedData().get("wea"));
    assertEquals(PendingMutation.Type.CREATE, storedMutation.getMutationType());
}
Also used : BlogOwner(com.amplifyframework.testmodels.commentsblog.BlogOwner) RandomString(com.amplifyframework.testutils.random.RandomString) SerializedModel(com.amplifyframework.core.model.SerializedModel) PersistentRecord(com.amplifyframework.datastore.syncengine.PendingMutation.PersistentRecord) Test(org.junit.Test)

Example 88 with BlogOwner

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

the class PersistentMutationOutboxTest method existingDeletionIncomingCreationYieldsError.

/**
 * When there is an existing deletion for a model, and a new creation for that
 * model comes in, an error should be returned. Even though the model may be staged
 * for deletion, that deletion hasn't happened yet. So, it doesn't make sense to create()
 * something that currently already exists. That's like an "update."
 * @throws DataStoreException On failure to query which mutation is present in storage
 * @throws InterruptedException If interrupted while awaiting terminal result in test observer
 */
@Test
public void existingDeletionIncomingCreationYieldsError() throws DataStoreException, InterruptedException {
    // Arrange an existing deletion mutation
    BlogOwner modelInExistingMutation = BlogOwner.builder().name("Papa Tony").build();
    PendingMutation<BlogOwner> existingDeletion = PendingMutation.deletion(modelInExistingMutation, schema);
    String existingDeletionId = existingDeletion.getMutationId().toString();
    mutationOutbox.enqueue(existingDeletion).blockingAwait(TIMEOUT_MS, TimeUnit.MILLISECONDS);
    // Act: try to create tony, but wait -- if we're already deleting him...
    BlogOwner modelInIncomingMutation = modelInExistingMutation.copyOfBuilder().name("Tony Jr.").build();
    PendingMutation<BlogOwner> incomingCreation = PendingMutation.creation(modelInIncomingMutation, schema);
    String incomingCreationId = incomingCreation.getMutationId().toString();
    TestObserver<Void> enqueueObserver = mutationOutbox.enqueue(incomingCreation).test();
    // Assert: caused a failure.
    enqueueObserver.await(TIMEOUT_MS, TimeUnit.MILLISECONDS);
    enqueueObserver.assertError(throwable -> throwable instanceof DataStoreException);
    // Assert: original mutation is present, but the new one isn't.
    PendingMutation.PersistentRecord storedMutation = storage.query(PersistentRecord.class, Where.id(existingDeletionId)).get(0);
    assertEquals(modelInExistingMutation, converter.fromRecord(storedMutation).getMutatedItem());
    assertTrue(storage.query(PersistentRecord.class, Where.id(incomingCreationId)).isEmpty());
    // Existing mutation still attainable as next mutation (right now, its the ONLY mutation in outbox)
    assertTrue(mutationOutbox.hasPendingMutation(modelInExistingMutation.getId()));
    assertEquals(existingDeletion, mutationOutbox.peek());
}
Also used : DataStoreException(com.amplifyframework.datastore.DataStoreException) PersistentRecord(com.amplifyframework.datastore.syncengine.PendingMutation.PersistentRecord) BlogOwner(com.amplifyframework.testmodels.commentsblog.BlogOwner) RandomString(com.amplifyframework.testutils.random.RandomString) PersistentRecord(com.amplifyframework.datastore.syncengine.PendingMutation.PersistentRecord) Test(org.junit.Test)

Example 89 with BlogOwner

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

the class PersistentMutationOutboxTest method existingDeletionIncomingUpdateYieldsError.

/**
 * If there is a pending deletion, enqueuing an update will fail, since the thing being
 * updated is not meant to exist.
 * @throws DataStoreException On failure to query storage, for the purpose of asserting the
 *                            state of mutations after the test action
 * @throws InterruptedException If interrupted while awaiting terminal result in test observer
 */
@Test
public void existingDeletionIncomingUpdateYieldsError() throws DataStoreException, InterruptedException {
    // Arrange an existing deletion mutation
    BlogOwner modelInExistingMutation = BlogOwner.builder().name("Papa Tony").build();
    PendingMutation<BlogOwner> existingDeletion = PendingMutation.deletion(modelInExistingMutation, schema);
    String existingDeletionId = existingDeletion.getMutationId().toString();
    mutationOutbox.enqueue(existingDeletion).blockingAwait(TIMEOUT_MS, TimeUnit.MILLISECONDS);
    // Act: try to update tony, but wait ... aren't we deleting tony?
    BlogOwner modelInIncomingMutation = modelInExistingMutation.copyOfBuilder().name("Tony Jr.").build();
    PendingMutation<BlogOwner> incomingUpdate = PendingMutation.update(modelInIncomingMutation, schema);
    String incomingUpdateId = incomingUpdate.getMutationId().toString();
    TestObserver<Void> enqueueObserver = mutationOutbox.enqueue(incomingUpdate).test();
    // Assert: caused a failure.
    enqueueObserver.await(TIMEOUT_MS, TimeUnit.MILLISECONDS);
    enqueueObserver.assertError(throwable -> throwable instanceof DataStoreException);
    // Assert: original mutation is present, but the new one isn't.
    PendingMutation.PersistentRecord storedMutation = storage.query(PersistentRecord.class, Where.id(existingDeletionId)).get(0);
    assertEquals(modelInExistingMutation, converter.fromRecord(storedMutation).getMutatedItem());
    assertTrue(storage.query(PersistentRecord.class, Where.id(incomingUpdateId)).isEmpty());
    // Existing mutation still attainable as next mutation (right now, its the ONLY mutation in outbox)
    assertTrue(mutationOutbox.hasPendingMutation(modelInExistingMutation.getId()));
    assertEquals(existingDeletion, mutationOutbox.peek());
}
Also used : DataStoreException(com.amplifyframework.datastore.DataStoreException) PersistentRecord(com.amplifyframework.datastore.syncengine.PendingMutation.PersistentRecord) BlogOwner(com.amplifyframework.testmodels.commentsblog.BlogOwner) RandomString(com.amplifyframework.testutils.random.RandomString) PersistentRecord(com.amplifyframework.datastore.syncengine.PendingMutation.PersistentRecord) Test(org.junit.Test)

Example 90 with BlogOwner

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

the class PersistentMutationOutboxTest method existingCreationIncomingDeletionRemovesExisting.

/**
 * When there is already a creation pending, and then we get a deletion for the same model ID,
 * we should just remove the creation. It means like "never mind, don't actually create."
 * @throws DataStoreException On failure to query storage for mutations state after test action
 */
@Test
public void existingCreationIncomingDeletionRemovesExisting() throws DataStoreException {
    BlogOwner joe = BlogOwner.builder().name("Original Joe").build();
    PendingMutation<BlogOwner> existingCreation = PendingMutation.creation(joe, schema);
    String existingCreationId = existingCreation.getMutationId().toString();
    mutationOutbox.enqueue(existingCreation).blockingAwait(TIMEOUT_MS, TimeUnit.MILLISECONDS);
    PendingMutation<BlogOwner> incomingDeletion = PendingMutation.deletion(joe, schema);
    String incomingDeletionId = incomingDeletion.getMutationId().toString();
    mutationOutbox.enqueue(incomingDeletion).blockingAwait(TIMEOUT_MS, TimeUnit.MILLISECONDS);
    assertTrue(storage.query(PersistentRecord.class, Where.id(existingCreationId)).isEmpty());
    assertTrue(storage.query(PersistentRecord.class, Where.id(incomingDeletionId)).isEmpty());
    // There are no pending mutations.
    assertNull(mutationOutbox.peek());
}
Also used : BlogOwner(com.amplifyframework.testmodels.commentsblog.BlogOwner) RandomString(com.amplifyframework.testutils.random.RandomString) Test(org.junit.Test)

Aggregations

BlogOwner (com.amplifyframework.testmodels.commentsblog.BlogOwner)150 Test (org.junit.Test)146 DataStoreException (com.amplifyframework.datastore.DataStoreException)35 Blog (com.amplifyframework.testmodels.commentsblog.Blog)32 ModelSchema (com.amplifyframework.core.model.ModelSchema)31 Post (com.amplifyframework.testmodels.commentsblog.Post)31 ArrayList (java.util.ArrayList)29 QueryPredicate (com.amplifyframework.core.model.query.predicate.QueryPredicate)25 Assert.assertEquals (org.junit.Assert.assertEquals)25 ModelMetadata (com.amplifyframework.datastore.appsync.ModelMetadata)24 Collections (java.util.Collections)24 HashSet (java.util.HashSet)23 List (java.util.List)23 PostStatus (com.amplifyframework.testmodels.commentsblog.PostStatus)22 HubAccumulator (com.amplifyframework.testutils.HubAccumulator)22 Arrays (java.util.Arrays)22 TimeUnit (java.util.concurrent.TimeUnit)22 Consumer (com.amplifyframework.core.Consumer)21 ModelWithMetadata (com.amplifyframework.datastore.appsync.ModelWithMetadata)21 Before (org.junit.Before)21