CompletedDownloadBatchesExtractor { List<CompletedDownloadBatch> extractMigrations() { Cursor batchesCursor = database.rawQuery(BATCHES_QUERY); if (batchesCursor == null) { return Collections.emptyList(); } try { List<CompletedDownloadBatch> completedDownloadBatches = new ArrayList<>(); while (batchesCursor.moveToNext()) { String batchId = batchesCursor.getString(BATCH_ID_COLUMN); Cursor downloadsCursor = database.rawQuery(DOWNLOADS_QUERY, batchId); if (downloadsCursor == null) { return Collections.emptyList(); } String batchTitle = batchesCursor.getString(TITLE_COLUMN); long downloadedDateTimeInMillis = batchesCursor.getLong(MODIFIED_TIMESTAMP_COLUMN); List<CompletedDownloadFile> downloadFiles = new ArrayList<>(); Set<String> uris = new HashSet<>(); Set<String> fileIds = new HashSet<>(); DownloadBatchId downloadBatchId = null; try { while (downloadsCursor.moveToNext()) { String originalFileId = downloadsCursor.getString(FILE_ID_COLUMN); String originalNetworkAddress = downloadsCursor.getString(NETWORK_ADDRESS_COLUMN); String originalUniqueFileLocation = downloadsCursor.getString(FILE_UNIQUE_LOCATION_COLUMN); String sanitizedOriginalUniqueFileLocation = MigrationStoragePathSanitizer.sanitize(originalUniqueFileLocation); String originalFileLocation = downloadsCursor.getString(FILE_ORIGINAL_LOCATION_COLUMN); if (downloadsCursor.isFirst()) { downloadBatchId = createDownloadBatchIdFrom(originalFileId, batchId); } if (uris.contains(originalNetworkAddress) && fileIds.contains(originalFileId)) { continue; } else { uris.add(originalNetworkAddress); fileIds.add(originalFileId); } FilePath newFilePath = MigrationPathExtractor.extractMigrationPath( basePath, sanitizedOriginalUniqueFileLocation, downloadBatchId ); long rawFileSize = fileSizeExtractor.fileSizeFor(originalFileLocation); FileSize fileSize = FileSizeCreator.createForCompletedDownloadBatch(rawFileSize); CompletedDownloadFile downloadFile = new CompletedDownloadFile( originalFileId, originalFileLocation, newFilePath.path(), fileSize, originalNetworkAddress ); downloadFiles.add(downloadFile); } } finally { downloadsCursor.close(); } DownloadBatchTitle downloadBatchTitle = DownloadBatchTitleCreator.createFrom(batchTitle); CompletedDownloadBatch completedDownloadBatch = new CompletedDownloadBatch( downloadBatchId, downloadBatchTitle, downloadedDateTimeInMillis, downloadFiles, primaryStorageWithDownloadsSubpackage ); completedDownloadBatches.add(completedDownloadBatch); } return completedDownloadBatches; } finally { batchesCursor.close(); } } CompletedDownloadBatchesExtractor(SqlDatabaseWrapper database,
                                      String basePath,
                                      FileSizeExtractor fileSizeExtractor,
                                      StorageRoot primaryStorageWithDownloadsSubpackage);   }
@Test public void returnsMigrations_WhenExtracting() { given(database.rawQuery(BATCHES_QUERY)).willReturn(BATCHES_CURSOR); given(database.rawQuery(ArgumentMatchers.eq(DOWNLOADS_QUERY), ArgumentMatchers.eq("1"))).willReturn(BATCH_ONE_DOWNLOADS_CURSOR); given(database.rawQuery(ArgumentMatchers.eq(DOWNLOADS_QUERY), ArgumentMatchers.eq("2"))).willReturn(BATCH_TWO_DOWNLOADS_CURSOR); given(fileSizeExtractor.fileSizeFor(ArgumentMatchers.anyString())) .willReturn(1000L) .willReturn(2000L) .willReturn(500L) .willReturn(750L); List<CompletedDownloadBatch> migrations = migrationExtractor.extractMigrations(); Truth.assertThat(migrations).isEqualTo(expectedMigrations()); }