Search in sources :

Example 31 with StorIOContentResolver

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");
    }
}
Also used : Query(com.pushtorefresh.storio3.contentresolver.queries.Query) StorIOException(com.pushtorefresh.storio3.StorIOException) StorIOContentResolver(com.pushtorefresh.storio3.contentresolver.StorIOContentResolver) Test(org.junit.Test)

Example 32 with StorIOContentResolver

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);
    }
}
Also used : ContentValues(android.content.ContentValues) InsertQuery(com.pushtorefresh.storio3.contentresolver.queries.InsertQuery) Query(com.pushtorefresh.storio3.contentresolver.queries.Query) UpdateQuery(com.pushtorefresh.storio3.contentresolver.queries.UpdateQuery) Cursor(android.database.Cursor) InsertQuery(com.pushtorefresh.storio3.contentresolver.queries.InsertQuery) NonNull(android.support.annotation.NonNull) StorIOContentResolver(com.pushtorefresh.storio3.contentresolver.StorIOContentResolver) Test(org.junit.Test)

Example 33 with StorIOContentResolver

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);
    }
}
Also used : ContentValues(android.content.ContentValues) InsertQuery(com.pushtorefresh.storio3.contentresolver.queries.InsertQuery) Query(com.pushtorefresh.storio3.contentresolver.queries.Query) UpdateQuery(com.pushtorefresh.storio3.contentresolver.queries.UpdateQuery) UpdateQuery(com.pushtorefresh.storio3.contentresolver.queries.UpdateQuery) Cursor(android.database.Cursor) NonNull(android.support.annotation.NonNull) StorIOContentResolver(com.pushtorefresh.storio3.contentresolver.StorIOContentResolver) Test(org.junit.Test)

Example 34 with StorIOContentResolver

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);
}
Also used : Query(com.pushtorefresh.storio3.contentresolver.queries.Query) StorIOContentResolver(com.pushtorefresh.storio3.contentresolver.StorIOContentResolver) Test(org.junit.Test)

Example 35 with StorIOContentResolver

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);
}
Also used : Query(com.pushtorefresh.storio3.contentresolver.queries.Query) NonNull(android.support.annotation.NonNull) Cursor(android.database.Cursor) StorIOContentResolver(com.pushtorefresh.storio3.contentresolver.StorIOContentResolver) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)31 StorIOContentResolver (com.pushtorefresh.storio3.contentresolver.StorIOContentResolver)23 Query (com.pushtorefresh.storio3.contentresolver.queries.Query)11 ContentResolver (android.content.ContentResolver)9 NonNull (android.support.annotation.NonNull)9 ContentValues (android.content.ContentValues)8 Cursor (android.database.Cursor)7 Uri (android.net.Uri)7 DeleteQuery (com.pushtorefresh.storio3.contentresolver.queries.DeleteQuery)6 TypeMappingFinder (com.pushtorefresh.storio3.TypeMappingFinder)5 InsertQuery (com.pushtorefresh.storio3.contentresolver.queries.InsertQuery)5 UpdateQuery (com.pushtorefresh.storio3.contentresolver.queries.UpdateQuery)5 DeleteResult (com.pushtorefresh.storio3.contentresolver.operations.delete.DeleteResult)4 PutResult (com.pushtorefresh.storio3.contentresolver.operations.put.PutResult)4 StorIOException (com.pushtorefresh.storio3.StorIOException)3 GetResolver (com.pushtorefresh.storio3.contentresolver.operations.get.GetResolver)3 Handler (android.os.Handler)2 TypeMappingFinderImpl (com.pushtorefresh.storio3.internal.TypeMappingFinderImpl)2 Completable (io.reactivex.Completable)2 ContentObserver (android.database.ContentObserver)1