CircuitBreakerDelegate implements CircuitBreaker<ET> { @Override public CompletableFuture<ET> executeAsyncCall(Supplier<CompletableFuture<ET>> eventFutureSupplier) { final EventHolder<ET> origEventHolder = new EventHolder<>(); Supplier<CompletableFuture<Del_ET>> coreSupplier = () -> eventFutureSupplier.get().thenApplyAsync(origEvent -> { origEventHolder.event = origEvent; return eventConverter.apply(origEvent); }); CompletableFuture<Del_ET> delegateFuture = delegate.executeAsyncCall(coreSupplier); return delegateFuture.thenApply(delegateResult -> origEventHolder.event); } CircuitBreakerDelegate(CircuitBreaker<Del_ET> delegate, Function<ET, Del_ET> eventConverter); @Override ManualModeTask<ET> newManualModeTask(); @Override CompletableFuture<ET> executeAsyncCall(Supplier<CompletableFuture<ET>> eventFutureSupplier); @Override ET executeBlockingCall(Callable<ET> eventSupplier); @Override CircuitBreakerDelegate<ET, Del_ET> onClose(Runnable listener); @Override CircuitBreakerDelegate<ET, Del_ET> onHalfOpen(Runnable listener); @Override CircuitBreakerDelegate<ET, Del_ET> onOpen(Runnable listener); @Override String getId();  }
@Test public void executeAsyncCall_calls_delegate_with_future_that_provides_converted_event_and_returns_future_that_provides_original_event() { CircuitBreaker<String> delegateCbSpy = spy(new CircuitBreakerImpl<>()); CircuitBreakerDelegate<Integer, String> wrapperCb = new CircuitBreakerDelegate<>(delegateCbSpy, eventConverter); int event = 42; Supplier<CompletableFuture<Integer>> wrapperFutureSupplier = () -> CompletableFuture.completedFuture(event); CompletableFuture<Integer> wrapperCallResult = wrapperCb.executeAsyncCall(wrapperFutureSupplier); ArgumentCaptor<Supplier> delegateCallArgumentCaptor = ArgumentCaptor.forClass(Supplier.class); verify(delegateCbSpy).executeAsyncCall(delegateCallArgumentCaptor.capture()); Supplier<CompletableFuture<String>> delegateFutureSupplier = delegateCallArgumentCaptor.getValue(); assertThat(wrapperCallResult.join()).isEqualTo(event); assertThat(delegateFutureSupplier.get().join()).isEqualTo(eventConverter.apply(event)); }