|
|
@@ -0,0 +1,181 @@
|
|
|
+package com.zksy.cockpit.event;
|
|
|
+
|
|
|
+import com.baomidou.mybatisplus.core.conditions.Wrapper;
|
|
|
+import com.fasterxml.jackson.databind.JsonNode;
|
|
|
+import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
+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.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.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.Collections;
|
|
|
+
|
|
|
+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.when;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 报警业务单号(alarmNo):取自 {@code alarm_data.alarm_no},原样透传;
|
|
|
+ * 主键 {@code alarmId} 仍在,二者并存,缺失时如实 null。
|
|
|
+ */
|
|
|
+class CockpitEventAlarmNoTest {
|
|
|
+ private static final String DEVICE_CODE = "TC-PS-WS-202609020012";
|
|
|
+ private static final String ALARM_ID = "b4dc6387944a1585a9b43dfca5cf680f";
|
|
|
+ private static final String ALARM_NO = "BJ202609291243545660";
|
|
|
+
|
|
|
+ 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 adapterExposesBusinessAlarmNoAlongsidePrimaryKey() {
|
|
|
+ stubBaseFacts();
|
|
|
+ AlarmData alarm = alarm(ALARM_ID, ALARM_NO);
|
|
|
+ when(alarmDataService.list()).thenReturn(Collections.singletonList(alarm));
|
|
|
+ when(earlyWarningService.list()).thenReturn(Collections.emptyList());
|
|
|
+ when(sysDictDataMapper.selectDictDataByType(any())).thenReturn(Collections.emptyList());
|
|
|
+
|
|
|
+ CockpitEventAlarm item = adapter.loadCurrent(DEVICE_CODE).getAlarms().get(0);
|
|
|
+
|
|
|
+ // 业务单号与主键并存:界面显示可读单号,主键继续供回联使用
|
|
|
+ assertEquals(ALARM_NO, item.getAlarmNo());
|
|
|
+ assertEquals(ALARM_ID, item.getAlarmId());
|
|
|
+ // 既有字段不受影响
|
|
|
+ assertEquals("WS_ENV_HIGH", item.getWarningCode());
|
|
|
+ assertEquals(2, item.getLevel());
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ void missingAlarmNoStaysNullAndDoesNotFallBackToPrimaryKey() {
|
|
|
+ stubBaseFacts();
|
|
|
+ when(alarmDataService.list()).thenReturn(Collections.singletonList(alarm(ALARM_ID, null)));
|
|
|
+ when(earlyWarningService.list()).thenReturn(Collections.emptyList());
|
|
|
+ when(sysDictDataMapper.selectDictDataByType(any())).thenReturn(Collections.emptyList());
|
|
|
+
|
|
|
+ CockpitEventAlarm item = adapter.loadCurrent(DEVICE_CODE).getAlarms().get(0);
|
|
|
+
|
|
|
+ assertNull(item.getAlarmNo(), "缺单号就该是 null,不得把主键冒充单号");
|
|
|
+ assertEquals(ALARM_ID, item.getAlarmId());
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ void blankAlarmNoIsNormalizedToNull() {
|
|
|
+ stubBaseFacts();
|
|
|
+ when(alarmDataService.list()).thenReturn(Collections.singletonList(alarm(ALARM_ID, " ")));
|
|
|
+ when(earlyWarningService.list()).thenReturn(Collections.emptyList());
|
|
|
+ when(sysDictDataMapper.selectDictDataByType(any())).thenReturn(Collections.emptyList());
|
|
|
+
|
|
|
+ assertNull(adapter.loadCurrent(DEVICE_CODE).getAlarms().get(0).getAlarmNo());
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ void rebuildInReadServiceKeepsAlarmNoInsteadOfDroppingIt() throws Exception {
|
|
|
+ CockpitEventAlarm source = new CockpitEventAlarm(
|
|
|
+ ALARM_ID, ALARM_NO, "WS_ENV_HIGH", "污水COD报警", "WS_ENV", 2, "YELLOW", "二级报警",
|
|
|
+ "化学需氧量(COD)报警超过上限", "UNHANDLED", "2026-09-02T10:00:00+08:00",
|
|
|
+ null, null, null, "mg/L", DEVICE_CODE, "污水环境监测设备", "太常片区");
|
|
|
+
|
|
|
+ // decorateAlarms 是 ReadService 内部重建路径(返回新的 DTO 实例),必须照抄 alarmNo。
|
|
|
+ java.lang.reflect.Method decorate = CockpitEventReadService.class
|
|
|
+ .getDeclaredMethod("decorateAlarms", java.util.List.class);
|
|
|
+ decorate.setAccessible(true);
|
|
|
+ java.util.List<CockpitEventAlarm> decorated = (java.util.List<CockpitEventAlarm>) decorate.invoke(
|
|
|
+ new CockpitEventReadService(
|
|
|
+ mock(CockpitEventReadAdapter.class),
|
|
|
+ Clock.fixed(Instant.parse("2026-09-02T02:00:00Z"), ZoneOffset.UTC)),
|
|
|
+ Collections.singletonList(source));
|
|
|
+
|
|
|
+ assertNotNull(decorated);
|
|
|
+ assertEquals(1, decorated.size());
|
|
|
+ assertNotNull(decorated.get(0));
|
|
|
+ assertEquals(ALARM_NO, decorated.get(0).getAlarmNo(), "重建对象不得丢单号");
|
|
|
+ assertEquals(ALARM_ID, decorated.get(0).getAlarmId());
|
|
|
+ assertEquals("二级报警", decorated.get(0).getLevelName());
|
|
|
+ assertEquals("化学需氧量(COD)报警超过上限", decorated.get(0).getContent());
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ void alarmNoSerializesAsPlainStringAndNullWhenMissing() throws Exception {
|
|
|
+ ObjectMapper mapper = new ObjectMapper();
|
|
|
+ CockpitEventAlarm withNo = new CockpitEventAlarm(
|
|
|
+ ALARM_ID, ALARM_NO, "WS_ENV_HIGH", "污水COD报警", "WS_ENV", 2, "YELLOW", "二级报警",
|
|
|
+ null, "UNHANDLED", "2026-09-02T10:00:00+08:00", null, null, null, null,
|
|
|
+ DEVICE_CODE, "污水环境监测设备", "太常片区");
|
|
|
+ CockpitEventAlarm withoutNo = new CockpitEventAlarm(
|
|
|
+ ALARM_ID, null, "WS_ENV_HIGH", "污水COD报警", "WS_ENV", 2, "YELLOW", "二级报警",
|
|
|
+ null, "UNHANDLED", "2026-09-02T10:00:00+08:00", null, null, null, null,
|
|
|
+ DEVICE_CODE, "污水环境监测设备", "太常片区");
|
|
|
+
|
|
|
+ JsonNode json = mapper.readTree(mapper.writeValueAsString(withNo));
|
|
|
+ JsonNode missing = mapper.readTree(mapper.writeValueAsString(withoutNo));
|
|
|
+
|
|
|
+ assertEquals(ALARM_NO, json.get("alarmNo").asText());
|
|
|
+ assertEquals(ALARM_ID, json.get("alarmId").asText());
|
|
|
+ // 缺失时键仍存在、值为 null(前端可据此回落),不是把键删掉
|
|
|
+ org.junit.jupiter.api.Assertions.assertTrue(missing.has("alarmNo"));
|
|
|
+ assertNull(missing.get("alarmNo").textValue());
|
|
|
+ }
|
|
|
+
|
|
|
+ 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 AlarmData alarm(String id, String alarmNo) {
|
|
|
+ AlarmData alarm = new AlarmData();
|
|
|
+ alarm.setId(id);
|
|
|
+ alarm.setAlarmNo(alarmNo);
|
|
|
+ alarm.setWarningCode("WS_ENV_HIGH");
|
|
|
+ alarm.setAlarmLevel("2");
|
|
|
+ alarm.setAlarmStatus(0);
|
|
|
+ alarm.setCreateTime(LocalDateTime.of(2026, 9, 2, 10, 0, 0));
|
|
|
+ alarm.setDeviceCode(DEVICE_CODE);
|
|
|
+ alarm.setDeviceName("污水环境监测设备");
|
|
|
+ return alarm;
|
|
|
+ }
|
|
|
+}
|