RestClient implements AutoCloseable { public <T extends BmcRequest> Response get( @NonNull WrappedInvocationBuilder ib, @NonNull T request) throws BmcException { InvocationInformation info = preprocessRequest(ib, request); try { return decorateSupplier(ib::get).get(); } catch (ProcessingException ex) { throw convertToBmcException(baseTarget, ex, info); } } RestClient(
            @NonNull Client client,
            @NonNull EntityFactory entityFactory,
            JaxRsCircuitBreaker circuitBreaker); void setEndpoint(@NonNull String endpoint); @Override void close(); WrappedWebTarget getBaseTarget(); Response get(
            @NonNull WrappedInvocationBuilder ib, @NonNull T request); Future<Response> get(
            @NonNull WrappedInvocationBuilder ib,
            @NonNull T request,
            @Nullable Consumer<Response> onSuccess,
            @Nullable Consumer<Throwable> onError); Response post(
            @NonNull WrappedInvocationBuilder ib, @Nullable Object body, @NonNull T request); Future<Response> post(
            @NonNull WrappedInvocationBuilder ib,
            @Nullable Object body,
            @NonNull T request,
            @Nullable Consumer<Response> onSuccess,
            @Nullable Consumer<Throwable> onError); Response post(
            @NonNull WrappedInvocationBuilder ib, @NonNull T request); Future<Response> post(
            @NonNull WrappedInvocationBuilder ib,
            @NonNull T request,
            @Nullable Consumer<Response> onSuccess,
            @Nullable Consumer<Throwable> onError); Response patch(
            @NonNull WrappedInvocationBuilder ib, @NonNull T request); Response patch(
            @NonNull WrappedInvocationBuilder ib, @Nullable Object body, @NonNull T request); Future<Response> patch(
            @NonNull WrappedInvocationBuilder ib,
            @NonNull T request,
            @Nullable Consumer<Response> onSuccess,
            @Nullable Consumer<Throwable> onError); Future<Response> patch(
            @NonNull WrappedInvocationBuilder ib,
            @Nullable Object body,
            @NonNull T request,
            @Nullable Consumer<Response> onSuccess,
            @Nullable Consumer<Throwable> onError); Response put(
            @NonNull WrappedInvocationBuilder ib, @NonNull T request); Response put(
            @NonNull WrappedInvocationBuilder ib, @Nullable Object body, @NonNull T request); Future<Response> put(
            @NonNull WrappedInvocationBuilder ib,
            @NonNull T request,
            @Nullable Consumer<Response> onSuccess,
            @Nullable Consumer<Throwable> onError); Future<Response> put(
            @NonNull WrappedInvocationBuilder ib,
            @Nullable Object body,
            @NonNull T request,
            @Nullable Consumer<Response> onSuccess,
            @Nullable Consumer<Throwable> onError); Response delete(
            @NonNull WrappedInvocationBuilder ib, @NonNull T request); Future<Response> delete(
            @NonNull WrappedInvocationBuilder ib,
            @NonNull T request,
            @Nullable Consumer<Response> onSuccess,
            @Nullable Consumer<Throwable> onError); Response head(
            @NonNull WrappedInvocationBuilder ib, @NonNull T request); Future<Response> head(
            @NonNull WrappedInvocationBuilder ib,
            @NonNull T request,
            @Nullable Consumer<Response> onSuccess,
            @Nullable Consumer<Throwable> onError);  }
@Test public void validateCircuitBreaker() { int MIN_NUM_CALLS = 4; RestClient client = RestClientFactoryBuilder.builder() .build() .create( null, null, ClientConfiguration.builder() .circuitBreakerConfiguration( CircuitBreakerConfiguration.builder() .slidingWindowSize(10) .minimumNumberOfCalls(MIN_NUM_CALLS) .waitDurationInOpenState( Duration.ofSeconds(2)) .build()) .build()); MultivaluedMap<String, Object> headers = new MultivaluedHashMap<>(); headers.putSingle(BmcException.OPC_REQUEST_ID_HEADER, "123"); BmcRequest request = new BmcRequest(); request.setInvocationCallback( new Consumer<Invocation.Builder>() { @Override public void accept(Invocation.Builder builder) { builder.headers(headers); } }); Response response503 = mock(Response.class); Mockito.when(response503.getStatus()).thenReturn(503); WrappedInvocationBuilder wib = Mockito.mock(WrappedInvocationBuilder.class); Mockito.when(wib.get()).thenReturn(response503); Mockito.when(wib.getHeaders()).thenReturn(headers); for (int i = 0; i < MIN_NUM_CALLS + 2; i++) { try { client.get(wib, request); assertTrue("Initial calls are not expected to fail", i < MIN_NUM_CALLS); } catch (BmcException e) { assertTrue("The rest of the call are expected to fail", i >= MIN_NUM_CALLS); assertTrue(e.getCause() instanceof CallNotAllowedException); } } }
@Test public void validateCircuitBreakerNotConfigured() { int MIN_NUM_CALLS = 4; RestClient client = RestClientFactoryBuilder.builder() .build() .create(null, null, ClientConfiguration.builder().build()); MultivaluedMap<String, Object> headers = new MultivaluedHashMap<>(); headers.putSingle(BmcException.OPC_REQUEST_ID_HEADER, "123"); BmcRequest request = new BmcRequest(); request.setInvocationCallback( new Consumer<Invocation.Builder>() { @Override public void accept(Invocation.Builder builder) { builder.headers(headers); } }); Response response503 = mock(Response.class); Mockito.when(response503.getStatus()).thenReturn(503); WrappedInvocationBuilder wib = Mockito.mock(WrappedInvocationBuilder.class); Mockito.when(wib.get()).thenReturn(response503); Mockito.when(wib.getHeaders()).thenReturn(headers); for (int i = 0; i < MIN_NUM_CALLS + 2; i++) { try { client.get(wib, request); } catch (BmcException e) { fail("The circuit-breaker should be disabled"); } } }