AnomalyToMetricMapper { public MetricData toMetricData(MappedMetricData anomaly) { if (anomaly == null) { return null; } val metricData = anomaly.getMetricData(); val metricDef = metricData.getMetricDefinition(); val tags = metricDef.getTags(); val kvTags = tags.getKv(); if (kvTags.containsKey(AA_DETECTOR_UUID) || kvTags.containsKey(AA_ANOMALY_LEVEL)) { return null; } val detectorUuid = anomaly.getDetectorUuid(); val outlierResult = (OutlierDetectorResult) anomaly.getAnomalyResult(); val outlierLevel = outlierResult.getAnomalyLevel(); AssertUtil.notNull(detectorUuid, "detectorUuid can't be null"); AssertUtil.notNull(outlierLevel, "anomalyLevel can't be null"); val newKVTags = new HashMap<>(kvTags); newKVTags.put(AA_DETECTOR_UUID, detectorUuid.toString()); newKVTags.put(AA_ANOMALY_LEVEL, outlierLevel.toString()); val newKey = metricDef.getKey(); val newTags = new TagCollection(newKVTags, Collections.EMPTY_SET); val newMeta = new TagCollection(Collections.EMPTY_MAP); val newMetricDef = new MetricDefinition(newKey, newTags, newMeta); val value = metricData.getValue(); val timestamp = metricData.getTimestamp(); return new MetricData(newMetricDef, value, timestamp); }  MetricData toMetricData(MappedMetricData anomaly); static String AA_DETECTOR_UUID; static String AA_ANOMALY_LEVEL; }
@Test public void testToMetricData_stringMetricKey() { val actualMetric = mapperUnderTest.toMetricData(anomalyWithStringMetricKey); val actualMetricDef = actualMetric.getMetricDefinition(); val actualTags = actualMetricDef.getTags(); val actualKvTags = actualTags.getKv(); val detectorUuid = anomalyWithStringMetricKey.getDetectorUuid().toString(); val outlierResult = (OutlierDetectorResult) anomalyWithStringMetricKey.getAnomalyResult(); val outlierLevel = outlierResult.getAnomalyLevel(); assertEquals(detectorUuid, actualKvTags.get(AnomalyToMetricMapper.AA_DETECTOR_UUID)); assertEquals(outlierLevel.toString(), actualKvTags.get(AnomalyToMetricMapper.AA_ANOMALY_LEVEL)); }
@Test public void testToMetricDataMapsNullToNull() { assertNull(mapperUnderTest.toMetricData(null)); }