### Question:
MetricsScraper { Map<String, Object> scrape(MBeanSelector selector, JsonObject response) { metrics = new HashMap<>(); scrapeItem(response, selector, globalQualifiers); return metrics; } MetricsScraper(String globalQualifiers);   }### Answer:
@Test public void whenResponseLacksServerRuntimes_generateEmptyMetrics() { assertThat(scraper.scrape(MBeanSelector.create(getFullMap()), getJsonResponse("{}")), anEmptyMap()); }
@Test public void generateFromFullResponse() { Map<String, Object> metrics = scraper.scrape(MBeanSelector.create(getFullMap()), getJsonResponse(RESPONSE)); assertThat(metrics, hasMetric("component_deploymentState{application=\"weblogic\",component=\"ejb30_weblogic\"}", 2)); assertThat(metrics, hasMetric("servlet_invocationTotalCount{application=\"weblogic\",component=\"ejb30_weblogic\",servletName=\"JspServlet\"}", 0)); }
@Test public void whenTypeNotSpecified_includeAllComponents() { Map<String, Object> metrics = scraper.scrape(MBeanSelector.create(getFullMap()), getJsonResponse(RESPONSE)); assertThat(metrics, hasMetric("component_deploymentState{application=\"weblogic\",component=\"ejb30_weblogic\"}", 2)); assertThat(metrics, hasMetric("component_deploymentState{application=\"mbeans\",component=\"EjbStatusBean\"}", 2)); }
@Test public void selectOnlyWebApps() { componentMap.put(MBeanSelector.TYPE, "WebAppComponentRuntime"); Map<String, Object> metrics = scraper.scrape(MBeanSelector.create(getFullMap()), getJsonResponse(RESPONSE)); assertThat(metrics, hasMetric("component_deploymentState{application=\"weblogic\",component=\"ejb30_weblogic\"}", 2)); assertThat(metrics, not(hasMetric("component_deploymentState{application=\"mbeans\",component=\"EjbStatusBean\"}", 2))); }
@Test public void whenValuesAtTopLevel_scrapeThem() { final MBeanSelector selector = MBeanSelector.create(getMetricsMap()); final JsonObject jsonResponse = getJsonResponse(METRICS_RESPONSE); Map<String, Object> metrics = scraper.scrape(selector, jsonResponse); assertThat(metrics, hasMetric("heapSizeCurrent", 123456)); assertThat(metrics, hasMetric("processCpuLoad", .0028)); }