@Test public void testGetProducts(TestContext context) throws Exception { String itemId1 = "111111"; JsonObject json1 = new JsonObject() .put("itemId", itemId1) .put("name", "productName1") .put("desc", "productDescription1") .put("price", new Double(100.0)); String itemId2 = "222222"; JsonObject json2 = new JsonObject() .put("itemId", itemId2) .put("name", "productName2") .put("desc", "productDescription2") .put("price", new Double(100.0)); List<Product> products = new ArrayList<>(); products.add(new Product(json1)); products.add(new Product(json2)); doAnswer(new Answer<Void>() { public Void answer(InvocationOnMock invocation){ Handler<AsyncResult<List<Product>>> handler = invocation.getArgument(0); handler.handle(Future.succeededFuture(products)); return null; } }).when(catalogService).getProducts(any()); Async async = context.async(); vertx.createHttpClient().get(port, "localhost", "/products", response -> { assertThat(response.statusCode(), equalTo(200)); assertThat(response.headers().get("Content-type"), equalTo("application/json")); response.bodyHandler(body -> { JsonArray json = body.toJsonArray(); Set<String> itemIds = json.stream() .map(j -> new Product((JsonObject)j)) .map(p -> p.getItemId()) .collect(Collectors.toSet()); assertThat(itemIds.size(), equalTo(2)); assertThat(itemIds, allOf(hasItem(itemId1),hasItem(itemId2))); 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);  }