FileDownloader implements AutoCloseable { public Optional<File> getFile(FileReference fileReference) { return getFile(new FileReferenceDownload(fileReference)); } FileDownloader(ConnectionPool connectionPool);  FileDownloader(ConnectionPool connectionPool, File downloadDirectory);   FileDownloader(ConnectionPool connectionPool, File downloadDirectory, File tmpDirectory, Duration timeout, Duration sleepBetweenRetries); Optional<File> getFile(FileReference fileReference); Optional<File> getFile(FileReferenceDownload fileReferenceDownload); Map<FileReference, Double> downloadStatus(); void downloadIfNeeded(FileReferenceDownload fileReferenceDownload); FileReferenceDownloader fileReferenceDownloader(); void close(); static File defaultDownloadDirectory; }
@Test public void getFile() throws IOException { File downloadDir = fileDownloader.downloadDirectory(); { String fileReferenceString = "foo"; String filename = "foo.jar"; FileReference fileReference = new FileReference(fileReferenceString); File fileReferenceFullPath = fileReferenceFullPath(downloadDir, fileReference); writeFileReference(downloadDir, fileReferenceString, filename); Optional<File> pathToFile = fileDownloader.getFile(fileReference); assertTrue(pathToFile.isPresent()); String downloadedFile = new File(fileReferenceFullPath, filename).getAbsolutePath(); assertEquals(new File(fileReferenceFullPath, filename).getAbsolutePath(), downloadedFile); assertEquals("content", IOUtils.readFile(pathToFile.get())); assertDownloadStatus(fileDownloader, fileReference, 1.0); } { connection.setResponseHandler(new MockConnection.UnknownFileReferenceResponseHandler()); FileReference fileReference = new FileReference("bar"); File fileReferenceFullPath = fileReferenceFullPath(downloadDir, fileReference); assertFalse(fileReferenceFullPath.getAbsolutePath(), fileDownloader.getFile(fileReference).isPresent()); assertDownloadStatus(fileDownloader, fileReference, 0.0); } { FileReference fileReference = new FileReference("fileReference"); File fileReferenceFullPath = fileReferenceFullPath(downloadDir, fileReference); assertFalse(fileReferenceFullPath.getAbsolutePath(), fileDownloader.getFile(fileReference).isPresent()); assertDownloadStatus(fileDownloader, fileReference, 0.0); String filename = "abc.jar"; receiveFile(fileDownloader, fileReference, filename, FileReferenceData.Type.file, "some other content"); Optional<File> downloadedFile = fileDownloader.getFile(fileReference); assertTrue(downloadedFile.isPresent()); File downloadedFileFullPath = new File(fileReferenceFullPath, filename); assertEquals(downloadedFileFullPath.getAbsolutePath(), downloadedFile.get().getAbsolutePath()); assertEquals("some other content", IOUtils.readFile(downloadedFile.get())); assertDownloadStatus(fileDownloader, fileReference, 1.0); } { FileReference fileReference = new FileReference("fileReferenceToDirWithManyFiles"); File fileReferenceFullPath = fileReferenceFullPath(downloadDir, fileReference); assertFalse(fileReferenceFullPath.getAbsolutePath(), fileDownloader.getFile(fileReference).isPresent()); assertDownloadStatus(fileDownloader, fileReference, 0.0); String filename = "abc.tar.gz"; Path tempPath = Files.createTempDirectory("dir"); File subdir = new File(tempPath.toFile(), "subdir"); File fooFile = new File(subdir, "foo"); IOUtils.writeFile(fooFile, "foo", false); File barFile = new File(subdir, "really-long-filename-over-100-bytes-aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"); IOUtils.writeFile(barFile, "bar", false); File tarFile = CompressedFileReference.compress(tempPath.toFile(), Arrays.asList(fooFile, barFile), new File(tempPath.toFile(), filename)); byte[] tarredContent = IOUtils.readFileBytes(tarFile); receiveFile(fileDownloader, fileReference, filename, FileReferenceData.Type.compressed, tarredContent); Optional<File> downloadedFile = fileDownloader.getFile(fileReference); assertTrue(downloadedFile.isPresent()); File downloadedFoo = new File(fileReferenceFullPath, tempPath.relativize(fooFile.toPath()).toString()); File downloadedBar = new File(fileReferenceFullPath, tempPath.relativize(barFile.toPath()).toString()); assertEquals("foo", IOUtils.readFile(downloadedFoo)); assertEquals("bar", IOUtils.readFile(downloadedBar)); assertDownloadStatus(fileDownloader, fileReference, 1.0); } }