ServiceUnavailableMiddleware implements Middleware<REQ, RES, REQ, RES> { @Override public RES handle(REQ req, MiddlewareChain<REQ, RES, ?, ?> next) { if (endpoint != null) { return endpoint.handle(req); } throw new ServiceUnavailableException(); } ServiceUnavailableMiddleware(Endpoint<REQ, RES> endpoint); @Override RES handle(REQ req, MiddlewareChain<REQ, RES, ?, ?> next);  }
@Test public void test() { Endpoint<String, String> endpoint = req -> "request: " + req; ServiceUnavailableMiddleware<String, String> middleware = new ServiceUnavailableMiddleware<>(endpoint); String res = middleware.handle("1", new DefaultMiddlewareChain<>(Predicates.any(), "", (req, chain) -> { fail("Unreachable"); return "ok"; })); assertThat(res).isEqualTo("request: 1"); }