SfcGeniusUtils { public static <T> CompletableFuture<T> toCompletableFuture(final ListenableFuture<T> listenableFuture, Executor executor) { CompletableFuture<T> completable = new CompletableFuture<T>() { @Override public boolean cancel(boolean mayInterruptIfRunning) { boolean result = listenableFuture.cancel(mayInterruptIfRunning); super.cancel(mayInterruptIfRunning); return result; } }; Futures.addCallback(listenableFuture, new FutureCallback<T>() { @Override public void onSuccess(@Nullable T t) { completable.complete(t); } @Override public void onFailure(Throwable throwable) { completable.completeExceptionally(throwable); } }, executor); return completable; } private  SfcGeniusUtils(); static CompletableFuture<T> toCompletableFuture(final ListenableFuture<T> listenableFuture,
            Executor executor); static BigInteger getDpnIdFromLowerLayerIfList(List<String> lowerLayerIfList);  }
@Test public void toCompletableFutureDone() throws Exception { ListenableFuture<DataObject> listenableFuture = Futures.immediateFuture(dataObject); CompletableFuture<DataObject> completableFuture; completableFuture = SfcGeniusUtils.toCompletableFuture(listenableFuture, executor); assertThat(completableFuture.isDone(), is(false)); verify(executor).execute(runnableCaptor.capture()); runnableCaptor.getValue().run(); assertThat(completableFuture.isDone(), is(true)); assertThat(completableFuture.get(), is(dataObject)); }
@Test public void toCompletableFutureExceptionallyDone() throws Exception { Throwable t = new Throwable(); ListenableFuture<DataObject> listenableFuture = Futures.immediateFailedFuture(t); CompletableFuture<DataObject> completableFuture; completableFuture = SfcGeniusUtils.toCompletableFuture(listenableFuture, executor); assertThat(completableFuture.isDone(), is(false)); verify(executor).execute(runnableCaptor.capture()); runnableCaptor.getValue().run(); assertThat(completableFuture.isCompletedExceptionally(), is(true)); try { completableFuture.join(); } catch (CompletionException e) { assertThat(e.getCause(), is(t)); } }
@Test public void toCompletableFutureCancelled() throws Exception { ListenableFuture<DataObject> listenableFuture = Futures.immediateCancelledFuture(); CompletableFuture<DataObject> completableFuture; completableFuture = SfcGeniusUtils.toCompletableFuture(listenableFuture, executor); assertThat(completableFuture.isDone(), is(false)); verify(executor).execute(runnableCaptor.capture()); runnableCaptor.getValue().run(); assertThat(completableFuture.isCancelled(), is(true)); }