DetectorManager { public DetectorResult detect(@NonNull MappedMetricData mappedMetricData) { try { MDC.put("DetectorUuid", mappedMetricData.getDetectorUuid().toString()); checkMappedMetricData(mappedMetricData); Optional<DetectorContainer> container = getDetector(mappedMetricData); if (container.isPresent()) { Optional<DetectorResult> optionalDetectorResult = doDetection(container.get(), mappedMetricData.getMetricData()); return optionalDetectorResult.orElse(null); } else { return null; } } finally { MDC.remove("DetectorUuid"); } } DetectorManager(DetectorSource detectorSource,
                           DataInitializer dataInitializer,
                           Config config,
                           Map<UUID, DetectorContainer> cachedDetectors,
                           MetricRegistry metricRegistry);  DetectorManager(DetectorSource detectorSource,
                           DataInitializer dataInitializer,
                           Config config,
                           MetricRegistry metricRegistry); DetectorResult detect(@NonNull MappedMetricData mappedMetricData); Meter getDetectorAndLevelMeter(String name, AnomalyLevel anomalyLevel);  }
@Test public void testClassify() { val result = managerUnderTest.detect(goodMappedMetricData); assertNotNull(result); assertSame(outlierDetectorResult, result); verify(cachedDetectors, atLeastOnce()).put(any(UUID.class), any(DetectorContainer.class)); }
@Test public void testClassify_proceedsWhenDataInitFails() { val result = managerUnderTest.detect(goodMappedMetricDataWithBadDataInit); assertNotNull(result); assertSame(outlierDetectorResult, result); verify(cachedDetectors, atLeastOnce()).put(any(UUID.class), any(DetectorContainer.class)); }
@Test public void testClassify_skipsWhenDataInitThrottled() { val result = managerUnderTest.detect(goodMappedMetricDataWithThrottledDataInit); assertNull(result); verify(cachedDetectors, never()).put(any(UUID.class), any(DetectorContainer.class)); verify(detector, never()).detect(any(MetricData.class)); }
@Test public void testClassify_getCached() { managerUnderTest.detect(goodMappedMetricData); managerUnderTest.detect(goodMappedMetricData); }
@Test public void testClassify_returnsNullWhenDetectorErrors() { doThrow(new RuntimeException("Some Detector Failure")).when(detector).detect(goodMetricData); DetectorResult detectorResult = managerUnderTest.detect(goodMappedMetricData); assertNull(detectorResult); }
@Test public void testClassifyMetricThatCantBeFound() { val result = managerUnderTest.detect(badMappedMetricData); assertNull(result); }
@Test(expected = NullPointerException.class) public void testMetricDataCantBeNull() { managerUnderTest.detect(null); }