### Question:
JsonUtils { static byte[] encode( Map<String, String> commonTags, List<Measurement> measurements) throws IOException { ByteArrayOutputStream baos = new ByteArrayOutputStream(); JsonGenerator gen = FACTORY.createGenerator(baos); gen.writeStartArray(); Map<String, Integer> strings = buildStringTable(gen, commonTags, measurements); for (Measurement m : measurements) { appendMeasurement(gen, strings, commonTags, m.id(), m.value()); } gen.writeEndArray(); gen.close(); return baos.toByteArray(); } private  JsonUtils();   }### Answer:
@Test public void encodeNoCommonTags() throws Exception { List<Measurement> ms = new ArrayList<>(); ms.add(count(42.0, "test")); Map<Id, Delta> values = decode(JsonUtils.encode(Collections.emptyMap(), ms)); Assertions.assertEquals(1, values.size()); ms.forEach(m -> Assertions.assertEquals(42.0, values.get(m.id()).value, 1e-12)); }
@Test public void encodeCommonTags() throws Exception { Map<String, String> commonTags = new HashMap<>(); commonTags.put("a", "1"); commonTags.put("b", "2"); List<Measurement> ms = new ArrayList<>(); ms.add(count(42.0, "test")); Map<Id, Delta> values = decode(JsonUtils.encode(commonTags, ms)); Assertions.assertEquals(1, values.size()); ms.forEach(m -> { Id id = m.id().withTags(commonTags); Assertions.assertEquals(42.0, values.get(id).value, 1e-12); }); }
@Test public void encodeIgnoresNaN() throws Exception { List<Measurement> ms = new ArrayList<>(); ms.add(count(Double.NaN, "test")); Map<Id, Delta> values = decode(JsonUtils.encode(Collections.emptyMap(), ms)); Assertions.assertEquals(0, values.size()); }
@Test public void encodeIgnoresAdd0() throws Exception { List<Measurement> ms = new ArrayList<>(); ms.add(count(0, "test")); Map<Id, Delta> values = decode(JsonUtils.encode(Collections.emptyMap(), ms)); Assertions.assertEquals(0, values.size()); }
@Test public void encodeSendsMax0() throws Exception { List<Measurement> ms = new ArrayList<>(); ms.add(max(0, "test")); Map<Id, Delta> values = decode(JsonUtils.encode(Collections.emptyMap(), ms)); Assertions.assertEquals(1, values.size()); ms.forEach(m -> { Id id = m.id(); Assertions.assertEquals(0.0, values.get(id).value, 1e-12); }); }
@Test public void encodeInvalidStatisticAsGauge() throws Exception { List<Measurement> ms = new ArrayList<>(); ms.add(count(42, "test", "statistic", "foo")); Map<Id, Delta> values = decode(JsonUtils.encode(Collections.emptyMap(), ms)); Assertions.assertEquals(1, values.size()); ms.forEach(m -> { Id id = m.id(); Assertions.assertEquals(values.get(id).op, 10); }); }
@Test public void encodeAssumesNoStatisticIsGauge() throws Exception { List<Measurement> ms = new ArrayList<>(); ms.add(unknown(42, "test")); Map<Id, Delta> values = decode(JsonUtils.encode(Collections.emptyMap(), ms)); Assertions.assertEquals(1, values.size()); ms.forEach(m -> { Id id = m.id(); Assertions.assertEquals(values.get(id).op, 10); }); }
@Test public void encodeSupportsKnownStats() throws Exception { for (Statistic stat : Statistic.values()) { List<Measurement> ms = new ArrayList<>(); ms.add(count(42, "test", "statistic", stat.value())); Map<Id, Delta> values = decode(JsonUtils.encode(Collections.emptyMap(), ms)); Assertions.assertEquals(1, values.size()); ms.forEach(m -> { Id id = m.id(); Assertions.assertEquals(42.0, values.get(id).value, 1e-12); }); } }