public static void fromJson(Iterable<java.util.Map.Entry<String, Object>> json, CheckList obj) { for (java.util.Map.Entry<String, Object> member : json) { switch (member.getKey()) { case "index": if (member.getValue() instanceof Number) { obj.setIndex(((Number)member.getValue()).longValue()); } break; case "list": if (member.getValue() instanceof JsonArray) { java.util.ArrayList<io.vertx.ext.consul.Check> list = new java.util.ArrayList<>(); ((Iterable<Object>)member.getValue()).forEach( item -> { if (item instanceof JsonObject) list.add(new io.vertx.ext.consul.Check((JsonObject)item)); }); obj.setList(list); } break; } } }
@Override public int hashCode() { int result = (int) (index ^ (index >>> 32)); result = 31 * result + (list != null ? sorted().hashCode() : 0); return result; }
@Test public void testCheckListCopy() { List<Check> checks = new ArrayList<>(); checks.add(randomCheck()); checks.add(randomCheck()); CheckList list = new CheckList() .setIndex(randomPositiveInt()) .setList(checks); checkCheckList(list, new CheckList(list)); checkCheckList(list, new CheckList(list.toJson())); List<Check> shuffledEvents = new ArrayList<>(); shuffledEvents.add(checks.get(1)); shuffledEvents.add(checks.get(0)); CheckList shuffled = new CheckList() .setIndex(list.getIndex()) .setList(shuffledEvents); checkCheckList(list, shuffled); }
@Override public ConsulClient healthStateWithOptions(HealthState healthState, CheckQueryOptions options, Handler<AsyncResult<CheckList>> resultHandler) { Query query = options == null ? null : Query.of("near", options.getNear()).put(options.getBlockingOptions()); requestArray(HttpMethod.GET, "/v1/health/state/" + healthState.key, query, null, resultHandler, (arr, headers) -> { List<Check> list = arr.stream().map(obj -> CheckParser.parse((JsonObject) obj)).collect(Collectors.toList()); return new CheckList().setList(list).setIndex(Long.parseLong(headers.get(INDEX_HEADER))); }); return this; }
private void checkCheckList(CheckList expected, CheckList actual) { assertEquals(expected, actual); assertEquals(expected.hashCode(), actual.hashCode()); assertEquals(expected.getIndex(), actual.getIndex()); List<Check> expectedList = expected.getList(); List<Check> actualList = actual.getList(); assertTrue(expectedList.containsAll(actualList)); assertTrue(actualList.containsAll(expectedList)); }
private Future serviceToRecord(Service service) { //use the checks to set the record status Future<CheckList> checkListFuture = Future.future(); client.healthChecks(service.getName(), checkListFuture); return checkListFuture.map(cl -> cl.getList().stream().map(Check::getStatus).allMatch(CheckStatus.PASSING::equals)) .map(st -> st ? new Record().setStatus(Status.UP) : new Record().setStatus(Status.DOWN)) .map(record -> { record.setMetadata(new JsonObject()); record.setLocation(new JsonObject()); record.setName(service.getName()); record.setRegistration(service.getId()); List<String> tags = service.getTags(); record.setType(ServiceType.UNKNOWN); ServiceTypes.all().forEachRemaining(type -> { if (service.getTags().contains(type.name())) { record.setType(type.name()); tags.remove(type.name()); } }); //retrieve the metadata object tags.stream().filter(t -> t.startsWith("metadata:")).map(s -> s.substring("metadata:".length())).map(JsonObject::new).forEach(json -> record.getMetadata().mergeIn(json)); //retrieve the location object tags.stream().filter(t -> t.startsWith("location:")).map(s -> s.substring("location:".length())).map(JsonObject::new).forEach(json -> record.getLocation().mergeIn(json)); record.getMetadata().put("tags", new JsonArray(tags.stream().filter(t -> !t.startsWith("metadata:") && !t.startsWith("location:")).collect(Collectors.toList()))); return record; }); } }
@Override public ConsulClient healthChecksWithOptions(String service, CheckQueryOptions options, Handler<AsyncResult<CheckList>> resultHandler) { Query query = options == null ? null : Query.of("near", options.getNear()).put(options.getBlockingOptions()); requestArray(HttpMethod.GET, "/v1/health/checks/" + urlEncode(service), query, null, resultHandler, (arr, headers) -> { List<Check> list = arr.stream().map(obj -> CheckParser.parse((JsonObject) obj)).collect(Collectors.toList()); return new CheckList().setList(list).setIndex(Long.parseLong(headers.get(INDEX_HEADER))); }); return this; }
@Test public void healthState() throws InterruptedException { String serviceName = randomAlphaString(10); ServiceOptions opts = new ServiceOptions() .setName(serviceName) .setId(serviceName) .setCheckOptions(new CheckOptions().setTtl("1m")); runAsync(h -> ctx.writeClient().registerService(opts, h)); CheckList list1 = getAsync(h -> ctx.readClient().healthState(HealthState.CRITICAL, h)); CountDownLatch latch = new CountDownLatch(1); waitBlockingQuery(latch, 10, list1.getIndex(), (idx, fut) -> { CheckQueryOptions options = new CheckQueryOptions() .setBlockingOptions(new BlockingQueryOptions().setIndex(idx)); ctx.readClient().healthStateWithOptions(HealthState.PASSING, options, h -> { List<String> names = h.result().getList().stream().map(Check::getServiceName).collect(Collectors.toList()); waitComplete(vertx, fut, h.result().getIndex(), names.contains(serviceName)); }); }); runAsync(h -> ctx.writeClient().passCheck("service:" + serviceName, h)); awaitLatch(latch); runAsync(h -> ctx.writeClient().deregisterService(serviceName, h)); }
assertEquals(list1.getList().size(), 1); assertEquals(list1.getList().get(0).getId(), "checkId1"); waitBlockingQuery(latch, 10, list1.getIndex(), (idx, fut) -> { CheckQueryOptions options = new CheckQueryOptions() .setBlockingOptions(new BlockingQueryOptions().setIndex(idx)); ctx.readClient().healthChecksWithOptions("serviceName", options, h -> { List<String> ids = h.result().getList().stream().map(Check::getId).collect(Collectors.toList()); boolean success = h.result().getList().size() == 2; success &= ids.contains("checkId1"); success &= ids.contains("checkId2"); waitComplete(vertx, fut, h.result().getIndex(), success); }); });