|
|
@@ -44,6 +44,7 @@ import static org.mockito.ArgumentMatchers.eq;
|
|
|
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;
|
|
|
|
|
|
/**
|
|
|
@@ -141,7 +142,7 @@ class BusinessCockpitMapSnapshotAdapterTest {
|
|
|
stubTypes(Collections.singletonList(type(99, "mystery_type", "未知类型", "9")));
|
|
|
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());
|
|
|
assertNull(snapshot.getTypes().get(0).getIndustry(), "未知类型根不得静默归类");
|
|
|
@@ -169,7 +170,7 @@ class BusinessCockpitMapSnapshotAdapterTest {
|
|
|
type(41, "manhole_sensor", "窨井传感器", "40")));
|
|
|
stubDevices(Collections.emptyList(), Collections.emptyList());
|
|
|
|
|
|
- CockpitMapSnapshot snapshot = adapter.load("2026-09-02T10:00:00+08:00");
|
|
|
+ CockpitMapSnapshot snapshot = adapter.load(null);
|
|
|
|
|
|
assertEquals("MANHOLE", snapshot.getTypes().stream()
|
|
|
.filter(item -> "41".equals(item.getCode()))
|
|
|
@@ -193,7 +194,8 @@ class BusinessCockpitMapSnapshotAdapterTest {
|
|
|
return fact(device.getEquipmentCode());
|
|
|
});
|
|
|
|
|
|
- CockpitMapSnapshot first = adapter.load(snapshotAt);
|
|
|
+ CockpitMapSnapshot first = adapter.load(null);
|
|
|
+ assertEquals(snapshotAt, first.getSnapshotAt());
|
|
|
|
|
|
stubDevices(Collections.singletonList(secondDevice), Collections.singletonList(secondDevice));
|
|
|
CockpitMapSnapshot second = adapter.load(snapshotAt);
|
|
|
@@ -249,7 +251,7 @@ class BusinessCockpitMapSnapshotAdapterTest {
|
|
|
.thenAnswer(invocation -> sharedValues.putIfAbsent(
|
|
|
invocation.getArgument(0), invocation.getArgument(1)) == null);
|
|
|
|
|
|
- String snapshotAt = "2026-09-02T10:09:00+08:00";
|
|
|
+ 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);
|
|
|
@@ -262,8 +264,9 @@ class BusinessCockpitMapSnapshotAdapterTest {
|
|
|
monitorPointService, equipmentBaseService, equipmentTypeService, deviceFactResolver,
|
|
|
Clock.fixed(Instant.parse("2026-09-02T02:00:00Z"), ZoneOffset.UTC), sharedRedis);
|
|
|
|
|
|
- CockpitMapSnapshot first = firstAdapter.load(snapshotAt);
|
|
|
- verify(values).setIfAbsent(eq("cockpit:map-snapshot:" + snapshotAt), any(), eq(19L * 60),
|
|
|
+ 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(
|
|
|
@@ -294,7 +297,8 @@ class BusinessCockpitMapSnapshotAdapterTest {
|
|
|
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);
|
|
|
+ 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);
|
|
|
@@ -324,13 +328,20 @@ class BusinessCockpitMapSnapshotAdapterTest {
|
|
|
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,
|
|
|
- Clock.fixed(Instant.parse("2026-09-02T02:00:00.900Z"), ZoneOffset.UTC), sharedRedis);
|
|
|
+ advancingClock, sharedRedis);
|
|
|
|
|
|
- fractionalClockAdapter.load("2026-09-02T10:00:00+08:00");
|
|
|
+ CockpitMapSnapshot issued = fractionalClockAdapter.load(null);
|
|
|
|
|
|
- verify(values).setIfAbsent(eq("cockpit:map-snapshot:2026-09-02T10:00:00+08:00"), any(), eq(600L),
|
|
|
+ verify(values).setIfAbsent(eq("cockpit:map-snapshot:" + issued.getSnapshotAt()), any(), eq(600L),
|
|
|
eq(java.util.concurrent.TimeUnit.SECONDS));
|
|
|
}
|
|
|
|
|
|
@@ -343,6 +354,7 @@ class BusinessCockpitMapSnapshotAdapterTest {
|
|
|
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());
|
|
|
@@ -353,7 +365,7 @@ class BusinessCockpitMapSnapshotAdapterTest {
|
|
|
advancingClock, sharedRedis);
|
|
|
|
|
|
assertThrows(CockpitSnapshotExpiredException.class,
|
|
|
- () -> expiringAdapter.load("2026-09-02T10:00:00+08:00"));
|
|
|
+ () -> expiringAdapter.load(null));
|
|
|
verify(values, never()).setIfAbsent(any(), any(), anyLong(), any());
|
|
|
}
|
|
|
|
|
|
@@ -362,6 +374,8 @@ class BusinessCockpitMapSnapshotAdapterTest {
|
|
|
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"),
|
|
|
@@ -372,11 +386,36 @@ class BusinessCockpitMapSnapshotAdapterTest {
|
|
|
BusinessCockpitMapSnapshotAdapter advancingAdapter = new BusinessCockpitMapSnapshotAdapter(
|
|
|
monitorPointService, equipmentBaseService, equipmentTypeService, deviceFactResolver, advancingClock);
|
|
|
String snapshotAt = "2026-09-02T10:00:00+08:00";
|
|
|
- advancingAdapter.load(snapshotAt);
|
|
|
+ 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());
|
|
|
@@ -402,10 +441,15 @@ class BusinessCockpitMapSnapshotAdapterTest {
|
|
|
when(deviceFactResolver.resolve(any(), anyString())).thenAnswer(invocation ->
|
|
|
fact(invocation.<EquipmentBase>getArgument(0).getEquipmentCode()));
|
|
|
|
|
|
- CockpitMapSnapshot first = adapter.load(firstSnapshotAt);
|
|
|
+ CockpitMapSnapshot first = adapter.load(null);
|
|
|
stubDevices(Collections.singletonList(secondDevice), Collections.singletonList(secondDevice));
|
|
|
- CockpitMapSnapshot second = adapter.load(secondSnapshotAt);
|
|
|
+ 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());
|
|
|
}
|