public static void toJson(ServiceEntry obj, java.util.Map<String, Object> json) { if (obj.getChecks() != null) { JsonArray array = new JsonArray(); obj.getChecks().forEach(item -> array.add(item.toJson())); json.put("checks", array); } if (obj.getNode() != null) { json.put("node", obj.getNode().toJson()); } if (obj.getService() != null) { json.put("service", obj.getService().toJson()); } } }
List<ServiceEntry> serviceEntries = list.stream() .filter(serviceEntry -> serviceEntry.getChecks().stream().allMatch(check -> isCheckOK(check.getStatus()))
List<ServiceEntry> serviceEntries = list.stream() .filter(serviceEntry -> serviceEntry.getChecks().stream().allMatch(check -> isCheckOK(check.getStatus()))
@Test public void testServiceEntryCopy() { ServiceEntry entry = randomServiceEntry(); checkServiceEntry(entry, new ServiceEntry(entry)); checkServiceEntry(entry, new ServiceEntry(entry.toJson())); List<Check> shuffledChecks = new ArrayList<>(); shuffledChecks.add(entry.getChecks().get(1)); shuffledChecks.add(entry.getChecks().get(0)); ServiceEntry shuffled = new ServiceEntry(entry) .setChecks(shuffledChecks); checkServiceEntry(entry, shuffled); }
private void checkServiceEntry(ServiceEntry expected, ServiceEntry actual) { assertEquals(expected, actual); assertEquals(expected.hashCode(), actual.hashCode()); assertEquals(expected.getNode(), actual.getNode()); assertEquals(expected.getService(), actual.getService()); List<Check> expectedList = expected.getChecks(); List<Check> actualList = actual.getChecks(); assertTrue(expectedList.containsAll(actualList)); assertTrue(actualList.containsAll(expectedList)); }
@Test public void watchService() throws InterruptedException { StateConsumer<String> consumer = new StateConsumer<>(); ServiceOptions service = new ServiceOptions() .setCheckOptions(new CheckOptions() .setStatus(CheckStatus.PASSING) .setTtl("4s") .setName(randomAlphaString(10))) .setId(randomAlphaString(10)) .setName(randomAlphaString(10)); Watch<ServiceEntryList> watch = Watch.service(service.getName(), vertx, ctx.readClientOptions()) .setHandler(list -> { if (list.succeeded()) { consumer.consume(list.nextResult().getList() .stream().filter(s -> s.getService().getName().equals(service.getName())) .map(e -> e.getService().getName() + "/" + e.getChecks().stream() .filter(c -> c.getId().equals("service:" + service.getId())) .map(c -> c.getStatus().name()).findFirst().orElse("")) .findFirst().orElse("")); } }) .start(); consumer.await(""); runAsync(h -> ctx.writeClient().registerService(service, h)); consumer.await(service.getName() + "/" + CheckStatus.PASSING.name()); consumer.await(service.getName() + "/" + CheckStatus.CRITICAL.name()); consumer.check(); watch.stop(); runAsync(h -> ctx.writeClient().deregisterService(service.getId(), h)); }