use of com.amplifyframework.core.model.query.ObserveQueryOptions in project amplify-android by aws-amplify.
the class ObserveQueryExecutorTest method observeQueryCancelsTheOperationOnQueryError.
/**
* testing cancel on observe query.
* @throws DataStoreException DataStoreException
*/
@Test
public void observeQueryCancelsTheOperationOnQueryError() throws DataStoreException {
Consumer<DataStoreQuerySnapshot<BlogOwner>> onQuerySnapshot = NoOpConsumer.create();
Consumer<DataStoreException> onObservationError = NoOpConsumer.create();
Action onObservationComplete = NoOpAction.create();
SQLCommandProcessor sqlCommandProcessor = mock(SQLCommandProcessor.class);
when(sqlCommandProcessor.rawQuery(any())).thenThrow(new DataStoreException("test", "test"));
SqlQueryProcessor sqlQueryProcessor = new SqlQueryProcessor(sqlCommandProcessor, mock(SQLiteCommandFactory.class), mock(SchemaRegistry.class));
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, sqlQueryProcessor, 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.core.model.query.ObserveQueryOptions in project amplify-android by aws-amplify.
the class ObserveQueryExecutorTest method observeQueryReturnsRecordsBasedOnMaxRecords.
/**
* observe Query Returns Records Based On Max Records.
* @throws InterruptedException InterruptedException
* @throws DataStoreException DataStoreException
* @throws AmplifyException AmplifyException
*/
@Test
public void observeQueryReturnsRecordsBasedOnMaxRecords() throws InterruptedException, AmplifyException {
CountDownLatch latch = new CountDownLatch(1);
CountDownLatch changeLatch = new CountDownLatch(3);
AtomicInteger count = new AtomicInteger();
BlogOwner blogOwner = BlogOwner.builder().name("Alan Turing").build();
List<BlogOwner> datastoreResultList = new ArrayList<>();
int maxRecords = 2;
datastoreResultList.add(blogOwner);
Consumer<Cancelable> observationStarted = NoOpConsumer.create();
SyncStatus mockSyncStatus = mock(SyncStatus.class);
when(mockSyncStatus.get(any(), any())).thenReturn(false).thenReturn(true).thenReturn(true);
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(3, value.getItems().size());
Assert.assertTrue(value.getIsSynced());
changeLatch.countDown();
} else if (count.get() == 2) {
Assert.assertEquals(4, value.getItems().size());
changeLatch.countDown();
} else {
Assert.assertEquals(5, value.getItems().size());
changeLatch.countDown();
}
count.getAndIncrement();
};
Consumer<DataStoreException> onObservationError = NoOpConsumer.create();
Action onObservationComplete = NoOpAction.create();
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, maxRecords);
observeQueryExecutor.observeQuery(BlogOwner.class, new ObserveQueryOptions(null, null), 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();
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());
}
Assert.assertTrue(changeLatch.await(7, TimeUnit.SECONDS));
}
use of com.amplifyframework.core.model.query.ObserveQueryOptions in project amplify-android by aws-amplify.
the class RxDataStoreBindingTest method observeQueryFailsWhenCategoryBehaviorDoes.
/**
* The Rx binding for the DataStore's observeQuery behavior is an Observable. It should
* fail with an exception when the DataStore observe method calls back its error consumer.
* @throws InterruptedException If interrupted while test observer is awaiting terminal event
*/
@Test
public void observeQueryFailsWhenCategoryBehaviorDoes() throws InterruptedException {
// Arrange for observer() to callback failure
DataStoreException expectedFailure = new DataStoreException("Expected", "Failure");
doAnswer(invocation -> {
// 0 = clazz, 1 = options, 2 = start consumer, 3 = item consumer, 4 = failure consumer, 5 = onComplete
final int positionOfOnStart = 3;
Consumer<Cancelable> onStart = invocation.getArgument(positionOfOnStart);
onStart.accept(new NoOpCancelable());
final int positionOfOnFailure = 4;
Consumer<DataStoreException> onFailure = invocation.getArgument(positionOfOnFailure);
onFailure.accept(expectedFailure);
// "void"
return null;
}).when(delegate).observeQuery(eq(Model.class), any(), anyConsumer(), anyConsumer(), anyConsumer(), anyAction());
// Act: observe the DataStore via Rx binding
TestObserver<DataStoreQuerySnapshot<Model>> observer = rxDataStore.observeQuery(Model.class, new ObserveQueryOptions()).test();
// Assert: failure is propagated
observer.await(TIMEOUT_SECONDS, TimeUnit.SECONDS);
observer.assertError(expectedFailure);
verify(delegate).observeQuery(eq(Model.class), any(), anyConsumer(), anyConsumer(), anyConsumer(), anyAction());
}
Aggregations