SW360ReleaseClientAdapterAsyncImpl implements SW360ReleaseClientAdapterAsync { @Override public CompletableFuture<SW360Release> createRelease(SW360Release release) { return FutureUtils.wrapInFuture(() -> { if (release.getId() != null) { throw new IllegalArgumentException("Release already has the id " + release.getId()); } return SW360ReleaseAdapterUtils.validateRelease(release); }, "Cannot create release for " + release.getName()) .thenCompose(this::assignReleaseToComponent) .thenCompose(getReleaseClient()::createRelease); } 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 testCreateRelease() { SW360SparseRelease sparseRelease = new SW360SparseRelease() .setVersion(release.getVersion() + "-noMatch"); String componentHref = "url/" + ID; Self componentSelf = new Self().setHref(componentHref); LinkObjects links = new LinkObjects() .setSelf(componentSelf); SW360Component component = getSw360Component(sparseRelease, "componentName"); component.setLinks(links); when(componentClientAdapter.getComponentByName(release.getName())) .thenReturn(CompletableFuture.completedFuture(Optional.of(component))); when(releaseClient.createRelease(release)) .thenReturn(CompletableFuture.completedFuture(release)); SW360Release createdRelease = block(releaseClientAdapter.createRelease(this.release)); assertThat(createdRelease).isEqualTo(release); verify(releaseClient).createRelease(release); assertThat(release.getComponentId()).isEqualTo(component.getId()); }
@Test public void testCreateReleaseInvalid() { release.setName(""); try { block(releaseClientAdapter.createRelease(release)); fail("Invalid release not detected"); } catch (SW360ClientException e) { assertThat(e.getMessage()).contains("Cannot create release", release.getName()); assertThat(e.getCause()).isInstanceOf(IllegalArgumentException.class); } }
@Test public void testCreateReleaseWithID() { addSelfLink(release); CompletableFuture<SW360Release> futResult = releaseClientAdapter.createRelease(this.release); try { block(futResult); fail("Release ID not detected"); } catch (SW360ClientException e) { assertThat(e.getMessage()).contains("Cannot create release", release.getName()); assertThat(e.getCause()).isInstanceOf(IllegalArgumentException.class); } }
@Test public void testCreateReleaseAlreadyPresent() { SW360SparseRelease sparseRelease = new SW360SparseRelease() .setVersion(release.getVersion()); String componentHref = "url/" + ID; Self componentSelf = new Self().setHref(componentHref); LinkObjects links = new LinkObjects() .setSelf(componentSelf); SW360Component component = getSw360Component(sparseRelease, "componentName"); component.setLinks(links); when(componentClientAdapter.getComponentByName(release.getName())) .thenReturn(CompletableFuture.completedFuture(Optional.of(component))); try { block(releaseClientAdapter.createRelease(release)); fail("Existing release not detected"); } catch (SW360ClientException e) { assertThat(e.getMessage()).contains("release already exists"); } }
@Test public void testCreateReleaseNewComponent() { SW360Component componentForRelease = SW360ComponentAdapterUtils.createFromRelease(release); SW360Component componentCreated = new SW360Component(); componentCreated.setName(release.getName()); componentCreated.getLinks().setSelf(new Self("https: when(componentClientAdapter.getComponentByName(release.getName())) .thenReturn(CompletableFuture.completedFuture(Optional.empty())); when(componentClientAdapter.createComponent(componentForRelease)) .thenReturn(CompletableFuture.completedFuture(componentCreated)); SW360Release releaseCreated = new SW360Release(); releaseCreated.setName(release.getName() + "_new"); when(releaseClient.createRelease(release)) .thenAnswer(invocationOnMock -> { SW360Release r = invocationOnMock.getArgument(0); assertThat(r.getComponentId()).isEqualTo(ID); return CompletableFuture.completedFuture(releaseCreated); }); SW360Release result = block(releaseClientAdapter.createRelease(release)); assertThat(result).isEqualTo(releaseCreated); }