use of com.pushtorefresh.storio3.contentresolver.StorIOContentResolver in project storio by pushtorefresh.
the class PreparedGetNumberOfResultsTest method shouldWrapExceptionIntoStorIOExceptionForBlocking.
@Test
public void shouldWrapExceptionIntoStorIOExceptionForBlocking() {
final StorIOContentResolver storIOContentResolver = mock(StorIOContentResolver.class);
// noinspection unchecked
final GetResolver<Integer> getResolver = mock(GetResolver.class);
when(getResolver.performGet(eq(storIOContentResolver), any(Query.class))).thenThrow(new IllegalStateException("test exception"));
try {
new PreparedGetNumberOfResults.Builder(storIOContentResolver).withQuery(Query.builder().uri(mock(Uri.class)).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.storio3.contentresolver.StorIOContentResolver in project storio by pushtorefresh.
the class DefaultPutResolverTest method shouldCloseCursorIfInsertThrowsException.
@Test
public void shouldCloseCursorIfInsertThrowsException() {
final StorIOContentResolver storIOContentResolver = mock(StorIOContentResolver.class);
final StorIOContentResolver.LowLevel lowLevel = mock(StorIOContentResolver.LowLevel.class);
when(storIOContentResolver.lowLevel()).thenReturn(lowLevel);
// item without id, should be inserted
final TestItem testItem = new TestItem(null);
final Query expectedQuery = Query.builder().uri(TestItem.CONTENT_URI).where(TestItem.COLUMN_ID + " = ?").whereArgs(testItem.getId()).build();
final Cursor cursor = mock(Cursor.class);
when(lowLevel.query(eq(expectedQuery))).thenReturn(cursor);
when(cursor.getCount()).thenReturn(// No results -> insert should be performed
0);
final InsertQuery expectedInsertQuery = InsertQuery.builder().uri(TestItem.CONTENT_URI).build();
when(lowLevel.insert(eq(expectedInsertQuery), any(ContentValues.class))).thenThrow(new IllegalStateException("Fake exception from ContentResolver"));
final PutResolver<TestItem> putResolver = new DefaultPutResolver<TestItem>() {
@NonNull
@Override
protected InsertQuery mapToInsertQuery(@NonNull TestItem object) {
return InsertQuery.builder().uri(TestItem.CONTENT_URI).build();
}
@NonNull
@Override
protected UpdateQuery mapToUpdateQuery(@NonNull TestItem object) {
return UpdateQuery.builder().uri(TestItem.CONTENT_URI).where(TestItem.COLUMN_ID + " = ?").whereArgs(object.getId()).build();
}
@NonNull
@Override
protected ContentValues mapToContentValues(@NonNull TestItem object) {
try {
return TestItem.MAP_TO_CONTENT_VALUES.apply(object);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
};
try {
putResolver.performPut(storIOContentResolver, testItem);
failBecauseExceptionWasNotThrown(IllegalStateException.class);
} catch (IllegalStateException expected) {
assertThat(expected).hasMessage("Fake exception from ContentResolver");
verify(storIOContentResolver).lowLevel();
// Checks that it asks actual ContentResolver for results
verify(lowLevel).query(eq(expectedQuery));
verify(cursor).getCount();
// Cursor must be closed in case of exception!
verify(cursor).close();
verify(lowLevel).insert(eq(expectedInsertQuery), any(ContentValues.class));
verifyNoMoreInteractions(storIOContentResolver, lowLevel, cursor);
}
}
use of com.pushtorefresh.storio3.contentresolver.StorIOContentResolver in project storio by pushtorefresh.
the class DefaultPutResolverTest method shouldCloseCursorIfUpdateThrowsException.
@Test
public void shouldCloseCursorIfUpdateThrowsException() {
final StorIOContentResolver storIOContentResolver = mock(StorIOContentResolver.class);
final StorIOContentResolver.LowLevel lowLevel = mock(StorIOContentResolver.LowLevel.class);
when(storIOContentResolver.lowLevel()).thenReturn(lowLevel);
// item with some id, should be updated
final TestItem testItem = new TestItem(1L);
final Query expectedQuery = Query.builder().uri(TestItem.CONTENT_URI).where(TestItem.COLUMN_ID + " = ?").whereArgs(testItem.getId()).build();
final Cursor cursor = mock(Cursor.class);
when(lowLevel.query(eq(expectedQuery))).thenReturn(cursor);
when(cursor.getCount()).thenReturn(// One result -> update should be performed
1);
final UpdateQuery expectedUpdateQuery = UpdateQuery.builder().uri(TestItem.CONTENT_URI).where(TestItem.COLUMN_ID + " = ?").whereArgs(testItem.getId()).build();
when(lowLevel.update(eq(expectedUpdateQuery), any(ContentValues.class))).thenThrow(new IllegalStateException("Fake exception from ContentResolver"));
final PutResolver<TestItem> putResolver = new DefaultPutResolver<TestItem>() {
@NonNull
@Override
protected InsertQuery mapToInsertQuery(@NonNull TestItem object) {
return InsertQuery.builder().uri(TestItem.CONTENT_URI).build();
}
@NonNull
@Override
protected UpdateQuery mapToUpdateQuery(@NonNull TestItem object) {
return UpdateQuery.builder().uri(TestItem.CONTENT_URI).where(TestItem.COLUMN_ID + " = ?").whereArgs(object.getId()).build();
}
@NonNull
@Override
protected ContentValues mapToContentValues(@NonNull TestItem object) {
try {
return TestItem.MAP_TO_CONTENT_VALUES.apply(object);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
};
try {
putResolver.performPut(storIOContentResolver, testItem);
failBecauseExceptionWasNotThrown(IllegalStateException.class);
} catch (IllegalStateException expected) {
assertThat(expected).hasMessage("Fake exception from ContentResolver");
verify(storIOContentResolver).lowLevel();
// Checks that it asks actual ContentResolver for results
verify(lowLevel).query(eq(expectedQuery));
verify(cursor).getCount();
// Cursor must be closed in case of exception!
verify(cursor).close();
verify(lowLevel).update(eq(expectedUpdateQuery), any(ContentValues.class));
verifyNoMoreInteractions(storIOContentResolver, lowLevel, cursor);
}
}
use of com.pushtorefresh.storio3.contentresolver.StorIOContentResolver in project storio by pushtorefresh.
the class PreparedGetCursorTest method shouldUseStandardGetResolverWithoutExplicitlyPassed.
@Test
public void shouldUseStandardGetResolverWithoutExplicitlyPassed() {
StorIOContentResolver storIOContentResolver = mock(StorIOContentResolver.class);
StorIOContentResolver.LowLevel lowLevel = mock(StorIOContentResolver.LowLevel.class);
when(storIOContentResolver.lowLevel()).thenReturn(lowLevel);
Query query = Query.builder().uri(mock(Uri.class)).build();
new PreparedGetCursor.Builder(storIOContentResolver).withQuery(query).prepare().executeAsBlocking();
verify(storIOContentResolver).lowLevel();
verify(storIOContentResolver).interceptors();
verify(lowLevel).query(query);
verifyNoMoreInteractions(storIOContentResolver, lowLevel);
}
use of com.pushtorefresh.storio3.contentresolver.StorIOContentResolver in project storio by pushtorefresh.
the class DefaultGetResolverTest method query.
@Test
public void query() {
final StorIOContentResolver storIOContentResolver = mock(StorIOContentResolver.class);
final StorIOContentResolver.LowLevel lowLevel = mock(StorIOContentResolver.LowLevel.class);
final Query query = Query.builder().uri(mock(Uri.class)).build();
final Cursor expectedCursor = mock(Cursor.class);
when(storIOContentResolver.lowLevel()).thenReturn(lowLevel);
when(lowLevel.query(query)).thenReturn(expectedCursor);
final GetResolver<TestItem> defaultGetResolver = new DefaultGetResolver<TestItem>() {
@NonNull
@Override
public TestItem mapFromCursor(@NonNull StorIOContentResolver storIOContentResolver, @NonNull Cursor cursor) {
assertThat(cursor).isSameAs(expectedCursor);
return new TestItem();
}
};
final Cursor actualCursor = defaultGetResolver.performGet(storIOContentResolver, query);
// only one request should occur
verify(lowLevel, times(1)).query(any(Query.class));
// and this request should be equals to original
verify(lowLevel, times(1)).query(query);
assertThat(actualCursor).isSameAs(expectedCursor);
}
Aggregations