From a2a8908f2d9d4e19201423ff58ee23c65a8df670 Mon Sep 17 00:00:00 2001
From: yeg <1347747024@qq.com>
Date: 星期六, 29 八月 2026 20:51:10 +0800
Subject: [PATCH] 实现测试原子开始接收

---
 ruoyi-admin/src/test/java/com/ruoyi/system/service/impl/TTestServiceImplTest.java |  138 ++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 138 insertions(+), 0 deletions(-)

diff --git a/ruoyi-admin/src/test/java/com/ruoyi/system/service/impl/TTestServiceImplTest.java b/ruoyi-admin/src/test/java/com/ruoyi/system/service/impl/TTestServiceImplTest.java
index 291e569..04ec96a 100644
--- a/ruoyi-admin/src/test/java/com/ruoyi/system/service/impl/TTestServiceImplTest.java
+++ b/ruoyi-admin/src/test/java/com/ruoyi/system/service/impl/TTestServiceImplTest.java
@@ -15,11 +15,13 @@
 import java.math.BigDecimal;
 import java.util.Collections;
 import java.util.LinkedHashMap;
+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 org.springframework.dao.DuplicateKeyException;
 import com.ruoyi.common.exception.ServiceException;
 import com.ruoyi.system.domain.TTest;
 import com.ruoyi.system.domain.TTestBattery;
@@ -28,6 +30,7 @@
 import com.ruoyi.system.domain.TTestFieldSnapshot;
 import com.ruoyi.system.domain.TTestPlanStep;
 import com.ruoyi.system.domain.TTestWarningThreshold;
+import com.ruoyi.system.domain.TDetectionChannel;
 import com.ruoyi.system.mapper.TTestMapper;
 import com.ruoyi.system.service.ISharedFieldSnapshotService;
 
@@ -350,6 +353,141 @@
         verify(testMapper).insertTestAudit(eq(10L), eq("TEST_DELETED"), eq(7L), eq("operator"), any(), isNull());
     }
 
+    @Test
+    void startReceptionShouldOpenAllSourceIntervalsAtomically()
+    {
+        TTest current = validTest();
+        current.setTestId(10L);
+        current.setReceptionStatus("NOT_STARTED");
+        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.selectCollectorChannelCount(8L)).thenReturn(4);
+        when(testMapper.collectorOnline(8L)).thenReturn(true);
+        when(testMapper.updateReceptionStarted(eq(10L), any(), eq("operator"))).thenReturn(1);
+
+        assertEquals(1, testService.startReception(10L, 7L, "operator"));
+        verify(testMapper).insertChannelLease(eq(100L), eq(10L), eq(30L), eq(7L), any());
+        verify(testMapper, times(3)).insertCollectorChannelLease(eq(8L), any(), eq(10L), eq(30L), eq(7L), any());
+        verify(testMapper, times(4)).insertReceptionInterval(any());
+        verify(testMapper).insertTestAudit(eq(10L), eq("RECEPTION_STARTED"), eq(7L), eq("operator"), isNull(), any());
+    }
+
+    @Test
+    void startReceptionShouldMarkRunningCabinetAttachmentWithoutControlCommand()
+    {
+        TTest current = validTest();
+        current.setTestId(10L);
+        current.setReceptionStatus("NOT_STARTED");
+        current.getBatteries().get(0).getBinding().setBindingId(30L);
+        when(testMapper.selectTestByIdForUpdate(10L)).thenReturn(current);
+        TDetectionChannel channel = new TDetectionChannel();
+        channel.setChannelId(100L);
+        channel.setOnlineStatus("ONLINE");
+        channel.setWorkStatus("WORKING");
+        channel.setLastSeenAtUtc(Instant.parse("2026-08-29T12:00:00Z"));
+        when(testMapper.selectReceptionChannelForUpdate(100L)).thenReturn(channel);
+        when(testMapper.countCollectorEnabled(8L)).thenReturn(1);
+        when(testMapper.selectCollectorChannelCount(8L)).thenReturn(4);
+        when(testMapper.collectorOnline(8L)).thenReturn(true);
+        when(testMapper.updateReceptionStarted(eq(10L), any(), eq("operator"))).thenReturn(1);
+
+        assertEquals(1, testService.startReception(10L, 7L, "operator"));
+        verify(testMapper).insertTestAudit(eq(10L), eq("RECEPTION_STARTED"), eq(7L), eq("operator"), isNull(),
+                org.mockito.ArgumentMatchers.argThat(value -> value != null && value.contains("runtimeAttach")
+                        && value.contains("运行中接入")));
+    }
+
+    @Test
+    void startReceptionShouldRollbackWhenDatabaseLeaseIsTaken()
+    {
+        TTest current = validTest();
+        current.setTestId(10L);
+        current.setReceptionStatus("NOT_STARTED");
+        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.selectCollectorChannelCount(8L)).thenReturn(4);
+        when(testMapper.collectorOnline(8L)).thenReturn(true);
+        when(testMapper.insertChannelLease(any(), any(), any(), any(), any()))
+                .thenThrow(new DuplicateKeyException("lease already exists"));
+
+        ServiceException exception = assertThrows(ServiceException.class,
+                () -> testService.startReception(10L, 7L, "operator"));
+
+        assertEquals("数据来源已被其他测试占用,请刷新后重试", exception.getMessage());
+        verify(testMapper, never()).insertReceptionInterval(any());
+        verify(testMapper, never()).updateReceptionStarted(any(), any(), any());
+        verify(testMapper, never()).insertTestAudit(anyLong(), any(), anyLong(), any(), any(), any());
+    }
+
+    @Test
+    void startReceptionShouldRejectOfflineSourceWithoutWritingAnything()
+    {
+        TTest current = validTest();
+        current.setTestId(10L);
+        current.setReceptionStatus("NOT_STARTED");
+        when(testMapper.selectTestByIdForUpdate(10L)).thenReturn(current);
+        TDetectionChannel channel = new TDetectionChannel();
+        channel.setChannelId(100L);
+        channel.setOnlineStatus("OFFLINE");
+        when(testMapper.selectReceptionChannelForUpdate(100L)).thenReturn(channel);
+
+        ServiceException exception = assertThrows(ServiceException.class,
+                () -> testService.startReception(10L, 7L, "operator"));
+
+        assertEquals("检测柜通道 100 当前离线或不可观测", exception.getMessage());
+        verify(testMapper, never()).insertChannelLease(any(), any(), any(), any(), any());
+        verify(testMapper, never()).insertReceptionInterval(any());
+        verify(testMapper, never()).updateReceptionStarted(any(), any(), any());
+    }
+
+    @Test
+    void startReceptionShouldRejectIncompleteCollectorMappingsBeforeWriting()
+    {
+        TTest current = validTest();
+        current.setTestId(10L);
+        current.setReceptionStatus("NOT_STARTED");
+        current.getBatteries().get(0).getBinding().setBindingId(30L);
+        current.getBatteries().get(0).setCollectorChannels(Collections.emptyList());
+        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);
+
+        ServiceException exception = assertThrows(ServiceException.class,
+                () -> testService.startReception(10L, 7L, "operator"));
+
+        assertEquals("每个电池号必须绑定主温度、正电位主通道和负电位主通道", exception.getMessage());
+        verify(testMapper, never()).insertChannelLease(any(), any(), any(), any(), any());
+        verify(testMapper, never()).updateReceptionStarted(any(), any(), any());
+    }
+
+    @Test
+    void startReceptionShouldRejectMissingOperatorExplicitly()
+    {
+        ServiceException exception = assertThrows(ServiceException.class,
+                () -> testService.startReception(10L, null, "operator"));
+
+        assertEquals("操作人不能为空", exception.getMessage());
+        verify(testMapper, never()).selectTestByIdForUpdate(anyLong());
+    }
+
     private void mockEnabledOptions()
     {
         mockEnabledPeopleAndType();

--
Gitblit v1.7.1