@Test public void testToString() { assertNotNull(new Announcement("environment", "node", "pool", "location", ImmutableSet.of( serviceAnnouncement("foo") .addProperty("http", "http://localhost:8080") .addProperty("jmx", "jmx://localhost:1234") .build()) )); }
@Test public void testEquivalence() { equivalenceTester() .addEquivalentGroup( new Announcement("environment", "node-A", "pool", "location", ImmutableSet.<ServiceAnnouncement>of(serviceAnnouncement("foo").build())), new Announcement("ENVIRONMENT", "node-A", "pool", "location", ImmutableSet.<ServiceAnnouncement>of(serviceAnnouncement("foo").build())), new Announcement("environment", "node-A", "pool", "LOCATION", ImmutableSet.<ServiceAnnouncement>of(serviceAnnouncement("foo").build())), new Announcement("environment", "node-A", "pool", "location", ImmutableSet.<ServiceAnnouncement>of(serviceAnnouncement("FOO").build()))) .addEquivalentGroup( new Announcement("environment", "node-B", "pool", "location", ImmutableSet.<ServiceAnnouncement>of(serviceAnnouncement("foo").build())), new Announcement("environment-X", "node-B", "pool", "location", ImmutableSet.<ServiceAnnouncement>of(serviceAnnouncement("foo").build())), new Announcement("environment", "node-B", "pool", "location-X", ImmutableSet.<ServiceAnnouncement>of(serviceAnnouncement("foo").build())), new Announcement("environment", "node-B", "pool", "location", ImmutableSet.<ServiceAnnouncement>of(serviceAnnouncement("bar").build()))) .check(); } }
@Test public void testToString() { assertNotNull(new Announcement("environment", "node", "pool", "location", ImmutableSet.of( serviceAnnouncement("foo") .addProperty("http", "http://localhost:8080") .addProperty("jmx", "jmx://localhost:1234") .build()))); }
@Test public void testToString() { assertNotNull(new Announcement("environment", "node", "pool", "location", ImmutableSet.of( serviceAnnouncement("foo") .addProperty("http", "http://localhost:8080") .addProperty("jmx", "jmx://localhost:1234") .build()))); }
@Test public void testEquivalence() { equivalenceTester() .addEquivalentGroup( new Announcement("environment", "node-A", "pool", "location", ImmutableSet.<ServiceAnnouncement>of(serviceAnnouncement("foo").build())), new Announcement("ENVIRONMENT", "node-A", "pool", "location", ImmutableSet.<ServiceAnnouncement>of(serviceAnnouncement("foo").build())), new Announcement("environment", "node-A", "pool", "LOCATION", ImmutableSet.<ServiceAnnouncement>of(serviceAnnouncement("foo").build())), new Announcement("environment", "node-A", "pool", "location", ImmutableSet.<ServiceAnnouncement>of(serviceAnnouncement("FOO").build()))) .addEquivalentGroup( new Announcement("environment", "node-B", "pool", "location", ImmutableSet.<ServiceAnnouncement>of(serviceAnnouncement("foo").build())), new Announcement("environment-X", "node-B", "pool", "location", ImmutableSet.<ServiceAnnouncement>of(serviceAnnouncement("foo").build())), new Announcement("environment", "node-B", "pool", "location-X", ImmutableSet.<ServiceAnnouncement>of(serviceAnnouncement("foo").build())), new Announcement("environment", "node-B", "pool", "location", ImmutableSet.<ServiceAnnouncement>of(serviceAnnouncement("bar").build()))) .check(); } }
@Test public void testEquivalence() { equivalenceTester() .addEquivalentGroup( new Announcement("environment", "node-A", "pool", "location", ImmutableSet.<ServiceAnnouncement>of(serviceAnnouncement("foo").build())), new Announcement("ENVIRONMENT", "node-A", "pool", "location", ImmutableSet.<ServiceAnnouncement>of(serviceAnnouncement("foo").build())), new Announcement("environment", "node-A", "pool", "LOCATION", ImmutableSet.<ServiceAnnouncement>of(serviceAnnouncement("foo").build())), new Announcement("environment", "node-A", "pool", "location", ImmutableSet.<ServiceAnnouncement>of(serviceAnnouncement("FOO").build())) ) .addEquivalentGroup( new Announcement("environment", "node-B", "pool", "location", ImmutableSet.<ServiceAnnouncement>of(serviceAnnouncement("foo").build())), new Announcement("environment-X", "node-B", "pool", "location", ImmutableSet.<ServiceAnnouncement>of(serviceAnnouncement("foo").build())), new Announcement("environment", "node-B", "pool", "location-X", ImmutableSet.<ServiceAnnouncement>of(serviceAnnouncement("foo").build())), new Announcement("environment", "node-B", "pool", "location", ImmutableSet.<ServiceAnnouncement>of(serviceAnnouncement("bar").build())) ) .check(); } }
@Test public void testJsonEncode() throws Exception { Announcement announcement = new Announcement("environment", "node", "pool", "location", ImmutableSet.of( serviceAnnouncement("foo") .addProperty("http", "http://localhost:8080") .addProperty("jmx", "jmx://localhost:1234") .build())); Map<String, Object> actual = objectCodec.fromJson(announcementCodec.toJson(announcement)); String json = Resources.toString(Resources.getResource("announcement.json"), UTF_8); Map<String, Object> expected = objectCodec.fromJson(json); // set id in expected List<Map<String, Object>> services = toServices(expected.get("services")); services.get(0).put("id", Iterables.getOnlyElement(announcement.getServices()).getId().toString()); assertEquals(actual, expected); }
@Test public void testJsonEncode() throws Exception { Announcement announcement = new Announcement("environment", "node", "pool", "location", ImmutableSet.of( serviceAnnouncement("foo") .addProperty("http", "http://localhost:8080") .addProperty("jmx", "jmx://localhost:1234") .build()) ); Map<String, Object> actual = objectCodec.fromJson(announcementCodec.toJson(announcement)); String json = Resources.toString(Resources.getResource("announcement.json"), Charsets.UTF_8); Map<String, Object> expected = objectCodec.fromJson(json); // set id in expected List<Map<String, Object>> services = (List<Map<String, Object>>) expected.get("services"); services.get(0).put("id", Iterables.getOnlyElement(announcement.getServices()).getId().toString()); assertEquals(actual, expected); }
@Test public void testJsonEncode() throws Exception { Announcement announcement = new Announcement("environment", "node", "pool", "location", ImmutableSet.of( serviceAnnouncement("foo") .addProperty("http", "http://localhost:8080") .addProperty("jmx", "jmx://localhost:1234") .build())); Map<String, Object> actual = objectCodec.fromJson(announcementCodec.toJson(announcement)); String json = Resources.toString(Resources.getResource("announcement.json"), UTF_8); Map<String, Object> expected = objectCodec.fromJson(json); // set id in expected List<Map<String, Object>> services = toServices(expected.get("services")); services.get(0).put("id", Iterables.getOnlyElement(announcement.getServices()).getId().toString()); assertEquals(actual, expected); }
@Override public CheckedFuture<Duration, DiscoveryException> announce(Set<ServiceAnnouncement> services) { Preconditions.checkNotNull(services, "services is null"); URI uri = discoveryServiceURI.get(); if (uri == null) { return Futures.immediateFailedCheckedFuture(new DiscoveryException("No discovery servers are available")); } Announcement announcement = new Announcement(nodeInfo.getEnvironment(), nodeInfo.getNodeId(), nodeInfo.getPool(), nodeInfo.getLocation(), services); Request request = preparePut() .setUri(URI.create(uri + "/v1/announcement/" + nodeInfo.getNodeId())) .setHeader("User-Agent", nodeInfo.getNodeId()) .setHeader("Content-Type", MEDIA_TYPE_JSON.toString()) .setBodyGenerator(jsonBodyGenerator(announcementCodec, announcement)) .build(); return httpClient.executeAsync(request, new DiscoveryResponseHandler<Duration>("Announcement") { @Override public Duration handle(Request request, Response response) throws DiscoveryException { int statusCode = response.getStatusCode(); if (!isSuccess(statusCode)) { throw new DiscoveryException(String.format("Announcement failed with status code %s: %s", statusCode, getBodyForError(response))); } Duration maxAge = extractMaxAge(response); return maxAge; } }); }
@Override public ListenableFuture<Duration> announce(Set<ServiceAnnouncement> services) { requireNonNull(services, "services is null"); URI uri = discoveryServiceURI.get(); if (uri == null) { return immediateFailedFuture(new DiscoveryException("No discovery servers are available")); } Announcement announcement = new Announcement(nodeInfo.getEnvironment(), nodeInfo.getNodeId(), nodeInfo.getPool(), nodeInfo.getLocation(), services); Request request = preparePut() .setUri(URI.create(uri + "/v1/announcement/" + nodeInfo.getNodeId())) .setHeader("User-Agent", nodeInfo.getNodeId()) .setHeader("Content-Type", MEDIA_TYPE_JSON.toString()) .setBodyGenerator(jsonBodyGenerator(announcementCodec, announcement)) .build(); return httpClient.executeAsync(request, new DiscoveryResponseHandler<Duration>("Announcement", uri) { @Override public Duration handle(Request request, Response response) throws DiscoveryException { int statusCode = response.getStatusCode(); if (!isSuccess(statusCode)) { throw new DiscoveryException(String.format("Announcement failed with status code %s: %s", statusCode, getBodyForError(response))); } Duration maxAge = extractMaxAge(response); return maxAge; } }); }
@Override public ListenableFuture<Duration> announce(Set<ServiceAnnouncement> services) { Preconditions.checkNotNull(services, "services is null"); URI uri = discoveryServiceURI.get(); if (uri == null) { return Futures.immediateFailedCheckedFuture(new DiscoveryException("No discovery servers are available")); } Announcement announcement = new Announcement(nodeInfo.getEnvironment(), nodeInfo.getNodeId(), nodeInfo.getPool(), nodeInfo.getLocation(), services); Request request = preparePut() .setUri(URI.create(uri + "/v1/announcement/" + nodeInfo.getNodeId())) .setHeader("User-Agent", nodeInfo.getNodeId()) .setHeader("Content-Type", MEDIA_TYPE_JSON.toString()) .setBodyGenerator(jsonBodyGenerator(announcementCodec, announcement)) .build(); return httpClient.executeAsync(request, new DiscoveryResponseHandler<Duration>("Announcement", uri) { @Override public Duration handle(Request request, Response response) throws DiscoveryException { int statusCode = response.getStatusCode(); if (!isSuccess(statusCode)) { throw new DiscoveryException(String.format("Announcement failed with status code %s: %s", statusCode, getBodyForError(response))); } Duration maxAge = extractMaxAge(response); return maxAge; } }); }