| | |
| | | import static org.junit.jupiter.api.Assertions.assertEquals; |
| | | import static org.junit.jupiter.api.Assertions.assertThrows; |
| | | import static org.mockito.ArgumentMatchers.any; |
| | | import static org.mockito.ArgumentMatchers.anyBoolean; |
| | | import static org.mockito.ArgumentMatchers.anyLong; |
| | | import static org.mockito.ArgumentMatchers.eq; |
| | | import static org.mockito.ArgumentMatchers.isNull; |
| | |
| | | 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.junit.jupiter.MockitoExtension; |
| | | import com.ruoyi.common.exception.ServiceException; |
| | | import com.ruoyi.system.domain.TTest; |
| | | import com.ruoyi.system.domain.TBatteryGroup; |
| | | import com.ruoyi.system.domain.TBatteryGroupRelationField; |
| | | import com.ruoyi.system.domain.TTestBattery; |
| | | import com.ruoyi.system.domain.TTestChannelBinding; |
| | | import com.ruoyi.system.domain.TTestCollectorChannel; |
| | | 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.ITBatteryGroupService; |
| | | import com.ruoyi.system.service.ISharedFieldSnapshotService; |
| | | |
| | | /** 测试配置服务的业务契约测试。 */ |
| | | @ExtendWith(MockitoExtension.class) |
| | |
| | | private TTestMapper testMapper; |
| | | |
| | | @Mock |
| | | private ITBatteryGroupService batteryGroupService; |
| | | private ISharedFieldSnapshotService sharedFieldSnapshotService; |
| | | |
| | | @InjectMocks |
| | | private TTestServiceImpl testService; |
| | |
| | | verify(testMapper).insertTestPlanStep(any(TTestPlanStep.class)); |
| | | verify(testMapper).insertTestChannelBinding(any(TTestChannelBinding.class)); |
| | | verify(testMapper, times(3)).insertTestCollectorChannel(any(TTestCollectorChannel.class)); |
| | | verify(testMapper).insertTestFieldSnapshot(any()); |
| | | verify(sharedFieldSnapshotService).saveForTest(eq(10L), any()); |
| | | verify(testMapper).insertTestThreshold(any(TTestWarningThreshold.class)); |
| | | verify(testMapper).insertTestAudit(anyLong(), any(), anyLong(), any(), isNull(), any()); |
| | | } |
| | | |
| | | @Test |
| | | void listShouldAggregateBatteryNumbersOutsideDatabaseQuery() |
| | | { |
| | | TTest first = new TTest(); |
| | | first.setTestId(10L); |
| | | TTest second = new TTest(); |
| | | second.setTestId(11L); |
| | | TTestBattery firstBattery = new TTestBattery(); |
| | | firstBattery.setTestId(10L); |
| | | firstBattery.setBatteryNo("BAT-001"); |
| | | TTestBattery secondBattery = new TTestBattery(); |
| | | secondBattery.setTestId(10L); |
| | | secondBattery.setBatteryNo("BAT-002"); |
| | | when(testMapper.selectTestList(any(TTest.class))).thenReturn(java.util.List.of(first, second)); |
| | | when(testMapper.selectBatteryNosByTestIds(java.util.List.of(10L, 11L))) |
| | | .thenReturn(java.util.List.of(firstBattery, secondBattery)); |
| | | |
| | | java.util.List<TTest> result = testService.selectTestList(new TTest()); |
| | | |
| | | assertEquals("BAT-001、BAT-002", result.get(0).getBatteryNo()); |
| | | assertEquals("", result.get(1).getBatteryNo()); |
| | | } |
| | | |
| | | @Test |
| | |
| | | } |
| | | |
| | | @Test |
| | | void createShouldRejectCaseInsensitiveDuplicateCollectorMeasurement() |
| | | { |
| | | TTest test = validTest(); |
| | | test.getBatteries().get(0).getCollectorChannels() |
| | | .add(collectorChannel("primary_temperature", "主温度重复", "S-04")); |
| | | mockEnabledOptions(); |
| | | |
| | | ServiceException exception = assertThrows(ServiceException.class, () -> testService.createTest(test)); |
| | | |
| | | assertEquals("同一电池号的采集器测量项不可重复", exception.getMessage()); |
| | | verify(testMapper, never()).insertTest(any(TTest.class)); |
| | | } |
| | | |
| | | @Test |
| | | void createShouldRejectBatteriesFromDifferentGroups() |
| | | { |
| | | TTest test = validTest(); |
| | |
| | | current.setReceptionStatus("NOT_STARTED"); |
| | | current.setMonitorTimeoutMinutes(30); |
| | | when(testMapper.selectTestByIdForUpdate(10L)).thenReturn(current); |
| | | when(testMapper.selectTestBatteries(10L)).thenReturn(current.getBatteries()); |
| | | when(sharedFieldSnapshotService.selectByTestId(10L)).thenReturn(java.util.List.of()); |
| | | when(sharedFieldSnapshotService.resolveForUpdate(1L, 1L, java.util.List.of())) |
| | | .thenReturn(java.util.List.of()); |
| | | mockEnabledOptions(); |
| | | mockGroupSnapshot(); |
| | | when(testMapper.updateTest(request)).thenReturn(1); |
| | | doAnswer(invocation -> { |
| | | invocation.<TTestBattery>getArgument(0).setRelationId(21L); |
| | |
| | | |
| | | when(testMapper.selectTestByIdForUpdate(10L)).thenReturn(current); |
| | | when(testMapper.selectTestBatteries(10L)).thenReturn(current.getBatteries()); |
| | | when(testMapper.selectTestFieldSnapshots(10L)).thenReturn(java.util.List.of(originalSnapshot)); |
| | | when(sharedFieldSnapshotService.selectByTestId(10L)).thenReturn(java.util.List.of(originalSnapshot)); |
| | | when(sharedFieldSnapshotService.resolveForUpdate(1L, 1L, java.util.List.of(originalSnapshot))) |
| | | .thenReturn(java.util.List.of(originalSnapshot)); |
| | | mockEnabledOptions(); |
| | | when(testMapper.updateTest(request)).thenReturn(1); |
| | | doAnswer(invocation -> { |
| | |
| | | |
| | | assertEquals(1, testService.updateTest(request)); |
| | | |
| | | verify(testMapper).insertTestFieldSnapshot(org.mockito.ArgumentMatchers.argThat(snapshot -> |
| | | Long.valueOf(11L).equals(snapshot.getFieldDefinitionId()) |
| | | && "额定容量".equals(snapshot.getFieldName()) |
| | | && "number".equals(snapshot.getFieldType()) |
| | | && "72".equals(snapshot.getFieldValue()))); |
| | | verify(batteryGroupService, never()).selectBatteryGroupById(anyLong()); |
| | | verify(sharedFieldSnapshotService).resolveForUpdate(1L, 1L, java.util.List.of(originalSnapshot)); |
| | | verify(sharedFieldSnapshotService).saveForTest(eq(10L), org.mockito.ArgumentMatchers.argThat(snapshots -> |
| | | snapshots.size() == 1 && "72".equals(snapshots.get(0).getFieldValue()))); |
| | | } |
| | | |
| | | @Test |
| | |
| | | assertEquals(1, testService.deleteTestByIds(new Long[] { 10L }, 7L, "operator")); |
| | | |
| | | verify(testMapper).insertTestAudit(eq(10L), eq("TEST_DELETED"), eq(7L), eq("operator"), any(), isNull()); |
| | | } |
| | | |
| | | @Test |
| | | void startReceptionShouldPersistReceptionWithoutLegacyIntervals() |
| | | { |
| | | 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(), anyBoolean(), eq("operator"))).thenReturn(1); |
| | | |
| | | assertEquals(1, testService.startReception(10L, 7L, "operator")); |
| | | 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(), anyBoolean(), 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("true"))); |
| | | } |
| | | |
| | | @Test |
| | | void startReceptionShouldNotDependOnDatabaseLease() |
| | | { |
| | | 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(), anyBoolean(), eq("operator"))).thenReturn(1); |
| | | |
| | | assertEquals(1, testService.startReception(10L, 7L, "operator")); |
| | | |
| | | } |
| | | |
| | | @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()).updateReceptionStarted(any(), any(), anyBoolean(), 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()).updateReceptionStarted(any(), any(), anyBoolean(), any()); |
| | | } |
| | | |
| | | @Test |
| | | void startReceptionShouldRejectMissingOperatorExplicitly() |
| | | { |
| | | ServiceException exception = assertThrows(ServiceException.class, |
| | | () -> testService.startReception(10L, null, "operator")); |
| | | |
| | | assertEquals("操作人不能为空", exception.getMessage()); |
| | | verify(testMapper, never()).selectTestByIdForUpdate(anyLong()); |
| | | } |
| | | |
| | | @Test |
| | | void pauseReceptionShouldStopPollingAndAppendAudit() |
| | | { |
| | | TTest current = validTest(); |
| | | current.setTestId(10L); |
| | | current.setReceptionStatus("RECEIVING"); |
| | | when(testMapper.selectTestByIdForUpdate(10L)).thenReturn(current); |
| | | when(testMapper.updateReceptionPaused(eq(10L), eq("operator"), any())).thenReturn(1); |
| | | |
| | | assertEquals(1, testService.pauseReception(10L, 7L, "operator")); |
| | | verify(testMapper).updateReceptionPaused(eq(10L), eq("operator"), any()); |
| | | verify(testMapper).insertTestAudit(eq(10L), eq("RECEPTION_PAUSED"), eq(7L), eq("operator"), isNull(), |
| | | org.mockito.ArgumentMatchers.argThat(value -> value != null && value.contains("STOPPED"))); |
| | | } |
| | | |
| | | @Test |
| | | void resumeReceptionShouldRestartPollingAndAppendAudit() |
| | | { |
| | | TTest current = validTest(); |
| | | current.setTestId(10L); |
| | | current.setReceptionStatus("PAUSED"); |
| | | List<TTestBattery> configuredBatteries = current.getBatteries(); |
| | | current.setBatteries(null); |
| | | when(testMapper.selectTestByIdForUpdate(10L)).thenReturn(current); |
| | | when(testMapper.selectTestBatteries(10L)).thenReturn(configuredBatteries); |
| | | when(testMapper.updateReceptionResumed(eq(10L), eq("operator"), any())).thenReturn(1); |
| | | |
| | | assertEquals(1, testService.resumeReception(10L, 7L, "operator")); |
| | | verify(testMapper).updateReceptionResumed(eq(10L), eq("operator"), any()); |
| | | verify(testMapper).insertTestAudit(eq(10L), eq("RECEPTION_RESUMED"), eq(7L), eq("operator"), isNull(), |
| | | org.mockito.ArgumentMatchers.argThat(value -> value != null && value.contains("RESUMED"))); |
| | | } |
| | | |
| | | @Test |
| | | void stopTestShouldCompleteWithoutLegacyLeaseCleanup() |
| | | { |
| | | TTest current = validTest(); |
| | | current.setTestId(10L); |
| | | current.setReceptionStatus("PAUSED"); |
| | | when(testMapper.selectTestByIdForUpdate(10L)).thenReturn(current); |
| | | when(testMapper.updateReceptionStopped(eq(10L), eq("operator"), any())).thenReturn(1); |
| | | |
| | | assertEquals(1, testService.stopTest(10L, 7L, "operator")); |
| | | verify(testMapper).updateReceptionStopped(eq(10L), eq("operator"), any()); |
| | | verify(testMapper).insertTestAudit(eq(10L), eq("TEST_STOPPED"), eq(7L), eq("operator"), isNull(), |
| | | org.mockito.ArgumentMatchers.argThat(value -> value != null && value.contains("COMPLETED"))); |
| | | } |
| | | |
| | | @Test |
| | | void stopTestShouldAuditImmediateStop() |
| | | { |
| | | TTest current = validTest(); |
| | | current.setTestId(10L); |
| | | current.setReceptionStatus("RECEIVING"); |
| | | when(testMapper.selectTestByIdForUpdate(10L)).thenReturn(current); |
| | | when(testMapper.updateReceptionStopped(eq(10L), eq("operator"), any())).thenReturn(1); |
| | | |
| | | testService.stopTest(10L, 7L, "operator"); |
| | | |
| | | verify(testMapper).insertTestAudit(eq(10L), eq("TEST_STOPPED"), eq(7L), eq("operator"), isNull(), |
| | | org.mockito.ArgumentMatchers.argThat(value -> value != null |
| | | && value.contains("IMMEDIATE"))); |
| | | } |
| | | |
| | | private void mockEnabledOptions() |
| | |
| | | |
| | | private void mockGroupSnapshot() |
| | | { |
| | | TBatteryGroupRelationField field = new TBatteryGroupRelationField(); |
| | | field.setBatteryGroupFieldId(11L); |
| | | field.setFieldName("额定容量"); |
| | | field.setFieldType("number"); |
| | | field.setFieldValue("72"); |
| | | TBatteryGroup group = new TBatteryGroup(); |
| | | group.setGroupId(1L); |
| | | group.setTemplateFields(java.util.List.of(field)); |
| | | when(batteryGroupService.selectBatteryGroupById(1L)).thenReturn(group); |
| | | TTestFieldSnapshot snapshot = new TTestFieldSnapshot(); |
| | | snapshot.setFieldDefinitionId(11L); |
| | | snapshot.setFieldName("额定容量"); |
| | | snapshot.setFieldType("number"); |
| | | snapshot.setFieldValue("72"); |
| | | when(sharedFieldSnapshotService.createForBatteryGroup(1L)).thenReturn(java.util.List.of(snapshot)); |
| | | } |
| | | |
| | | private TTest validTest() |