Example usage for java.util.concurrent CompletionStage toCompletableFuture

List of usage examples for java.util.concurrent CompletionStage toCompletableFuture

Introduction

In this page you can find the example usage for java.util.concurrent CompletionStage toCompletableFuture.

Prototype

public CompletableFuture<T> toCompletableFuture();

Source Link

Document

Returns a CompletableFuture maintaining the same completion properties as this stage.

Usage

From source file:com.spotify.apollo.entity.EntityMiddlewareTest.java

static Response<ByteString> await(CompletionStage<Response<ByteString>> resp)
        throws InterruptedException, ExecutionException, TimeoutException {
    return resp.toCompletableFuture().get(5, TimeUnit.SECONDS);
}

From source file:com.spotify.styx.api.MiddlewaresTest.java

@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()));

    assertThat(completionStage.toCompletableFuture().get().payload().isPresent(), is(false));
}

From source file:com.spotify.styx.api.MiddlewaresTest.java

@Test
public void testJsonAsync() throws Exception {
    AsyncHandler<Response<ByteString>> outerHandler = Middlewares.jsonAsync()
            .apply(rc -> CompletableFuture.completedFuture(Response.forPayload(TEST_STRUCT)));

    CompletionStage<Response<ByteString>> completionStage = outerHandler
            .invoke(RequestContexts.create(mock(Request.class), mock(Client.class), Collections.emptyMap()));

    assertThat(completionStage.toCompletableFuture().get().payload().get().utf8(),
            is("{\"foo\":\"blah\"," + "\"inner_object\":{" + "\"field_name_convention\":\"bloh\","
                    + "\"enum_field\":\"enum_value\"}}"));
}

From source file:com.spotify.styx.api.MiddlewaresTest.java

@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()));

    Response<ByteString> response = completionStage.toCompletableFuture().get();
    assertThat(response.payload().isPresent(), is(false));
    assertThat(response.status().family(), is(Status.Family.SERVER_ERROR));
}

From source file:com.spotify.styx.api.MiddlewaresTest.java

@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(Mockito.mock(Request.class), mock(Client.class), Collections.emptyMap()));

    MatcherAssert.assertThat(completionStage.toCompletableFuture().get().payload().get().utf8(),
            Is.is("{\"foo\":\"blah\"," + "\"inner_object\":{" + "\"field_name_convention\":\"bloh\","
                    + "\"enum_field\":\"enum_value\"}}"));
}

From source file:com.spotify.styx.api.MiddlewaresTest.java

@Test
public void testInvalidClient() throws ExecutionException, InterruptedException {
    Supplier<Optional<List<String>>> supplier = () -> Optional.of(ImmutableList.of("Styx CLI 0.1.1"));
    RequestContext requestContext = mockRequestContext(true);
    CompletionStage completionStage = Middlewares.clientValidator(supplier)
            .apply(mockInnerHandler(requestContext, null)).invoke(requestContext);

    // noinspection unchecked
    assertThat(((Response<ByteString>) completionStage.toCompletableFuture().get()).status().family(),
            is(Status.Family.CLIENT_ERROR));
}

From source file:org.trellisldp.http.impl.BaseTestHandler.java

protected void unwrapAsyncError(final CompletionStage async) {
    try {//ww w .j a v a  2 s  .  c  o  m
        async.toCompletableFuture().join();
    } catch (final CompletionException ex) {
        if (ex.getCause() instanceof WebApplicationException) {
            throw (WebApplicationException) ex.getCause();
        }
        throw ex;
    }
}