|
@@ -0,0 +1,172 @@
|
|
|
|
|
+package com.zksy.cockpit.collectionRecords;
|
|
|
|
|
+
|
|
|
|
|
+import com.zksy.base.alarm.domain.WarningThreshold;
|
|
|
|
|
+import com.zksy.base.alarm.mapper.WarningThresholdMapper;
|
|
|
|
|
+import com.zksy.cockpit.collectionRecords.CockpitCollectionRecordsResponse.Assessment;
|
|
|
|
|
+import org.junit.jupiter.api.Test;
|
|
|
|
|
+
|
|
|
|
|
+import java.util.Arrays;
|
|
|
|
|
+import java.util.Collections;
|
|
|
|
|
+import java.util.HashMap;
|
|
|
|
|
+import java.util.Map;
|
|
|
|
|
+
|
|
|
|
|
+import static org.junit.jupiter.api.Assertions.*;
|
|
|
|
|
+import static org.mockito.ArgumentMatchers.any;
|
|
|
|
|
+import static org.mockito.Mockito.*;
|
|
|
|
|
+
|
|
|
|
|
+class CollectionRecordAssessmentServiceTest {
|
|
|
|
|
+ private final WarningThresholdMapper mapper = mock(WarningThresholdMapper.class);
|
|
|
|
|
+ private final CollectionRecordAssessmentService service = new CollectionRecordAssessmentService(mapper);
|
|
|
|
|
+
|
|
|
|
|
+ @Test
|
|
|
|
|
+ void inclusiveAlarmBoundaryAndEveryDeviationLevelKeepStatusAndLevelColorsSeparate() {
|
|
|
|
|
+ double[] values = {100, 109.99, 110, 130, 160};
|
|
|
|
|
+ int[] levels = {4, 4, 3, 2, 1};
|
|
|
|
|
+ String[] colors = {"GREEN", "GREEN", "BLUE", "YELLOW", "RED"};
|
|
|
|
|
+ for (int i = 0; i < values.length; i++) {
|
|
|
|
|
+ Assessment a = assess("radar_data", "water_level", values[i], "WARN-WATER-LEVEL", 0.0, 100.0);
|
|
|
|
|
+ assertAssessment(a, "ALARM", levels[i], "THRESHOLD_REACHED");
|
|
|
|
|
+ assertEquals(colors[i], a.getLevelColor());
|
|
|
|
|
+ }
|
|
|
|
|
+ assertAssessment(assess("radar_data", "water_level", 0, "WARN-WATER-LEVEL", 0.0, 100.0), "ALARM", 4, "THRESHOLD_REACHED");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Test
|
|
|
|
|
+ void warningBandAndApproachLevelsMatchSharedRuleIncludingLowerSide() {
|
|
|
|
|
+ double[] values = {70, 82, 89.5, 95.5};
|
|
|
|
|
+ int[] levels = {4, 3, 2, 1};
|
|
|
|
|
+ for (int i = 0; i < values.length; i++) {
|
|
|
|
|
+ assertAssessment(assess("radar_data", "water_level", values[i], "WARN-WATER-LEVEL", 0.0, 100.0), "WARNING", levels[i], "NEAR_THRESHOLD");
|
|
|
|
|
+ assertAssessment(assess("radar_data", "water_level", 100 - values[i], "WARN-WATER-LEVEL", 0.0, 100.0), "WARNING", levels[i], "NEAR_THRESHOLD");
|
|
|
|
|
+ }
|
|
|
|
|
+ assertAssessment(assess("radar_data", "water_level", 50, "WARN-WATER-LEVEL", 0.0, 100.0), "NORMAL", null, "WITHIN_THRESHOLD");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Test
|
|
|
|
|
+ void gasIgnoresConfiguredLowerBoundAndUsesZeroOnlyForAlarmLevel() {
|
|
|
|
|
+ assertAssessment(assess("gas_monitor_data", "gas_concentration", 0, "WARN-GAS-CONCENTRATION", 10.0, 100.0), "NORMAL", null, "WITHIN_THRESHOLD");
|
|
|
|
|
+ assertAssessment(assess("gas_monitor_data", "gas_concentration", 130, "WARN-GAS-CONCENTRATION", 10.0, 100.0), "ALARM", 2, "THRESHOLD_REACHED");
|
|
|
|
|
+ assertAssessment(assess("gas_monitor_data", "gas_concentration", 95.5, "WARN-GAS-CONCENTRATION", 10.0, 100.0), "WARNING", 1, "NEAR_THRESHOLD");
|
|
|
|
|
+ assertAssessment(assess("gas_monitor_data", "gas_concentration", 1, "WARN-GAS-CONCENTRATION", 10.0, null), "UNKNOWN", null, "NO_THRESHOLD");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Test
|
|
|
|
|
+ void firePressureSingleSideUsesCollectorBaselinesButTelemetryDoesNotInventThem() {
|
|
|
|
|
+ assertAssessment(assess("firefighting_pressure", "pressure_value", 160, "WARN-FIRE-PRESSURE", null, 100.0), "ALARM", 1, "THRESHOLD_REACHED");
|
|
|
|
|
+ assertAssessment(assess("firefighting_pressure", "pressure_value", 4, "WARN-FIRE-PRESSURE", 10.0, null), "ALARM", 1, "THRESHOLD_REACHED");
|
|
|
|
|
+ assertAssessment(assess("firefighting_pressure", "pressure_value", 10.45, "WARN-FIRE-PRESSURE", 10.0, null), "WARNING", 1, "NEAR_THRESHOLD");
|
|
|
|
|
+ assertAssessment(assess("telemetry_data", "pressure", 160, "WARN-PRESSURE", null, 100.0), "ALARM", 4, "THRESHOLD_REACHED");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Test
|
|
|
|
|
+ void nullMalformedAndAbsentThresholdValuesNeverBecomeNormal() {
|
|
|
|
|
+ assertAssessment(assess("radar_data", "water_level", null, "WARN-WATER-LEVEL", 0.0, 100.0), "UNKNOWN", null, "MISSING_VALUE");
|
|
|
|
|
+ for (Object value : new Object[]{"bad", "NaN", Double.POSITIVE_INFINITY, "1 MPa"}) {
|
|
|
|
|
+ assertAssessment(assess("radar_data", "water_level", value, "WARN-WATER-LEVEL", 0.0, 100.0), "UNKNOWN", null, "INVALID_VALUE");
|
|
|
|
|
+ }
|
|
|
|
|
+ assertAssessment(assess("radar_data", "water_level", 1, "WARN-WATER-LEVEL", null, null), "UNKNOWN", null, "NO_THRESHOLD");
|
|
|
|
|
+ assertAssessment(assess("radar_data", "water_level", 1, "WARN-WATER-LEVEL", 100.0, 0.0), "UNKNOWN", null, "INVALID_THRESHOLD");
|
|
|
|
|
+ assertAssessment(assess("radar_data", "water_level", 1, "WARN-WATER-LEVEL", 0.0, Double.NaN), "UNKNOWN", null, "INVALID_THRESHOLD");
|
|
|
|
|
+ assertAssessment(service.assess("radar_data", "water_level", Collections.singletonMap("water_level", 1), Collections.emptyMap()), "UNKNOWN", null, "NO_THRESHOLD");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Test
|
|
|
|
|
+ void environmentNodeBindsTheRealRawValueNotDisplayOrOtherChannels() {
|
|
|
|
|
+ Map<String, WarningThreshold> thresholds = new HashMap<>();
|
|
|
|
|
+ thresholds.put("WARN-SUSPENDED-SOLIDS", threshold("WARN-SUSPENDED-SOLIDS", 0.0, 100.0));
|
|
|
|
|
+ thresholds.put("WARN-COD", threshold("WARN-COD", 0.0, 100.0));
|
|
|
|
|
+ thresholds.put("WARN-AMMONIA-NITROGEN", threshold("WARN-AMMONIA-NITROGEN", 0.0, 200.0));
|
|
|
|
|
+ Map<String, Object> raw = new HashMap<>();
|
|
|
|
|
+ raw.put("node_id", 1); raw.put("float_value", 160); raw.put("hum", 50); raw.put("temperature", 999);
|
|
|
|
|
+ assertAssessment(service.assess("e_real_time_data", "indicator", raw, thresholds), "ALARM", 1, "THRESHOLD_REACHED");
|
|
|
|
|
+ raw.put("node_id", new java.math.BigDecimal("2.0")); raw.put("hum", 95.5);
|
|
|
|
|
+ assertAssessment(service.assess("e_real_time_data", "indicator", raw, thresholds), "WARNING", 1, "NEAR_THRESHOLD");
|
|
|
|
|
+ raw.put("node_id", 3);
|
|
|
|
|
+ assertAssessment(service.assess("e_real_time_data", "indicator", raw, thresholds), "NORMAL", null, "WITHIN_THRESHOLD");
|
|
|
|
|
+ raw.put("node_id", 99);
|
|
|
|
|
+ assertAssessment(service.assess("e_real_time_data", "indicator", raw, thresholds), "UNKNOWN", null, "METRIC_UNMAPPED");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Test
|
|
|
|
|
+ void manholeFlagsAreIndependentAndTiltUsesItsOwnFlagNotItsAngle() {
|
|
|
|
|
+ Map<String, Object> raw = new HashMap<>();
|
|
|
|
|
+ raw.put("tilt_angle", 0); raw.put("alarm_status", "1");
|
|
|
|
|
+ raw.put("water_infiltration_alarm_status", 0); raw.put("water_level_alarm_status", 2);
|
|
|
|
|
+ assertAssessment(service.assess("jg_device_data", "tilt_angle", raw, Collections.emptyMap()), "ALARM", 1, "REPORTED_ALARM");
|
|
|
|
|
+ assertAssessment(service.assess("jg_device_data", "water_infiltration_alarm_status", raw, Collections.emptyMap()), "NORMAL", null, "REPORTED_NORMAL");
|
|
|
|
|
+ assertAssessment(service.assess("jg_device_data", "water_level_alarm_status", raw, Collections.emptyMap()), "UNKNOWN", null, "INVALID_STATUS");
|
|
|
|
|
+ raw.remove("alarm_status");
|
|
|
|
|
+ assertAssessment(service.assess("jg_device_data", "tilt_angle", raw, Collections.emptyMap()), "UNKNOWN", null, "MISSING_STATUS");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Test
|
|
|
|
|
+ void unrelatedNumericAndAudioCellsAreExplicitlyUnknown() {
|
|
|
|
|
+ assertAssessment(assess("gas_monitor_data", "temperature", 99, "WARN-GAS-CONCENTRATION", 0.0, 10.0), "UNKNOWN", null, "METRIC_UNMAPPED");
|
|
|
|
|
+ assertAssessment(service.assess("noise_info", "filepath", Collections.emptyMap(), Collections.emptyMap()), "UNKNOWN", null, "NOT_APPLICABLE");
|
|
|
|
|
+ assertAssessment(assess("noise_info", "rms", 99, "WARN-LEAK", 1.0, 10.0), "UNKNOWN", null, "COMPOSITE_RULE_REQUIRED");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Test
|
|
|
|
|
+ void timeSelectionKeepsCurrentCollectorPrecedenceAndFirstRowFallback() {
|
|
|
|
|
+ WarningThreshold full = threshold("x", 0.0, 100.0);
|
|
|
|
|
+ WarningThreshold morning = threshold("x", 0.0, 10.0);
|
|
|
|
|
+ morning.setPeriodStart("08:00"); morning.setPeriodEnd("10:00");
|
|
|
|
|
+ for (String time : new String[]{"08:00", "09:00", "10:00"}) {
|
|
|
|
|
+ assertSame(morning, CollectionRecordAssessmentService.selectThresholds(Arrays.asList(full, morning), time).get("x"));
|
|
|
|
|
+ }
|
|
|
|
|
+ assertSame(full, CollectionRecordAssessmentService.selectThresholds(Arrays.asList(morning, full), "12:00").get("x"));
|
|
|
|
|
+ assertSame(morning, CollectionRecordAssessmentService.selectThresholds(Collections.singletonList(morning), "12:00").get("x"));
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Test
|
|
|
|
|
+ void oneBatchReadFeedsFiftyRowsWithoutAnyWritesOrPerCellQueries() {
|
|
|
|
|
+ when(mapper.selectList(any())).thenReturn(Collections.singletonList(threshold("WARN-WATER-LEVEL", 0.0, 100.0)));
|
|
|
|
|
+ Map<String, WarningThreshold> thresholds = service.loadThresholds("device-1");
|
|
|
|
|
+ for (int i = 0; i < 50; i++) service.assess("radar_data", "water_level", Collections.singletonMap("water_level", i), thresholds);
|
|
|
|
|
+ verify(mapper).selectList(any());
|
|
|
|
|
+ verifyNoMoreInteractions(mapper);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Test
|
|
|
|
|
+ void unavailableRulesAreDistinctFromEmptyConfigurationAndCannotReuseThresholdValues() {
|
|
|
|
|
+ Map<String, Object> raw = Collections.singletonMap("water_level", 100);
|
|
|
|
|
+ Map<String, WarningThreshold> configured = Collections.singletonMap("WARN-WATER-LEVEL", threshold("WARN-WATER-LEVEL", 0.0, 100.0));
|
|
|
|
|
+ assertAssessment(service.assess("radar_data", "water_level", raw, configured, false), "UNKNOWN", null, "RULE_UNAVAILABLE");
|
|
|
|
|
+ assertAssessment(service.assess("radar_data", "water_level", raw, Collections.emptyMap(), true), "UNKNOWN", null, "NO_THRESHOLD");
|
|
|
|
|
+ assertAssessment(service.assess("radar_data", "water_level", raw, null, true), "UNKNOWN", null, "NO_THRESHOLD");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Test
|
|
|
|
|
+ void configurationOutageDoesNotAffectReportedStatusesOrNonApplicableAudio() {
|
|
|
|
|
+ Map<String, Object> raw = new HashMap<>();
|
|
|
|
|
+ raw.put("alarm_status", "1"); raw.put("water_infiltration_alarm_status", "0");
|
|
|
|
|
+ assertAssessment(service.assess("jg_device_data", "tilt_angle", raw, Collections.emptyMap(), false), "ALARM", 1, "REPORTED_ALARM");
|
|
|
|
|
+ assertAssessment(service.assess("jg_device_data", "water_infiltration_alarm_status", raw, Collections.emptyMap(), false), "NORMAL", null, "REPORTED_NORMAL");
|
|
|
|
|
+ assertAssessment(service.assess("noise_info", "filepath", raw, Collections.emptyMap(), false), "UNKNOWN", null, "NOT_APPLICABLE");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Test
|
|
|
|
|
+ void nullOrEmptyConfigurationAndUnknownNodeNeverDereferenceNullKeys() {
|
|
|
|
|
+ when(mapper.selectList(any())).thenReturn(null);
|
|
|
|
|
+ assertTrue(service.loadThresholds("device-1").isEmpty());
|
|
|
|
|
+ assertTrue(CollectionRecordAssessmentService.selectThresholds(Collections.emptyList(), "12:00").isEmpty());
|
|
|
|
|
+ Map<String, Object> raw = new HashMap<>(); raw.put("hum", 1);
|
|
|
|
|
+ for (Object node : new Object[]{null, 99, "bad", "2.5"}) {
|
|
|
|
|
+ raw.put("node_id", node);
|
|
|
|
|
+ assertAssessment(service.assess("e_real_time_data", "indicator", raw, Collections.emptyMap()), "UNKNOWN", null, "METRIC_UNMAPPED");
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private Assessment assess(String table, String key, Object value, String code, Double min, Double max) {
|
|
|
|
|
+ return service.assess(table, key, Collections.singletonMap(key, value), Collections.singletonMap(code, threshold(code, min, max)));
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private static WarningThreshold threshold(String code, Double min, Double max) {
|
|
|
|
|
+ WarningThreshold t = new WarningThreshold(); t.setWarningCode(code); t.setMinValue(min); t.setMaxValue(max); return t;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private static void assertAssessment(Assessment actual, String status, Integer level, String reason) {
|
|
|
|
|
+ assertEquals(status, actual.getStatus()); assertEquals(level, actual.getLevel()); assertEquals(reason, actual.getReason());
|
|
|
|
|
+ if (level == null) assertNull(actual.getLevelColor());
|
|
|
|
|
+ }
|
|
|
|
|
+}
|