use of com.pushtorefresh.storio.StorIOException in project storio by pushtorefresh.
the class PreparedGetNumberOfResults method asRxObservable.
/**
* Creates "Hot" {@link Observable} which will be subscribed to changes of tables from query
* and will emit result each time change occurs.
* <p>
* First result will be emitted immediately after subscription,
* other emissions will occur only if changes of tables from query will occur during lifetime of
* the {@link Observable}.
* <dl>
* <dt><b>Scheduler:</b></dt>
* <dd>Operates on {@link StorIOSQLite#defaultScheduler()} if not {@code null}.</dd>
* </dl>
* <p>
* Please don't forget to unsubscribe from this {@link Observable} because
* it's "Hot" and endless.
*
* @return non-null {@link Observable} which will emit non-null
* number of results of the executed query and will be subscribed to changes of tables from query.
*/
@NonNull
@CheckResult
@Override
public Observable<Integer> asRxObservable() {
throwExceptionIfRxJavaIsNotAvailable("asRxObservable()");
final Set<String> tables;
if (query != null) {
tables = new HashSet<String>(1);
tables.add(query.table());
} else if (rawQuery != null) {
tables = rawQuery.observesTables();
} else {
throw new StorIOException("Please specify query");
}
final Observable<Integer> observable;
if (!tables.isEmpty()) {
observable = storIOSQLite.observeChangesInTables(// each change triggers executeAsBlocking
tables).map(MapSomethingToExecuteAsBlocking.newInstance(this)).startWith(// start stream with first query result
Observable.create(OnSubscribeExecuteAsBlocking.newInstance(this))).onBackpressureLatest();
} else {
observable = Observable.create(OnSubscribeExecuteAsBlocking.newInstance(this));
}
return RxJavaUtils.subscribeOn(storIOSQLite, observable);
}
use of com.pushtorefresh.storio.StorIOException in project storio by pushtorefresh.
the class PreparedGetNumberOfResultsTest method shouldWrapExceptionIntoStorIOExceptionForObservable.
@Test
public void shouldWrapExceptionIntoStorIOExceptionForObservable() {
final StorIOSQLite storIOSQLite = mock(StorIOSQLite.class);
when(storIOSQLite.observeChangesInTables(eq(singleton("test_table")))).thenReturn(Observable.<Changes>empty());
//noinspection unchecked
final GetResolver<Integer> getResolver = mock(GetResolver.class);
when(getResolver.performGet(eq(storIOSQLite), any(Query.class))).thenThrow(new IllegalStateException("test exception"));
final TestSubscriber<Integer> testSubscriber = new TestSubscriber<Integer>();
new PreparedGetNumberOfResults.Builder(storIOSQLite).withQuery(Query.builder().table("test_table").build()).withGetResolver(getResolver).prepare().asRxObservable().subscribe(testSubscriber);
testSubscriber.awaitTerminalEvent(60, SECONDS);
testSubscriber.assertError(StorIOException.class);
assertThat(testSubscriber.getOnErrorEvents()).hasSize(1);
StorIOException storIOException = (StorIOException) testSubscriber.getOnErrorEvents().get(0);
IllegalStateException cause = (IllegalStateException) storIOException.getCause();
assertThat(cause).hasMessage("test exception");
testSubscriber.unsubscribe();
}
use of com.pushtorefresh.storio.StorIOException in project storio by pushtorefresh.
the class PreparedGetNumberOfResultsTest method shouldWrapExceptionIntoStorIOExceptionForBlocking.
@Test
public void shouldWrapExceptionIntoStorIOExceptionForBlocking() {
final StorIOSQLite storIOSQLite = mock(StorIOSQLite.class);
//noinspection unchecked
final GetResolver<Integer> getResolver = mock(GetResolver.class);
when(getResolver.performGet(eq(storIOSQLite), any(Query.class))).thenThrow(new IllegalStateException("test exception"));
try {
new PreparedGetNumberOfResults.Builder(storIOSQLite).withQuery(Query.builder().table("test_table").build()).withGetResolver(getResolver).prepare().executeAsBlocking();
failBecauseExceptionWasNotThrown(StorIOException.class);
} catch (StorIOException expected) {
IllegalStateException cause = (IllegalStateException) expected.getCause();
assertThat(cause).hasMessage("test exception");
}
}
use of com.pushtorefresh.storio.StorIOException in project storio by pushtorefresh.
the class PreparedPutContentValuesIterableTest method verifyBehaviorInCaseOfExceptionWithoutTransactionCompletable.
@Test
public void verifyBehaviorInCaseOfExceptionWithoutTransactionCompletable() {
final StorIOSQLite storIOSQLite = mock(StorIOSQLite.class);
final StorIOSQLite.Internal internal = mock(StorIOSQLite.Internal.class);
//noinspection unchecked
final PutResolver<ContentValues> putResolver = mock(PutResolver.class);
final List<ContentValues> contentValues = singletonList(mock(ContentValues.class));
when(putResolver.performPut(same(storIOSQLite), any(ContentValues.class))).thenThrow(new IllegalStateException("test exception"));
final TestSubscriber<PutResults<ContentValues>> testSubscriber = new TestSubscriber<PutResults<ContentValues>>();
new PreparedPutContentValuesIterable.Builder(storIOSQLite, contentValues).withPutResolver(putResolver).useTransaction(false).prepare().asRxCompletable().subscribe(testSubscriber);
testSubscriber.awaitTerminalEvent();
testSubscriber.assertNoValues();
testSubscriber.assertError(StorIOException.class);
//noinspection ThrowableResultOfMethodCallIgnored
StorIOException expected = (StorIOException) testSubscriber.getOnErrorEvents().get(0);
IllegalStateException cause = (IllegalStateException) expected.getCause();
assertThat(cause).hasMessage("test exception");
// Main check of this test
verify(internal, never()).endTransaction();
verify(storIOSQLite).lowLevel();
verify(storIOSQLite).defaultScheduler();
verify(putResolver).performPut(same(storIOSQLite), any(ContentValues.class));
verifyNoMoreInteractions(storIOSQLite, internal, putResolver);
}
use of com.pushtorefresh.storio.StorIOException in project storio by pushtorefresh.
the class PreparedPutContentValuesIterableTest method verifyBehaviorInCaseOfExceptionWithoutTransactionObservable.
@Test
public void verifyBehaviorInCaseOfExceptionWithoutTransactionObservable() {
final StorIOSQLite storIOSQLite = mock(StorIOSQLite.class);
final StorIOSQLite.Internal internal = mock(StorIOSQLite.Internal.class);
//noinspection unchecked
final PutResolver<ContentValues> putResolver = mock(PutResolver.class);
final List<ContentValues> contentValues = singletonList(mock(ContentValues.class));
when(putResolver.performPut(same(storIOSQLite), any(ContentValues.class))).thenThrow(new IllegalStateException("test exception"));
final TestSubscriber<PutResults<ContentValues>> testSubscriber = new TestSubscriber<PutResults<ContentValues>>();
new PreparedPutContentValuesIterable.Builder(storIOSQLite, contentValues).withPutResolver(putResolver).useTransaction(false).prepare().asRxObservable().subscribe(testSubscriber);
testSubscriber.awaitTerminalEvent();
testSubscriber.assertNoValues();
testSubscriber.assertError(StorIOException.class);
//noinspection ThrowableResultOfMethodCallIgnored
StorIOException expected = (StorIOException) testSubscriber.getOnErrorEvents().get(0);
IllegalStateException cause = (IllegalStateException) expected.getCause();
assertThat(cause).hasMessage("test exception");
// Main check of this test
verify(internal, never()).endTransaction();
verify(storIOSQLite).lowLevel();
verify(storIOSQLite).defaultScheduler();
verify(putResolver).performPut(same(storIOSQLite), any(ContentValues.class));
verifyNoMoreInteractions(storIOSQLite, internal, putResolver);
}
Aggregations