private Record createRecord(Service service) { String address = service.getAddress(); int port = service.getPort(); JsonObject metadata = service.toJson(); if (service.getTags() != null) { service.getTags().forEach(tag -> metadata.put(tag, true)); } Record record = new Record() .setName(service.getName()) .setMetadata(metadata); // To determine the record type, check if we have a tag with a "type" name record.setType(ServiceType.UNKNOWN); ServiceTypes.all().forEachRemaining(type -> { if (metadata.getBoolean(type.name(), false)) { record.setType(type.name()); } }); JsonObject location = new JsonObject(); location.put("host", address); location.put("port", port); // Manage HTTP endpoint if (record.getType().equals("http-endpoint")) { if (metadata.getBoolean("ssl", false)) { location.put("ssl", true); } location = new HttpLocation(location).toJson(); } record.setLocation(location); return record; }
private Record createRecord(Service service) { String address = service.getAddress(); int port = service.getPort(); JsonObject metadata = service.toJson(); if (service.getTags() != null) { service.getTags().forEach(tag -> metadata.put(tag, true)); } Record record = new Record() .setName(service.getName()) .setMetadata(metadata); // To determine the record type, check if we have a tag with a "type" name record.setType(ServiceType.UNKNOWN); ServiceTypes.all().forEachRemaining(type -> { if (metadata.getBoolean(type.name(), false)) { record.setType(type.name()); } }); JsonObject location = new JsonObject(); location.put("host", address); location.put("port", port); // Manage HTTP endpoint if (record.getType().equals("http-endpoint")) { if (metadata.getBoolean("ssl", false)) { location.put("ssl", true); } location = new HttpLocation(location).toJson(); } record.setLocation(location); 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)); }