StatusResource { @Timed @ExceptionMetered @GET public Response get() { SortedMap<String, HealthCheck.Result> results = memoizedCheck.get(); List<String> failing = results.entrySet().stream() .filter(r -> !r.getValue().isHealthy()) .map(Map.Entry::getKey) .collect(toList()); if (!failing.isEmpty()) { logger.warn("Health checks failed: {}", results); String message = "failing health checks: " + Arrays.toString(failing.toArray()); StatusResponse sr = new StatusResponse("critical", message, results); return Response.serverError().entity(sr).build(); } StatusResponse sr = new StatusResponse("ok", "ok", results); return Response.ok(sr).build(); } @Inject  StatusResource(KeywhizConfig keywhizConfig, Environment environment); @Timed @ExceptionMetered @GET Response get();  }
@Test public void testStatusOk() throws Exception { when(registry.runHealthChecks()).thenReturn(new TreeMap<>()); Response r = status.get(); assertThat(r.getStatus()).isEqualTo(200); }
@Test public void testStatusWarn() throws Exception { TreeMap<String, HealthCheck.Result> map = new TreeMap<>(); map.put("test", HealthCheck.Result.unhealthy("failing")); when(registry.runHealthChecks()).thenReturn(map); Response r = status.get(); assertThat(r.getStatus()).isEqualTo(500); }