ServiceDiscoveryRestEndpoint { private void publish(RoutingContext routingContext) { JsonObject json = routingContext.getBodyAsJson(); Record record = new Record(json); discovery.publish(record, ar -> { if (ar.failed()) { routingContext.fail(ar.cause()); } else { routingContext.response().setStatusCode(201) .putHeader("Content-Type", "application/json") .end(ar.result().toJson().toString()); } }); } private  ServiceDiscoveryRestEndpoint(Router router, ServiceDiscovery discovery, String root); static ServiceDiscoveryRestEndpoint create(Router router, ServiceDiscovery discovery); static ServiceDiscoveryRestEndpoint create(Router router, ServiceDiscovery discovery, String root); static final String DEFAULT_ROOT; }
@Test public void testUpdateWithUUIDMismatch() throws UnsupportedEncodingException { HelloService svc = new HelloServiceImpl("stuff"); ProxyHelper.registerService(HelloService.class, vertx, svc, "address"); Record record = new Record() .setName("Hello") .setLocation(new JsonObject().put(Record.ENDPOINT, "address")); discovery.publish(record, (r) -> { }); await().until(() -> record.getRegistration() != null); Record retrieved = retrieve(record.getRegistration()); assertThat(retrieved.getStatus()).isEqualTo(Status.UP); retrieved.setStatus(Status.OUT_OF_SERVICE).setRegistration("not-the-right-one").getMetadata().put("foo", "bar"); Restafari.Response response = given().body(retrieved.toJson().toString()) .put("/discovery/" + record.getRegistration()); assertThat(response.getStatusCode()).isEqualTo(400); }
@Test public void testThatWeGetTheTwoPublishedServicesWithMetadata() { HelloService svc = new HelloServiceImpl("stuff"); ProxyHelper.registerService(HelloService.class, vertx, svc, "address"); Record record1 = EventBusService.createRecord("Hello", "address", HelloService.class, new JsonObject().put("key", "foo")); Record record2 = EventBusService.createRecord("Hello-2", "address", HelloService.class, new JsonObject().put("key", "bar")); discovery.publish(record1, (r) -> { }); discovery.publish(record2, (r) -> { }); await().until(() -> record1.getRegistration() != null); await().until(() -> record2.getRegistration() != null); Restafari.Response response = get("/discovery"); JsonArray services = new JsonArray(response.asString()); assertThat(services.size()).isEqualTo(2); for (Object json : services) { Record rec = new Record((JsonObject) json); assertThat(rec.getStatus()).isEqualTo(Status.UP); assertThat(rec.getRegistration()).isNotNull(); assertThat(rec.getName()).startsWith("Hello"); assertThat(rec.getMetadata().getString("key")).isNotNull(); get("/discovery/" + rec.getRegistration()).then().body("name", not(nullValue())); } }
@Test public void testLookupWithQuery() throws UnsupportedEncodingException { HelloService svc = new HelloServiceImpl("stuff"); ProxyHelper.registerService(HelloService.class, vertx, svc, "address"); Record record1 = EventBusService.createRecord("Hello", "address", HelloService.class, new JsonObject().put("key", "foo")); Record record2 = EventBusService.createRecord("Hello-2", "address", HelloService.class, new JsonObject().put("key", "bar")); discovery.publish(record1, (r) -> { }); discovery.publish(record2, (r) -> { }); await().until(() -> record1.getRegistration() != null); await().until(() -> record2.getRegistration() != null); JsonArray services = given() .param("query", "{\"name\":\"Hello\"}") .get("/discovery") .asJsonArray(); assertThat(services.size()).isEqualTo(1); }
@Test public void testLookupWithNonMatchingQuery() throws UnsupportedEncodingException { HelloService svc = new HelloServiceImpl("stuff"); ProxyHelper.registerService(HelloService.class, vertx, svc, "address"); Record record1 = EventBusService.createRecord("Hello", "address", HelloService.class, new JsonObject().put("key", "foo")); Record record2 = EventBusService.createRecord("Hello-2", "address", HelloService.class, new JsonObject().put("key", "bar")); discovery.publish(record1, (r) -> { }); discovery.publish(record2, (r) -> { }); await().until(() -> record1.getRegistration() != null); await().until(() -> record2.getRegistration() != null); JsonArray services = given() .param("query", "{\"stuff\":\"*\"}") .get("/discovery") .asJsonArray(); assertThat(services.size()).isEqualTo(0); }
@Test public void testRetrievingMissingRecord() { HelloService svc = new HelloServiceImpl("stuff"); ProxyHelper.registerService(HelloService.class, vertx, svc, "address"); Record record = new Record() .setName("Hello") .setLocation(new JsonObject().put(Record.ENDPOINT, "address")); discovery.publish(record, (r) -> { }); await().until(() -> record.getRegistration() != null); Record retrieved = retrieve(record.getRegistration()); assertThat(retrieved.getStatus()).isEqualTo(Status.UP); Restafari.Response response2 = delete("/discovery/" + record.getRegistration()); assertThat(response2.getStatusCode()).isEqualTo(204); Restafari.Response response = get("/discovery/" + record.getRegistration()); assertThat(response.getStatusCode()).isEqualTo(404); }