Search in sources :

Example 21 with BlogOwner

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

the class SyncProcessorTest method randomBlogOwnerWithMetadata.

private static ModelWithMetadata<BlogOwner> randomBlogOwnerWithMetadata() {
    BlogOwner blogOwner = BlogOwner.builder().name(RandomString.string()).id(RandomString.string()).build();
    Temporal.Timestamp randomTimestamp = new Temporal.Timestamp(new Random().nextLong(), TimeUnit.SECONDS);
    return new ModelWithMetadata<>(blogOwner, new ModelMetadata(blogOwner.getId(), null, new Random().nextInt(), randomTimestamp));
}
Also used : ModelWithMetadata(com.amplifyframework.datastore.appsync.ModelWithMetadata) Temporal(com.amplifyframework.core.model.temporal.Temporal) Random(java.util.Random) BlogOwner(com.amplifyframework.testmodels.commentsblog.BlogOwner) ModelMetadata(com.amplifyframework.datastore.appsync.ModelMetadata)

Example 22 with BlogOwner

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

the class ConflictResolverTest method conflictIsResolvedByApplyingRemoteData.

/**
 * When the user elects to apply the remote data, the following is expected:
 * 1. No additional calls are made to app sync.
 * @throws DataStoreException On failure to obtain configuration from provider,
 *                            or on failure to arrange metadata into storage
 */
@Test
public void conflictIsResolvedByApplyingRemoteData() throws DataStoreException {
    // The user provides a conflict handler which will always apply the remote
    // copy of the data.
    when(configurationProvider.getConfiguration()).thenReturn(DataStoreConfiguration.builder().conflictHandler(DataStoreConflictHandler.alwaysApplyRemote()).build());
    // Arrange some local pending mutation
    BlogOwner localSusan = BlogOwner.builder().name("Local Susan").build();
    PendingMutation<BlogOwner> mutation = PendingMutation.update(localSusan, schema);
    // Arrange some server data, that is in conflict
    BlogOwner serverSusan = localSusan.copyOfBuilder().name("Remote Susan").build();
    Temporal.Timestamp now = Temporal.Timestamp.now();
    ModelMetadata modelMetadata = new ModelMetadata(serverSusan.getId(), false, 2, now);
    ModelWithMetadata<BlogOwner> serverData = new ModelWithMetadata<>(serverSusan, modelMetadata);
    // Arrange a conflict error that we could hypothetically get from AppSync
    AppSyncConflictUnhandledError<BlogOwner> unhandledConflictError = AppSyncConflictUnhandledErrorFactory.createUnhandledConflictError(serverData);
    // When we try to resolve the conflict, the final resolved conflict
    // is identically the server's version.
    resolver.resolve(mutation, unhandledConflictError).test().awaitDone(TIMEOUT_SECONDS, TimeUnit.SECONDS).assertValue(serverData);
    // AppSync wasn't called, since the server was already "correct."
    verifyNoInteractions(appSync);
}
Also used : ModelWithMetadata(com.amplifyframework.datastore.appsync.ModelWithMetadata) Temporal(com.amplifyframework.core.model.temporal.Temporal) BlogOwner(com.amplifyframework.testmodels.commentsblog.BlogOwner) ModelMetadata(com.amplifyframework.datastore.appsync.ModelMetadata) Test(org.junit.Test)

Example 23 with BlogOwner

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

the class GsonPendingMutationConverterTest method convertPendingMutationWithSerializedModelToRecordAndBack.

/**
 * Validate that the {@link GsonPendingMutationConverter} can be
 * used to convert a sample {@link PendingMutation} to a
 * {@link PendingMutation.PersistentRecord}, and vice-versa.
 * @throws DataStoreException from DataStore conversion
 * @throws AmplifyException On failure to arrange model schema
 */
@Test
public void convertPendingMutationWithSerializedModelToRecordAndBack() throws AmplifyException {
    // Arrange a PendingMutation<SerializedModel>
    BlogOwner blogOwner = BlogOwner.builder().name("Joe Swanson").build();
    ModelSchema schema = ModelSchema.fromModelClass(BlogOwner.class);
    SerializedModel serializedBlogOwner = SerializedModel.create(blogOwner, schema);
    PendingMutation<SerializedModel> originalMutation = PendingMutation.creation(serializedBlogOwner, schema);
    String expectedMutationId = originalMutation.getMutationId().toString();
    // Instantiate the object under test
    PendingMutation.Converter converter = new GsonPendingMutationConverter();
    // Try to construct a record from the PendingMutation instance.
    PendingMutation.PersistentRecord record = converter.toRecord(originalMutation);
    assertNotNull(record);
    assertEquals(expectedMutationId, record.getId());
    // Now, try to convert it back...
    PendingMutation<SerializedModel> reconstructedItemChange = converter.fromRecord(record);
    assertEquals(originalMutation, reconstructedItemChange);
}
Also used : ModelSchema(com.amplifyframework.core.model.ModelSchema) BlogOwner(com.amplifyframework.testmodels.commentsblog.BlogOwner) SerializedModel(com.amplifyframework.core.model.SerializedModel) Test(org.junit.Test)

Example 24 with BlogOwner

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

the class MergerTest method mergeDeletionForExistingItem.

/**
 * Assume there is a item A in the store. Then, we try to merge
 * a mutation to delete item A. This should succeed. After the
 * merge, A should NOT be in the store anymore.
 * @throws DataStoreException On failure to arrange test data into store,
 *                            or on failure to query results for test assertions
 * @throws InterruptedException If interrupted while awaiting terminal result in test observer
 */
@Test
public void mergeDeletionForExistingItem() throws DataStoreException, InterruptedException {
    // Arrange: A blog owner, and some metadata about it, are in the store.
    BlogOwner blogOwner = BlogOwner.builder().name("Jameson").build();
    ModelMetadata originalMetadata = new ModelMetadata(blogOwner.getId(), false, 1, Temporal.Timestamp.now());
    storageAdapter.save(blogOwner, originalMetadata);
    // Just to be sure, our arrangement worked, and that thing is in there, right? Good.
    assertEquals(Collections.singletonList(blogOwner), storageAdapter.query(BlogOwner.class));
    // Act: merge a model deletion.
    ModelMetadata deletionMetadata = new ModelMetadata(blogOwner.getId(), true, 2, Temporal.Timestamp.now());
    TestObserver<Void> observer = merger.merge(new ModelWithMetadata<>(blogOwner, deletionMetadata)).test();
    assertTrue(observer.await(REASONABLE_WAIT_TIME, TimeUnit.MILLISECONDS));
    observer.assertNoErrors().assertComplete();
    // Assert: the blog owner is no longer in the store.
    assertEquals(0, storageAdapter.query(BlogOwner.class).size());
}
Also used : ModelWithMetadata(com.amplifyframework.datastore.appsync.ModelWithMetadata) BlogOwner(com.amplifyframework.testmodels.commentsblog.BlogOwner) ModelMetadata(com.amplifyframework.datastore.appsync.ModelMetadata) Test(org.junit.Test)

Example 25 with BlogOwner

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

the class MergerTest method itemWithSameVersionIsNotMerged.

/**
 * If the incoming change has the SAME version as the data currently in the DB, we refuse to update it.
 * The user may have updated the data locally via the DataStore API. So we would clobber it.
 * The version must be strictly HIGHER than the current version, in order for the merge to succeed.
 * @throws DataStoreException On failure to interact with storage during arrange/verify
 * @throws InterruptedException If interrupted while awaiting terminal result in test observer
 */
@Test
public void itemWithSameVersionIsNotMerged() throws DataStoreException, InterruptedException {
    // Arrange a model and metadata into storage.
    BlogOwner existingModel = BlogOwner.builder().name("Cornelius Daniels").build();
    ModelMetadata existingMetadata = new ModelMetadata(existingModel.getModelName() + "|" + existingModel.getId(), false, 55, Temporal.Timestamp.now());
    storageAdapter.save(existingModel, existingMetadata);
    // Act: try to merge, but specify a LOWER version.
    BlogOwner incomingModel = existingModel.copyOfBuilder().name("Cornelius Daniels, but woke af, now.").build();
    ModelMetadata lowerVersionMetadata = new ModelMetadata(incomingModel.getId(), false, 33, Temporal.Timestamp.now());
    ModelWithMetadata<BlogOwner> modelWithLowerVersionMetadata = new ModelWithMetadata<>(incomingModel, lowerVersionMetadata);
    TestObserver<Void> mergeObserver = merger.merge(modelWithLowerVersionMetadata).test();
    mergeObserver.await(REASONABLE_WAIT_TIME, TimeUnit.MILLISECONDS);
    mergeObserver.assertNoErrors().assertComplete();
    // Assert: Joey is still the same old Joey.
    List<BlogOwner> actualBlogOwners = storageAdapter.query(BlogOwner.class);
    assertEquals(1, actualBlogOwners.size());
    assertEquals(existingModel, actualBlogOwners.get(0));
    // And his metadata is the still the same.
    assertEquals(Collections.singletonList(existingMetadata), storageAdapter.query(ModelMetadata.class, Where.id(existingModel.getModelName() + "|" + existingModel.getId())));
}
Also used : ModelWithMetadata(com.amplifyframework.datastore.appsync.ModelWithMetadata) BlogOwner(com.amplifyframework.testmodels.commentsblog.BlogOwner) ModelMetadata(com.amplifyframework.datastore.appsync.ModelMetadata) 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