ElasticsearchClient { public ElasticsearchClusterStats getClusterStats() throws DiskSpaceCheckerException { final ElasticsearchClusterStats stats; if (cacheUpToDate()) { stats = latestStats; } else { stats = getRemoteClusterStats(); synchronized (statsLock) { this.latestStats = stats; this.lastUpdate = System.currentTimeMillis(); } } return stats; } ElasticsearchClient(Client client, String baseUrl, long cacheMs); ElasticsearchClusterStats getClusterStats();  }
@Test(expected = uk.co.flax.harahachibu.services.DiskSpaceCheckerException.class) public void throwsExceptionWhenServerReturns404() throws Exception { mockServer.when(request().withPath(ElasticsearchClient.CLUSTER_STATS_ENDPOINT)) .respond(response() .withStatusCode(HttpServletResponse.SC_NOT_FOUND)); esClient.getClusterStats(); }
@Test(expected = uk.co.flax.harahachibu.services.DiskSpaceCheckerException.class) public void throwsExceptionWhenServerReturnsNonsense() throws Exception { mockServer.when(request().withPath(ElasticsearchClient.CLUSTER_STATS_ENDPOINT)) .respond(response() .withBody("blah blah blah") .withStatusCode(HttpServletResponse.SC_OK)); esClient.getClusterStats(); }
@Test(expected = uk.co.flax.harahachibu.services.DiskSpaceCheckerException.class) public void throwsExceptionWhenServerNotRunning() throws Exception { mockServer.stop(); esClient.getClusterStats(); }
@Test(expected = uk.co.flax.harahachibu.services.DiskSpaceCheckerException.class) public void throwsExceptionWhenServerTimesOut() throws Exception { mockServer.when(request().withPath(ElasticsearchClient.CLUSTER_STATS_ENDPOINT)) .respond(response() .withDelay(TimeUnit.SECONDS, 5) .withBody(new JsonBody(clusterStatsJson)) .withHeaders(new Header("Content-Type", "application/json")) .withStatusCode(HttpServletResponse.SC_OK)); esClient.getClusterStats(); }
@Test public void returnsClusterStats() throws Exception { mockServer.when(request().withPath(ElasticsearchClient.CLUSTER_STATS_ENDPOINT)) .respond(response() .withBody(new JsonBody(clusterStatsJson)) .withHeaders(new Header("Content-Type", "application/json")) .withStatusCode(HttpServletResponse.SC_OK)); ElasticsearchClusterStats clusterStats = esClient.getClusterStats(); assertThat(clusterStats).isNotNull(); assertThat(clusterStats.getStatus()).isEqualTo("red"); assertThat(clusterStats.getNodes().getFileSystem().getTotalBytes()).isEqualTo(206289465344L); assertThat(clusterStats.getNodes().getFileSystem().getFreeBytes()).isEqualTo(132861665280L); assertThat(clusterStats.getNodes().getFileSystem().getAvailableBytes()).isEqualTo(122359132160L); }
@Test public void serverCalledOnceInCacheTime() throws Exception { mockServer.when(request().withPath(ElasticsearchClient.CLUSTER_STATS_ENDPOINT)) .respond(response() .withBody(new JsonBody(clusterStatsJson)) .withHeaders(new Header("Content-Type", "application/json")) .withStatusCode(HttpServletResponse.SC_OK)); ElasticsearchClusterStats clusterStats = esClient.getClusterStats(); assertThat(clusterStats).isNotNull(); assertThat(clusterStats.getStatus()).isEqualTo("red"); assertThat(clusterStats.getNodes().getFileSystem().getTotalBytes()).isEqualTo(206289465344L); assertThat(clusterStats.getNodes().getFileSystem().getFreeBytes()).isEqualTo(132861665280L); assertThat(clusterStats.getNodes().getFileSystem().getAvailableBytes()).isEqualTo(122359132160L); clusterStats = esClient.getClusterStats(); assertThat(clusterStats).isNotNull(); assertThat(clusterStats.getStatus()).isEqualTo("red"); assertThat(clusterStats.getNodes().getFileSystem().getTotalBytes()).isEqualTo(206289465344L); assertThat(clusterStats.getNodes().getFileSystem().getFreeBytes()).isEqualTo(132861665280L); assertThat(clusterStats.getNodes().getFileSystem().getAvailableBytes()).isEqualTo(122359132160L); mockServer.verify(request().withPath(ElasticsearchClient.CLUSTER_STATS_ENDPOINT), VerificationTimes.once()); }
@Test public void serverCalledTwiceOutsideCacheTime() throws Exception { mockServer.when(request().withPath(ElasticsearchClient.CLUSTER_STATS_ENDPOINT)) .respond(response() .withBody(new JsonBody(clusterStatsJson)) .withHeaders(new Header("Content-Type", "application/json")) .withStatusCode(HttpServletResponse.SC_OK)); ElasticsearchClusterStats clusterStats = esClient.getClusterStats(); assertThat(clusterStats).isNotNull(); assertThat(clusterStats.getStatus()).isEqualTo("red"); assertThat(clusterStats.getNodes().getFileSystem().getTotalBytes()).isEqualTo(206289465344L); assertThat(clusterStats.getNodes().getFileSystem().getFreeBytes()).isEqualTo(132861665280L); assertThat(clusterStats.getNodes().getFileSystem().getAvailableBytes()).isEqualTo(122359132160L); Thread.sleep(cacheMs); clusterStats = esClient.getClusterStats(); assertThat(clusterStats).isNotNull(); assertThat(clusterStats.getStatus()).isEqualTo("red"); assertThat(clusterStats.getNodes().getFileSystem().getTotalBytes()).isEqualTo(206289465344L); assertThat(clusterStats.getNodes().getFileSystem().getFreeBytes()).isEqualTo(132861665280L); assertThat(clusterStats.getNodes().getFileSystem().getAvailableBytes()).isEqualTo(122359132160L); mockServer.verify(request().withPath(ElasticsearchClient.CLUSTER_STATS_ENDPOINT), VerificationTimes.exactly(2)); }