yeg
2026-08-30 c2eefadb8239df552ef43d7ba55d5735bb603ed1
ruoyi-admin/src/test/java/com/ruoyi/system/service/impl/TTestServiceImplTest.java
@@ -15,12 +15,16 @@
import java.math.BigDecimal;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.time.Instant;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import com.ruoyi.device.bts.BtsDevelopmentSimulator;
import com.ruoyi.device.simulator.DevelopmentCollectorSimulation.CollectorRealtimeSnapshot;
import com.ruoyi.device.simulator.BtsSimulatorStateStore.CollectorRealtimeMeasurement;
import org.springframework.dao.DuplicateKeyException;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.system.domain.TTest;
@@ -31,6 +35,7 @@
import com.ruoyi.system.domain.TTestPlanStep;
import com.ruoyi.system.domain.TTestWarningThreshold;
import com.ruoyi.system.domain.TDetectionChannel;
import com.ruoyi.system.domain.TTestReceptionInterval;
import com.ruoyi.system.mapper.TTestMapper;
import com.ruoyi.system.service.ISharedFieldSnapshotService;
@@ -43,6 +48,9 @@
    @Mock
    private ISharedFieldSnapshotService sharedFieldSnapshotService;
    @Mock
    private BtsDevelopmentSimulator collectorSimulator;
    @InjectMocks
    private TTestServiceImpl testService;
@@ -488,6 +496,91 @@
        verify(testMapper, never()).selectTestByIdForUpdate(anyLong());
    }
    @Test
    void pauseReceptionShouldCloseAllOpenIntervalsAndAppendAudit()
    {
        TTest current = validTest();
        current.setTestId(10L);
        current.setReceptionStatus("RECEIVING");
        when(testMapper.selectTestByIdForUpdate(10L)).thenReturn(current);
        TTestReceptionInterval cabinet = receptionInterval(1L, "CABINET_CHANNEL", 100L, null);
        TTestReceptionInterval collector = receptionInterval(2L, "COLLECTOR_CHANNEL", 8L, "S-01");
        when(testMapper.selectOpenReceptionIntervalsForUpdate(10L)).thenReturn(List.of(cabinet, collector));
        TDetectionChannel channel = new TDetectionChannel();
        channel.setChannelId(100L); channel.setOnlineStatus("ONLINE"); channel.setLastSeenAtUtc(Instant.parse("2026-08-29T12:00:00Z"));
        when(testMapper.selectReceptionChannelForUpdate(100L)).thenReturn(channel);
        when(testMapper.collectorOnline(8L)).thenReturn(true);
        mockCollectorBoundary();
        when(testMapper.closeReceptionInterval(anyLong(), any(), any())).thenReturn(1);
        when(testMapper.updateReceptionPaused(eq(10L), eq("operator"), any())).thenReturn(1);
        assertEquals(1, testService.pauseReception(10L, 7L, "operator"));
        verify(testMapper, times(2)).closeReceptionInterval(anyLong(), any(), any());
        verify(testMapper).updateReceptionPaused(eq(10L), eq("operator"), any());
        verify(testMapper).insertTestAudit(eq(10L), eq("RECEPTION_PAUSED"), eq(7L), eq("operator"), isNull(), any());
    }
    @Test
    void pauseReceptionShouldFailBeforeWritesWhenBoundaryUnavailable()
    {
        TTest current = validTest();
        current.setTestId(10L); current.setReceptionStatus("RECEIVING");
        when(testMapper.selectTestByIdForUpdate(10L)).thenReturn(current);
        TTestReceptionInterval interval = receptionInterval(1L, "CABINET_CHANNEL", 100L, null);
        when(testMapper.selectOpenReceptionIntervalsForUpdate(10L)).thenReturn(List.of(interval));
        when(testMapper.selectReceptionChannelForUpdate(100L)).thenReturn(null);
        ServiceException exception = assertThrows(ServiceException.class,
                () -> testService.pauseReception(10L, 7L, "operator"));
        assertEquals("检测柜通道 100 当前无法确认暂停接收边界", exception.getMessage());
        verify(testMapper, never()).closeReceptionInterval(anyLong(), any(), any());
        verify(testMapper, never()).updateReceptionPaused(anyLong(), any(), any());
    }
    @Test
    void resumeReceptionShouldOpenNewIntervalsAndAppendAudit()
    {
        TTest current = validTest();
        current.setTestId(10L); current.setReceptionStatus("PAUSED");
        current.getBatteries().get(0).getBinding().setBindingId(30L);
        when(testMapper.selectTestByIdForUpdate(10L)).thenReturn(current);
        TDetectionChannel channel = new TDetectionChannel();
        channel.setChannelId(100L); channel.setOnlineStatus("ONLINE"); channel.setLastSeenAtUtc(Instant.parse("2026-08-29T12:00:00Z"));
        when(testMapper.selectReceptionChannelForUpdate(100L)).thenReturn(channel);
        when(testMapper.countCollectorEnabled(8L)).thenReturn(1);
        when(testMapper.collectorOnline(8L)).thenReturn(true);
        when(testMapper.selectCollectorChannelCount(8L)).thenReturn(4);
        mockCollectorBoundary();
        when(testMapper.updateReceptionResumed(eq(10L), eq("operator"), any())).thenReturn(1);
        assertEquals(1, testService.resumeReception(10L, 7L, "operator"));
        verify(testMapper, times(4)).insertReceptionInterval(any());
        verify(testMapper).updateReceptionResumed(eq(10L), eq("operator"), any());
        verify(testMapper).insertTestAudit(eq(10L), eq("RECEPTION_RESUMED"), eq(7L), eq("operator"), isNull(), any());
    }
    private TTestReceptionInterval receptionInterval(Long id, String sourceType, Long sourceId, String channelCode)
    {
        TTestReceptionInterval interval = new TTestReceptionInterval();
        interval.setIntervalId(id); interval.setTestId(10L); interval.setSourceType(sourceType);
        interval.setSourceId(sourceId); interval.setSourceChannelCode(channelCode); interval.setIntervalStatus("OPEN");
        return interval;
    }
    private void mockCollectorBoundary()
    {
        Instant deviceTime = Instant.parse("2026-08-29T12:00:00Z");
        CollectorRealtimeMeasurement measurement = new CollectorRealtimeMeasurement("dev-sim", "collector-8", "S-01",
                1L, deviceTime, "2026-08-29 20:00:00", deviceTime, "PRIMARY_TEMPERATURE", new BigDecimal("25"), "℃");
        CollectorRealtimeMeasurement positive = new CollectorRealtimeMeasurement("dev-sim", "collector-8", "S-02",
                1L, deviceTime, "2026-08-29 20:00:00", deviceTime, "PRIMARY_POSITIVE_POTENTIAL", new BigDecimal("1"), "V");
        CollectorRealtimeMeasurement negative = new CollectorRealtimeMeasurement("dev-sim", "collector-8", "S-03",
                1L, deviceTime, "2026-08-29 20:00:00", deviceTime, "PRIMARY_NEGATIVE_POTENTIAL", new BigDecimal("1"), "V");
        when(collectorSimulator.collectorRealtime(8)).thenReturn(
                new CollectorRealtimeSnapshot("collector-8", true, 1000,
                        List.of(measurement, positive, negative)));
    }
    private void mockEnabledOptions()
    {
        mockEnabledPeopleAndType();