SourcesExporter { public Collection<ReleaseWithSources> downloadSources(SW360ReleaseClientAdapterAsync releaseAdapter, Collection<SW360Release> releases) { List<CompletableFuture<ReleaseWithSources>> downloadFutures = releases.stream() .map(release -> downloadSourcesForRelease(releaseAdapter, release)) .collect(Collectors.toList()); return FutureUtils.sequence(downloadFutures, ex -> false).join(); } SourcesExporter(Path sourcePath); Collection<ReleaseWithSources> downloadSources(SW360ReleaseClientAdapterAsync releaseAdapter,
                                                          Collection<SW360Release> releases); void removeUnreferencedFiles(Collection<ReleaseWithSources> releases);  }
@Test public void testAttachmentsAreDownloadedSuccessfully() { Set<SW360SparseAttachment> attachments1 = createAttachments(1, 1); Set<SW360SparseAttachment> attachments2 = createAttachments(2, 2); SW360Release release1 = createReleaseWithAttachments(1, attachments1); SW360Release release2 = createReleaseWithAttachments(2, attachments2); List<SW360Release> releases = Arrays.asList(release1, release2); List<ReleaseWithSources> expResult = Arrays.asList(createReleaseWithSources(release1, attachments1), createReleaseWithSources(release2, attachments2)); expectDownloads(release1, attachments1); expectDownloads(release2, attachments2); Collection<ReleaseWithSources> result = sourcesExporter.downloadSources(releaseAdapter, releases); assertThat(result).containsExactlyInAnyOrderElementsOf(expResult); verifyDownloads(release1, attachments1); verifyDownloads(release2, attachments2); }
@Test public void testOnlySourceAttachmentsAreDownloaded() { Set<SW360SparseAttachment> sourceAttachments = createAttachments(1, 4); SW360SparseAttachment otherAttachment = createAttachment(1, 42); otherAttachment.setAttachmentType(SW360AttachmentType.SCREENSHOT); Set<SW360SparseAttachment> attachments = new HashSet<>(sourceAttachments); attachments.add(otherAttachment); SW360Release release = createReleaseWithAttachments(1, attachments); ReleaseWithSources releaseWithSources = createReleaseWithSources(release, sourceAttachments); expectDownloads(release, sourceAttachments); Collection<ReleaseWithSources> result = sourcesExporter.downloadSources(releaseAdapter, Collections.singleton(release)); assertThat(result).containsOnly(releaseWithSources); verifyDownloads(release, sourceAttachments); }
@Test public void testExceptionsDuringAttachmentDownloadAreHandled() { Set<SW360SparseAttachment> sourceAttachments = createAttachments(1, 2); SW360SparseAttachment failedAttachment = createAttachment(1, 42); Set<SW360SparseAttachment> attachments = new HashSet<>(sourceAttachments); attachments.add(failedAttachment); SW360Release release = createReleaseWithAttachments(1, attachments); ReleaseWithSources releaseWithSources = createReleaseWithSources(release, sourceAttachments); expectDownloads(release, sourceAttachments); when(releaseAdapter.processAttachment(eq(release), eq(failedAttachment.getAttachmentId()), any())) .thenReturn(failedFuture(new IOException("Download failed"))); Collection<ReleaseWithSources> result = sourcesExporter.downloadSources(releaseAdapter, Collections.singleton(release)); assertThat(result).containsOnly(releaseWithSources); }
@Test public void testPathsAreSanitizedForDownload() { SW360Release release = new SW360Release(); release.setName("a \"complex\" *release*/name?! -with >0 #special.characters+"); release.setVersion("10%"); SW360SparseAttachment attachment = createAttachment(1, 2); attachment.setFilename("strange/file\\.txt"); Set<SW360SparseAttachment> sourceAttachments = Collections.singleton(attachment); initAttachments(release, sourceAttachments); expectDownloads(release, sourceAttachments); Collection<ReleaseWithSources> result = sourcesExporter.downloadSources(releaseAdapter, Collections.singleton(release)); assertThat(result).hasSize(1); ArgumentCaptor<SW360AttachmentUtils.AttachmentDownloadProcessorCreateDownloadFolderWithParents> captor = ArgumentCaptor.forClass(SW360AttachmentUtils.AttachmentDownloadProcessorCreateDownloadFolderWithParents.class); verify(releaseAdapter).processAttachment(eq(release), eq(attachment.getAttachmentId()), captor.capture()); SW360AttachmentUtils.AttachmentDownloadProcessorCreateDownloadFolderWithParents processor = captor.getValue(); Path expPath = sourcePath.resolve("a__complex___release__name___-with__0__special.characters_").resolve("10_"); assertThat(processor.getDownloadPath()).isEqualTo(expPath); assertThat(processor.getFileName()).isEqualTo("strange_file_.txt"); }
@Test public void testNoDownloadIfIdenticalFileExists() { SW360SparseAttachment attachment = createAttachment(1, 1); attachment.setSha1(TEST_FILE_SHA1); SW360Release release = createReleaseWithAttachments(1, Collections.singleton(attachment)); ReleaseWithSources releaseWithSources = createReleaseWithSources(release, Collections.singleton(attachment)); createTestFile(attachmentPath(release, attachment)); Collection<ReleaseWithSources> result = sourcesExporter.downloadSources(releaseAdapter, Collections.singleton(release)); assertThat(result).containsOnly(releaseWithSources); }
@Test public void testDownloadIfHashIsDifferent() { SW360SparseAttachment attachment = createAttachment(1, 1); attachment.setSha1(TEST_FILE_SHA1); Set<SW360SparseAttachment> attachments = Collections.singleton(attachment); SW360Release release = createReleaseWithAttachments(1, attachments); ReleaseWithSources releaseWithSources = createReleaseWithSources(release, attachments); createTestFile(attachmentPath(release, attachment), "other content".getBytes(StandardCharsets.UTF_8)); expectDownloads(release, attachments); Collection<ReleaseWithSources> result = sourcesExporter.downloadSources(releaseAdapter, Collections.singleton(release)); assertThat(result).containsOnly(releaseWithSources); verify(releaseAdapter).processAttachment(eq(release), eq(attachment.getAttachmentId()), any()); }
@Test public void testDownloadIfHashCalculationFails() { SW360SparseAttachment attachment = createAttachment(1, 1); attachment.setSha1(TEST_FILE_SHA1); Set<SW360SparseAttachment> attachments = Collections.singleton(attachment); SW360Release release = createReleaseWithAttachments(1, attachments); ReleaseWithSources releaseWithSources = createReleaseWithSources(release, attachments); createTestFile(sourcePath.resolve(attachment.getFilename())); expectDownloads(release, attachments); sourcesExporter = new SourcesExporter(sourcePath) { @Override String calculateLocalAttachmentHash(Path localPath) { throw new SW360ClientException("Hash failure"); } }; Collection<ReleaseWithSources> result = sourcesExporter.downloadSources(releaseAdapter, Collections.singleton(release)); assertThat(result).containsOnly(releaseWithSources); verify(releaseAdapter).processAttachment(eq(release), eq(attachment.getAttachmentId()), any(SW360AttachmentUtils.AttachmentDownloadProcessorCreateDownloadFolderWithParents.class)); }