SW360ComponentClientAdapterAsyncImpl implements SW360ComponentClientAdapterAsync { @Override public CompletableFuture<Optional<SW360Component>> getComponentByName(String componentName) { ComponentSearchParams searchParams = ComponentSearchParams.builder() .withName(componentName) .build(); return getComponentClient().search(searchParams) .thenCompose(components -> components.getResult().stream() .filter(c -> c.getName().equals(componentName)) .map(c -> SW360HalResourceUtility.getLastIndexOfSelfLink(c.getLinks()).orElse("")) .map(this::getComponentById) .findFirst() .orElse(CompletableFuture.completedFuture(Optional.empty())) ); } SW360ComponentClientAdapterAsyncImpl(SW360ComponentClient client); @Override SW360ComponentClient getComponentClient(); @Override CompletableFuture<SW360Component> createComponent(SW360Component component); @Override CompletableFuture<Optional<SW360Component>> getComponentById(String componentId); @Override CompletableFuture<Optional<SW360Component>> getComponentByName(String componentName); @Override CompletableFuture<List<SW360SparseComponent>> search(ComponentSearchParams searchParams); @Override CompletableFuture<PagingResult<SW360SparseComponent>> searchWithPaging(ComponentSearchParams searchParams); @Override CompletableFuture<SW360Component> updateComponent(SW360Component component); @Override CompletableFuture<MultiStatusResponse> deleteComponents(Collection<String> idsToDelete); @Override CompletableFuture<Void> deleteComponent(String componentId);  }
@Test public void testGetComponentByName() { LinkObjects linkObjects = makeLinkObjects(); sparseComponent.setName(COMPONENT_NAME) .setLinks(linkObjects); component.setName(COMPONENT_NAME); when(componentClient.getComponent(COMPONENT_ID)) .thenReturn(CompletableFuture.completedFuture(component)); when(componentClient.search(NAME_SEARCH_PARAMS)) .thenReturn(createSearchResult(Collections.singletonList(sparseComponent))); Optional<SW360Component> componentByName = block(componentClientAdapter.getComponentByName(COMPONENT_NAME)); assertThat(componentByName).isPresent(); assertThat(componentByName).hasValue(component); verify(componentClient).getComponent(COMPONENT_ID); }