SW360AttachmentUtils { public static <T extends SW360HalResource<?, ?>> CompletableFuture<Optional<Path>> downloadAttachment(SW360AttachmentAwareClient<? extends T> client, T entity, SW360SparseAttachment attachment, Path downloadPath) { return Optional.ofNullable(entity.getSelfLink()) .map(self -> { AttachmentDownloadProcessor downloadProcessor = defaultAttachmentDownloadProcessor(attachment, downloadPath); return optionalFuture(client.processAttachment(self.getHref(), attachment.getId(), downloadProcessor)); }) .orElseGet(() -> CompletableFuture.completedFuture(Optional.empty())); } private  SW360AttachmentUtils(); static CompletableFuture<AttachmentUploadResult<T>> uploadAttachments(SW360AttachmentAwareClient<T> client, AttachmentUploadRequest<T> uploadRequest,
                      Function<? super T, Set<SW360SparseAttachment>> getAttachmentsFunc); static CompletableFuture<Optional<Path>> downloadAttachment(SW360AttachmentAwareClient<? extends T> client, T entity, SW360SparseAttachment attachment,
                       Path downloadPath); static AttachmentDownloadProcessor defaultAttachmentDownloadProcessor(SW360SparseAttachment attachment,
                                                                                 Path downloadPath); static AttachmentDownloadProcessor defaultAttachmentDownloadProcessor(String fileName, Path downloadPath); static String calculateHash(Path file, MessageDigest digest); static String calculateSha1Hash(Path file);  }
@Test public void testDownloadAttachment() { String attachmentId = "attach-0123456789"; String fileName = "downloadedAttachment.doc"; Path downloadPath = folder.getRoot().toPath().resolve("downloads"); String releaseLink = "https: SW360Release release = new SW360Release(); release.getLinks().setSelf(new Self(releaseLink)); SW360SparseAttachment attachment = new SW360SparseAttachment(); attachment.setFilename(fileName); attachment.getLinks().setSelf(new Self("https: SW360ReleaseClient releaseClient = mock(SW360ReleaseClient.class); when(releaseClient.processAttachment(eq(releaseLink), eq(attachmentId), any())) .thenAnswer((Answer<CompletableFuture<Path>>) invocationOnMock -> { SW360AttachmentUtils.AttachmentDownloadProcessor processor = invocationOnMock.getArgument(2); assertThat(processor.getCopyOptions()).containsOnly(StandardCopyOption.REPLACE_EXISTING); try (InputStream stream = Files.newInputStream(testFile())) { return CompletableFuture.completedFuture(processor.processAttachmentStream(stream)); } }); Optional<Path> optTarget = FutureUtils.block(SW360AttachmentUtils.downloadAttachment(releaseClient, release, attachment, downloadPath)); assertThat(optTarget).isNotEmpty(); Path target = optTarget.get(); assertThat(target).isEqualTo(downloadPath.resolve(fileName)); checkFileDownloaded(target); }