|
|
@@ -0,0 +1,293 @@
|
|
|
+package com.zksy.web.controller.cockpit;
|
|
|
+
|
|
|
+import com.zksy.base.domain.EquipmentBase;
|
|
|
+import com.zksy.base.domain.EquipmentType;
|
|
|
+import com.zksy.base.mapper.EquipmentBaseMapper;
|
|
|
+import com.zksy.base.mapper.EquipmentStatusMapper;
|
|
|
+import com.zksy.base.mapper.EquipmentTypeMapper;
|
|
|
+import com.zksy.base.perception.mapper.PerceptionMapper;
|
|
|
+import com.zksy.base.perception.service.impl.PerceptionServiceImpl;
|
|
|
+import com.zksy.base.warning.mapper.EquipmentWarningTypeMapper;
|
|
|
+import com.zksy.cockpit.collectionRecords.CockpitCollectionRecordsService;
|
|
|
+import org.junit.jupiter.api.BeforeEach;
|
|
|
+import org.junit.jupiter.api.Test;
|
|
|
+import org.springframework.test.util.ReflectionTestUtils;
|
|
|
+import org.springframework.test.web.servlet.MockMvc;
|
|
|
+import org.springframework.test.web.servlet.setup.MockMvcBuilders;
|
|
|
+
|
|
|
+import java.time.LocalDateTime;
|
|
|
+import java.util.Collections;
|
|
|
+import java.util.LinkedHashMap;
|
|
|
+import java.util.Map;
|
|
|
+
|
|
|
+import static org.mockito.ArgumentMatchers.any;
|
|
|
+import static org.mockito.Mockito.mock;
|
|
|
+import static org.mockito.Mockito.when;
|
|
|
+import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
|
|
|
+import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
|
|
|
+import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
|
|
+
|
|
|
+/** 公开HTTP seam + 真实Service/profile,仅数据库读取边界使用fixture。 */
|
|
|
+class CockpitCollectionRecordsControllerTest {
|
|
|
+ static final String CODE = "TC-GS-202609020001";
|
|
|
+ static final String ID = "internal-equipment-uuid";
|
|
|
+ private EquipmentBaseMapper equipmentMapper;
|
|
|
+ private EquipmentTypeMapper typeMapper;
|
|
|
+ private PerceptionMapper recordsMapper;
|
|
|
+ private MockMvc mvc;
|
|
|
+
|
|
|
+ @BeforeEach
|
|
|
+ void setup() {
|
|
|
+ equipmentMapper = mock(EquipmentBaseMapper.class);
|
|
|
+ typeMapper = mock(EquipmentTypeMapper.class);
|
|
|
+ recordsMapper = mock(PerceptionMapper.class);
|
|
|
+ EquipmentBase base = new EquipmentBase();
|
|
|
+ base.setEquipmentId(ID);
|
|
|
+ base.setEquipmentCode(CODE);
|
|
|
+ base.setEquipmentTypeId(10);
|
|
|
+ when(equipmentMapper.selectOne(any())).thenAnswer(invocation -> {
|
|
|
+ com.baomidou.mybatisplus.core.conditions.query.QueryWrapper<?> query = invocation.getArgument(0);
|
|
|
+ query.getSqlSegment();
|
|
|
+ return query.getParamNameValuePairs().containsValue(CODE) ? base : null;
|
|
|
+ });
|
|
|
+ when(equipmentMapper.selectById(ID)).thenReturn(base);
|
|
|
+ EquipmentType type = new EquipmentType();
|
|
|
+ type.setOrigCollTable("firefighting_pressure");
|
|
|
+ when(typeMapper.selectById(10)).thenReturn(type);
|
|
|
+ EquipmentWarningTypeMapper indicators = mock(EquipmentWarningTypeMapper.class);
|
|
|
+ when(indicators.selectList(any())).thenReturn(Collections.emptyList());
|
|
|
+ PerceptionServiceImpl profile = new PerceptionServiceImpl();
|
|
|
+ ReflectionTestUtils.setField(profile, "equipmentBaseMapper", equipmentMapper);
|
|
|
+ ReflectionTestUtils.setField(profile, "equipmentTypeMapper", typeMapper);
|
|
|
+ ReflectionTestUtils.setField(profile, "equipmentStatusMapper", mock(EquipmentStatusMapper.class));
|
|
|
+ ReflectionTestUtils.setField(profile, "perceptionMapper", recordsMapper);
|
|
|
+ ReflectionTestUtils.setField(profile, "equipmentWarningTypeMapper", indicators);
|
|
|
+ mvc = MockMvcBuilders.standaloneSetup(new CockpitCollectionRecordsController(
|
|
|
+ new CockpitCollectionRecordsService(equipmentMapper, profile)))
|
|
|
+ .setMessageConverters(new org.springframework.http.converter.json.MappingJackson2HttpMessageConverter())
|
|
|
+ .build();
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ void pressureRecordsExposeReferenceColumnsAndCreateTimeWithoutInternalId() throws Exception {
|
|
|
+ when(recordsMapper.selectLatestRecords("firefighting_pressure", "telemetering_station", CODE, 50))
|
|
|
+ .thenReturn(Collections.singletonList(record("2026-11-04T10:20:00", "pressure_value", 0)));
|
|
|
+ mvc.perform(get("/api/cockpit/v1/equipments/" + CODE + "/collection-records")
|
|
|
+ .header("X-Request-Id", "collection-001"))
|
|
|
+ .andExpect(status().isOk())
|
|
|
+ .andExpect(jsonPath("$.data.requestId").value("collection-001"))
|
|
|
+ .andExpect(jsonPath("$.data.apiVersion").value("v1"))
|
|
|
+ .andExpect(jsonPath("$.data.readOnly").value(true))
|
|
|
+ .andExpect(jsonPath("$.data.deviceCode").value(CODE))
|
|
|
+ .andExpect(jsonPath("$.data.state").value("READY"))
|
|
|
+ .andExpect(jsonPath("$.data.equipmentId").doesNotExist())
|
|
|
+ .andExpect(jsonPath("$.data.recordView.layout").value("wide"))
|
|
|
+ .andExpect(jsonPath("$.data.recordView.columns[1].key").value("pressure_value"))
|
|
|
+ .andExpect(jsonPath("$.data.recordView.columns[1].label").value("压力"))
|
|
|
+ .andExpect(jsonPath("$.data.recordView.columns[1].unit").value("MPa"))
|
|
|
+ .andExpect(jsonPath("$.data.recordView.columns[1].isAlarm").value(false))
|
|
|
+ .andExpect(jsonPath("$.data.recordView.rows[0].createTime").value("2026-11-04 10:20:00"))
|
|
|
+ .andExpect(jsonPath("$.data.recordView.rows[0].cells[0].value").value("0"));
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ void invalidCodeIsBadRequestWithoutReadingDatabase() throws Exception {
|
|
|
+ mvc.perform(get("/api/cockpit/v1/equipments/%20/collection-records")
|
|
|
+ .header("X-Request-Id", "invalid-001"))
|
|
|
+ .andExpect(status().isBadRequest())
|
|
|
+ .andExpect(jsonPath("$.data.state").value("FAILED"))
|
|
|
+ .andExpect(jsonPath("$.data.stateReason").value("设备编码不能为空"))
|
|
|
+ .andExpect(jsonPath("$.data.requestId").value("invalid-001"));
|
|
|
+ org.mockito.Mockito.verifyNoInteractions(equipmentMapper, recordsMapper);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ void unknownBusinessCodeIsNotFoundNotEmpty() throws Exception {
|
|
|
+ org.mockito.Mockito.doReturn(null).when(equipmentMapper).selectOne(any());
|
|
|
+ mvc.perform(get("/api/cockpit/v1/equipments/MISSING/collection-records"))
|
|
|
+ .andExpect(status().isNotFound())
|
|
|
+ .andExpect(jsonPath("$.data.state").value("FAILED"))
|
|
|
+ .andExpect(jsonPath("$.data.stateReason").value("设备不存在"));
|
|
|
+ org.mockito.Mockito.verifyNoInteractions(recordsMapper);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ void databaseFailureIsUnavailableNotSuccessfulEmpty() throws Exception {
|
|
|
+ org.mockito.Mockito.doThrow(new IllegalStateException("password secret database"))
|
|
|
+ .when(equipmentMapper).selectOne(any());
|
|
|
+ mvc.perform(get("/api/cockpit/v1/equipments/" + CODE + "/collection-records"))
|
|
|
+ .andExpect(status().isInternalServerError())
|
|
|
+ .andExpect(jsonPath("$.data.state").value("FAILED"))
|
|
|
+ .andExpect(jsonPath("$.data.stateReason").value("驾驶舱采集记录读取失败,暂不可用"))
|
|
|
+ .andExpect(jsonPath("$.data.recordView.rows").isEmpty());
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ void configuredCollectionWithNoRowsIsEmpty() throws Exception {
|
|
|
+ when(recordsMapper.selectLatestRecords("firefighting_pressure", "telemetering_station", CODE, 50))
|
|
|
+ .thenReturn(Collections.emptyList());
|
|
|
+ mvc.perform(get("/api/cockpit/v1/equipments/" + CODE + "/collection-records"))
|
|
|
+ .andExpect(status().isOk())
|
|
|
+ .andExpect(jsonPath("$.data.state").value("EMPTY"))
|
|
|
+ .andExpect(jsonPath("$.data.stateReason").value("暂无采集记录"))
|
|
|
+ .andExpect(jsonPath("$.data.recordView.columns[0].key").value("create_time"))
|
|
|
+ .andExpect(jsonPath("$.data.recordView.rows").isEmpty());
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ void unsupportedCollectionTableIsUnavailableNotEmpty() throws Exception {
|
|
|
+ EquipmentType type = new EquipmentType();
|
|
|
+ type.setOrigCollTable("not_a_whitelisted_collection");
|
|
|
+ when(typeMapper.selectById(10)).thenReturn(type);
|
|
|
+ mvc.perform(get("/api/cockpit/v1/equipments/" + CODE + "/collection-records"))
|
|
|
+ .andExpect(status().isOk())
|
|
|
+ .andExpect(jsonPath("$.data.state").value("UNAVAILABLE"))
|
|
|
+ .andExpect(jsonPath("$.data.stateReason").value("设备采集表未配置或暂不支持"))
|
|
|
+ .andExpect(jsonPath("$.data.recordView.rows").isEmpty());
|
|
|
+ org.mockito.Mockito.verifyNoInteractions(recordsMapper);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ void missingSampleTimeAndNullValueRemainUnknownInsteadOfReportTimeOrZero() throws Exception {
|
|
|
+ when(recordsMapper.selectLatestRecords("firefighting_pressure", "telemetering_station", CODE, 50))
|
|
|
+ .thenReturn(Collections.singletonList(record(null, "pressure_value", null)));
|
|
|
+ mvc.perform(get("/api/cockpit/v1/equipments/" + CODE + "/collection-records"))
|
|
|
+ .andExpect(status().isOk())
|
|
|
+ .andExpect(jsonPath("$.data.recordView.rows[0].createTime").value(org.hamcrest.Matchers.nullValue()))
|
|
|
+ .andExpect(jsonPath("$.data.recordView.rows[0].cells[0].value").value(org.hamcrest.Matchers.nullValue()))
|
|
|
+ .andExpect(jsonPath("$.data.recordView.rows[0].cells[0].display").value("—"));
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ void environmentHistoryKeepsOnlyWaterQualityFactorsAndAllSampleTimes() throws Exception {
|
|
|
+ EquipmentType type = new EquipmentType();
|
|
|
+ type.setOrigCollTable("e_real_time_data");
|
|
|
+ when(typeMapper.selectById(10)).thenReturn(type);
|
|
|
+ Map<String, Object> cod = record("2026-11-04T10:20:00", "hum", 0);
|
|
|
+ cod.put("node_id", 2);
|
|
|
+ cod.put("temperature", 99);
|
|
|
+ Map<String, Object> ammonia = record("2026-11-04T10:20:00", "hum", 2);
|
|
|
+ ammonia.put("node_id", 3);
|
|
|
+ Map<String, Object> older = record("2026-11-04T10:19:00", "hum", 7);
|
|
|
+ older.put("node_id", 5);
|
|
|
+ when(recordsMapper.selectLatestRecords("e_real_time_data", "device_id", CODE, 50))
|
|
|
+ .thenReturn(java.util.Arrays.asList(cod, ammonia, older));
|
|
|
+ mvc.perform(get("/api/cockpit/v1/equipments/" + CODE + "/collection-records"))
|
|
|
+ .andExpect(status().isOk())
|
|
|
+ .andExpect(jsonPath("$.data.recordView.layout").value("long"))
|
|
|
+ .andExpect(jsonPath("$.data.recordView.columns.length()").value(2))
|
|
|
+ .andExpect(jsonPath("$.data.recordView.rows.length()").value(3))
|
|
|
+ .andExpect(jsonPath("$.data.recordView.rows[0].cells.length()").value(1))
|
|
|
+ .andExpect(jsonPath("$.data.recordView.rows[0].cells[0].key").value("indicator"))
|
|
|
+ .andExpect(jsonPath("$.data.recordView.rows[0].cells[0].display").value("化学需氧量(COD):0mg/L"))
|
|
|
+ .andExpect(jsonPath("$.data.recordView.rows[1].cells[0].display").value("氨氮:2mg/L"))
|
|
|
+ .andExpect(jsonPath("$.data.recordView.rows[2].createTime").value("2026-11-04 10:19:00"))
|
|
|
+ .andExpect(jsonPath("$.data.recordView.rows[2].cells[0].display").value("酸碱度(pH):7"));
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ void endpointCapsHistoryAtNewestFiftyCreateTimes() throws Exception {
|
|
|
+ java.util.List<Map<String, Object>> records = new java.util.ArrayList<>();
|
|
|
+ for (int minute = 0; minute < 55; minute++) {
|
|
|
+ records.add(record(LocalDateTime.parse("2026-11-04T10:00:00").plusMinutes(minute).toString(),
|
|
|
+ "pressure_value", minute));
|
|
|
+ }
|
|
|
+ when(recordsMapper.selectLatestRecords("firefighting_pressure", "telemetering_station", CODE, 50))
|
|
|
+ .thenReturn(records);
|
|
|
+ mvc.perform(get("/api/cockpit/v1/equipments/" + CODE + "/collection-records"))
|
|
|
+ .andExpect(status().isOk())
|
|
|
+ .andExpect(jsonPath("$.data.recordView.rows.length()").value(50))
|
|
|
+ .andExpect(jsonPath("$.data.recordView.rows[0].createTime").value("2026-11-04 10:54:00"))
|
|
|
+ .andExpect(jsonPath("$.data.recordView.rows[49].createTime").value("2026-11-04 10:05:00"));
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ void unknownAlarmStatusIsNotFabricatedAsNormalWhileZeroRemainsNormal() throws Exception {
|
|
|
+ EquipmentType type = new EquipmentType();
|
|
|
+ type.setOrigCollTable("jg_device_data");
|
|
|
+ when(typeMapper.selectById(10)).thenReturn(type);
|
|
|
+ Map<String, Object> record = record("2026-11-04T10:20:00", "water_infiltration_alarm_status", null);
|
|
|
+ record.put("water_level_alarm_status", 0);
|
|
|
+ record.put("battery_level", 0);
|
|
|
+ when(recordsMapper.selectLatestRecords("jg_device_data", "imei_card_number", CODE, 50))
|
|
|
+ .thenReturn(Collections.singletonList(record));
|
|
|
+ mvc.perform(get("/api/cockpit/v1/equipments/" + CODE + "/collection-records"))
|
|
|
+ .andExpect(status().isOk())
|
|
|
+ .andExpect(jsonPath("$.data.recordView.columns[2].isAlarm").value(true))
|
|
|
+ .andExpect(jsonPath("$.data.recordView.rows[0].cells[1].value").value(org.hamcrest.Matchers.nullValue()))
|
|
|
+ .andExpect(jsonPath("$.data.recordView.rows[0].cells[1].display").value("—"))
|
|
|
+ .andExpect(jsonPath("$.data.recordView.rows[0].cells[1].alarm").value(false))
|
|
|
+ .andExpect(jsonPath("$.data.recordView.rows[0].cells[2].value").value("正常"))
|
|
|
+ .andExpect(jsonPath("$.data.recordView.rows[0].cells[4].value").value("0"));
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ void internalIdCannotReplaceBusinessCodeAndMutationsAreRejected() throws Exception {
|
|
|
+ mvc.perform(get("/api/cockpit/v1/equipments/" + ID + "/collection-records"))
|
|
|
+ .andExpect(status().isNotFound());
|
|
|
+ mvc.perform(org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post(
|
|
|
+ "/api/cockpit/v1/equipments/" + CODE + "/collection-records"))
|
|
|
+ .andExpect(status().isMethodNotAllowed());
|
|
|
+ org.mockito.Mockito.verifyNoInteractions(recordsMapper);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ void noiseAudioPathAndReferenceColumnsArePassedThroughWithoutPrivateRecords() throws Exception {
|
|
|
+ EquipmentType type = new EquipmentType();
|
|
|
+ type.setOrigCollTable("noise_info");
|
|
|
+ when(typeMapper.selectById(10)).thenReturn(type);
|
|
|
+ Map<String, Object> record = record("2026-11-04T10:20:00", "filepath", "/device-audio/noise-001.wav");
|
|
|
+ record.put("rms", 0);
|
|
|
+ record.put("gain", 2);
|
|
|
+ record.put("center_frequency", 99);
|
|
|
+ when(recordsMapper.selectLatestRecords("noise_info", "encode", CODE, 50))
|
|
|
+ .thenReturn(Collections.singletonList(record));
|
|
|
+ String response = mvc.perform(get("/api/cockpit/v1/equipments/" + CODE + "/collection-records"))
|
|
|
+ .andExpect(status().isOk())
|
|
|
+ .andExpect(jsonPath("$.data.recordView.columns.length()").value(5))
|
|
|
+ .andExpect(jsonPath("$.data.recordView.columns[4].key").value("filepath"))
|
|
|
+ .andExpect(jsonPath("$.data.recordView.columns[4].label").value("音频文件"))
|
|
|
+ .andExpect(jsonPath("$.data.recordView.rows[0].cells[0].value").value("0"))
|
|
|
+ .andExpect(jsonPath("$.data.recordView.rows[0].cells[3].value").value("/device-audio/noise-001.wav"))
|
|
|
+ .andExpect(jsonPath("$.data.base").doesNotExist())
|
|
|
+ .andExpect(jsonPath("$.data.records").doesNotExist())
|
|
|
+ .andReturn().getResponse().getContentAsString();
|
|
|
+ org.junit.jupiter.api.Assertions.assertFalse(response.contains("private-record-id"));
|
|
|
+ org.junit.jupiter.api.Assertions.assertFalse(response.contains(ID));
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ void missingLongFactorRemainsNullWithChineseDisplay() throws Exception {
|
|
|
+ EquipmentType type = new EquipmentType();
|
|
|
+ type.setOrigCollTable("e_real_time_data");
|
|
|
+ when(typeMapper.selectById(10)).thenReturn(type);
|
|
|
+ Map<String, Object> record = record(null, "hum", null);
|
|
|
+ record.put("node_id", 3);
|
|
|
+ when(recordsMapper.selectLatestRecords("e_real_time_data", "device_id", CODE, 50))
|
|
|
+ .thenReturn(Collections.singletonList(record));
|
|
|
+ mvc.perform(get("/api/cockpit/v1/equipments/" + CODE + "/collection-records"))
|
|
|
+ .andExpect(status().isOk())
|
|
|
+ .andExpect(jsonPath("$.data.recordView.rows[0].cells[0].value").value(org.hamcrest.Matchers.nullValue()))
|
|
|
+ .andExpect(jsonPath("$.data.recordView.rows[0].cells[0].display").value("氨氮:—"));
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ void malformedEncodingAndEmbeddedControlCharacterAreInvalidBeforeDatabaseRead() throws Exception {
|
|
|
+ mvc.perform(get("/api/cockpit/v1/equipments/%ZZ/collection-records"))
|
|
|
+ .andExpect(status().isBadRequest())
|
|
|
+ .andExpect(jsonPath("$.data.stateReason").value("设备编码不合法"));
|
|
|
+ mvc.perform(get("/api/cockpit/v1/equipments/TC%0AGS/collection-records"))
|
|
|
+ .andExpect(status().isBadRequest())
|
|
|
+ .andExpect(jsonPath("$.data.stateReason").value("设备编码不合法"));
|
|
|
+ org.mockito.Mockito.verifyNoInteractions(equipmentMapper, recordsMapper);
|
|
|
+ }
|
|
|
+
|
|
|
+ static Map<String, Object> record(String time, String key, Object value) {
|
|
|
+ Map<String, Object> record = new LinkedHashMap<>();
|
|
|
+ record.put("create_time", time == null ? null : LocalDateTime.parse(time));
|
|
|
+ record.put(key, value);
|
|
|
+ record.put("report_time", LocalDateTime.parse("2027-01-01T00:00:00"));
|
|
|
+ record.put("id", "private-record-id");
|
|
|
+ return record;
|
|
|
+ }
|
|
|
+}
|