|
|
@@ -0,0 +1,179 @@
|
|
|
+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.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.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 static org.junit.jupiter.api.Assertions.assertArrayEquals;
|
|
|
+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;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 事件描述原文(content):预警取 early_warning.warning_content、报警取 alarm_data.alarm_content,
|
|
|
+ * 原样透传(含管理端文案里的换行),缺失为 null;不额外查库。
|
|
|
+ */
|
|
|
+class CockpitEventContentTest {
|
|
|
+ private static final String DEVICE_CODE = "TC-PS-WS-202609020012";
|
|
|
+ private static final String WARNING_CONTENT =
|
|
|
+ "化学需氧量(COD)预警接近上限\n实际值:14.6 mg/L;阈值:上限 20 mg/L";
|
|
|
+
|
|
|
+ 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 storedDescriptionsArePassedThroughVerbatimForBothFamilies() {
|
|
|
+ stubBaseFacts();
|
|
|
+ EarlyWarning warning = warning("WRN-1", "1");
|
|
|
+ warning.setWarningContent(WARNING_CONTENT);
|
|
|
+ AlarmData alarm = alarm("ALM-1", "2");
|
|
|
+ alarm.setAlarmContent("化学需氧量(COD)报警超过上限\n实际值:24.6 mg/L;阈值:上限 20 mg/L");
|
|
|
+ when(earlyWarningService.list()).thenReturn(Collections.singletonList(warning));
|
|
|
+ when(alarmDataService.list()).thenReturn(Collections.singletonList(alarm));
|
|
|
+ when(sysDictDataMapper.selectDictDataByType(any())).thenReturn(Collections.emptyList());
|
|
|
+
|
|
|
+ CockpitEventSnapshot snapshot = adapter.loadCurrent(DEVICE_CODE);
|
|
|
+
|
|
|
+ assertNotNull(snapshot);
|
|
|
+ // 逐字相同(含换行与空格),不做二次格式化。
|
|
|
+ assertEquals(WARNING_CONTENT, snapshot.getWarnings().get(0).getContent());
|
|
|
+ assertEquals("化学需氧量(COD)报警超过上限\n实际值:24.6 mg/L;阈值:上限 20 mg/L",
|
|
|
+ snapshot.getAlarms().get(0).getContent());
|
|
|
+ // 既有字段照旧。
|
|
|
+ assertEquals("WRN-1", snapshot.getWarnings().get(0).getWarningNo());
|
|
|
+ assertEquals(1, snapshot.getWarnings().get(0).getLevel());
|
|
|
+ assertEquals("ALM-1", snapshot.getAlarms().get(0).getAlarmId());
|
|
|
+ // 描述随已加载实体一起读出,没有为它新增逐条查询。
|
|
|
+ verify(earlyWarningService, times(1)).list();
|
|
|
+ verify(alarmDataService, times(1)).list();
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ void missingStoredDescriptionStaysNullInsteadOfBeingSynthesized() {
|
|
|
+ stubBaseFacts();
|
|
|
+ EarlyWarning warning = warning("WRN-1", "2");
|
|
|
+ warning.setWarningContent(null);
|
|
|
+ AlarmData alarm = alarm("ALM-1", "1");
|
|
|
+ alarm.setAlarmContent(" ");
|
|
|
+ when(earlyWarningService.list()).thenReturn(Collections.singletonList(warning));
|
|
|
+ when(alarmDataService.list()).thenReturn(Collections.singletonList(alarm));
|
|
|
+ when(sysDictDataMapper.selectDictDataByType(any())).thenReturn(Collections.emptyList());
|
|
|
+
|
|
|
+ CockpitEventSnapshot snapshot = adapter.loadCurrent(DEVICE_CODE);
|
|
|
+
|
|
|
+ assertNotNull(snapshot);
|
|
|
+ assertNull(snapshot.getWarnings().get(0).getContent());
|
|
|
+ // 空串/空白不当作描述,也不拼装替代文案。
|
|
|
+ assertNull(snapshot.getAlarms().get(0).getContent());
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ void contentSerializesAsPlainStringWithNewlinesPreserved() throws Exception {
|
|
|
+ ObjectMapper mapper = new ObjectMapper();
|
|
|
+ CockpitEventWarning warning = new CockpitEventWarning(
|
|
|
+ "WRN-1", "污水COD预警", "WS_ENV", 1, "RED", "一级预警",
|
|
|
+ WARNING_CONTENT, "PROCESSING", "2026-09-02T09:59:30+08:00",
|
|
|
+ null, null, DEVICE_CODE, "污水环境监测设备", "太常片区");
|
|
|
+ CockpitEventAlarm alarm = new CockpitEventAlarm(
|
|
|
+ "ALM-1", "WS_ENV_HIGH", "污水COD报警", "WS_ENV", 2, "YELLOW", "二级报警",
|
|
|
+ null, "UNHANDLED", "2026-09-02T10:00:00+08:00", null, null, null, null,
|
|
|
+ DEVICE_CODE, "污水环境监测设备", "太常片区");
|
|
|
+
|
|
|
+ JsonNode warningJson = mapper.readTree(mapper.writeValueAsString(warning));
|
|
|
+ assertEquals(WARNING_CONTENT, warningJson.get("content").asText());
|
|
|
+ assertArrayEquals(WARNING_CONTENT.split("\n"), warningJson.get("content").asText().split("\n"));
|
|
|
+ // 既有字段仍在。
|
|
|
+ assertEquals(1, warningJson.get("level").asInt());
|
|
|
+ assertEquals("RED", warningJson.get("levelColor").asText());
|
|
|
+
|
|
|
+ JsonNode alarmJson = mapper.readTree(mapper.writeValueAsString(alarm));
|
|
|
+ assertNull(alarmJson.get("content").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 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;
|
|
|
+ }
|
|
|
+}
|