|
|
@@ -6,13 +6,17 @@ import com.zksy.base.domain.MonitorPoint;
|
|
|
import com.zksy.base.service.EquipmentBaseService;
|
|
|
import com.zksy.base.service.EquipmentTypeService;
|
|
|
import com.zksy.base.service.MonitorPointService;
|
|
|
+import com.zksy.cockpit.foundation.CockpitSnapshotExpiredException;
|
|
|
import com.zksy.cockpit.map.BusinessCockpitMapSnapshotAdapter;
|
|
|
import com.zksy.cockpit.map.CockpitMapSnapshot;
|
|
|
import com.zksy.cockpit.map.CockpitMapSnapshotService;
|
|
|
import com.zksy.cockpit.point.BusinessCockpitDeviceFactResolver;
|
|
|
import com.zksy.cockpit.point.CockpitMonitoringPointDeviceSnapshot;
|
|
|
+import com.zksy.framework.config.FastJson2JsonRedisSerializer;
|
|
|
import org.junit.jupiter.api.BeforeEach;
|
|
|
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.time.Clock;
|
|
|
@@ -21,12 +25,16 @@ import java.time.ZoneOffset;
|
|
|
import java.util.Arrays;
|
|
|
import java.util.Collections;
|
|
|
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.assertFalse;
|
|
|
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.ArgumentMatchers.any;
|
|
|
+import static org.mockito.ArgumentMatchers.anyLong;
|
|
|
import static org.mockito.ArgumentMatchers.anyString;
|
|
|
import static org.mockito.ArgumentMatchers.eq;
|
|
|
import static org.mockito.Mockito.mock;
|
|
|
@@ -179,6 +187,96 @@ class BusinessCockpitMapSnapshotAdapterTest {
|
|
|
"同一 snapshotAt 必须复用首次读取的点位设备关系");
|
|
|
}
|
|
|
|
|
|
+ @Test
|
|
|
+ void mapSnapshotRoundTripsThroughRedisJsonSerializer() {
|
|
|
+ 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(fact("DEVICE-1")))),
|
|
|
+ Collections.emptyList());
|
|
|
+ FastJson2JsonRedisSerializer<Object> serializer = new FastJson2JsonRedisSerializer<>(Object.class);
|
|
|
+
|
|
|
+ Object restored = serializer.deserialize(serializer.serialize(snapshot));
|
|
|
+
|
|
|
+ assertTrue(restored instanceof CockpitMapSnapshot);
|
|
|
+ CockpitMapSnapshot restoredSnapshot = (CockpitMapSnapshot) restored;
|
|
|
+ assertEquals("DEVICE-1", restoredSnapshot.getMonitoringPoints().get(0)
|
|
|
+ .getDevices().get(0).getDeviceCode());
|
|
|
+ }
|
|
|
+
|
|
|
+ @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.MINUTES)))
|
|
|
+ .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(snapshotAt);
|
|
|
+ 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));
|
|
|
+
|
|
|
+ service.load("req", 1, 1, snapshotAt, null, null, null);
|
|
|
+ when(equipmentBaseService.getEquipmentByPoint("P-2"))
|
|
|
+ .thenReturn(Collections.singletonList(firstDevice));
|
|
|
+ var secondPage = service.load("req", 2, 1, snapshotAt, null, null, null);
|
|
|
+
|
|
|
+ assertEquals("POINT-2", secondPage.getMonitoringPoints().get(0).getPointCode());
|
|
|
+ assertEquals(Collections.singletonList("DEVICE-2"),
|
|
|
+ secondPage.getMonitoringPoints().get(0).getDeviceCodes());
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ void expiredSnapshotAtIsRejectedInsteadOfLoadingCurrentRelations() {
|
|
|
+ when(monitorPointService.list()).thenReturn(Collections.emptyList());
|
|
|
+
|
|
|
+ assertThrows(CockpitSnapshotExpiredException.class,
|
|
|
+ () -> adapter.load("2026-09-02T09:49:59+08:00"));
|
|
|
+ }
|
|
|
+
|
|
|
@Test
|
|
|
void differentSnapshotAtReadsASeparateSourceSnapshot() {
|
|
|
String firstSnapshotAt = "2026-09-02T10:00:00+08:00";
|