SW360MetaDataUpdater { public SW360Release getOrCreateRelease(SW360Release sw360ReleaseFromArtifact, boolean updateExisting) { Optional<SW360SparseRelease> optSparseReleaseByIds = releaseClientAdapter.getSparseReleaseByExternalIds(sw360ReleaseFromArtifact.getExternalIds()); Optional<SW360SparseRelease> optSparseRelease = optSparseReleaseByIds.isPresent() ? optSparseReleaseByIds : releaseClientAdapter.getSparseReleaseByNameAndVersion(sw360ReleaseFromArtifact.getName(), sw360ReleaseFromArtifact.getVersion()); Optional<SW360Release> optRelease = optSparseRelease.flatMap(releaseClientAdapter::enrichSparseRelease) .map(sw360ReleaseFromArtifact::mergeWith); if (optRelease.isPresent()) { SW360Release release = optRelease.get(); return updateExisting ? releaseClientAdapter.updateRelease(release) : release; } return releaseClientAdapter.createRelease(sw360ReleaseFromArtifact); } 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 testGetOrCreateRelease() { final SW360Release release = new SW360Release(); release.setName("test-component"); release.setVersion("0.1-alpha"); final SW360Release newRelease = new SW360Release(); when(releaseClientAdapter.getSparseReleaseByExternalIds(any())).thenReturn(Optional.empty()); when(releaseClientAdapter.getSparseReleaseByNameAndVersion(release.getName(), release.getVersion())) .thenReturn(Optional.empty()); when(releaseClientAdapter.createRelease(release)).thenReturn(newRelease); assertThat(metaDataUpdater.getOrCreateRelease(release, true)).isEqualTo(newRelease); verify(releaseClientAdapter, never()).updateRelease(any()); }
@Test public void testGetOrCreateReleaseFoundByExternalIDs() { SW360SparseRelease sparseRelease = new SW360SparseRelease(); SW360Release foundRelease = new SW360Release(); SW360Release queryRelease = new SW360Release(); SW360Release patchedRelease = new SW360Release(); Map<String, String> extIDs = Collections.singletonMap("foo", "bar"); final String copyright = "(C) Test copyright"; queryRelease.setExternalIds(extIDs); foundRelease.setCopyrights(copyright); when(releaseClientAdapter.getSparseReleaseByExternalIds(extIDs)).thenReturn(Optional.of(sparseRelease)); when(releaseClientAdapter.enrichSparseRelease(sparseRelease)).thenReturn(Optional.of(foundRelease)); when(releaseClientAdapter.updateRelease(any())) .thenAnswer((Answer<SW360Release>) invocationOnMock -> { SW360Release rel = invocationOnMock.getArgument(0); assertThat(rel.getExternalIds()).isEqualTo(extIDs); assertThat(rel.getCopyrights()).isEqualTo(copyright); return patchedRelease; }); assertThat(metaDataUpdater.getOrCreateRelease(queryRelease, true)).isEqualTo(patchedRelease); }
@Test public void testGetOrCreateReleaseFoundByNameAndVersion() { SW360SparseRelease sparseRelease = new SW360SparseRelease(); SW360Release foundRelease = new SW360Release(); SW360Release queryRelease = new SW360Release(); queryRelease.setExternalIds(Collections.singletonMap("id", "42")); foundRelease.setExternalIds(Collections.singletonMap("id2", "47")); queryRelease.setName("theComponent"); queryRelease.setVersion("100.0"); when(releaseClientAdapter.getSparseReleaseByExternalIds(queryRelease.getExternalIds())).thenReturn(Optional.empty()); when(releaseClientAdapter.getSparseReleaseByNameAndVersion(queryRelease.getName(), queryRelease.getVersion())) .thenReturn(Optional.of(sparseRelease)); when(releaseClientAdapter.enrichSparseRelease(sparseRelease)).thenReturn(Optional.of(foundRelease)); assertThat(metaDataUpdater.getOrCreateRelease(queryRelease, false)).isEqualTo(queryRelease); assertThat(queryRelease.getExternalIds()).containsKey("id2"); verify(releaseClientAdapter, never()).updateRelease(any()); }