HttpDiscoveryAnnouncementClient implements DiscoveryAnnouncementClient { @Override public ListenableFuture<Duration> announce(Set<ServiceAnnouncement> services) { requireNonNull(services, "services is null"); Builder builder; final boolean servicesEmpty = services.isEmpty(); if (servicesEmpty) { builder = prepareDelete(); } else { Announcement announcement = new Announcement(nodeInfo.getEnvironment(), nodeInfo.getNodeId(), nodeInfo.getPool(), nodeInfo.getLocation(), services); builder = preparePut() .setHeader("Content-Type", MEDIA_TYPE_JSON.toString()) .setBodySource(jsonBodyGenerator(announcementCodec, announcement)); } Request request = builder .setUri(URI.create("v1/announcement/" + nodeInfo.getNodeId())) .setHeader("User-Agent", nodeInfo.getNodeId()) .build(); return httpClient.executeAsync(request, new DiscoveryResponseHandler<Duration>("Announcement") { @Override public Duration handle(Request request, Response response) throws DiscoveryException { int statusCode = response.getStatusCode(); if (statusCode == 404 && servicesEmpty) { statusCode = 200; } if (!isSuccess(statusCode)) { throw new DiscoveryException(String.format("Announcement failed with status code %s: %s", statusCode, getBodyForError(response))); } return extractMaxAge(response); } }); } @Inject  HttpDiscoveryAnnouncementClient(NodeInfo nodeInfo,
            JsonCodec<Announcement> announcementCodec,
            @ForDiscoveryClient HttpClient httpClient); @Override ListenableFuture<Duration> announce(Set<ServiceAnnouncement> services); @Override ListenableFuture<Void> unannounce();  }
@Test public void testAnnounceCacheControl() throws Exception { httpClient.setProcessor((request -> mockResponse() .status(ACCEPTED) .header("Cache-Control", "max-age=75") .build())); Duration duration = client.announce(announcements).get(); assertEquals(duration, new Duration(75, TimeUnit.SECONDS)); }
@Test public void testAnnounceBadStatus() throws Exception { httpClient.setProcessor((request -> mockResponse(NOT_FOUND))); try { client.announce(announcements).get(); fail("expected ExecutionException"); } catch (ExecutionException e) { assertInstanceOf(e.getCause(), DiscoveryException.class); assertContains(e.getCause().getMessage(), "Announcement failed with status code 404: "); } }
@Test public void testAnnounceConnectException() throws Exception { httpClient.setProcessor((request -> { throw new ConnectException("test exception"); })); try { client.announce(announcements).get(); fail("expected ExecutionException"); } catch (ExecutionException e) { assertInstanceOf(e.getCause(), DiscoveryException.class); assertEquals(e.getCause().getMessage(), "Announcement failed"); } }
@Test public void testAnnounceZeroAnnouncements() throws Exception { httpClient.setProcessor((request -> { assertEquals(request.getMethod(), "DELETE"); assertEquals(request.getUri(), URI.create("v1/announcement/" + nodeInfo.getNodeId())); assertEquals(request.getHeader("User-Agent"), nodeInfo.getNodeId()); return mockResponse(NOT_FOUND); })); Duration duration = client.announce(ImmutableSet.of()).get(); assertEquals(duration, new Duration(10, TimeUnit.SECONDS)); assertEquals(httpClient.getRequestCount(), 1); }
@Test public void testAnnounceZeroAnnouncementsBadStatus() throws Exception { httpClient.setProcessor((request -> mockResponse(INTERNAL_SERVER_ERROR))); try { client.announce(ImmutableSet.of()).get(); fail("expected ExecutionException"); } catch (ExecutionException e) { assertInstanceOf(e.getCause(), DiscoveryException.class); assertContains(e.getCause().getMessage(), "Announcement failed with status code 500: "); } }
@Test public void testAnnounce() throws Exception { httpClient.setProcessor((request) -> { assertEquals(request.getMethod(), "PUT"); assertEquals(request.getUri(), URI.create("v1/announcement/" + nodeInfo.getNodeId())); assertEquals(request.getHeader("User-Agent"), nodeInfo.getNodeId()); assertEquals(request.getHeader("Content-Type"), "application/json"); return mockResponse(ACCEPTED); }); Duration duration = client.announce(announcements).get(); assertEquals(duration, new Duration(10, TimeUnit.SECONDS)); assertEquals(httpClient.getRequestCount(), 1); }