### Question:
MetricsHandler implements WebServiceHandler { @Override public Eventual<HttpResponse> handle(HttpRequest request, HttpInterceptor.Context context) { return this.urlMatcher.handle(request, context); } MetricsHandler(MetricRegistry metricRegistry, Optional<Duration> cacheExpiration); @Override Eventual<HttpResponse> handle(HttpRequest request, HttpInterceptor.Context context);  }### Answer:
@Test public void respondsToRequestWithJsonResponse() { HttpResponse response = Mono.from(handler.handle(get("/admin/metrics").build(), requestContext())).block(); assertThat(response.status(), is(OK)); assertThat(response.contentType().get(), is(JSON_UTF_8.toString())); }
@Test public void exposesRegisteredMetrics() { metricRegistry.counter("foo").inc(); HttpResponse response = Mono.from(handler.handle(get("/admin/metrics").build(), requestContext())).block(); assertThat(response.bodyAs(UTF_8), matchesRegex(quote("{\"version\":\"") + "\\d+\\.\\d+\\.\\d+" + quote("\",\"gauges\":{},\"counters\":{\"foo\":{\"count\":1}},\"histograms\":{},\"meters\":{},\"timers\":{}}"))); }
@Test public void canRequestMetricsBeginningWithPrefix() { metricRegistry.counter("foo.bar").inc(1); metricRegistry.counter("foo.bar.baz").inc(1); metricRegistry.counter("foo.barx").inc(1); HttpResponse response = Mono.from(handler.handle(get("/admin/metrics/foo.bar").build(), requestContext())).block(); assertThat(response.bodyAs(UTF_8), is("{\"foo.bar\":{\"count\":1},\"foo.bar.baz\":{\"count\":1}}")); }
@Test public void ifNoMetricsMatchNameThen404NotFoundIsReturned() { HttpResponse response = Mono.from(handler.handle(get("/admin/metrics/foo.bar").build(), requestContext())).block(); assertThat(response.status(), is(NOT_FOUND)); }
@Test public void canSearchForTermWithinMetricName() { metricRegistry.counter("foo.bar.a").inc(1); metricRegistry.counter("foo.bar.b").inc(1); metricRegistry.counter("baz.bar.foo").inc(1); metricRegistry.counter("foo.baz.a").inc(1); HttpResponse response = Mono.from(handler.handle(get("/admin/metrics/?filter=bar").build(), requestContext())).block(); assertThat(response.status(), is(OK)); assertThat(response.bodyAs(UTF_8), is("{" + "\"baz.bar.foo\":{\"count\":1}," + "\"foo.bar.a\":{\"count\":1}," + "\"foo.bar.b\":{\"count\":1}" + "}")); }
@Test public void canRequestMetricsBeginningWithPrefixAndSearchForTermTogether() { metricRegistry.counter("foo.bar.a").inc(1); metricRegistry.counter("foo.bar.b").inc(1); metricRegistry.counter("baz.bar.foo").inc(1); metricRegistry.counter("foo.baz.a").inc(1); metricRegistry.counter("foo.baz.a.bar").inc(1); HttpResponse response = Mono.from(handler.handle(get("/admin/metrics/foo?filter=bar").build(), requestContext())).block(); assertThat(response.status(), is(OK)); assertThat(response.bodyAs(UTF_8), is("{" + "\"foo.bar.a\":{\"count\":1}," + "\"foo.bar.b\":{\"count\":1}," + "\"foo.baz.a.bar\":{\"count\":1}" + "}")); }
@Test public void searchReturnsEmptyJsonObjectWhenThereAreNoResults() { metricRegistry.counter("foo.bar.a").inc(1); metricRegistry.counter("foo.bar.b").inc(1); HttpResponse response = Mono.from(handler.handle(get("/admin/metrics/?filter=notpresent").build(), requestContext())).block(); assertThat(response.status(), is(OK)); assertThat(response.bodyAs(UTF_8), is("{}")); }