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()); } } }
static ServiceEntry parse(JsonObject json) { return new ServiceEntry() .setNode(NodeParser.parse(json.getJsonObject(NODE_KEY))) .setService(ServiceParser.parseAgentInfo(json.getJsonObject(SERVICE_KEY))) .setChecks(json.getJsonArray(CHECKS_KEY).stream().map(obj -> CheckParser.parse((JsonObject) obj)).collect(Collectors.toList())); } }
public static void toJson(ServiceEntryList obj, java.util.Map<String, Object> json) { json.put("index", obj.getIndex()); if (obj.getList() != null) { JsonArray array = new JsonArray(); obj.getList().forEach(item -> array.add(item.toJson())); json.put("list", array); } } }
public static void fromJson(Iterable<java.util.Map.Entry<String, Object>> json, ServiceEntry obj) { for (java.util.Map.Entry<String, Object> member : json) { switch (member.getKey()) { case "checks": 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.setChecks(list); } break; case "node": if (member.getValue() instanceof JsonObject) { obj.setNode(new io.vertx.ext.consul.Node((JsonObject)member.getValue())); } break; case "service": if (member.getValue() instanceof JsonObject) { obj.setService(new io.vertx.ext.consul.Service((JsonObject)member.getValue())); } break; } } }
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 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); }
List<ServiceEntry> serviceEntries = list.stream() .filter(serviceEntry -> serviceEntry.getChecks().stream().allMatch(check -> isCheckOK(check.getStatus())) String id = consulService.getService().getId(); String name = consulService.getService().getName(); Record record = createRecord(consulService.getService());
@Test public void entry() { ServiceEntry entry = new ServiceEntry().setChecks(Arrays.asList( new Check().setStatus(CheckStatus.WARNING), new Check().setStatus(CheckStatus.PASSING) )); assertEquals(entry.aggregatedStatus(), CheckStatus.WARNING); } }
private List<ServiceEntry> sorted() { List<ServiceEntry> sorted = null; if (list != null) { sorted = new ArrayList<>(list); sorted.sort(Comparator.comparing(e -> e.getService().getId())); } return sorted; } }
public static void fromJson(Iterable<java.util.Map.Entry<String, Object>> json, ServiceEntryList 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.ServiceEntry> list = new java.util.ArrayList<>(); ((Iterable<Object>)member.getValue()).forEach( item -> { if (item instanceof JsonObject) list.add(new io.vertx.ext.consul.ServiceEntry((JsonObject)item)); }); obj.setList(list); } break; } } }
List<ServiceEntry> serviceEntries = list.stream() .filter(serviceEntry -> serviceEntry.getChecks().stream().allMatch(check -> isCheckOK(check.getStatus())) String id = consulService.getService().getId(); String name = consulService.getService().getName(); Record record = createRecord(consulService.getService());
List<String> ids = list1.getList().stream().map(entry -> entry.getService().getId()).collect(Collectors.toList()); assertTrue(ids.contains("id1")); assertTrue(ids.contains("id2")); ServiceEntryList list2 = getAsync(h -> ctx.readClient().healthServiceNodesWithOptions("service", true, opts2, h)); assertEquals(list2.getList().size(), 1); assertEquals(list2.getList().get(0).getService().getId(), "id2");
((Iterable<Object>)member.getValue()).forEach( item -> { if (item instanceof JsonObject) list.add(new io.vertx.ext.consul.ServiceEntry((JsonObject)item)); }); obj.setNodes(list);
public static ServiceEntry randomServiceEntry() { return new ServiceEntry() .setNode(randomNode()) .setService(randomService()) .setChecks(randomObjectList(2, RandomObjects::randomCheck)); }
@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)); }
public static void toJson(PreparedQueryExecuteResponse obj, java.util.Map<String, Object> json) { if (obj.getDc() != null) { json.put("dc", obj.getDc()); } if (obj.getDnsTtl() != null) { json.put("dnsTtl", obj.getDnsTtl()); } json.put("failovers", obj.getFailovers()); if (obj.getNodes() != null) { JsonArray array = new JsonArray(); obj.getNodes().forEach(item -> array.add(item.toJson())); json.put("nodes", array); } if (obj.getService() != null) { json.put("service", obj.getService()); } } }