### Question:
JsonMetadataExporter implements Exporter { @Override public StringBuilder exportMetricsByName(MetricRegistry.Type scope, String name) { MetricRegistry registry = MetricRegistries.get(scope); if (registry == null) { return null; } Metadata metadata = registry.getMetadata().get(name); if (metadata == null) { return null; } JsonObjectBuilder builder = JsonProviderHolder.get().createObjectBuilder(); metricJSON(builder, name, metadata, getKnownTagsByMetricName(registry, name)); return stringify(builder.build()); } JsonMetadataExporter(); @Override String getContentType(); @Override StringBuilder exportOneScope(MetricRegistry.Type scope); @Override StringBuilder exportAllScopes(); @Override StringBuilder exportOneMetric(MetricRegistry.Type scope, MetricID metricID); @Override StringBuilder exportMetricsByName(MetricRegistry.Type scope, String name);  }### Answer:
@Test public void exportByMetricNameWithOneMetricSingleTag() { JsonMetadataExporter exporter = new JsonMetadataExporter(); MetricRegistry applicationRegistry = MetricRegistries.get(MetricRegistry.Type.APPLICATION); applicationRegistry.counter("counter1", new Tag("key1", "value1")); String result = exporter.exportMetricsByName(MetricRegistry.Type.APPLICATION, "counter1").toString(); JsonObject json = Json.createReader(new StringReader(result)).read().asJsonObject(); JsonArray outerTagsArray = json.getJsonObject("counter1").getJsonArray("tags"); assertEquals(1, outerTagsArray.size()); JsonArray innerArray = outerTagsArray.getJsonArray(0); assertEquals(1, innerArray.size()); assertEquals("key1=value1", innerArray.getString(0)); }
@Test public void exportByMetricNameWithOneMetricMultipleTags() { JsonMetadataExporter exporter = new JsonMetadataExporter(); MetricRegistry applicationRegistry = MetricRegistries.get(MetricRegistry.Type.APPLICATION); applicationRegistry.counter("counter1", new Tag("key1", "value1"), new Tag("color", "blue")); String result = exporter.exportMetricsByName(MetricRegistry.Type.APPLICATION, "counter1").toString(); JsonObject json = Json.createReader(new StringReader(result)).read().asJsonObject(); JsonArray outerTagsArray = json.getJsonObject("counter1").getJsonArray("tags"); assertEquals(1, outerTagsArray.size()); JsonArray innerArray = outerTagsArray.getJsonArray(0); assertEquals(2, innerArray.size()); assertEquals("color=blue", innerArray.getString(0)); assertEquals("key1=value1", innerArray.getString(1)); }
@Test public void exportByMetricNameWithMultipleMetrics() { JsonMetadataExporter exporter = new JsonMetadataExporter(); MetricRegistry applicationRegistry = MetricRegistries.get(MetricRegistry.Type.APPLICATION); applicationRegistry.counter("counter1", new Tag("key1", "value1")); applicationRegistry.counter("counter1", new Tag("key1", "value2")); applicationRegistry.counter("counter1", new Tag("key1", "value3")); String result = exporter.exportMetricsByName(MetricRegistry.Type.APPLICATION, "counter1").toString(); JsonObject json = Json.createReader(new StringReader(result)).read().asJsonObject(); JsonArray outerTagsArray = json.getJsonObject("counter1").getJsonArray("tags"); assertEquals(3, outerTagsArray.size()); JsonArray innerArray1 = outerTagsArray.getJsonArray(0); assertEquals(1, innerArray1.size()); assertEquals("key1=value1", innerArray1.getString(0)); JsonArray innerArray2 = outerTagsArray.getJsonArray(1); assertEquals(1, innerArray2.size()); assertEquals("key1=value2", innerArray2.getString(0)); JsonArray innerArray3 = outerTagsArray.getJsonArray(2); assertEquals(1, innerArray3.size()); assertEquals("key1=value3", innerArray3.getString(0)); }