MetricsConverter { public static Map<String, Map<String, Object>> convert(MetricsDump dump) { HashMap<String, Map<String, Object>> returnValue = new HashMap<>(); dump.metrics.entrySet().stream() .map(entry -> new Tuple3<>( entry.getValue().getClass().getSimpleName(), entry.getKey(), entry.getValue())) .map(tupleTypeAndNameAndMetric -> { Map<String, Object> map = toMap(tupleTypeAndNameAndMetric._3); map.put("type", tupleTypeAndNameAndMetric._1); if (dump.additionalInfo != null) { dump.additionalInfo.forEach(pair -> map.put(pair._1, pair._2)); } return new Pair<>(tupleTypeAndNameAndMetric._2, map); }) .forEach(metricNameMapPair -> returnValue.put(metricNameMapPair._1, metricNameMapPair._2)); return returnValue; } private  MetricsConverter(); static Map<String, Map<String, Object>> convert(MetricsDump dump);  }
@Test void nullDumpThrows() { assertThrows(NullPointerException.class, () -> MetricsConverter.convert(null)); }
@Test void nullAdditionalAttributesIsOk() { Nova nova = Nova.builder().build(); assertNotNull(MetricsConverter.convert(nova.metrics.dump())); }
@Test void metricsDumpConvertedAsExpected() { Nova nova = Nova.builder().build(); nova.metrics.addAdditionalInfoForDumps("key", "val"); MetricsDump dump = nova.metrics.dump(); Map<String, Map<String, Object>> dumpAsMap = MetricsConverter.convert(dump); dump.metrics.keySet().forEach(key -> assertTrue(dumpAsMap.containsKey(key.toString()))); assertThat(dumpAsMap.size(), is(dump.metrics.size() )); dump.metrics.keySet().forEach(metricName -> { String key = metricName.toString(); assertTrue(dumpAsMap.containsKey(key)); assertTrue(dumpAsMap.get(key).containsKey("type")); assertThat(dumpAsMap.get(key).get("key"), is("val")); }); }
@Test void additionalAttributesUsedForEveryMetric() { Nova nova = Nova.builder().build(); nova.metrics.addAdditionalInfoForDumps("key1", "value1"); nova.metrics.addAdditionalInfoForDumps("key2", "value2"); MetricsDump dump = nova.metrics.dump(); Map<String, Map<String, Object>> dumpAsMap = MetricsConverter.convert(dump); dumpAsMap.entrySet().stream() .filter(entry -> dump.metrics.keySet().contains(entry.getKey())) .forEach(entry -> { assertThat(entry.getValue().get("key1"), is("value1")); assertThat(entry.getValue().get("key2"), is("value2")); }); }