use of com.amplifyframework.testmodels.commentsblog.BlogOwner in project amplify-android by aws-amplify.
the class OrchestratorTest method itemsPlacedInStorageArePublishedToNetwork.
/**
* When an item is placed into storage, a cascade of
* things happen which should ultimately result in a mutation call
* to the API category, with an {@link MutationType} corresponding to the type of
* modification that was made to the storage.
* @throws AmplifyException On failure to load model schema into registry
*/
// Casting ? in HubEvent<?> to PendingMutation<? extends Model>
@SuppressWarnings("unchecked")
@Test
public void itemsPlacedInStorageArePublishedToNetwork() throws AmplifyException {
// Arrange: orchestrator is running
orchestrator.start().test();
orchestratorInitObserver.await(10, TimeUnit.SECONDS);
HubAccumulator accumulator = HubAccumulator.create(HubChannel.DATASTORE, isProcessed(susan), 1).start();
// Act: Put BlogOwner into storage, and wait for it to complete.
SynchronousStorageAdapter.delegatingTo(localStorageAdapter).save(susan);
// Assert that the event is published out to the API
assertEquals(Collections.singletonList(susan), Observable.fromIterable(accumulator.await(10, TimeUnit.SECONDS)).map(HubEvent::getData).map(data -> (OutboxMutationEvent<BlogOwner>) data).map(OutboxMutationEvent::getElement).map(OutboxMutationEvent.OutboxMutationEventElement::getModel).toList().blockingGet());
assertTrue(orchestrator.stop().blockingAwait(5, TimeUnit.SECONDS));
}
use of com.amplifyframework.testmodels.commentsblog.BlogOwner in project amplify-android by aws-amplify.
the class PendingMutationTest method pendingMutationsAreComparable.
/**
* It is possible to sort pending mutations according to their TimeBasedUUID field.
* @throws AmplifyException On failure to arrange model schema
*/
@Test
public void pendingMutationsAreComparable() throws AmplifyException {
// First, create a bunch of pending mutations.
final List<PendingMutation<? extends Model>> expectedOrder = new ArrayList<>();
final int desiredQuantity = 10;
for (int index = 0; index < desiredQuantity; index++) {
// to make sure the comparator can work across model types
if (new SecureRandom().nextBoolean()) {
BlogOwner blogger = BlogOwner.builder().name(String.format(Locale.US, "Blogger #%d", index)).build();
ModelSchema schema = ModelSchema.fromModelClass(BlogOwner.class);
expectedOrder.add(PendingMutation.creation(blogger, schema));
} else {
Post post = Post.builder().title(String.format(Locale.US, "Title #%d", index)).status(PostStatus.ACTIVE).rating(5).build();
ModelSchema schema = ModelSchema.fromModelClass(Post.class);
expectedOrder.add(PendingMutation.creation(post, schema));
}
}
// Okay! Now, shuffle them.
List<PendingMutation<? extends Model>> shuffled = new ArrayList<>(expectedOrder);
Collections.shuffle(shuffled, new SecureRandom());
// Now sort them according to the item comparator, {@link PendingMutation#compareTo(Object)}.
List<PendingMutation<? extends Model>> actualOrder = new ArrayList<>(shuffled);
Collections.sort(actualOrder);
assertEquals(expectedOrder, actualOrder);
}
use of com.amplifyframework.testmodels.commentsblog.BlogOwner in project amplify-android by aws-amplify.
the class ModelSorterTest method sortReturnsSortedList.
/**
* Sort method returns sorted list.
*/
@Test
public void sortReturnsSortedList() {
List<String> names = Arrays.asList("John", "Jacob", "Joe", "Bob", "Bobby", "Bobb", "Dan", "Dany", "Daniel");
List<String> weas = Arrays.asList("pon", "lth", "ver", "kly", "ken", "sel", "ner", "rer", "ned");
List<BlogOwner> owners = new ArrayList<>();
for (int i = 0; i < names.size() / 2; i++) {
BlogOwner owner = BlogOwner.builder().name(names.get(i)).wea(weas.get(i)).build();
owners.add(owner);
}
List<QuerySortBy> sortBy = new ArrayList<>();
sortBy.add(BlogOwner.NAME.descending());
sortBy.add(BlogOwner.WEA.ascending());
ModelSorter<BlogOwner> subject = new ModelSorter<>();
subject.sort(new ObserveQueryOptions(null, sortBy), owners, BlogOwner.class, value -> {
});
List<BlogOwner> sorted = new ArrayList<>(owners);
Collections.sort(sorted, Comparator.comparing(BlogOwner::getName).reversed().thenComparing(BlogOwner::getWea));
assertEquals(sorted, owners);
}
use of com.amplifyframework.testmodels.commentsblog.BlogOwner in project amplify-android by aws-amplify.
the class ObserveQueryExecutorTest method observeQueryCancelsTheOperationOnCancel.
/**
* testing cancel on observe query.
* @throws DataStoreException DataStoreException
*/
@Test
public void observeQueryCancelsTheOperationOnCancel() throws DataStoreException {
final BlogOwner blogOwner = BlogOwner.builder().name("Alan Turing").build();
List<BlogOwner> resultList = new ArrayList<>();
resultList.add(blogOwner);
Consumer<DataStoreQuerySnapshot<BlogOwner>> onQuerySnapshot = NoOpConsumer.create();
Consumer<DataStoreException> onObservationError = NoOpConsumer.create();
Action onObservationComplete = () -> {
};
SqlQueryProcessor mockSqlQueryProcessor = mock(SqlQueryProcessor.class);
when(mockSqlQueryProcessor.queryOfflineData(eq(BlogOwner.class), any(), any())).thenReturn(resultList);
Subject<StorageItemChange<? extends Model>> subject = PublishSubject.<StorageItemChange<? extends Model>>create().toSerialized();
ExecutorService threadPool = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors() * 5);
ObserveQueryExecutor<BlogOwner> observeQueryExecutor = new ObserveQueryExecutor<>(subject, mockSqlQueryProcessor, threadPool, mock(SyncStatus.class), new ModelSorter<>(), DataStoreConfiguration.defaults());
Consumer<Cancelable> observationStarted = value -> {
value.cancel();
Assert.assertTrue(observeQueryExecutor.getIsCancelled());
assertEquals(0, observeQueryExecutor.getCompleteMap().size());
assertEquals(0, observeQueryExecutor.getChangeList().size());
subject.test().assertNoErrors().isDisposed();
};
observeQueryExecutor.observeQuery(BlogOwner.class, new ObserveQueryOptions(null, null), observationStarted, onQuerySnapshot, onObservationError, onObservationComplete);
}
use of com.amplifyframework.testmodels.commentsblog.BlogOwner in project amplify-android by aws-amplify.
the class ObserveQueryExecutorTest method observeQueryReturnsRecordsBasedOnMaxTime.
/**
* observe Query Returns batched Records Based On MaxTime.
* @throws InterruptedException InterruptedException
* @throws DataStoreException DataStoreException
*/
@Test
public void observeQueryReturnsRecordsBasedOnMaxTime() throws InterruptedException, DataStoreException {
CountDownLatch latch = new CountDownLatch(1);
CountDownLatch changeLatch = new CountDownLatch(1);
AtomicInteger count = new AtomicInteger();
BlogOwner blogOwner = BlogOwner.builder().name("Alan Turing").build();
List<BlogOwner> datastoreResultList = new ArrayList<>();
int maxRecords = 50;
datastoreResultList.add(blogOwner);
Consumer<Cancelable> observationStarted = NoOpConsumer.create();
SyncStatus mockSyncStatus = mock(SyncStatus.class);
when(mockSyncStatus.get(any(), any())).thenReturn(false);
Subject<StorageItemChange<? extends Model>> subject = PublishSubject.<StorageItemChange<? extends Model>>create().toSerialized();
Consumer<DataStoreQuerySnapshot<BlogOwner>> onQuerySnapshot = value -> {
if (count.get() == 0) {
Assert.assertTrue(value.getItems().contains(blogOwner));
latch.countDown();
} else if (count.get() == 1) {
Assert.assertEquals(6, value.getItems().size());
changeLatch.countDown();
}
count.getAndIncrement();
};
Consumer<DataStoreException> onObservationError = NoOpConsumer.create();
Action onObservationComplete = () -> {
};
SqlQueryProcessor mockSqlQueryProcessor = mock(SqlQueryProcessor.class);
when(mockSqlQueryProcessor.queryOfflineData(eq(BlogOwner.class), any(), any())).thenReturn(datastoreResultList);
when(mockSqlQueryProcessor.modelExists(any(), any())).thenReturn(true);
ExecutorService threadPool = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors() * 5);
ObserveQueryExecutor<BlogOwner> observeQueryExecutor = new ObserveQueryExecutor<>(subject, mockSqlQueryProcessor, threadPool, mockSyncStatus, new ModelSorter<>(), maxRecords, 1);
observeQueryExecutor.observeQuery(BlogOwner.class, new ObserveQueryOptions(), observationStarted, onQuerySnapshot, onObservationError, onObservationComplete);
Assert.assertTrue(latch.await(1, TimeUnit.SECONDS));
for (int i = 0; i < 5; i++) {
BlogOwner itemChange = BlogOwner.builder().name("Alan Turing" + i).build();
try {
subject.onNext(StorageItemChange.<BlogOwner>builder().changeId(UUID.randomUUID().toString()).initiator(StorageItemChange.Initiator.SYNC_ENGINE).item(itemChange).patchItem(SerializedModel.create(itemChange, ModelSchema.fromModelClass(BlogOwner.class))).modelSchema(ModelSchema.fromModelClass(BlogOwner.class)).predicate(QueryPredicates.all()).type(StorageItemChange.Type.UPDATE).build());
} catch (AmplifyException exception) {
exception.printStackTrace();
}
}
Assert.assertTrue(changeLatch.await(5, TimeUnit.SECONDS));
}
Aggregations