Middlewares { public static Middleware<SyncHandler<? extends Response<?>>, AsyncHandler<Response<ByteString>>> json() { return innerHandler -> jsonAsync().apply(Middleware.syncToAsync(innerHandler)); } private  Middlewares(); static Middleware<SyncHandler<? extends Response<?>>, AsyncHandler<Response<ByteString>>> json(); static Middleware<AsyncHandler<Response<T>>, AsyncHandler<Response<T>>> clientValidator(
      Supplier<Set<String>> supplier); static Middleware<AsyncHandler<Response<T>>, AsyncHandler<Response<T>>> exceptionAndRequestIdHandler(); static Middleware<AsyncHandler<Response<T>>, AsyncHandler<Response<T>>> tracer(
      Tracer tracer, String service); static Middleware<Requested<Authenticated<Response<T>>>, SyncHandler<Response<T>>> authed(
      RequestAuthenticator authenticator); static Middleware<Authenticated<EntityResponseHandler<E, R>>, SyncHandler<Response<ByteString>>> authedEntity(
      Middleware<EntityResponseHandler<E, R>, SyncHandler<Response<ByteString>>> entityMiddleware,
      Middleware<Requested<Authenticated<Response<ByteString>>>, SyncHandler<Response<ByteString>>> authed); static Middleware<Authenticated<EntityResponseHandler<E, R>>, SyncHandler<Response<ByteString>>> authedEntity(
      RequestAuthenticator authenticator,
      Middleware<EntityResponseHandler<E, R>, SyncHandler<Response<ByteString>>> entityMiddleware); static Middleware<AsyncHandler<Response<T>>, AsyncHandler<Response<T>>> authenticator(
      RequestAuthenticator authenticator);  }
@Test public void testJson() throws Exception { AsyncHandler<Response<ByteString>> outerHandler = Middlewares.json().apply( rc -> Response.forPayload(TEST_STRUCT) ); CompletionStage<Response<ByteString>> completionStage = outerHandler.invoke( RequestContexts.create(mock(Request.class), mock(Client.class), Collections.emptyMap(), System.nanoTime(), RequestMetadataImpl.create(Instant.EPOCH, Optional.empty(), Optional.empty()))); assertThat(completionStage.toCompletableFuture().get().payload().orElseThrow().utf8(), is( "{\"foo\":\"blah\"," + "\"inner_object\":{" + "\"field_name_convention\":\"bloh\"," + "\"enum_field\":\"enum_value\"}}" )); }
@Test public void testNoPayloadResponse() throws Exception { AsyncHandler<Response<ByteString>> outerHandler = Middlewares.json().apply( rc -> Response.forStatus(Status.INTERNAL_SERVER_ERROR) ); CompletionStage<Response<ByteString>> completionStage = outerHandler.invoke( RequestContexts.create(mock(Request.class), mock(Client.class), Collections.emptyMap(), System.nanoTime(), RequestMetadataImpl.create(Instant.EPOCH, Optional.empty(), Optional.empty()))); assertThat(completionStage.toCompletableFuture().get().payload().isPresent(), is(false)); }
@Test public void testInvalidPayload() throws Exception { AsyncHandler<Response<ByteString>> outerHandler = Middlewares.json().apply( rc -> Response.forPayload(new NotSerializable()) ); CompletionStage<Response<ByteString>> completionStage = outerHandler.invoke( RequestContexts.create(mock(Request.class), mock(Client.class), Collections.emptyMap(), System.nanoTime(), RequestMetadataImpl.create(Instant.EPOCH, Optional.empty(), Optional.empty()))); Response<ByteString> response = completionStage.toCompletableFuture().get(); assertThat(response.payload().isPresent(), is(false)); assertThat(response.status().family(), is(Status.Family.SERVER_ERROR)); }