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; } }
String id = consulService.getService().getId(); String name = consulService.getService().getName(); Record record = createRecord(consulService.getService());
String id = consulService.getService().getId(); String name = consulService.getService().getName(); Record record = createRecord(consulService.getService());
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; }); } }
private static String checkService(TestContext tc, List<Service> list, String name, ServiceOptions options) { Service s = list.stream() .filter(i -> name.equals(i.getName())) .findFirst() .orElseThrow(NoSuchElementException::new); String serviceId = s.getId(); tc.assertEquals(s.getTags(), options.getTags()); tc.assertEquals(s.getAddress(), options.getAddress()); tc.assertEquals(s.getPort(), options.getPort()); for (Map.Entry<String, String> entry : options.getMeta().entrySet()) { tc.assertEquals(s.getMeta().get(entry.getKey()), entry.getValue()); } return serviceId; }
private void checkService(Service expected, Service actual) { assertEquals(expected, actual); assertEquals(expected.hashCode(), actual.hashCode()); assertEquals(expected.getNode(), actual.getNode()); assertEquals(expected.getNodeAddress(), actual.getNodeAddress()); assertEquals(expected.getId(), actual.getId()); assertEquals(expected.getName(), actual.getName()); assertEquals(expected.getAddress(), actual.getAddress()); assertEquals(expected.getPort(), actual.getPort()); List<String> expectedList = expected.getTags(); List<String> actualList = actual.getTags(); assertTrue(expectedList.containsAll(actualList)); assertTrue(actualList.containsAll(expectedList)); }
@Test public void bindCheckToService() { String serviceName = "serviceName"; ServiceOptions service = new ServiceOptions() .setName(serviceName) .setAddress("10.0.0.1") .setPort(8080); runAsync(h -> ctx.writeClient().registerService(service, h)); List<Service> services = getAsync(h -> ctx.writeClient().localServices(h)); Service s = services.stream().filter(i -> "serviceName".equals(i.getName())).findFirst().get(); String serviceId = s.getId(); assertEquals(s.getAddress(), "10.0.0.1"); assertEquals(s.getPort(), 8080); CheckOptions check = new CheckOptions() .setId("checkId") .setName("checkName") .setNotes("checkNotes") .setServiceId(serviceId) .setStatus(CheckStatus.PASSING) .setTtl("10s"); runAsync(h -> ctx.writeClient().registerCheck(check, h)); List<Check> checks = getAsync(h -> ctx.writeClient().localChecks(h)); Check c = checks.stream().filter(i -> "checkId".equals(i.getId())).findFirst().get(); assertEquals(c.getServiceId(), serviceId); assertEquals(c.getStatus(), CheckStatus.PASSING); assertEquals(c.getNotes(), "checkNotes"); runAsync(h -> ctx.writeClient().deregisterService(serviceId, h)); }
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");