BulkFetcher { 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; } BulkFetcher(BackupItemsFetcher itemsFetcher); @NonNull BackupCursors fetch(final @NonNull EnumSet<DataType> types,
                                        final @Nullable ContactGroupIds groups,
                                        final int maxItems);  }
@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); }
@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 shouldFetchAllItemsEmptyList() throws Exception { BackupCursors cursors = bulkFetcher.fetch(EnumSet.noneOf(DataType.class), null, 50); assertThat(cursors.count()).isEqualTo(0); }