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));
}
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);
}
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);
}
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());
}
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())));
}
Aggregations