use of com.pushtorefresh.storio.contentresolver.StorIOContentResolver in project storio by pushtorefresh.
the class DefaultStorIOContentResolverTest method defaultSchedulerReturnsNullIfSpecifiedSchedulerNull.
@Test
public void defaultSchedulerReturnsNullIfSpecifiedSchedulerNull() {
StorIOContentResolver storIOContentResolver = DefaultStorIOContentResolver.builder().contentResolver(mock(ContentResolver.class)).defaultScheduler(null).build();
assertThat(storIOContentResolver.defaultScheduler()).isNull();
}
use of com.pushtorefresh.storio.contentresolver.StorIOContentResolver in project storio by pushtorefresh.
the class DefaultStorIOContentResolverTest method defaultSchedulerReturnsIOSchedulerIfNotSpecified.
@Test
public void defaultSchedulerReturnsIOSchedulerIfNotSpecified() {
StorIOContentResolver storIOContentResolver = DefaultStorIOContentResolver.builder().contentResolver(mock(ContentResolver.class)).build();
assertThat(storIOContentResolver.defaultScheduler()).isSameAs(io());
}
use of com.pushtorefresh.storio.contentresolver.StorIOContentResolver in project storio by pushtorefresh.
the class DefaultStorIOContentResolverTest method typeMappingShouldWorkWithSpecifiedTypeMappingFinder.
@Test
public void typeMappingShouldWorkWithSpecifiedTypeMappingFinder() {
TypeMappingFinder typeMappingFinder = new TypeMappingFinderImpl();
//noinspection unchecked
ContentResolverTypeMapping<ClassEntity> typeMapping = ContentResolverTypeMapping.builder().putResolver(mock(PutResolver.class)).getResolver(mock(GetResolver.class)).deleteResolver(mock(DeleteResolver.class)).build();
StorIOContentResolver storIOContentResolver = DefaultStorIOContentResolver.builder().contentResolver(mock(ContentResolver.class)).typeMappingFinder(typeMappingFinder).addTypeMapping(ClassEntity.class, typeMapping).build();
assertThat(storIOContentResolver.lowLevel().typeMapping(ClassEntity.class)).isEqualTo(typeMapping);
}
use of com.pushtorefresh.storio.contentresolver.StorIOContentResolver in project storio by pushtorefresh.
the class DefaultDeleteResolverTest method performDelete.
@Test
public void performDelete() {
final StorIOContentResolver storIOContentResolver = mock(StorIOContentResolver.class);
final StorIOContentResolver.LowLevel lowLevel = mock(StorIOContentResolver.LowLevel.class);
when(storIOContentResolver.lowLevel()).thenReturn(lowLevel);
final int expectedNumberOfRowsDeleted = 1;
when(lowLevel.delete(any(DeleteQuery.class))).thenReturn(expectedNumberOfRowsDeleted);
final Uri expectedUri = mock(Uri.class);
final DeleteQuery expectedDeleteQuery = DeleteQuery.builder().uri(expectedUri).where("test where clause").whereArgs("test").build();
final TestItem testItem = TestItem.newInstance();
final DefaultDeleteResolver<TestItem> defaultDeleteResolver = new DefaultDeleteResolver<TestItem>() {
@NonNull
@Override
protected DeleteQuery mapToDeleteQuery(@NonNull TestItem object) {
assertThat(object).isSameAs(testItem);
return expectedDeleteQuery;
}
};
// Performing Delete Operation
final DeleteResult deleteResult = defaultDeleteResolver.performDelete(storIOContentResolver, testItem);
// checks that required delete was performed
verify(lowLevel, times(1)).delete(expectedDeleteQuery);
// only one delete should be performed
verify(lowLevel, times(1)).delete(any(DeleteQuery.class));
// delete result checks
assertThat(deleteResult.numberOfRowsDeleted()).isEqualTo(expectedNumberOfRowsDeleted);
assertThat(deleteResult.affectedUris()).hasSize(1);
assertThat(deleteResult.affectedUris()).contains(expectedUri);
}
use of com.pushtorefresh.storio.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) {
return TestItem.MAP_TO_CONTENT_VALUES.call(object);
}
};
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);
}
}
Aggregations