Search in sources :

Example 16 with StorIOContentResolver

use of com.pushtorefresh.storio.contentresolver.StorIOContentResolver in project storio by pushtorefresh.

the class DefaultStorIOContentResolverTest method shouldThrowExceptionIfContentResolverReturnsNull.

@Test
public void shouldThrowExceptionIfContentResolverReturnsNull() {
    ContentResolver contentResolver = mock(ContentResolver.class);
    StorIOContentResolver storIOContentResolver = DefaultStorIOContentResolver.builder().contentResolver(contentResolver).build();
    Query query = Query.builder().uri(mock(Uri.class)).build();
    when(contentResolver.query(any(Uri.class), any(String[].class), anyString(), any(String[].class), anyString())).thenReturn(// Notice, we return null instead of Cursor
    null);
    try {
        storIOContentResolver.lowLevel().query(query);
    } catch (IllegalStateException expected) {
        assertThat(expected).hasMessage("Cursor returned by content provider is null");
    }
}
Also used : Query(com.pushtorefresh.storio.contentresolver.queries.Query) StorIOContentResolver(com.pushtorefresh.storio.contentresolver.StorIOContentResolver) Uri(android.net.Uri) ContentResolver(android.content.ContentResolver) StorIOContentResolver(com.pushtorefresh.storio.contentresolver.StorIOContentResolver) Test(org.junit.Test)

Example 17 with StorIOContentResolver

use of com.pushtorefresh.storio.contentresolver.StorIOContentResolver in project storio by pushtorefresh.

the class DefaultStorIOContentResolverTest method defaultSchedulerReturnsSpecifiedScheduler.

@Test
public void defaultSchedulerReturnsSpecifiedScheduler() {
    Scheduler scheduler = mock(Scheduler.class);
    StorIOContentResolver storIOContentResolver = DefaultStorIOContentResolver.builder().contentResolver(mock(ContentResolver.class)).defaultScheduler(scheduler).build();
    assertThat(storIOContentResolver.defaultScheduler()).isSameAs(scheduler);
}
Also used : Scheduler(rx.Scheduler) StorIOContentResolver(com.pushtorefresh.storio.contentresolver.StorIOContentResolver) ContentResolver(android.content.ContentResolver) StorIOContentResolver(com.pushtorefresh.storio.contentresolver.StorIOContentResolver) Test(org.junit.Test)

Example 18 with StorIOContentResolver

use of com.pushtorefresh.storio.contentresolver.StorIOContentResolver in project storio by pushtorefresh.

the class DefaultPutResolverTest method insert.

/**
     * Verifies behavior of {@link DefaultPutResolver} for "insert"
     */
@Test
public void insert() {
    final StorIOContentResolver storIOContentResolver = mock(StorIOContentResolver.class);
    final StorIOContentResolver.LowLevel lowLevel = mock(StorIOContentResolver.LowLevel.class);
    // item without id, should be inserted
    final TestItem testItem = new TestItem(null);
    when(storIOContentResolver.lowLevel()).thenReturn(lowLevel);
    final Uri expectedInsertedUri = mock(Uri.class);
    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);
    when(lowLevel.insert(any(InsertQuery.class), any(ContentValues.class))).thenReturn(expectedInsertedUri);
    final InsertQuery expectedInsertQuery = InsertQuery.builder().uri(TestItem.CONTENT_URI).build();
    final PutResolver<TestItem> putResolver = new DefaultPutResolver<TestItem>() {

        @NonNull
        @Override
        protected InsertQuery mapToInsertQuery(@NonNull TestItem object) {
            return expectedInsertQuery;
        }

        @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);
        }
    };
    final ContentValues expectedContentValues = TestItem.MAP_TO_CONTENT_VALUES.call(testItem);
    // Performing Put that should "insert"
    final PutResult putResult = putResolver.performPut(storIOContentResolver, testItem);
    // checks that it asks db for results
    verify(lowLevel).query(eq(expectedQuery));
    // checks that cursor was closed
    verify(cursor).close();
    // only one query should occur
    verify(lowLevel).query(any(Query.class));
    // checks that required insert was performed
    verify(lowLevel).insert(eq(expectedInsertQuery), eq(expectedContentValues));
    // only one insert should occur
    verify(lowLevel).insert(any(InsertQuery.class), any(ContentValues.class));
    // no updates should occur
    verify(lowLevel, never()).update(any(UpdateQuery.class), any(ContentValues.class));
    // put result checks
    assertThat(putResult.wasInserted()).isTrue();
    assertThat(putResult.wasUpdated()).isFalse();
    assertThat(putResult.insertedUri()).isEqualTo(expectedInsertedUri);
    assertThat(putResult.numberOfRowsUpdated()).isNull();
}
Also used : ContentValues(android.content.ContentValues) Query(com.pushtorefresh.storio.contentresolver.queries.Query) InsertQuery(com.pushtorefresh.storio.contentresolver.queries.InsertQuery) UpdateQuery(com.pushtorefresh.storio.contentresolver.queries.UpdateQuery) UpdateQuery(com.pushtorefresh.storio.contentresolver.queries.UpdateQuery) Cursor(android.database.Cursor) Uri(android.net.Uri) InsertQuery(com.pushtorefresh.storio.contentresolver.queries.InsertQuery) NonNull(android.support.annotation.NonNull) StorIOContentResolver(com.pushtorefresh.storio.contentresolver.StorIOContentResolver) Test(org.junit.Test)

Example 19 with StorIOContentResolver

use of com.pushtorefresh.storio.contentresolver.StorIOContentResolver in project storio by pushtorefresh.

the class DefaultPutResolverTest method update.

/**
     * Verifies behavior of {@link DefaultPutResolver} for "update"
     */
@Test
public void update() {
    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) {
            return TestItem.MAP_TO_CONTENT_VALUES.call(object);
        }
    };
    final ContentValues expectedContentValues = TestItem.MAP_TO_CONTENT_VALUES.call(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();
}
Also used : ContentValues(android.content.ContentValues) Query(com.pushtorefresh.storio.contentresolver.queries.Query) InsertQuery(com.pushtorefresh.storio.contentresolver.queries.InsertQuery) UpdateQuery(com.pushtorefresh.storio.contentresolver.queries.UpdateQuery) UpdateQuery(com.pushtorefresh.storio.contentresolver.queries.UpdateQuery) Cursor(android.database.Cursor) InsertQuery(com.pushtorefresh.storio.contentresolver.queries.InsertQuery) NonNull(android.support.annotation.NonNull) StorIOContentResolver(com.pushtorefresh.storio.contentresolver.StorIOContentResolver) Test(org.junit.Test)

Example 20 with StorIOContentResolver

use of com.pushtorefresh.storio.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) {
            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).update(eq(expectedUpdateQuery), any(ContentValues.class));
        verifyNoMoreInteractions(storIOContentResolver, lowLevel, cursor);
    }
}
Also used : ContentValues(android.content.ContentValues) Query(com.pushtorefresh.storio.contentresolver.queries.Query) InsertQuery(com.pushtorefresh.storio.contentresolver.queries.InsertQuery) UpdateQuery(com.pushtorefresh.storio.contentresolver.queries.UpdateQuery) NonNull(android.support.annotation.NonNull) UpdateQuery(com.pushtorefresh.storio.contentresolver.queries.UpdateQuery) Cursor(android.database.Cursor) StorIOContentResolver(com.pushtorefresh.storio.contentresolver.StorIOContentResolver) Test(org.junit.Test)

Aggregations

StorIOContentResolver (com.pushtorefresh.storio.contentresolver.StorIOContentResolver)23 Test (org.junit.Test)20 Query (com.pushtorefresh.storio.contentresolver.queries.Query)11 NonNull (android.support.annotation.NonNull)9 ContentResolver (android.content.ContentResolver)8 Cursor (android.database.Cursor)6 Uri (android.net.Uri)6 ContentValues (android.content.ContentValues)5 StorIOException (com.pushtorefresh.storio.StorIOException)5 InsertQuery (com.pushtorefresh.storio.contentresolver.queries.InsertQuery)5 UpdateQuery (com.pushtorefresh.storio.contentresolver.queries.UpdateQuery)5 TypeMappingFinder (com.pushtorefresh.storio.TypeMappingFinder)4 GetResolver (com.pushtorefresh.storio.contentresolver.operations.get.GetResolver)3 WorkerThread (android.support.annotation.WorkerThread)2 ContentResolverTypeMapping (com.pushtorefresh.storio.contentresolver.ContentResolverTypeMapping)2 TypeMappingFinderImpl (com.pushtorefresh.storio.internal.TypeMappingFinderImpl)2 SimpleImmutableEntry (java.util.AbstractMap.SimpleImmutableEntry)2 HashMap (java.util.HashMap)2 TestSubscriber (rx.observers.TestSubscriber)2 Handler (android.os.Handler)1