use of com.pushtorefresh.storio3.contentresolver.StorIOContentResolver in project storio by pushtorefresh.
the class DefaultPutResolverTest method update.
/**
* Verifies behavior of {@link DefaultPutResolver} for "update"
*/
@Test
public void update() throws Exception {
final StorIOContentResolver storIOContentResolver = mock(StorIOContentResolver.class);
final StorIOContentResolver.LowLevel lowLevel = mock(StorIOContentResolver.LowLevel.class);
// item with some id, should be updated
final TestItem testItem = new TestItem(1L);
when(storIOContentResolver.lowLevel()).thenReturn(lowLevel);
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(// Some rows already in db -> update should be performed
1);
final Integer expectedNumberOfRowsUpdated = 1;
when(lowLevel.update(any(UpdateQuery.class), any(ContentValues.class))).thenReturn(expectedNumberOfRowsUpdated);
final UpdateQuery expectedUpdateQuery = UpdateQuery.builder().uri(TestItem.CONTENT_URI).where(TestItem.COLUMN_ID + " = ?").whereArgs(testItem.getId()).build();
final PutResolver<TestItem> putResolver = new DefaultPutResolver<TestItem>() {
@NonNull
@Override
protected InsertQuery mapToInsertQuery(@NonNull TestItem object) {
fail("Should not be called");
return null;
}
@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);
}
}
};
final ContentValues expectedContentValues = TestItem.MAP_TO_CONTENT_VALUES.apply(testItem);
// Performing Put that should "update"
final PutResult putResult = putResolver.performPut(storIOContentResolver, testItem);
// checks that it asks db for results
verify(lowLevel, times(1)).query(eq(expectedQuery));
// checks that cursor was closed
verify(cursor, times(1)).close();
// only one query should occur
verify(lowLevel, times(1)).query(any(Query.class));
// checks that required update was performed
verify(lowLevel, times(1)).update(eq(expectedUpdateQuery), eq(expectedContentValues));
// only one update should occur
verify(lowLevel, times(1)).update(any(UpdateQuery.class), any(ContentValues.class));
// no inserts should occur
verify(lowLevel, times(0)).insert(any(InsertQuery.class), any(ContentValues.class));
// put result checks
assertThat(putResult.wasInserted()).isFalse();
assertThat(putResult.wasUpdated()).isTrue();
assertThat(putResult.numberOfRowsUpdated()).isEqualTo(expectedNumberOfRowsUpdated);
assertThat(putResult.insertedUri()).isNull();
}
use of com.pushtorefresh.storio3.contentresolver.StorIOContentResolver in project storio by pushtorefresh.
the class PreparedGetCursorTest method checkThatStandardGetResolverDoesNotModifyCursor.
@Test
public void checkThatStandardGetResolverDoesNotModifyCursor() {
StorIOContentResolver storIOContentResolver = mock(StorIOContentResolver.class);
Cursor cursor = mock(Cursor.class);
Cursor cursorAfterMap = PreparedGetCursor.CompleteBuilder.STANDARD_GET_RESOLVER.mapFromCursor(storIOContentResolver, cursor);
assertThat(cursorAfterMap).isEqualTo(cursor);
}
use of com.pushtorefresh.storio3.contentresolver.StorIOContentResolver in project storio by pushtorefresh.
the class DefaultStorIOContentResolverTest method getTypeMappingFinder.
@Nullable
private static TypeMappingFinder getTypeMappingFinder(@NonNull DefaultStorIOContentResolver storIOContentResolver) throws NoSuchFieldException, IllegalAccessException {
Field field = DefaultStorIOContentResolver.LowLevelImpl.class.getDeclaredField("typeMappingFinder");
field.setAccessible(true);
return (TypeMappingFinder) field.get(storIOContentResolver.lowLevel());
}
use of com.pushtorefresh.storio3.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.storio3.contentresolver.StorIOContentResolver in project storio by pushtorefresh.
the class DefaultStorIOContentResolverTest method typeMappingShouldWorkForMultipleTypes.
@Test
public void typeMappingShouldWorkForMultipleTypes() {
class AnotherEntity {
}
TypeMappingFinder typeMappingFinder = new TypeMappingFinderImpl();
// noinspection unchecked
ContentResolverTypeMapping<ClassEntity> typeMapping = ContentResolverTypeMapping.builder().putResolver(mock(PutResolver.class)).getResolver(mock(GetResolver.class)).deleteResolver(mock(DeleteResolver.class)).build();
// noinspection unchecked
ContentResolverTypeMapping<AnotherEntity> anotherMapping = 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).addTypeMapping(AnotherEntity.class, anotherMapping).build();
assertThat(storIOContentResolver.lowLevel().typeMapping(ClassEntity.class)).isEqualTo(typeMapping);
assertThat(storIOContentResolver.lowLevel().typeMapping(AnotherEntity.class)).isEqualTo(anotherMapping);
}
Aggregations