SW360MetaDataUpdater { public AttachmentUploadResult<SW360Release> uploadAttachments(SW360Release sw360Release, Map<Path, SW360AttachmentType> attachments, boolean force) { Set<String> attachmentsToDelete = new HashSet<>(); Map<AttachmentUploadRequest.Item, Throwable> conflictFailures = new HashMap<>(); AttachmentUploadRequest.Builder<SW360Release> builder = AttachmentUploadRequest.builder(sw360Release); for (Map.Entry<Path, SW360AttachmentType> e : attachments.entrySet()) { String fileName = String.valueOf(e.getKey().getFileName()); Optional<SW360SparseAttachment> optAttachment = findAttachmentByFileName(sw360Release, fileName); boolean shouldUpload = optAttachment.map(attachment -> checkUploadCriteria(e.getKey(), attachment, attachmentsToDelete, conflictFailures, force)) .orElse(true); if (shouldUpload) { builder = builder.addAttachment(e.getKey(), e.getValue()); } } deleteConflictingAttachments(sw360Release, attachmentsToDelete); AttachmentUploadRequest<SW360Release> uploadRequest = builder.build(); if (!uploadRequest.getItems().isEmpty()) { AttachmentUploadResult<SW360Release> result = releaseClientAdapter.uploadAttachments(uploadRequest); LOGGER.debug("Result of attachment upload operation: {}", result); if (!result.isSuccess()) { LOGGER.error("Failed to upload attachments: {}", result.failedUploads()); } return appendFailures(result, conflictFailures); } return appendFailures(new AttachmentUploadResult<>(sw360Release), conflictFailures); } SW360MetaDataUpdater(SW360Connection connection); Set<SW360License> getLicenses(Collection<License> licenses); SW360Release getOrCreateRelease(SW360Release sw360ReleaseFromArtifact, boolean updateExisting); void createProject(String projectName, String projectVersion, Collection<SW360Release> releases); AttachmentUploadResult<SW360Release> uploadAttachments(SW360Release sw360Release,
                                                                  Map<Path, SW360AttachmentType> attachments,
                                                                  boolean force); SW360Release deleteSourceAttachments(SW360Release release); SW360Release deleteAttachments(SW360Release release, Predicate<SW360SparseAttachment> filter);  }
@Test public void testUploadAttachments() throws IOException { SW360Release release = createRelease(RELEASE_NAME); Path uploadPath = createTestFile(attachmentPath("upload.doc"), TEST_FILE_CONTENT); SW360AttachmentType attachmentType = SW360AttachmentType.SOURCE; Map<Path, SW360AttachmentType> attachments = Collections.singletonMap(uploadPath, attachmentType); AttachmentUploadRequest<SW360Release> expRequest = AttachmentUploadRequest.builder(release) .addAttachment(uploadPath, attachmentType) .build(); AttachmentUploadResult<SW360Release> expResult = new AttachmentUploadResult<>(createRelease("update")); when(releaseClientAdapter.uploadAttachments(expRequest)) .thenReturn(expResult); AttachmentUploadResult<SW360Release> uploadResult = metaDataUpdater.uploadAttachments(release, attachments, false); assertThat(uploadResult).isEqualTo(expResult); verify(releaseClientAdapter).uploadAttachments(expRequest); verifyNoMoreInteractions(releaseClientAdapter); }
@Test public void testUploadAttachmentsSkipUploads() throws IOException { Path uploadPath = createTestFile(attachmentPath("upload.doc"), TEST_FILE_CONTENT); SW360AttachmentType attachmentType = SW360AttachmentType.SOURCE; SW360SparseAttachment attachment = createAttachment(uploadPath.getFileName().toString(), attachmentType); SW360Release release = addAttachment(createRelease(RELEASE_NAME), attachment); Map<Path, SW360AttachmentType> attachments = Collections.singletonMap(uploadPath, attachmentType); AttachmentUploadResult<SW360Release> uploadResult = metaDataUpdater.uploadAttachments(release, attachments, false); assertThat(uploadResult).isEqualTo(new AttachmentUploadResult<>(release)); }
@Test public void testUploadAttachmentsConflictNoForce() throws IOException { Path uploadPath = createTestFile(attachmentPath("upload.doc"), TEST_FILE_CONTENT + "_changed"); SW360AttachmentType attachmentType = SW360AttachmentType.SOURCE; SW360SparseAttachment attachment = createAttachment(uploadPath.getFileName().toString(), attachmentType); SW360Release release = addAttachment(createRelease(RELEASE_NAME), attachment); Map<Path, SW360AttachmentType> attachments = Collections.singletonMap(uploadPath, attachmentType); AttachmentUploadResult<SW360Release> uploadResult = metaDataUpdater.uploadAttachments(release, attachments, false); assertThat(uploadResult.getTarget()).isEqualTo(release); assertThat(uploadResult.successfulUploads()).isEmpty(); AttachmentUploadRequest.Item expFailedItem = new AttachmentUploadRequest.Item(uploadPath, attachmentType); assertThat(uploadResult.failedUploads().keySet()) .containsOnly(expFailedItem); Throwable throwable = uploadResult.failedUploads().get(expFailedItem); assertThat(throwable).isInstanceOf(IllegalStateException.class); assertThat(throwable.getMessage()).contains(uploadPath.toString()); }
@Test public void testUploadAttachmentsConflictFailuresAreAddedToResult() throws IOException { Path uploadPath1 = createTestFile(attachmentPath("conflict.doc"), TEST_FILE_CONTENT + "_other"); Path uploadPath2 = createTestFile(attachmentPath("sources.zip"), TEST_FILE_CONTENT); SW360AttachmentType attachmentType = SW360AttachmentType.SOURCE; Map<Path, SW360AttachmentType> attachments = new HashMap<>(); attachments.put(uploadPath1, attachmentType); attachments.put(uploadPath2, attachmentType); SW360SparseAttachment attachment = createAttachment(uploadPath1.getFileName().toString(), attachmentType); SW360Release release = addAttachment(createRelease(RELEASE_NAME), attachment); AttachmentUploadRequest<SW360Release> expRequest = AttachmentUploadRequest.builder(release) .addAttachment(uploadPath2, attachmentType) .build(); AttachmentUploadResult<SW360Release> expResult = new AttachmentUploadResult<>(createRelease("update")); when(releaseClientAdapter.uploadAttachments(expRequest)) .thenReturn(expResult); AttachmentUploadResult<SW360Release> uploadResult = metaDataUpdater.uploadAttachments(release, attachments, false); assertThat(uploadResult.failedUploads().keySet()) .containsOnly(new AttachmentUploadRequest.Item(uploadPath1, attachmentType)); }
@Test public void testExceptionsWhenCalculatingAttachmentHashAreHandled() throws IOException { final String attachmentFileName = "failingHashAttachmentFileName.doc"; SW360Release release = addSourceAttachment(createRelease(RELEASE_NAME), attachmentFileName); Path uploadPath = createTestFile(attachmentPath(attachmentFileName), TEST_FILE_CONTENT); Map<Path, SW360AttachmentType> attachments = Collections.singletonMap(uploadPath, SW360AttachmentType.SOURCE); AttachmentUploadRequest<SW360Release> expRequest = AttachmentUploadRequest.builder(release) .addAttachment(uploadPath, SW360AttachmentType.SOURCE) .build(); when(releaseClientAdapter.uploadAttachments(expRequest)) .thenReturn(new AttachmentUploadResult<>(release)); metaDataUpdater = new SW360MetaDataUpdater(connection) { @Override String calculateAttachmentHash(Path path) { throw new SW360ClientException("Hash failed"); } }; metaDataUpdater.uploadAttachments(release, attachments, true); verify(releaseClientAdapter).uploadAttachments(expRequest); }