|
|
@@ -0,0 +1,288 @@
|
|
|
+package com.zksy.web.controller.cockpit;
|
|
|
+
|
|
|
+import com.zksy.cockpit.foundation.CockpitInvalidRequestException;
|
|
|
+import com.zksy.cockpit.foundation.CockpitSnapshotExpiredException;
|
|
|
+import com.zksy.cockpit.map.CockpitMapSnapshot;
|
|
|
+import com.zksy.cockpit.map.CockpitMapSnapshotAdapter;
|
|
|
+import com.zksy.cockpit.map.CockpitMapSnapshotService;
|
|
|
+import com.zksy.cockpit.map.CockpitMonitoringPointSummary;
|
|
|
+import com.zksy.cockpit.point.CockpitMonitoringPointDetailAdapter;
|
|
|
+import com.zksy.cockpit.point.CockpitMonitoringPointDetailResponse;
|
|
|
+import com.zksy.cockpit.point.CockpitMonitoringPointDetailService;
|
|
|
+import com.zksy.cockpit.point.CockpitMonitoringPointDeviceCard;
|
|
|
+import com.zksy.cockpit.point.CockpitMonitoringPointDeviceSnapshot;
|
|
|
+import org.junit.jupiter.api.BeforeEach;
|
|
|
+import org.junit.jupiter.api.Test;
|
|
|
+
|
|
|
+import java.math.BigDecimal;
|
|
|
+import java.time.Clock;
|
|
|
+import java.time.Instant;
|
|
|
+import java.time.ZoneOffset;
|
|
|
+import java.util.Arrays;
|
|
|
+import java.util.Collections;
|
|
|
+import java.util.Comparator;
|
|
|
+import java.util.List;
|
|
|
+import java.util.stream.Collectors;
|
|
|
+
|
|
|
+import static org.junit.jupiter.api.Assertions.assertEquals;
|
|
|
+import static org.junit.jupiter.api.Assertions.assertFalse;
|
|
|
+import static org.junit.jupiter.api.Assertions.assertNotNull;
|
|
|
+import static org.junit.jupiter.api.Assertions.assertNull;
|
|
|
+import static org.junit.jupiter.api.Assertions.assertThrows;
|
|
|
+import static org.junit.jupiter.api.Assertions.assertTrue;
|
|
|
+import static org.mockito.Mockito.mock;
|
|
|
+import static org.mockito.Mockito.verifyNoInteractions;
|
|
|
+import static org.mockito.Mockito.when;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 跨端点同快照契约:同一 snapshotAt 下,监测点详情 devices[] 与 map-snapshot
|
|
|
+ * 对应点位的 deviceCodes 集合必须一致(含离线设备、不重复、不静默丢失),
|
|
|
+ * 且详情设备坐标/校验对象与快照同源。
|
|
|
+ */
|
|
|
+class CockpitMonitoringPointDetailSnapshotContractTest {
|
|
|
+
|
|
|
+ private static final String SNAPSHOT_AT = "2026-09-02T10:00:00+08:00";
|
|
|
+ private static final Clock CLOCK =
|
|
|
+ Clock.fixed(Instant.parse("2026-09-02T02:00:00Z"), ZoneOffset.UTC);
|
|
|
+
|
|
|
+ private CockpitMonitoringPointDetailAdapter detailAdapter;
|
|
|
+ private CockpitMapSnapshotAdapter mapAdapter;
|
|
|
+ private CockpitMonitoringPointDetailService detailService;
|
|
|
+ private CockpitMapSnapshotService mapService;
|
|
|
+
|
|
|
+ @BeforeEach
|
|
|
+ void setUp() {
|
|
|
+ detailAdapter = mock(CockpitMonitoringPointDetailAdapter.class);
|
|
|
+ mapAdapter = mock(CockpitMapSnapshotAdapter.class);
|
|
|
+ detailService = new CockpitMonitoringPointDetailService(detailAdapter, mapAdapter, CLOCK);
|
|
|
+ mapService = new CockpitMapSnapshotService(mapAdapter, CLOCK);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ void detailDevicesMatchMapSnapshotDeviceCodesAtSameSnapshotIncludingOffline() {
|
|
|
+ CockpitMapSnapshot snapshot = snapshotWith(SNAPSHOT_AT,
|
|
|
+ new CockpitMapSnapshot.SourceMonitoringPoint(
|
|
|
+ "MP-001", "供水干管监测点", null, "11",
|
|
|
+ new BigDecimal("110.141"), new BigDecimal("28.452"), false,
|
|
|
+ Arrays.asList(
|
|
|
+ device("EQ-003", "110.142", "28.453", false, true, false, false, true),
|
|
|
+ device("EQ-001", "110.143", "28.454", false, true, true, false, true),
|
|
|
+ device("EQ-005", "110.144", "28.455", false, false, false, true, true),
|
|
|
+ device("EQ-002", "110.145", "28.456", false, true, false, false, false),
|
|
|
+ device("EQ-004", null, null, false, false, false, false, false))));
|
|
|
+ when(mapAdapter.load(SNAPSHOT_AT)).thenReturn(snapshot);
|
|
|
+
|
|
|
+ CockpitMonitoringPointDetailResponse response =
|
|
|
+ detailService.load("req", "MP-001", SNAPSHOT_AT);
|
|
|
+ assertNotNull(response);
|
|
|
+
|
|
|
+ List<String> detailDeviceCodes = response.getDevices().stream()
|
|
|
+ .map(CockpitMonitoringPointDeviceCard::getDeviceCode)
|
|
|
+ .sorted(Comparator.nullsLast(String::compareTo))
|
|
|
+ .collect(Collectors.toList());
|
|
|
+
|
|
|
+ List<CockpitMonitoringPointSummary> points = mapService.load(
|
|
|
+ "req", 1, 500, SNAPSHOT_AT, null, null, null).getMonitoringPoints();
|
|
|
+ assertEquals(1, points.size());
|
|
|
+ List<String> mapDeviceCodes = points.get(0).getDeviceCodes().stream()
|
|
|
+ .sorted(Comparator.nullsLast(String::compareTo))
|
|
|
+ .collect(Collectors.toList());
|
|
|
+
|
|
|
+ assertEquals(5, detailDeviceCodes.size(), "详情必须包含全部关联设备");
|
|
|
+ assertEquals(mapDeviceCodes, detailDeviceCodes,
|
|
|
+ "同一 snapshotAt 下详情设备集合必须与 map-snapshot deviceCodes 一致(含离线、不重复)");
|
|
|
+ assertEquals(detailDeviceCodes.stream().distinct().count(), detailDeviceCodes.size(),
|
|
|
+ "详情设备集合不得重复");
|
|
|
+ assertTrue(mapDeviceCodes.contains("EQ-002"), "离线设备必须在集合中");
|
|
|
+ assertTrue(detailDeviceCodes.contains("EQ-004"), "缺坐标设备必须在集合中");
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ void detailCoordinatesAndValidationArePreservedFromSnapshotSource() {
|
|
|
+ CockpitMapSnapshot snapshot = snapshotWith(SNAPSHOT_AT,
|
|
|
+ new CockpitMapSnapshot.SourceMonitoringPoint(
|
|
|
+ "MP-001", "供水干管监测点", null, "11",
|
|
|
+ new BigDecimal("110.141"), new BigDecimal("28.452"), false,
|
|
|
+ Arrays.asList(
|
|
|
+ device("EQ-VALID", "110.142", "28.453", false, true, true, false, false),
|
|
|
+ device("EQ-MISSING", null, null, false, false, false, false, false),
|
|
|
+ device("EQ-INVALID", "200.000", "28.453", true, true, false, false, false),
|
|
|
+ device("EQ-UNVERIFIED", "110.146", "28.457", false, true, false, false, false))));
|
|
|
+ when(mapAdapter.load(SNAPSHOT_AT)).thenReturn(snapshot);
|
|
|
+
|
|
|
+ CockpitMonitoringPointDetailResponse response =
|
|
|
+ detailService.load("req", "MP-001", SNAPSHOT_AT);
|
|
|
+
|
|
|
+ List<CockpitMonitoringPointDeviceCard> devices = response.getDevices();
|
|
|
+ assertEquals(4, devices.size());
|
|
|
+
|
|
|
+ CockpitMonitoringPointDeviceCard valid = byCode(devices, "EQ-VALID");
|
|
|
+ assertEquals(new BigDecimal("110.142"), valid.getLongitude());
|
|
|
+ assertEquals(new BigDecimal("28.453"), valid.getLatitude());
|
|
|
+ assertEquals("UNVERIFIED", valid.getCoordinateValidation().getState());
|
|
|
+ assertFalse(valid.getCoordinateValidation().isRenderable());
|
|
|
+
|
|
|
+ CockpitMonitoringPointDeviceCard missing = byCode(devices, "EQ-MISSING");
|
|
|
+ assertNull(missing.getLongitude());
|
|
|
+ assertNull(missing.getLatitude());
|
|
|
+ assertFalse(missing.getCoordinateValidation().isRenderable());
|
|
|
+ assertEquals("UNVERIFIED_COORDINATE", missing.getCoordinateValidation().getReason());
|
|
|
+
|
|
|
+ CockpitMonitoringPointDeviceCard invalid = byCode(devices, "EQ-INVALID");
|
|
|
+ assertEquals("INVALID", invalid.getCoordinateValidation().getState());
|
|
|
+ assertEquals("LONGITUDE_OUT_OF_RANGE", invalid.getCoordinateValidation().getReason());
|
|
|
+ assertFalse(invalid.getCoordinateValidation().isRenderable());
|
|
|
+
|
|
|
+ CockpitMonitoringPointDeviceCard unverified = byCode(devices, "EQ-UNVERIFIED");
|
|
|
+ assertEquals("UNVERIFIED", unverified.getCoordinateValidation().getState());
|
|
|
+ assertEquals("UNVERIFIED_COORDINATE", unverified.getCoordinateValidation().getReason());
|
|
|
+ assertFalse(unverified.getCoordinateValidation().isRenderable());
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ void snapshotTimestampMismatchFailsLikeMapSnapshot() {
|
|
|
+ CockpitMapSnapshot snapshot = snapshotWith("2026-09-02T11:00:00+08:00",
|
|
|
+ new CockpitMapSnapshot.SourceMonitoringPoint(
|
|
|
+ "MP-001", "供水干管监测点", null, "11",
|
|
|
+ new BigDecimal("110.141"), new BigDecimal("28.452"), false,
|
|
|
+ Collections.emptyList()));
|
|
|
+ when(mapAdapter.load(SNAPSHOT_AT)).thenReturn(snapshot);
|
|
|
+
|
|
|
+ assertThrows(CockpitSnapshotExpiredException.class,
|
|
|
+ () -> detailService.load("req", "MP-001", SNAPSHOT_AT));
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ void snapshotMissingPointReturnsNullLikeAdapterPath() {
|
|
|
+ CockpitMapSnapshot snapshot = snapshotWith(SNAPSHOT_AT,
|
|
|
+ new CockpitMapSnapshot.SourceMonitoringPoint(
|
|
|
+ "MP-OTHER", "另一个点位", null, "11",
|
|
|
+ new BigDecimal("110.1"), new BigDecimal("28.4"), false,
|
|
|
+ Collections.emptyList()));
|
|
|
+ when(mapAdapter.load(SNAPSHOT_AT)).thenReturn(snapshot);
|
|
|
+
|
|
|
+ assertThrows(com.zksy.cockpit.foundation.CockpitMonitoringPointNotFoundException.class,
|
|
|
+ () -> detailService.load("req", "MP-404", SNAPSHOT_AT));
|
|
|
+ verifyNoInteractions(detailAdapter);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ void withoutSnapshotAtFallsBackToDetailAdapterPath() {
|
|
|
+ when(detailAdapter.load("MP-001"))
|
|
|
+ .thenReturn(new com.zksy.cockpit.point.CockpitMonitoringPointDetail(
|
|
|
+ "MP-001", "供水干管监测点",
|
|
|
+ new BigDecimal("110.141"), new BigDecimal("28.452"), false,
|
|
|
+ Collections.singletonList(device("EQ-001", "110.142", "28.453",
|
|
|
+ false, true, false, false, false))));
|
|
|
+
|
|
|
+ CockpitMonitoringPointDetailResponse response =
|
|
|
+ detailService.load("req", "MP-001", null);
|
|
|
+ assertNotNull(response);
|
|
|
+ assertEquals(1, response.getDevices().size());
|
|
|
+ assertEquals("EQ-001", response.getDevices().get(0).getDeviceCode());
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ void twoParameterLoadDelegatesToNullSnapshotAt() {
|
|
|
+ when(detailAdapter.load("MP-001"))
|
|
|
+ .thenReturn(new com.zksy.cockpit.point.CockpitMonitoringPointDetail(
|
|
|
+ "MP-001", "供水干管监测点",
|
|
|
+ new BigDecimal("110.141"), new BigDecimal("28.452"), false,
|
|
|
+ Collections.singletonList(device("EQ-001", "110.142", "28.453",
|
|
|
+ false, true, false, false, false))));
|
|
|
+
|
|
|
+ CockpitMonitoringPointDetailResponse response =
|
|
|
+ detailService.load("req", "MP-001");
|
|
|
+ assertNotNull(response);
|
|
|
+ assertEquals(1, response.getDevices().size());
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ void malformedSnapshotAtFailsLikeMapSnapshotBeforeReading() {
|
|
|
+ assertThrows(CockpitInvalidRequestException.class,
|
|
|
+ () -> detailService.load("req", "MP-001", "not-a-timestamp"));
|
|
|
+ verifyNoInteractions(mapAdapter);
|
|
|
+ verifyNoInteractions(detailAdapter);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ void duplicateAndBlankDeviceCodesAreNormalizedLikeMapSnapshot() {
|
|
|
+ CockpitMapSnapshot snapshot = snapshotWith(SNAPSHOT_AT,
|
|
|
+ new CockpitMapSnapshot.SourceMonitoringPoint(
|
|
|
+ "MP-001", "供水干管监测点", null, "11",
|
|
|
+ new BigDecimal("110.141"), new BigDecimal("28.452"), false,
|
|
|
+ Arrays.asList(
|
|
|
+ device("EQ-001", "110.142", "28.453", false, true, false, false, false),
|
|
|
+ device(" EQ-001 ", "110.143", "28.454", false, true, false, false, false),
|
|
|
+ device(null, "110.144", "28.455", false, false, false, false, false),
|
|
|
+ device("", "110.145", "28.456", false, false, false, false, false),
|
|
|
+ device("EQ-002", null, null, false, false, false, false, false))));
|
|
|
+ when(mapAdapter.load(SNAPSHOT_AT)).thenReturn(snapshot);
|
|
|
+
|
|
|
+ CockpitMonitoringPointDetailResponse response =
|
|
|
+ detailService.load("req", "MP-001", SNAPSHOT_AT);
|
|
|
+
|
|
|
+ List<String> detailDeviceCodes = response.getDevices().stream()
|
|
|
+ .map(CockpitMonitoringPointDeviceCard::getDeviceCode)
|
|
|
+ .sorted(Comparator.nullsLast(String::compareTo))
|
|
|
+ .collect(Collectors.toList());
|
|
|
+
|
|
|
+ List<CockpitMonitoringPointSummary> points = mapService.load(
|
|
|
+ "req", 1, 500, SNAPSHOT_AT, null, null, null).getMonitoringPoints();
|
|
|
+ List<String> mapDeviceCodes = points.get(0).getDeviceCodes().stream()
|
|
|
+ .sorted(Comparator.nullsLast(String::compareTo))
|
|
|
+ .collect(Collectors.toList());
|
|
|
+
|
|
|
+ assertEquals(Arrays.asList("EQ-001", "EQ-002"), detailDeviceCodes,
|
|
|
+ "详情必须按 map 口径去重 trim 滤空码:EQ-001 只出现一次,空码/空白码剔除");
|
|
|
+ assertEquals(mapDeviceCodes, detailDeviceCodes,
|
|
|
+ "重复/空码数据下详情与 map-snapshot 集合仍必须一致");
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ void verifiedValidCoordinatesRender() {
|
|
|
+ CockpitMapSnapshot snapshot = snapshotWith(SNAPSHOT_AT,
|
|
|
+ new CockpitMapSnapshot.SourceMonitoringPoint(
|
|
|
+ "MP-001", "供水干管监测点", null, "11",
|
|
|
+ new BigDecimal("110.141"), new BigDecimal("28.452"), false,
|
|
|
+ Collections.singletonList(
|
|
|
+ device("EQ-VERIFIED", "110.142", "28.453",
|
|
|
+ true, true, false, false, false))));
|
|
|
+ when(mapAdapter.load(SNAPSHOT_AT)).thenReturn(snapshot);
|
|
|
+
|
|
|
+ CockpitMonitoringPointDetailResponse response =
|
|
|
+ detailService.load("req", "MP-001", SNAPSHOT_AT);
|
|
|
+
|
|
|
+ CockpitMonitoringPointDeviceCard verified = byCode(response.getDevices(), "EQ-VERIFIED");
|
|
|
+ assertEquals(new BigDecimal("110.142"), verified.getLongitude());
|
|
|
+ assertEquals(new BigDecimal("28.453"), verified.getLatitude());
|
|
|
+ assertEquals("VALID", verified.getCoordinateValidation().getState());
|
|
|
+ assertTrue(verified.getCoordinateValidation().isRenderable());
|
|
|
+ }
|
|
|
+
|
|
|
+ private static CockpitMapSnapshot snapshotWith(String snapshotAt,
|
|
|
+ CockpitMapSnapshot.SourceMonitoringPoint point) {
|
|
|
+ return new CockpitMapSnapshot(snapshotAt, Collections.emptyList(),
|
|
|
+ Collections.singletonList(point), Collections.emptyList());
|
|
|
+ }
|
|
|
+
|
|
|
+ private static CockpitMonitoringPointDeviceCard byCode(
|
|
|
+ List<CockpitMonitoringPointDeviceCard> devices, String code) {
|
|
|
+ return devices.stream()
|
|
|
+ .filter(device -> code.equals(device.getDeviceCode()))
|
|
|
+ .findFirst()
|
|
|
+ .orElseThrow(() -> new AssertionError("missing device " + code));
|
|
|
+ }
|
|
|
+
|
|
|
+ private static CockpitMonitoringPointDeviceSnapshot device(String code, String longitude, String latitude,
|
|
|
+ boolean coordinateVerified, boolean online, boolean alarm, boolean warning,
|
|
|
+ boolean workOrderInProgress) {
|
|
|
+ return new CockpitMonitoringPointDeviceSnapshot(
|
|
|
+ code, code + "设备", "压力计",
|
|
|
+ longitude == null ? null : new BigDecimal(longitude),
|
|
|
+ latitude == null ? null : new BigDecimal(latitude),
|
|
|
+ coordinateVerified,
|
|
|
+ online, alarm, warning, workOrderInProgress,
|
|
|
+ Collections.emptyList(), null);
|
|
|
+ }
|
|
|
+}
|