public @NonNull BackupCursors fetch(final @NonNull EnumSet<DataType> types, final @Nullable ContactGroupIds groups, final int maxItems) { int max = maxItems; BackupCursors cursors = new BackupCursors(); for (DataType type : types) { Cursor cursor = itemsFetcher.getItemsForDataType(type, groups, max); cursors.add(type, cursor); if (max > 0) { max = Math.max(max - cursor.getCount(), 0); } if (max == 0) break; } return cursors; }
private void mockFetch(DataType type, final int n) { when(fetcher.getItemsForDataType(eq(type), any(ContactGroupIds.class), anyInt())).then(new Answer<Object>() { @Override public Object answer(InvocationOnMock invocationOnMock) throws Throwable { return testMessages(n); } }); }
private void mockAllFetchEmpty() { when(fetcher.getItemsForDataType(any(DataType.class), any(ContactGroupIds.class), anyInt())).thenReturn(emptyCursor()); } }
@Test public void shouldFetchAllItemsRespectingMaxItems() throws Exception { when(fetcher.getItemsForDataType(SMS, null, 5)).thenReturn(cursor(5)); BackupCursors cursors = bulkFetcher.fetch(EnumSet.of(SMS, MMS), null, 5); assertThat(cursors.count()).isEqualTo(5); assertThat(cursors.count(SMS)).isEqualTo(5); verify(fetcher, never()).getItemsForDataType(eq(DataType.MMS), any(ContactGroupIds.class), anyInt()); }
@Test public void shouldCatchNullPointerExceptions() throws Exception { preferences.getDataTypePreferences().setBackupEnabled(true, SMS); when(resolver.query(any(Uri.class), any(String[].class), anyString(), any(String[].class), anyString())) .thenThrow(new NullPointerException()); mockEmptyQuery(); assertThat(fetcher.getItemsForDataType(SMS, null, -1).getCount()).isEqualTo(0); }
@Test public void shouldCatchSQLiteExceptions() throws Exception { preferences.getDataTypePreferences().setBackupEnabled(true, SMS); when(resolver.query(any(Uri.class), any(String[].class), anyString(), any(String[].class), anyString())) .thenThrow(new SQLiteException()); mockEmptyQuery(); assertThat(fetcher.getItemsForDataType(SMS, null, -1).getCount()).isEqualTo(0); }
@Test public void shouldGetItemsForDataType() throws Exception { preferences.getDataTypePreferences().setBackupEnabled(true, SMS); assertThat(fetcher.getItemsForDataType(SMS, null, -1).getCount()).isEqualTo(0); verifyZeroInteractions(resolver); }
@Test public void shouldFetchAllItems() throws Exception { when(fetcher.getItemsForDataType(SMS, null, 50)).thenReturn(cursor(3)); when(fetcher.getItemsForDataType(MMS, null, 47)).thenReturn(cursor(5)); BackupCursors cursors = bulkFetcher.fetch(EnumSet.of(SMS, MMS), null, 50); assertThat(cursors.count()).isEqualTo(8); assertThat(cursors.count(SMS)).isEqualTo(3); assertThat(cursors.count(MMS)).isEqualTo(5); }