|
|
@@ -0,0 +1,196 @@
|
|
|
+package com.zksy.cockpit.event;
|
|
|
+
|
|
|
+import com.baomidou.mybatisplus.core.conditions.Wrapper;
|
|
|
+import com.zksy.base.alarm.domain.AlarmData;
|
|
|
+import com.zksy.base.alarm.service.AlarmDataService;
|
|
|
+import com.zksy.base.domain.EquipmentBase;
|
|
|
+import com.zksy.base.domain.EquipmentType;
|
|
|
+import com.zksy.base.service.EquipmentBaseService;
|
|
|
+import com.zksy.base.service.EquipmentTypeService;
|
|
|
+import com.zksy.base.service.MonitorPointService;
|
|
|
+import com.zksy.base.warning.domain.EarlyWarning;
|
|
|
+import com.zksy.base.warning.mapper.AlarmWarningRelMapper;
|
|
|
+import com.zksy.base.warning.mapper.EquipmentWarningTypeMapper;
|
|
|
+import com.zksy.base.warning.mapper.WarningTypeMapper;
|
|
|
+import com.zksy.base.warning.service.EarlyWarningService;
|
|
|
+import com.zksy.common.core.domain.entity.SysDictData;
|
|
|
+import com.zksy.ops.service.WorkOrderService;
|
|
|
+import com.zksy.system.mapper.SysDictDataMapper;
|
|
|
+import org.junit.jupiter.api.Test;
|
|
|
+
|
|
|
+import java.time.Clock;
|
|
|
+import java.time.Instant;
|
|
|
+import java.time.LocalDateTime;
|
|
|
+import java.time.ZoneOffset;
|
|
|
+import java.util.Arrays;
|
|
|
+import java.util.Collections;
|
|
|
+import java.util.List;
|
|
|
+
|
|
|
+import static org.junit.jupiter.api.Assertions.assertEquals;
|
|
|
+import static org.junit.jupiter.api.Assertions.assertNotNull;
|
|
|
+import static org.junit.jupiter.api.Assertions.assertNull;
|
|
|
+import static org.mockito.ArgumentMatchers.any;
|
|
|
+import static org.mockito.Mockito.mock;
|
|
|
+import static org.mockito.Mockito.times;
|
|
|
+import static org.mockito.Mockito.verify;
|
|
|
+import static org.mockito.Mockito.when;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 设备详情事件适配器:等级名称按族取字典(预警 alert_level、报警 alarm_level),
|
|
|
+ * 缺项回退既有「N级预警 / N级报警」,且每族每请求只查一次字典。
|
|
|
+ */
|
|
|
+class BusinessCockpitEventAdapterLevelNameTest {
|
|
|
+ private static final String DEVICE_CODE = "TC-PS-WS-202609020012";
|
|
|
+
|
|
|
+ private final EquipmentBaseService equipmentBaseService = mock(EquipmentBaseService.class);
|
|
|
+ private final EquipmentTypeService equipmentTypeService = mock(EquipmentTypeService.class);
|
|
|
+ private final MonitorPointService monitorPointService = mock(MonitorPointService.class);
|
|
|
+ private final EarlyWarningService earlyWarningService = mock(EarlyWarningService.class);
|
|
|
+ private final AlarmDataService alarmDataService = mock(AlarmDataService.class);
|
|
|
+ private final EquipmentWarningTypeMapper warningTypeMapper = mock(EquipmentWarningTypeMapper.class);
|
|
|
+ private final WarningTypeMapper refWarningTypeMapper = mock(WarningTypeMapper.class);
|
|
|
+ private final AlarmWarningRelMapper alarmWarningRelMapper = mock(AlarmWarningRelMapper.class);
|
|
|
+ private final WorkOrderService workOrderService = mock(WorkOrderService.class);
|
|
|
+ private final SysDictDataMapper sysDictDataMapper = mock(SysDictDataMapper.class);
|
|
|
+
|
|
|
+ private final BusinessCockpitEventAdapter adapter = new BusinessCockpitEventAdapter(
|
|
|
+ equipmentBaseService, equipmentTypeService, monitorPointService, earlyWarningService,
|
|
|
+ alarmDataService, warningTypeMapper, refWarningTypeMapper, alarmWarningRelMapper,
|
|
|
+ workOrderService, sysDictDataMapper,
|
|
|
+ Clock.fixed(Instant.parse("2026-09-02T02:00:00Z"), ZoneOffset.UTC));
|
|
|
+
|
|
|
+ @Test
|
|
|
+ void warningUsesAlertLevelDictAndAlarmUsesAlarmLevelDictInOneBatch() {
|
|
|
+ stubBaseFacts();
|
|
|
+ when(earlyWarningService.list()).thenReturn(Arrays.asList(
|
|
|
+ warning("WRN-1", "1"), warning("WRN-2", "2")));
|
|
|
+ // 报警等级写在 alarm_level 字典里;与预警字典刻意给不同的标签,证明按族取值。
|
|
|
+ when(alarmDataService.list()).thenReturn(Collections.singletonList(alarm("ALM-1", "4")));
|
|
|
+ when(sysDictDataMapper.selectDictDataByType(CockpitEventLevelName.WARNING_LEVEL_DICT))
|
|
|
+ .thenReturn(Collections.singletonList(dict("alert_level", "1", "一级预警")));
|
|
|
+ when(sysDictDataMapper.selectDictDataByType(CockpitEventLevelName.ALARM_LEVEL_DICT))
|
|
|
+ .thenReturn(Collections.singletonList(dict("alarm_level", "4", "四级报警")));
|
|
|
+
|
|
|
+ CockpitEventSnapshot snapshot = adapter.loadCurrent(DEVICE_CODE);
|
|
|
+
|
|
|
+ assertNotNull(snapshot);
|
|
|
+ // 字典命中:取 dict_label。
|
|
|
+ assertEquals("一级预警", snapshot.getWarnings().get(0).getLevelName());
|
|
|
+ // 字典缺项:回退既有「N级预警」口径,不猜、不硬编码前端。
|
|
|
+ assertEquals("二级预警", snapshot.getWarnings().get(1).getLevelName());
|
|
|
+ assertEquals("四级报警", snapshot.getAlarms().get(0).getLevelName());
|
|
|
+ // 既有字段与行为不变。
|
|
|
+ assertEquals(1, snapshot.getWarnings().get(0).getLevel());
|
|
|
+ assertEquals("RED", snapshot.getWarnings().get(0).getLevelColor());
|
|
|
+ assertEquals(4, snapshot.getAlarms().get(0).getLevel());
|
|
|
+ assertEquals("GREEN", snapshot.getAlarms().get(0).getLevelColor());
|
|
|
+ // 批量:每族一次字典查询,不随事件条数增长。
|
|
|
+ verify(sysDictDataMapper, times(1)).selectDictDataByType(CockpitEventLevelName.WARNING_LEVEL_DICT);
|
|
|
+ verify(sysDictDataMapper, times(1)).selectDictDataByType(CockpitEventLevelName.ALARM_LEVEL_DICT);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ void dictionaryUnavailableFallsBackAndMissingLevelStaysNull() {
|
|
|
+ stubBaseFacts();
|
|
|
+ when(earlyWarningService.list()).thenReturn(Arrays.asList(
|
|
|
+ warning("WRN-1", "3"), warning("WRN-2", null)));
|
|
|
+ when(alarmDataService.list()).thenReturn(Collections.singletonList(alarm("ALM-1", "2")));
|
|
|
+ when(sysDictDataMapper.selectDictDataByType(any())).thenReturn(Collections.emptyList());
|
|
|
+
|
|
|
+ CockpitEventSnapshot snapshot = adapter.loadCurrent(DEVICE_CODE);
|
|
|
+
|
|
|
+ assertNotNull(snapshot);
|
|
|
+ assertEquals("三级预警", snapshot.getWarnings().get(0).getLevelName());
|
|
|
+ assertNull(snapshot.getWarnings().get(1).getLevel());
|
|
|
+ assertNull(snapshot.getWarnings().get(1).getLevelName());
|
|
|
+ assertEquals("二级报警", snapshot.getAlarms().get(0).getLevelName());
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ void dictionaryFailureDegradesToFallbackInsteadOfFailingTheEvents() {
|
|
|
+ stubBaseFacts();
|
|
|
+ when(earlyWarningService.list()).thenReturn(Collections.singletonList(warning("WRN-1", "2")));
|
|
|
+ when(alarmDataService.list()).thenReturn(Collections.singletonList(alarm("ALM-1", "1")));
|
|
|
+ when(sysDictDataMapper.selectDictDataByType(CockpitEventLevelName.WARNING_LEVEL_DICT))
|
|
|
+ .thenThrow(new IllegalStateException("dict table unavailable"));
|
|
|
+ when(sysDictDataMapper.selectDictDataByType(CockpitEventLevelName.ALARM_LEVEL_DICT))
|
|
|
+ .thenThrow(new IllegalStateException("dict table unavailable"));
|
|
|
+
|
|
|
+ CockpitEventSnapshot snapshot = adapter.loadCurrent(DEVICE_CODE);
|
|
|
+
|
|
|
+ assertNotNull(snapshot);
|
|
|
+ assertEquals("二级预警", snapshot.getWarnings().get(0).getLevelName());
|
|
|
+ assertEquals("一级报警", snapshot.getAlarms().get(0).getLevelName());
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ void familyWithoutEventsDoesNotTouchItsDictionary() {
|
|
|
+ stubBaseFacts();
|
|
|
+ when(earlyWarningService.list()).thenReturn(Collections.singletonList(warning("WRN-1", "1")));
|
|
|
+ when(alarmDataService.list()).thenReturn(Collections.emptyList());
|
|
|
+ when(sysDictDataMapper.selectDictDataByType(CockpitEventLevelName.WARNING_LEVEL_DICT))
|
|
|
+ .thenReturn(Collections.singletonList(dict("alert_level", "1", "一级预警")));
|
|
|
+
|
|
|
+ CockpitEventSnapshot snapshot = adapter.loadCurrent(DEVICE_CODE);
|
|
|
+
|
|
|
+ assertNotNull(snapshot);
|
|
|
+ assertEquals("一级预警", snapshot.getWarnings().get(0).getLevelName());
|
|
|
+ verify(sysDictDataMapper, times(1)).selectDictDataByType(CockpitEventLevelName.WARNING_LEVEL_DICT);
|
|
|
+ verify(sysDictDataMapper, times(0)).selectDictDataByType(CockpitEventLevelName.ALARM_LEVEL_DICT);
|
|
|
+ }
|
|
|
+
|
|
|
+ private void stubBaseFacts() {
|
|
|
+ EquipmentType type = new EquipmentType();
|
|
|
+ type.setId(7);
|
|
|
+ type.setTypeName("污水环境监测设备");
|
|
|
+ when(equipmentTypeService.list()).thenReturn(Collections.singletonList(type));
|
|
|
+
|
|
|
+ EquipmentBase device = new EquipmentBase();
|
|
|
+ device.setEquipmentId("EQ-1");
|
|
|
+ device.setEquipmentCode(DEVICE_CODE);
|
|
|
+ device.setEquipmentName("污水环境监测设备");
|
|
|
+ device.setEquipmentTypeId(7);
|
|
|
+ when(equipmentBaseService.list()).thenReturn(Collections.singletonList(device));
|
|
|
+ when(equipmentBaseService.getEquipmentByPoint(any())).thenReturn(Collections.emptyList());
|
|
|
+
|
|
|
+ when(monitorPointService.list()).thenReturn(Collections.emptyList());
|
|
|
+ when(warningTypeMapper.selectList(any(Wrapper.class))).thenReturn(Collections.emptyList());
|
|
|
+ when(alarmWarningRelMapper.selectList(any(Wrapper.class))).thenReturn(Collections.emptyList());
|
|
|
+ when(workOrderService.list()).thenReturn(Collections.emptyList());
|
|
|
+ }
|
|
|
+
|
|
|
+ private static EarlyWarning warning(String warningNo, String level) {
|
|
|
+ EarlyWarning warning = new EarlyWarning();
|
|
|
+ warning.setWarningId("WID-" + warningNo);
|
|
|
+ warning.setWarningNo(warningNo);
|
|
|
+ warning.setWarningType("WS_ENV");
|
|
|
+ warning.setWarningLevel(level);
|
|
|
+ warning.setStatus("PROCESSING");
|
|
|
+ warning.setPublishTime(LocalDateTime.of(2026, 9, 2, 9, 59, 30));
|
|
|
+ warning.setDeviceCode(DEVICE_CODE);
|
|
|
+ warning.setDeviceName("污水环境监测设备");
|
|
|
+ return warning;
|
|
|
+ }
|
|
|
+
|
|
|
+ private static AlarmData alarm(String id, String level) {
|
|
|
+ AlarmData alarm = new AlarmData();
|
|
|
+ alarm.setId(id);
|
|
|
+ alarm.setAlarmNo(id);
|
|
|
+ alarm.setWarningCode("WS_ENV_HIGH");
|
|
|
+ alarm.setAlarmLevel(level);
|
|
|
+ alarm.setAlarmStatus(0);
|
|
|
+ alarm.setCreateTime(LocalDateTime.of(2026, 9, 2, 10, 0, 0));
|
|
|
+ alarm.setDeviceCode(DEVICE_CODE);
|
|
|
+ alarm.setDeviceName("污水环境监测设备");
|
|
|
+ return alarm;
|
|
|
+ }
|
|
|
+
|
|
|
+ private static SysDictData dict(String dictType, String value, String label) {
|
|
|
+ SysDictData data = new SysDictData();
|
|
|
+ data.setDictType(dictType);
|
|
|
+ data.setDictValue(value);
|
|
|
+ data.setDictLabel(label);
|
|
|
+ data.setStatus("0");
|
|
|
+ return data;
|
|
|
+ }
|
|
|
+}
|