DetectorMapper { public void detectorMappingCacheSync(long currentTime) { long updateDurationInSeconds = (currentTime - syncedUpTillTime) / 1000; if (updateDurationInSeconds <= 0) { return; } List<DetectorMapping> detectorMappings = detectorSource.findUpdatedDetectorMappings(updateDurationInSeconds); List<DetectorMapping> disabledDetectorMappings = detectorMappings.stream() .filter(dt -> !dt.isEnabled()) .collect(Collectors.toList()); if (!disabledDetectorMappings.isEmpty()) { cache.removeDisabledDetectorMappings(disabledDetectorMappings); log.info("Removing disabled mapping: {}", disabledDetectorMappings); } List<DetectorMapping> newDetectorMappings = detectorMappings.stream() .filter(DetectorMapping::isEnabled) .collect(Collectors.toList()); if (!newDetectorMappings.isEmpty()) { cache.invalidateMetricsWithOldDetectorMappings(newDetectorMappings); log.info("Invalidating metrics for modified mappings: {}", newDetectorMappings); } syncedUpTillTime = currentTime; } DetectorMapper(DetectorSource detectorSource, DetectorMapperCache cache, int detectorCacheUpdateTimePeriod);  DetectorMapper(DetectorSource detectorSource, Config config, MetricRegistry metricRegistry); int optimalBatchSize(); List<Detector> getDetectorsFromCache(MetricDefinition metricDefinition); boolean isSuccessfulDetectorMappingLookup(List<Map<String, String>> cacheMissedMetricTags); void detectorMappingCacheSync(long currentTime);  }
@Test public void detectorCacheUpdateTest() { DetectorMapping disabledDetectorMapping = new DetectorMapping().setDetector(buildDetector("cid", "2c49ba26-1a7d-43f4-b70c-c6644a2c1689")).setEnabled(false); DetectorMapping modifiedDetectorMapping = new DetectorMapping().setDetector(buildDetector("cid", "4d49ba26-1a7d-43f4-b70c-ee644a2c1689")).setEnabled(true); List<DetectorMapping> updateDetectorMappings = Arrays.asList(disabledDetectorMapping, modifiedDetectorMapping); when(detectorSource.findUpdatedDetectorMappings(60)).thenReturn(updateDetectorMappings); doAnswer((e) -> null).when(cache).removeDisabledDetectorMappings(anyList()); doAnswer((e) -> null).when(cache).invalidateMetricsWithOldDetectorMappings(anyList()); detectorMapper.detectorMappingCacheSync(System.currentTimeMillis() + 60000); verify(cache).removeDisabledDetectorMappings(Collections.singletonList(disabledDetectorMapping)); verify(cache).invalidateMetricsWithOldDetectorMappings(Collections.singletonList(modifiedDetectorMapping)); }