@Test public void testGetProductsWithTimeOut(TestContext context) { doAnswer(new Answer<Void>() { public Void answer(InvocationOnMock invocation) { vertx.setTimer(1500, l -> { Handler<AsyncResult<List<Product>>> handler = invocation.getArgument(0); handler.handle(Future.succeededFuture(new ArrayList<>())); }); return null; } }).when(catalogService).getProducts(any()); Async async = context.async(); vertx.createHttpClient().get(port, "localhost", "/products", response -> { assertThat(response.statusCode(), equalTo(503)); response.bodyHandler(body -> { assertThat(body.toString(), equalTo("Service Unavailable")); verify(catalogService).getProducts(any()); async.complete(); }) .exceptionHandler(context.exceptionHandler()); }) .exceptionHandler(context.exceptionHandler()) .end(); }
private void getProducts(RoutingContext rc) { catalogService.getProducts(ar -> { if (ar.succeeded()) { List<Product> products = ar.result(); JsonArray json = new JsonArray(); products.stream() .map(Product::toJson) .forEach(json::add); rc.response() .putHeader("Content-type", "application/json") .end(json.encodePrettily()); } else { rc.fail(ar.cause()); } }); }
ApiVerticle extends AbstractVerticle { private void getProducts(RoutingContext rc) { catalogService.getProducts(ar -> { if (ar.succeeded()) { List<Product> products = ar.result(); JsonArray json = new JsonArray(); products.stream() .map(Product::toJson) .forEach(json::add); rc.response() .putHeader("Content-type", "application/json") .end(json.encodePrettily()); } else { rc.fail(ar.cause()); } }); } }
ApiVerticle extends AbstractVerticle { private void getProducts(RoutingContext rc) { catalogService.getProducts(ar -> { if (ar.succeeded()) { List<Product> products = ar.result(); JsonArray json = new JsonArray(); products.stream() .map(Product::toJson) .forEach(json::add); rc.response() .putHeader("Content-type", "application/json") .end(json.encodePrettily()); } else { rc.fail(ar.cause()); } }); } ApiVerticle(CatalogService catalogService); }
ApiVerticle extends AbstractVerticle { private void getProducts(RoutingContext rc) { catalogService.getProducts(ar -> { if (ar.succeeded()) { List<Product> products = ar.result(); JsonArray json = new JsonArray(); products.stream() .map(Product::toJson) .forEach(json::add); rc.response() .putHeader("Content-type", "application/json") .end(json.encodePrettily()); } else { rc.fail(ar.cause()); } }); } ApiVerticle(CatalogService catalogService); @Override void start(Future<Void> startFuture); }
ApiVerticle extends AbstractVerticle { private void getProducts(RoutingContext rc) { catalogService.getProducts(ar -> { if (ar.succeeded()) { List<Product> products = ar.result(); JsonArray json = new JsonArray(); products.stream() .map(Product::toJson) .forEach(json::add); rc.response() .putHeader("Content-type", "application/json") .end(json.encodePrettily()); } else { rc.fail(ar.cause()); } }); } ApiVerticle(CatalogService catalogService); @Override void start(Future<Void> startFuture);  }