|
@@ -6,13 +6,21 @@ import com.zksy.base.domain.MonitorPoint;
|
|
|
import com.zksy.base.service.EquipmentBaseService;
|
|
import com.zksy.base.service.EquipmentBaseService;
|
|
|
import com.zksy.base.service.EquipmentTypeService;
|
|
import com.zksy.base.service.EquipmentTypeService;
|
|
|
import com.zksy.base.service.MonitorPointService;
|
|
import com.zksy.base.service.MonitorPointService;
|
|
|
|
|
+import com.zksy.cockpit.foundation.CockpitCoordinateValidator;
|
|
|
|
|
+import com.zksy.cockpit.foundation.CockpitSnapshotExpiredException;
|
|
|
import com.zksy.cockpit.map.BusinessCockpitMapSnapshotAdapter;
|
|
import com.zksy.cockpit.map.BusinessCockpitMapSnapshotAdapter;
|
|
|
import com.zksy.cockpit.map.CockpitMapSnapshot;
|
|
import com.zksy.cockpit.map.CockpitMapSnapshot;
|
|
|
import com.zksy.cockpit.map.CockpitMapSnapshotService;
|
|
import com.zksy.cockpit.map.CockpitMapSnapshotService;
|
|
|
import com.zksy.cockpit.point.BusinessCockpitDeviceFactResolver;
|
|
import com.zksy.cockpit.point.BusinessCockpitDeviceFactResolver;
|
|
|
|
|
+import com.zksy.cockpit.point.CockpitMonitoringPointDetailAdapter;
|
|
|
|
|
+import com.zksy.cockpit.point.CockpitMonitoringPointDetailService;
|
|
|
|
|
+import com.zksy.cockpit.point.CockpitMonitoringPointDeviceCard;
|
|
|
import com.zksy.cockpit.point.CockpitMonitoringPointDeviceSnapshot;
|
|
import com.zksy.cockpit.point.CockpitMonitoringPointDeviceSnapshot;
|
|
|
|
|
+import com.zksy.framework.config.FastJson2JsonRedisSerializer;
|
|
|
import org.junit.jupiter.api.BeforeEach;
|
|
import org.junit.jupiter.api.BeforeEach;
|
|
|
import org.junit.jupiter.api.Test;
|
|
import org.junit.jupiter.api.Test;
|
|
|
|
|
+import org.springframework.data.redis.core.RedisTemplate;
|
|
|
|
|
+import org.springframework.data.redis.core.ValueOperations;
|
|
|
|
|
|
|
|
import java.math.BigDecimal;
|
|
import java.math.BigDecimal;
|
|
|
import java.time.Clock;
|
|
import java.time.Clock;
|
|
@@ -21,15 +29,22 @@ import java.time.ZoneOffset;
|
|
|
import java.util.Arrays;
|
|
import java.util.Arrays;
|
|
|
import java.util.Collections;
|
|
import java.util.Collections;
|
|
|
import java.util.List;
|
|
import java.util.List;
|
|
|
|
|
+import java.util.Map;
|
|
|
|
|
+import java.util.concurrent.ConcurrentHashMap;
|
|
|
|
|
|
|
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
|
|
import static org.junit.jupiter.api.Assertions.assertFalse;
|
|
import static org.junit.jupiter.api.Assertions.assertFalse;
|
|
|
import static org.junit.jupiter.api.Assertions.assertNull;
|
|
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.junit.jupiter.api.Assertions.assertTrue;
|
|
|
import static org.mockito.ArgumentMatchers.any;
|
|
import static org.mockito.ArgumentMatchers.any;
|
|
|
|
|
+import static org.mockito.ArgumentMatchers.anyLong;
|
|
|
import static org.mockito.ArgumentMatchers.anyString;
|
|
import static org.mockito.ArgumentMatchers.anyString;
|
|
|
import static org.mockito.ArgumentMatchers.eq;
|
|
import static org.mockito.ArgumentMatchers.eq;
|
|
|
import static org.mockito.Mockito.mock;
|
|
import static org.mockito.Mockito.mock;
|
|
|
|
|
+import static org.mockito.Mockito.never;
|
|
|
|
|
+import static org.mockito.Mockito.verify;
|
|
|
|
|
+import static org.mockito.Mockito.verifyNoInteractions;
|
|
|
import static org.mockito.Mockito.when;
|
|
import static org.mockito.Mockito.when;
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
@@ -127,7 +142,7 @@ class BusinessCockpitMapSnapshotAdapterTest {
|
|
|
stubTypes(Collections.singletonList(type(99, "mystery_type", "未知类型", "9")));
|
|
stubTypes(Collections.singletonList(type(99, "mystery_type", "未知类型", "9")));
|
|
|
stubDevices(Collections.emptyList(), Collections.emptyList());
|
|
stubDevices(Collections.emptyList(), Collections.emptyList());
|
|
|
|
|
|
|
|
- CockpitMapSnapshot snapshot = adapter.load("2026-09-02T10:00:00+08:00");
|
|
|
|
|
|
|
+ CockpitMapSnapshot snapshot = adapter.load(null);
|
|
|
|
|
|
|
|
assertEquals(1, snapshot.getTypes().size());
|
|
assertEquals(1, snapshot.getTypes().size());
|
|
|
assertNull(snapshot.getTypes().get(0).getIndustry(), "未知类型根不得静默归类");
|
|
assertNull(snapshot.getTypes().get(0).getIndustry(), "未知类型根不得静默归类");
|
|
@@ -146,7 +161,298 @@ class BusinessCockpitMapSnapshotAdapterTest {
|
|
|
"窨井根映射为 MANHOLE 传值,交由服务层判非正式行业,而非在适配器吞掉");
|
|
"窨井根映射为 MANHOLE 传值,交由服务层判非正式行业,而非在适配器吞掉");
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- // ---- stubbing helpers ----
|
|
|
|
|
|
|
+ @Test
|
|
|
|
|
+ void nestedEquipmentTypeResolvesIndustryFromRoot() {
|
|
|
|
|
+ stubPoints(Collections.emptyList());
|
|
|
|
|
+ stubTypes(Arrays.asList(
|
|
|
|
|
+ type(4, "manhole_root", "窨井设备", "0"),
|
|
|
|
|
+ type(40, "manhole_family", "窨井监测族", "4"),
|
|
|
|
|
+ type(41, "manhole_sensor", "窨井传感器", "40")));
|
|
|
|
|
+ stubDevices(Collections.emptyList(), Collections.emptyList());
|
|
|
|
|
+
|
|
|
|
|
+ CockpitMapSnapshot snapshot = adapter.load(null);
|
|
|
|
|
+
|
|
|
|
|
+ assertEquals("MANHOLE", snapshot.getTypes().stream()
|
|
|
|
|
+ .filter(item -> "41".equals(item.getCode()))
|
|
|
|
|
+ .findFirst().orElseThrow(AssertionError::new).getIndustry());
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Test
|
|
|
|
|
+ void sameSnapshotAtReusesTheFirstSourceSnapshotAfterRelationsChange() {
|
|
|
|
|
+ String snapshotAt = "2026-09-02T10:00:00+08:00";
|
|
|
|
|
+ MonitorPoint point = point("P-1", "POINT-1", "点位一", new BigDecimal("110.4"), new BigDecimal("28.4"));
|
|
|
|
|
+ EquipmentType equipmentType = type(11, "pressure", "压力计", "1");
|
|
|
|
|
+ EquipmentBase firstDevice = device("D-1", "DEVICE-1", "设备一", "11",
|
|
|
|
|
+ new BigDecimal("110.401"), new BigDecimal("28.401"));
|
|
|
|
|
+ EquipmentBase secondDevice = device("D-2", "DEVICE-2", "设备二", "11",
|
|
|
|
|
+ new BigDecimal("110.402"), new BigDecimal("28.402"));
|
|
|
|
|
+ stubPoints(Collections.singletonList(point));
|
|
|
|
|
+ stubTypes(Collections.singletonList(equipmentType));
|
|
|
|
|
+ stubDevices(Collections.singletonList(firstDevice), Collections.singletonList(firstDevice));
|
|
|
|
|
+ when(deviceFactResolver.resolve(any(), anyString())).thenAnswer(invocation -> {
|
|
|
|
|
+ EquipmentBase device = invocation.getArgument(0);
|
|
|
|
|
+ return fact(device.getEquipmentCode());
|
|
|
|
|
+ });
|
|
|
|
|
+
|
|
|
|
|
+ CockpitMapSnapshot first = adapter.load(null);
|
|
|
|
|
+ assertEquals(snapshotAt, first.getSnapshotAt());
|
|
|
|
|
+
|
|
|
|
|
+ stubDevices(Collections.singletonList(secondDevice), Collections.singletonList(secondDevice));
|
|
|
|
|
+ CockpitMapSnapshot second = adapter.load(snapshotAt);
|
|
|
|
|
+
|
|
|
|
|
+ assertEquals(Collections.singletonList("DEVICE-1"),
|
|
|
|
|
+ first.getMonitoringPoints().get(0).getDevices().stream()
|
|
|
|
|
+ .map(CockpitMonitoringPointDeviceSnapshot::getDeviceCode)
|
|
|
|
|
+ .collect(java.util.stream.Collectors.toList()));
|
|
|
|
|
+ assertEquals(Collections.singletonList("DEVICE-1"),
|
|
|
|
|
+ second.getMonitoringPoints().get(0).getDevices().stream()
|
|
|
|
|
+ .map(CockpitMonitoringPointDeviceSnapshot::getDeviceCode)
|
|
|
|
|
+ .collect(java.util.stream.Collectors.toList()),
|
|
|
|
|
+ "同一 snapshotAt 必须复用首次读取的点位设备关系");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Test
|
|
|
|
|
+ void mapSnapshotRoundTripsThroughRedisJsonSerializer() {
|
|
|
|
|
+ CockpitMonitoringPointDeviceSnapshot device = new CockpitMonitoringPointDeviceSnapshot(
|
|
|
|
|
+ "DEVICE-1", "设备一", "压力计", new BigDecimal("110.4"), new BigDecimal("28.4"),
|
|
|
|
|
+ CockpitCoordinateValidator.validate(
|
|
|
|
|
+ new BigDecimal("110.4"), new BigDecimal("28.4"), false),
|
|
|
|
|
+ false, false, false, false, Collections.emptyList(), null);
|
|
|
|
|
+ CockpitMapSnapshot snapshot = new CockpitMapSnapshot("2026-09-02T10:00:00+08:00",
|
|
|
|
|
+ Collections.emptyList(),
|
|
|
|
|
+ Collections.singletonList(new CockpitMapSnapshot.SourceMonitoringPoint(
|
|
|
|
|
+ "POINT-1", "点位一", null, null, null, null, false,
|
|
|
|
|
+ Collections.singletonList(device))),
|
|
|
|
|
+ Collections.emptyList());
|
|
|
|
|
+ FastJson2JsonRedisSerializer<Object> serializer = new FastJson2JsonRedisSerializer<>(Object.class);
|
|
|
|
|
+
|
|
|
|
|
+ Object restored = serializer.deserialize(serializer.serialize(snapshot));
|
|
|
|
|
+
|
|
|
|
|
+ assertTrue(restored instanceof CockpitMapSnapshot);
|
|
|
|
|
+ CockpitMapSnapshot restoredSnapshot = (CockpitMapSnapshot) restored;
|
|
|
|
|
+ CockpitMonitoringPointDeviceSnapshot restoredDevice =
|
|
|
|
|
+ restoredSnapshot.getMonitoringPoints().get(0).getDevices().get(0);
|
|
|
|
|
+ assertEquals("DEVICE-1", restoredDevice.getDeviceCode());
|
|
|
|
|
+ assertEquals(new BigDecimal("110.4"), restoredDevice.getLongitude());
|
|
|
|
|
+ assertEquals("UNVERIFIED", restoredDevice.getCoordinateValidation().getState());
|
|
|
|
|
+ assertEquals("UNVERIFIED_COORDINATE", restoredDevice.getCoordinateValidation().getReason());
|
|
|
|
|
+ assertFalse(restoredDevice.getCoordinateValidation().isRenderable());
|
|
|
|
|
+ assertFalse(restoredDevice.isOnline());
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Test
|
|
|
|
|
+ void separateAdaptersShareTheFirstSnapshotThroughRedis() {
|
|
|
|
|
+ RedisTemplate<Object, Object> sharedRedis = mock(RedisTemplate.class);
|
|
|
|
|
+ ValueOperations<Object, Object> values = mock(ValueOperations.class);
|
|
|
|
|
+ Map<Object, Object> sharedValues = new ConcurrentHashMap<>();
|
|
|
|
|
+ when(sharedRedis.opsForValue()).thenReturn(values);
|
|
|
|
|
+ when(values.get(any())).thenAnswer(invocation -> sharedValues.get(invocation.getArgument(0)));
|
|
|
|
|
+ when(values.setIfAbsent(any(), any(), anyLong(), eq(java.util.concurrent.TimeUnit.SECONDS)))
|
|
|
|
|
+ .thenAnswer(invocation -> sharedValues.putIfAbsent(
|
|
|
|
|
+ invocation.getArgument(0), invocation.getArgument(1)) == null);
|
|
|
|
|
+
|
|
|
|
|
+ String snapshotAt = "2026-09-02T10:00:00+08:00";
|
|
|
|
|
+ MonitorPoint point = point("P-1", "POINT-1", "点位一", null, null);
|
|
|
|
|
+ EquipmentBase firstDevice = device("D-1", "DEVICE-1", "设备一", "11", null, null);
|
|
|
|
|
+ EquipmentBase changedDevice = device("D-2", "DEVICE-2", "设备二", "11", null, null);
|
|
|
|
|
+ stubPoints(Collections.singletonList(point));
|
|
|
|
|
+ stubTypes(Collections.singletonList(type(11, "pressure", "压力计", "1")));
|
|
|
|
|
+ stubDevices(Collections.singletonList(firstDevice), Collections.singletonList(firstDevice));
|
|
|
|
|
+ when(deviceFactResolver.resolve(any(), anyString())).thenAnswer(invocation ->
|
|
|
|
|
+ fact(invocation.<EquipmentBase>getArgument(0).getEquipmentCode()));
|
|
|
|
|
+ BusinessCockpitMapSnapshotAdapter firstAdapter = new BusinessCockpitMapSnapshotAdapter(
|
|
|
|
|
+ monitorPointService, equipmentBaseService, equipmentTypeService, deviceFactResolver,
|
|
|
|
|
+ Clock.fixed(Instant.parse("2026-09-02T02:00:00Z"), ZoneOffset.UTC), sharedRedis);
|
|
|
|
|
+
|
|
|
|
|
+ CockpitMapSnapshot first = firstAdapter.load(null);
|
|
|
|
|
+ assertEquals(snapshotAt, first.getSnapshotAt());
|
|
|
|
|
+ verify(values).setIfAbsent(eq("cockpit:map-snapshot:" + snapshotAt), any(), eq(600L),
|
|
|
|
|
+ eq(java.util.concurrent.TimeUnit.SECONDS));
|
|
|
|
|
+ stubDevices(Collections.singletonList(changedDevice), Collections.singletonList(changedDevice));
|
|
|
|
|
+ BusinessCockpitMapSnapshotAdapter secondAdapter = new BusinessCockpitMapSnapshotAdapter(
|
|
|
|
|
+ monitorPointService, equipmentBaseService, equipmentTypeService, deviceFactResolver,
|
|
|
|
|
+ Clock.fixed(Instant.parse("2026-09-02T02:00:00Z"), ZoneOffset.UTC), sharedRedis);
|
|
|
|
|
+ CockpitMapSnapshot second = secondAdapter.load(snapshotAt);
|
|
|
|
|
+
|
|
|
|
|
+ assertEquals("DEVICE-1", first.getMonitoringPoints().get(0).getDevices().get(0).getDeviceCode());
|
|
|
|
|
+ assertEquals("DEVICE-1", second.getMonitoringPoints().get(0).getDevices().get(0).getDeviceCode());
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Test
|
|
|
|
|
+ void mapPaginationKeepsDeviceCodesFromTheSameSnapshot() {
|
|
|
|
|
+ String snapshotAt = "2026-09-02T10:00:00+08:00";
|
|
|
|
|
+ MonitorPoint firstPoint = point("P-1", "POINT-1", "点位一", null, null);
|
|
|
|
|
+ MonitorPoint secondPoint = point("P-2", "POINT-2", "点位二", null, null);
|
|
|
|
|
+ EquipmentBase firstDevice = device("D-1", "DEVICE-1", "设备一", "11", null, null);
|
|
|
|
|
+ EquipmentBase secondDevice = device("D-2", "DEVICE-2", "设备二", "11", null, null);
|
|
|
|
|
+ stubPoints(Arrays.asList(firstPoint, secondPoint));
|
|
|
|
|
+ stubTypes(Collections.singletonList(type(11, "pressure", "压力计", "1")));
|
|
|
|
|
+ when(equipmentBaseService.list()).thenReturn(Arrays.asList(firstDevice, secondDevice));
|
|
|
|
|
+ when(equipmentBaseService.getEquipmentByPoint("P-1"))
|
|
|
|
|
+ .thenReturn(Collections.singletonList(firstDevice));
|
|
|
|
|
+ when(equipmentBaseService.getEquipmentByPoint("P-2"))
|
|
|
|
|
+ .thenReturn(Collections.singletonList(secondDevice));
|
|
|
|
|
+ when(deviceFactResolver.resolve(any(), anyString())).thenAnswer(invocation ->
|
|
|
|
|
+ fact(invocation.<EquipmentBase>getArgument(0).getEquipmentCode()));
|
|
|
|
|
+ CockpitMapSnapshotService service = new CockpitMapSnapshotService(adapter,
|
|
|
|
|
+ Clock.fixed(Instant.parse("2026-09-02T02:00:00Z"), ZoneOffset.UTC));
|
|
|
|
|
+
|
|
|
|
|
+ var firstPage = service.load("req", 1, 1, null, null, null, null);
|
|
|
|
|
+ assertEquals(snapshotAt, firstPage.getSnapshotAt());
|
|
|
|
|
+ when(equipmentBaseService.getEquipmentByPoint("P-2"))
|
|
|
|
|
+ .thenReturn(Collections.singletonList(firstDevice));
|
|
|
|
|
+ var secondPage = service.load("req", 2, 1, snapshotAt, null, null, null);
|
|
|
|
|
+
|
|
|
|
|
+ CockpitMonitoringPointDetailService detailService = new CockpitMonitoringPointDetailService(
|
|
|
|
|
+ mock(CockpitMonitoringPointDetailAdapter.class), adapter,
|
|
|
|
|
+ Clock.fixed(Instant.parse("2026-09-02T02:00:00Z"), ZoneOffset.UTC));
|
|
|
|
|
+ var detail = detailService.load("req", "POINT-2", snapshotAt);
|
|
|
|
|
+
|
|
|
|
|
+ assertEquals("POINT-2", secondPage.getMonitoringPoints().get(0).getPointCode());
|
|
|
|
|
+ assertEquals(Collections.singletonList("DEVICE-2"),
|
|
|
|
|
+ secondPage.getMonitoringPoints().get(0).getDeviceCodes());
|
|
|
|
|
+ assertEquals(secondPage.getMonitoringPoints().get(0).getDeviceCodes(),
|
|
|
|
|
+ detail.getDevices().stream()
|
|
|
|
|
+ .map(CockpitMonitoringPointDeviceCard::getDeviceCode)
|
|
|
|
|
+ .collect(java.util.stream.Collectors.toList()));
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Test
|
|
|
|
|
+ void redisTtlRoundsUpToTheSnapshotExpiryBoundary() {
|
|
|
|
|
+ RedisTemplate<Object, Object> sharedRedis = mock(RedisTemplate.class);
|
|
|
|
|
+ ValueOperations<Object, Object> values = mock(ValueOperations.class);
|
|
|
|
|
+ when(sharedRedis.opsForValue()).thenReturn(values);
|
|
|
|
|
+ when(values.get(any())).thenReturn(null);
|
|
|
|
|
+ when(values.setIfAbsent(any(), any(), anyLong(), eq(java.util.concurrent.TimeUnit.SECONDS)))
|
|
|
|
|
+ .thenReturn(Boolean.TRUE);
|
|
|
|
|
+ when(monitorPointService.list()).thenReturn(Collections.emptyList());
|
|
|
|
|
+ when(equipmentBaseService.list()).thenReturn(Collections.emptyList());
|
|
|
|
|
+ when(equipmentTypeService.list()).thenReturn(Collections.emptyList());
|
|
|
|
|
+ Clock advancingClock = mock(Clock.class);
|
|
|
|
|
+ when(advancingClock.getZone()).thenReturn(ZoneOffset.UTC);
|
|
|
|
|
+ when(advancingClock.instant()).thenReturn(
|
|
|
|
|
+ Instant.parse("2026-09-02T02:00:00.900Z"),
|
|
|
|
|
+ Instant.parse("2026-09-02T02:00:00.900Z"),
|
|
|
|
|
+ Instant.parse("2026-09-02T02:00:00.950Z"),
|
|
|
|
|
+ Instant.parse("2026-09-02T02:00:00.950Z"));
|
|
|
|
|
+ BusinessCockpitMapSnapshotAdapter fractionalClockAdapter = new BusinessCockpitMapSnapshotAdapter(
|
|
|
|
|
+ monitorPointService, equipmentBaseService, equipmentTypeService, deviceFactResolver,
|
|
|
|
|
+ advancingClock, sharedRedis);
|
|
|
|
|
+
|
|
|
|
|
+ CockpitMapSnapshot issued = fractionalClockAdapter.load(null);
|
|
|
|
|
+
|
|
|
|
|
+ verify(values).setIfAbsent(eq("cockpit:map-snapshot:" + issued.getSnapshotAt()), any(), eq(600L),
|
|
|
|
|
+ eq(java.util.concurrent.TimeUnit.SECONDS));
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Test
|
|
|
|
|
+ void snapshotThatExpiresDuringBuildIsNotPublishedToRedis() {
|
|
|
|
|
+ RedisTemplate<Object, Object> sharedRedis = mock(RedisTemplate.class);
|
|
|
|
|
+ ValueOperations<Object, Object> values = mock(ValueOperations.class);
|
|
|
|
|
+ Clock advancingClock = mock(Clock.class);
|
|
|
|
|
+ when(sharedRedis.opsForValue()).thenReturn(values);
|
|
|
|
|
+ when(values.get(any())).thenReturn(null);
|
|
|
|
|
+ when(advancingClock.getZone()).thenReturn(ZoneOffset.UTC);
|
|
|
|
|
+ when(advancingClock.instant()).thenReturn(
|
|
|
|
|
+ Instant.parse("2026-09-02T02:00:00Z"),
|
|
|
|
|
+ Instant.parse("2026-09-02T02:00:00Z"),
|
|
|
|
|
+ Instant.parse("2026-09-02T02:10:00.001Z"));
|
|
|
|
|
+ when(monitorPointService.list()).thenReturn(Collections.emptyList());
|
|
|
|
|
+ when(equipmentBaseService.list()).thenReturn(Collections.emptyList());
|
|
|
|
|
+ when(equipmentTypeService.list()).thenReturn(Collections.emptyList());
|
|
|
|
|
+ BusinessCockpitMapSnapshotAdapter expiringAdapter = new BusinessCockpitMapSnapshotAdapter(
|
|
|
|
|
+ monitorPointService, equipmentBaseService, equipmentTypeService, deviceFactResolver,
|
|
|
|
|
+ advancingClock, sharedRedis);
|
|
|
|
|
+
|
|
|
|
|
+ assertThrows(CockpitSnapshotExpiredException.class,
|
|
|
|
|
+ () -> expiringAdapter.load(null));
|
|
|
|
|
+ verify(values, never()).setIfAbsent(any(), any(), anyLong(), any());
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Test
|
|
|
|
|
+ void cachedSnapshotIsRejectedWhenClockExpiresBeforeReturn() {
|
|
|
|
|
+ Clock advancingClock = mock(Clock.class);
|
|
|
|
|
+ when(advancingClock.getZone()).thenReturn(ZoneOffset.UTC);
|
|
|
|
|
+ when(advancingClock.instant()).thenReturn(
|
|
|
|
|
+ Instant.parse("2026-09-02T02:00:00Z"),
|
|
|
|
|
+ Instant.parse("2026-09-02T02:00:00Z"),
|
|
|
|
|
+ Instant.parse("2026-09-02T02:00:00Z"),
|
|
|
|
|
+ Instant.parse("2026-09-02T02:00:00Z"),
|
|
|
|
|
+ Instant.parse("2026-09-02T02:09:59.999Z"),
|
|
|
|
|
+ Instant.parse("2026-09-02T02:10:00.001Z"));
|
|
|
|
|
+ when(monitorPointService.list()).thenReturn(Collections.emptyList());
|
|
|
|
|
+ when(equipmentBaseService.list()).thenReturn(Collections.emptyList());
|
|
|
|
|
+ when(equipmentTypeService.list()).thenReturn(Collections.emptyList());
|
|
|
|
|
+ BusinessCockpitMapSnapshotAdapter advancingAdapter = new BusinessCockpitMapSnapshotAdapter(
|
|
|
|
|
+ monitorPointService, equipmentBaseService, equipmentTypeService, deviceFactResolver, advancingClock);
|
|
|
|
|
+ String snapshotAt = "2026-09-02T10:00:00+08:00";
|
|
|
|
|
+ advancingAdapter.load(null);
|
|
|
|
|
+
|
|
|
|
|
+ assertThrows(CockpitSnapshotExpiredException.class, () -> advancingAdapter.load(snapshotAt));
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Test
|
|
|
|
|
+ void unissuedSnapshotAtIsExpiredWithoutReadingCurrentRelations() {
|
|
|
|
|
+ assertThrows(CockpitSnapshotExpiredException.class,
|
|
|
|
|
+ () -> adapter.load("2026-09-02T10:00:01+08:00"));
|
|
|
|
|
+
|
|
|
|
|
+ verifyNoInteractions(monitorPointService, equipmentBaseService, equipmentTypeService);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Test
|
|
|
|
|
+ void missingSharedSnapshotAtIsExpiredWithoutRebuilding() {
|
|
|
|
|
+ RedisTemplate<Object, Object> sharedRedis = mock(RedisTemplate.class);
|
|
|
|
|
+ ValueOperations<Object, Object> values = mock(ValueOperations.class);
|
|
|
|
|
+ when(sharedRedis.opsForValue()).thenReturn(values);
|
|
|
|
|
+ when(values.get(any())).thenReturn(null);
|
|
|
|
|
+ BusinessCockpitMapSnapshotAdapter sharedAdapter = new BusinessCockpitMapSnapshotAdapter(
|
|
|
|
|
+ monitorPointService, equipmentBaseService, equipmentTypeService, deviceFactResolver,
|
|
|
|
|
+ Clock.fixed(Instant.parse("2026-09-02T02:00:00Z"), ZoneOffset.UTC), sharedRedis);
|
|
|
|
|
+
|
|
|
|
|
+ assertThrows(CockpitSnapshotExpiredException.class,
|
|
|
|
|
+ () -> sharedAdapter.load("2026-09-02T10:00:01+08:00"));
|
|
|
|
|
+
|
|
|
|
|
+ verify(values, never()).setIfAbsent(any(), any(), anyLong(), any());
|
|
|
|
|
+ verifyNoInteractions(monitorPointService, equipmentBaseService, equipmentTypeService);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Test
|
|
|
|
|
+ void expiredSnapshotAtIsRejectedInsteadOfLoadingCurrentRelations() {
|
|
|
|
|
+ when(monitorPointService.list()).thenReturn(Collections.emptyList());
|
|
|
|
|
+
|
|
|
|
|
+ assertThrows(CockpitSnapshotExpiredException.class,
|
|
|
|
|
+ () -> adapter.load("2026-09-02T09:50:00+08:00"));
|
|
|
|
|
+ assertThrows(CockpitSnapshotExpiredException.class,
|
|
|
|
|
+ () -> adapter.load("2026-09-02T09:49:59+08:00"));
|
|
|
|
|
+ assertThrows(CockpitSnapshotExpiredException.class,
|
|
|
|
|
+ () -> adapter.load("2026-09-02T10:10:00+08:00"));
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Test
|
|
|
|
|
+ void differentSnapshotAtReadsASeparateSourceSnapshot() {
|
|
|
|
|
+ String firstSnapshotAt = "2026-09-02T10:00:00+08:00";
|
|
|
|
|
+ String secondSnapshotAt = "2026-09-02T10:01:00+08:00";
|
|
|
|
|
+ MonitorPoint point = point("P-1", "POINT-1", "点位一", new BigDecimal("110.4"), new BigDecimal("28.4"));
|
|
|
|
|
+ stubPoints(Collections.singletonList(point));
|
|
|
|
|
+ stubTypes(Collections.singletonList(type(11, "pressure", "压力计", "1")));
|
|
|
|
|
+ EquipmentBase firstDevice = device("D-1", "DEVICE-1", "设备一", "11", null, null);
|
|
|
|
|
+ EquipmentBase secondDevice = device("D-2", "DEVICE-2", "设备二", "11", null, null);
|
|
|
|
|
+ stubDevices(Collections.singletonList(firstDevice), Collections.singletonList(firstDevice));
|
|
|
|
|
+ when(deviceFactResolver.resolve(any(), anyString())).thenAnswer(invocation ->
|
|
|
|
|
+ fact(invocation.<EquipmentBase>getArgument(0).getEquipmentCode()));
|
|
|
|
|
+
|
|
|
|
|
+ CockpitMapSnapshot first = adapter.load(null);
|
|
|
|
|
+ stubDevices(Collections.singletonList(secondDevice), Collections.singletonList(secondDevice));
|
|
|
|
|
+ BusinessCockpitMapSnapshotAdapter laterAdapter = new BusinessCockpitMapSnapshotAdapter(
|
|
|
|
|
+ monitorPointService, equipmentBaseService, equipmentTypeService, deviceFactResolver,
|
|
|
|
|
+ Clock.fixed(Instant.parse("2026-09-02T02:01:00Z"), ZoneOffset.UTC));
|
|
|
|
|
+ CockpitMapSnapshot second = laterAdapter.load(null);
|
|
|
|
|
+
|
|
|
|
|
+ assertEquals(firstSnapshotAt, first.getSnapshotAt());
|
|
|
|
|
+ assertEquals(secondSnapshotAt, second.getSnapshotAt());
|
|
|
|
|
+ assertEquals("DEVICE-1", first.getMonitoringPoints().get(0).getDevices().get(0).getDeviceCode());
|
|
|
|
|
+ assertEquals("DEVICE-2", second.getMonitoringPoints().get(0).getDevices().get(0).getDeviceCode());
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
private void stubPoints(List<MonitorPoint> points) {
|
|
private void stubPoints(List<MonitorPoint> points) {
|
|
|
when(monitorPointService.list()).thenReturn(points);
|
|
when(monitorPointService.list()).thenReturn(points);
|