MOfNAggregationFilter implements DetectionFilter { @Override public void doFilter(@NonNull DetectorRequest detectorRequest, @NonNull DetectorResponse detectorResponse, @NonNull DetectionFilterChain chain) { chain.doFilter(detectorRequest, detectorResponse); DetectorResult result = detectorResponse.getDetectorResult(); if (result instanceof OutlierDetectorResult) { buffer[bufferIndex++] = result.getAnomalyLevel(); if (bufferIndex >= getN()) { bufferIndex = 0; } val outlierDetectorResult = (OutlierDetectorResult) result; val aggregatedResult = new OutlierDetectorResult() .setAnomalyLevel(result.getAnomalyLevel()) .setPredicted(outlierDetectorResult.getPredicted()) .setThresholds(outlierDetectorResult.getThresholds()) .setTrusted(result.isTrusted()); if (numAnomalies() >= getM()) { aggregatedResult.setAnomalyLevel(AnomalyLevel.STRONG); } detectorResponse.setDetectorResult(aggregatedResult); } else { log.warn(String.format("%s can only be used with %s types of DetectorResults. Skipping aggregation.", this.getClass().getSimpleName(), OutlierDetectorResult.class.getSimpleName())); chain.doFilter(detectorRequest, detectorResponse); } } @JsonCreator  MOfNAggregationFilter(@JsonProperty("m") int m, @JsonProperty("n") int n); @Override void doFilter(@NonNull DetectorRequest detectorRequest,
                         @NonNull DetectorResponse detectorResponse,
                         @NonNull DetectionFilterChain chain);  }
@Test public void testFilter_ignoresNonOutlierResult() { val nonOutlierResponse = detectorResponse(mockNonOutlierResult); new MOfNAggregationFilter(3, 5).doFilter(detectorRequest, nonOutlierResponse, mockFilterChain); assertSame(mockNonOutlierResult, nonOutlierResponse.getDetectorResult()); }
@Test(expected = NullPointerException.class) public void testFilter_nullDetectorRequest() { new MOfNAggregationFilter(3, 5).doFilter(null, detectorResponse(mockNonOutlierResult), mockFilterChain); }
@Test(expected = NullPointerException.class) public void testFilter_nullDetectorResponse() { new MOfNAggregationFilter(3, 5).doFilter(detectorRequest, null, mockFilterChain); }
@Test(expected = NullPointerException.class) public void testFilter_nullFilterChain() { new MOfNAggregationFilter(3, 5).doFilter(detectorRequest, detectorResponse(mockNonOutlierResult), null); }
@Test public void testFilter_nullResponseArgument() { try { new MOfNAggregationFilter(3, 5).doFilter(detectorRequest, null, mockFilterChain); fail("Expected NullPointerException to be thrown"); } catch (NullPointerException e) { assertEquals("detectorResponse is marked non-null but is null", e.getMessage()); } }