SW360ReleaseClientAdapterAsyncImpl implements SW360ReleaseClientAdapterAsync { @Override public CompletableFuture<AttachmentUploadResult<SW360Release>> uploadAttachments(AttachmentUploadRequest<SW360Release> uploadRequest) { return SW360AttachmentUtils.uploadAttachments(getReleaseClient(), uploadRequest, release -> release.getEmbedded().getAttachments()); } SW360ReleaseClientAdapterAsyncImpl(SW360ReleaseClient client,
                                              SW360ComponentClientAdapterAsync componentClientAdapter); @Override SW360ReleaseClient getReleaseClient(); SW360ComponentClientAdapterAsync getComponentAdapter(); @Override CompletableFuture<SW360Release> createRelease(SW360Release release); @Override CompletableFuture<AttachmentUploadResult<SW360Release>> uploadAttachments(AttachmentUploadRequest<SW360Release> uploadRequest); @Override CompletableFuture<Optional<SW360Release>> getReleaseById(String releaseId); @Override CompletableFuture<Optional<SW360Release>> enrichSparseRelease(SW360SparseRelease sparseRelease); @Override CompletableFuture<Optional<SW360SparseRelease>> getSparseReleaseByExternalIds(Map<String, ?> externalIds); @Override CompletableFuture<Optional<SW360SparseRelease>> getSparseReleaseByNameAndVersion(String componentName,
                                                                                            String version); @Override CompletableFuture<Optional<SW360Release>> getReleaseByVersion(SW360Component component, String releaseVersion); @Override CompletableFuture<Optional<Path>> downloadAttachment(SW360Release release, SW360SparseAttachment attachment,
                                                                Path downloadPath); @Override CompletableFuture<T> processAttachment(SW360Release release, String attachmentId, SW360AttachmentAwareClient.AttachmentProcessor<? extends T> processor); @Override CompletableFuture<SW360Release> deleteAttachments(SW360Release release, Collection<String> attachmentIds); @Override CompletableFuture<SW360Release> updateRelease(SW360Release release); @Override CompletableFuture<MultiStatusResponse> deleteReleases(Collection<String> idsToDelete); @Override CompletableFuture<Void> deleteRelease(String releaseId);  }
@Test public void testUploadAttachmentsSuccess() { Path uploadPath1 = Paths.get("file1.doc"); SW360AttachmentType attachmentType1 = SW360AttachmentType.DOCUMENT; Path uploadPath2 = Paths.get("sources.zip"); SW360AttachmentType attachmentType2 = SW360AttachmentType.SOURCE; release.setEmbedded(createEmbeddedReleaseWithAttachments()); SW360Release updatedRelease1 = createReleaseWithAttachments("attach1"); SW360Release updatedRelease2 = createReleaseWithAttachments("attach1", "attach2"); AttachmentUploadRequest<SW360Release> uploadRequest = AttachmentUploadRequest.builder(release) .addAttachment(uploadPath1, attachmentType1) .addAttachment(uploadPath2, attachmentType2) .build(); when(releaseClient.uploadAndAttachAttachment(release, uploadPath1, attachmentType1)) .thenReturn(CompletableFuture.completedFuture(updatedRelease1)); when(releaseClient.uploadAndAttachAttachment(updatedRelease1, uploadPath2, attachmentType2)) .thenReturn(CompletableFuture.completedFuture(updatedRelease2)); AttachmentUploadResult<SW360Release> result = block(releaseClientAdapter.uploadAttachments(uploadRequest)); assertThat(result.getTarget()).isEqualTo(updatedRelease2); assertThat(result.isSuccess()).isTrue(); assertThat(result.failedUploads()).isEmpty(); assertThat(result.successfulUploads()).contains(new AttachmentUploadRequest.Item(uploadPath1, attachmentType1), new AttachmentUploadRequest.Item(uploadPath2, attachmentType2)); }
@Test public void testUploadAttachmentsWithFailures() { Path uploadPath1 = Paths.get("failedUpload1.err"); SW360AttachmentType attachmentType1 = SW360AttachmentType.DESIGN; Throwable failure1 = new IOException("I/O exception during upload"); Path uploadPath2 = Paths.get("failedUpload2.exc"); SW360AttachmentType attachmentType2 = SW360AttachmentType.BINARY_SELF; Throwable failure2 = new SW360ClientException("Forbidden upload"); Path uploadPath3 = Paths.get("success.yes"); SW360AttachmentType attachmentType3 = SW360AttachmentType.CLEARING_REPORT; release.setEmbedded(createEmbeddedReleaseWithAttachments()); SW360Release updatedRelease = createReleaseWithAttachments("attach1"); AttachmentUploadRequest<SW360Release> uploadRequest = AttachmentUploadRequest.builder(release) .addAttachment(uploadPath1, attachmentType1) .addAttachment(uploadPath2, attachmentType2) .addAttachment(uploadPath3, attachmentType3) .build(); when(releaseClient.uploadAndAttachAttachment(release, uploadPath1, attachmentType1)) .thenReturn(FutureUtils.failedFuture(failure1)); when(releaseClient.uploadAndAttachAttachment(release, uploadPath2, attachmentType2)) .thenReturn(FutureUtils.failedFuture(failure2)); when(releaseClient.uploadAndAttachAttachment(release, uploadPath3, attachmentType3)) .thenReturn(CompletableFuture.completedFuture(updatedRelease)); AttachmentUploadResult<SW360Release> result = block(releaseClientAdapter.uploadAttachments(uploadRequest)); assertThat(result.getTarget()).isEqualTo(updatedRelease); assertThat(result.isSuccess()).isFalse(); assertThat(result.successfulUploads()) .containsOnly(new AttachmentUploadRequest.Item(uploadPath3, attachmentType3)); assertThat(result.failedUploads()).hasSize(2); assertThat(result.failedUploads().get(new AttachmentUploadRequest.Item(uploadPath1, attachmentType1))) .isEqualTo(failure1); assertThat(result.failedUploads().get(new AttachmentUploadRequest.Item(uploadPath2, attachmentType2))) .isEqualTo(failure2); }
@Test public void testUploadAttachmentsDuplicate() { String fileName = "alreadyExistingAttachment.1st"; Path uploadPath = Paths.get(fileName); SW360AttachmentType attachmentType = SW360AttachmentType.LEGAL_EVALUATION; release.setEmbedded(createEmbeddedReleaseWithAttachments(fileName)); AttachmentUploadRequest<SW360Release> uploadRequest = AttachmentUploadRequest.builder(release) .addAttachment(uploadPath, attachmentType) .build(); AttachmentUploadResult<SW360Release> result = block(releaseClientAdapter.uploadAttachments(uploadRequest)); assertThat(result.getTarget()).isEqualTo(release); assertThat(result.isSuccess()).isFalse(); assertThat(result.failedUploads().get(new AttachmentUploadRequest.Item(uploadPath, attachmentType))) .isInstanceOf(SW360ClientException.class); verifyZeroInteractions(releaseClient); }