Example usage for java.util.concurrent CompletableFuture get

List of usage examples for java.util.concurrent CompletableFuture get

Introduction

In this page you can find the example usage for java.util.concurrent CompletableFuture get.

Prototype

@SuppressWarnings("unchecked")
public T get(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException 

Source Link

Document

Waits if necessary for at most the given time for this future to complete, and then returns its result, if available.

Usage

From source file:com.devicehive.rpcclient.RpcClientActionTest.java

@Test
public void testListDeviceClassAction() throws Exception {
    ListDeviceClassRequest deviceClassRequest = new ListDeviceClassRequest();
    deviceClassRequest.setName(UUID.randomUUID().toString()); // nonexistent name

    Request request = Request.newBuilder().withBody(deviceClassRequest).build();
    CompletableFuture<Response> future = new CompletableFuture<>();
    client.call(request, future::complete);

    Response response = future.get(10, TimeUnit.SECONDS);
    ListDeviceClassResponse responseBody = (ListDeviceClassResponse) response.getBody();
    assertNotNull(responseBody.getDeviceClasses().isEmpty());
}

From source file:com.devicehive.rpcclient.RpcClientActionTest.java

@Test
public void testListDeviceAction() throws Exception {
    ListDeviceRequest deviceRequest = new ListDeviceRequest();
    deviceRequest.setName(UUID.randomUUID().toString()); // nonexistent name
    deviceRequest.setSortOrderAsc(false);

    Request request = Request.newBuilder().withBody(deviceRequest).build();
    CompletableFuture<Response> future = new CompletableFuture<>();
    client.call(request, future::complete);

    Response response = future.get(10, TimeUnit.SECONDS);
    ListDeviceResponse responseBody = (ListDeviceResponse) response.getBody();
    assertNotNull(responseBody.getDevices().isEmpty());
}

From source file:com.devicehive.rpcclient.RpcClientActionTest.java

@Test
public void testNotificationSearchAction() throws Exception {
    NotificationSearchRequest searchRequest = new NotificationSearchRequest();
    searchRequest.setId(Long.MAX_VALUE); // nonexistent id
    searchRequest.setGuid(UUID.randomUUID().toString()); // random guid

    Request request = Request.newBuilder().withPartitionKey(searchRequest.getGuid()).withBody(searchRequest)
            .build();/*from   w w  w .  java  2  s.  c  o  m*/
    CompletableFuture<Response> future = new CompletableFuture<>();
    client.call(request, future::complete);

    Response response = future.get(10, TimeUnit.SECONDS);
    NotificationSearchResponse responseBody = (NotificationSearchResponse) response.getBody();
    assertTrue(responseBody.getNotifications().isEmpty());
}

From source file:com.devicehive.rpcclient.RpcClientActionTest.java

@Test
public void testCommandSearchAction() throws Exception {
    CommandSearchRequest searchRequest = new CommandSearchRequest();
    searchRequest.setId(Long.MAX_VALUE); // nonexistent id
    searchRequest.setGuid(UUID.randomUUID().toString()); // random guid

    Request request = Request.newBuilder().withPartitionKey(searchRequest.getGuid()).withBody(searchRequest)
            .build();/*from w  w w.  j av a 2  s  .c  om*/
    CompletableFuture<Response> future = new CompletableFuture<>();
    client.call(request, future::complete);

    Response response = future.get(10, TimeUnit.SECONDS);
    CommandSearchResponse responseBody = (CommandSearchResponse) response.getBody();
    assertTrue(responseBody.getCommands().isEmpty());
}

From source file:com.devicehive.rpcclient.RpcClientActionTest.java

@Test
public void testCommandInsertAction() throws Exception {
    DeviceCommand command = new DeviceCommand();
    command.setCommand("test_command");
    command.setDeviceGuid(UUID.randomUUID().toString());
    CommandInsertRequest insertRequest = new CommandInsertRequest(command);

    Request request = Request.newBuilder().withPartitionKey(insertRequest.getDeviceCommand().getDeviceGuid())
            .withBody(insertRequest).build();
    CompletableFuture<Response> future = new CompletableFuture<>();
    client.call(request, future::complete);

    Response response = future.get(10, TimeUnit.SECONDS);
    CommandInsertResponse responseBody = (CommandInsertResponse) response.getBody();
    assertNotNull(responseBody.getDeviceCommand());
}

From source file:com.devicehive.rpcclient.RpcClientActionTest.java

@Test
public void testCommandUpdateAction() throws Exception {
    DeviceCommand command = new DeviceCommand();
    command.setCommand("test_command");
    command.setResult(new JsonStringWrapper("{\"result\": \"OK\"}"));
    command.setDeviceGuid(UUID.randomUUID().toString());
    CommandUpdateRequest updateRequest = new CommandUpdateRequest(command);

    Request request = Request.newBuilder().withPartitionKey(updateRequest.getDeviceCommand().getDeviceGuid())
            .withBody(updateRequest).build();
    CompletableFuture<Response> future = new CompletableFuture<>();
    client.call(request, future::complete);

    Response response = future.get(10, TimeUnit.SECONDS);
    assertNotNull(response);//from w  ww  .ja  v  a2  s .c  o  m
}

From source file:com.devicehive.rpcclient.RpcClientActionTest.java

@Test
public void testNotificationInsertAction() throws Exception {
    DeviceNotification notification = new DeviceNotification();
    notification.setNotification("test_notification");
    notification.setDeviceGuid(UUID.randomUUID().toString());
    NotificationInsertRequest insertRequest = new NotificationInsertRequest(notification);

    Request request = Request.newBuilder()
            .withPartitionKey(insertRequest.getDeviceNotification().getDeviceGuid()).withBody(insertRequest)
            .build();//  w  w  w  .ja  v a 2 s  .com
    CompletableFuture<Response> future = new CompletableFuture<>();
    client.call(request, future::complete);

    Response response = future.get(10, TimeUnit.SECONDS);
    NotificationInsertResponse responseBody = (NotificationInsertResponse) response.getBody();
    assertNotNull(responseBody.getDeviceNotification());
}

From source file:io.ventu.rpc.integration.rest.TestRestInvocationRoundtrip.java

@Test
public void invoke_basicRoundtrip_server401_error()
        throws InterruptedException, TimeoutException, ExecutionException {
    RemoteInvoker invoker = HttpRestInvoker.host("localhost").port(23332).ssl(false).build();

    try {/* ww w .  j ava2 s  .  c  om*/
        CompletableFuture<Res> future = invoker.invoke(new Req(25), Res.class);
        exception.expect(ExecutionException.class);
        future.get(500, TimeUnit.MILLISECONDS);
    } catch (ExecutionException ex) {
        HttpException cause = (HttpException) ex.getCause();
        assertEquals(401, cause.getStatusCode());
        throw ex;
    } finally {
        invoker.close().get(500, TimeUnit.MILLISECONDS);
    }
}

From source file:io.ventu.rpc.integration.rest.TestRestInvocationRoundtrip.java

@Test
public void invoke_invalidRequest_error() throws InterruptedException, TimeoutException, ExecutionException {
    RemoteInvoker invoker = HttpRestInvoker.host("localhost").port(23332).ssl(false).build();

    try {/*from w ww . j  ava2s. com*/
        CompletableFuture<Res> future = invoker.invoke(new Invalid(25), Res.class);
        exception.expect(ExecutionException.class);
        future.get(500, TimeUnit.MILLISECONDS);
    } catch (ExecutionException ex) {
        EncodingException cause = (EncodingException) ex.getCause();
        assertEquals(
                "Failed to encode JSON: No serializer found for class io.ventu.rpc.integration.rest.TestRestInvocationRoundtrip$Invalid and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS) )",
                cause.getMessage());
        throw ex;
    } finally {
        invoker.close().get(500, TimeUnit.MILLISECONDS);
    }
}

From source file:io.ventu.rpc.integration.rest.TestRestInvocationRoundtrip.java

@Test
public void invoke_basicRoundtrip_responseValidator_error()
        throws InterruptedException, TimeoutException, ExecutionException {
    RemoteInvoker invoker = HttpRestInvoker.host("localhost").port(23332).header("APIKEY", "123").ssl(false)
            .responseValidator(new Validator() {
                @Override//from w  ww . ja  v a2s .co m
                public <RS> void validate(RS value) throws ApiException, IllegalArgumentException {
                    throw new ApiException("boom");
                }
            }).build();

    try {
        CompletableFuture<Res> future = invoker.invoke(new Req(25), Res.class);
        exception.expect(ExecutionException.class);
        future.get(500, TimeUnit.MILLISECONDS);
    } catch (ExecutionException ex) {
        ApiException cause = (ApiException) ex.getCause();
        assertEquals("boom", cause.getMessage());
        throw ex;
    } finally {
        invoker.close().get(500, TimeUnit.MILLISECONDS);
    }
}