yeg
2026-09-05 92f32acd847ea1700590bc669750f23ef499840e
ruoyi-admin/src/test/java/com/battery/business/service/impl/TTestServiceImplTest.java
File was renamed from ruoyi-admin/src/test/java/com/ruoyi/system/service/impl/TTestServiceImplTest.java
@@ -1,4 +1,4 @@
package com.ruoyi.system.service.impl;
package com.battery.business.service.impl;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
@@ -25,16 +25,13 @@
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.system.domain.TTest;
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.ISharedFieldSnapshotService;
import com.battery.business.domain.TTest;
import com.battery.business.domain.TTestBattery;
import com.battery.business.domain.TTestChannelBinding;
import com.battery.business.domain.TTestCollectorChannel;
import com.battery.business.domain.TTestWarningThreshold;
import com.battery.business.domain.TDetectionChannel;
import com.battery.business.mapper.TTestMapper;
/** 测试配置服务的业务契约测试。 */
@ExtendWith(MockitoExtension.class)
@@ -43,8 +40,7 @@
    @Mock
    private TTestMapper testMapper;
    @Mock
    private ISharedFieldSnapshotService sharedFieldSnapshotService;
    @InjectMocks
    private TTestServiceImpl testService;
@@ -67,12 +63,9 @@
        assertEquals("WAITING", test.getTestStatus());
        assertEquals("NOT_STARTED", test.getReceptionStatus());
        assertEquals(7L, test.getCreatorId());
        verify(testMapper, never()).insertTestPlanStep(any(TTestPlanStep.class));
        verify(testMapper).insertTestChannelBinding(any(TTestChannelBinding.class));
        verify(testMapper, times(3)).insertTestCollectorChannel(any(TTestCollectorChannel.class));
        verify(sharedFieldSnapshotService, never()).saveForTest(anyLong(), any());
        verify(testMapper).insertTestThreshold(any(TTestWarningThreshold.class));
        verify(testMapper, never()).insertTestAudit(anyLong(), any(), anyLong(), any(), isNull(), any());
    }
    @Test
@@ -130,7 +123,6 @@
    void createShouldAllowPlanStepsToBeConfiguredLater()
    {
        TTest test = validTest();
        test.setPlanSteps(Collections.emptyList());
        mockEnabledOptions();
        when(testMapper.insertTest(any(TTest.class))).thenAnswer(invocation -> {
            invocation.<TTest>getArgument(0).setTestId(10L);
@@ -142,8 +134,6 @@
        }).when(testMapper).insertTestBattery(any(TTestBattery.class));
        assertEquals(10L, testService.createTest(test));
        verify(testMapper, never()).insertTestPlanStep(any(TTestPlanStep.class));
    }
    @Test
@@ -312,37 +302,6 @@
        assertEquals(1, testService.updateTest(request));
        assertEquals(45, request.getMonitorTimeoutMinutes());
        verify(testMapper).deleteTestBindingsByTestId(10L);
        verify(testMapper, never()).deleteTestPlanStepsByTestId(10L);
        verify(testMapper, never()).insertTestAudit(anyLong(), any(), anyLong(), any(), any(), any());
    }
    @Test
    void updateSameGroupShouldPreserveOriginalFieldSnapshot()
    {
        TTest request = validTest();
        request.setTestId(10L);
        TTest current = validTest();
        current.setTestId(10L);
        current.setCreatorId(7L);
        current.setReceptionStatus("NOT_STARTED");
        TTestFieldSnapshot originalSnapshot = new TTestFieldSnapshot();
        originalSnapshot.setFieldDefinitionId(11L);
        originalSnapshot.setFieldName("额定容量");
        originalSnapshot.setFieldType("number");
        originalSnapshot.setFieldValue("72");
        when(testMapper.selectTestByIdForUpdate(10L)).thenReturn(current);
        when(testMapper.selectTestBatteries(10L)).thenReturn(current.getBatteries());
        mockEnabledOptions();
        when(testMapper.updateTest(request)).thenReturn(1);
        doAnswer(invocation -> {
            invocation.<TTestBattery>getArgument(0).setRelationId(21L);
            return 1;
        }).when(testMapper).insertTestBattery(any(TTestBattery.class));
        assertEquals(1, testService.updateTest(request));
        verifyNoInteractions(sharedFieldSnapshotService);
    }
    @Test
@@ -355,24 +314,21 @@
        when(testMapper.deleteTestById(10L, "operator")).thenReturn(1);
        assertEquals(1, testService.deleteTestByIds(new Long[] { 10L }, 7L, "operator"));
        verify(testMapper, never()).insertTestAudit(anyLong(), any(), anyLong(), any(), any(), any());
    }
    @Test
    void deleteShouldRejectMismatchedTestAndReceptionStates()
    void deletesStartedTestAndAllChildFacts()
    {
        TTest current = validTest();
        current.setTestId(10L);
        current.setTestStatus("RUNNING");
        current.setReceptionStatus("NOT_STARTED");
        when(testMapper.selectTestByIdForUpdate(10L)).thenReturn(current);
        ServiceException exception = assertThrows(ServiceException.class,
                () -> testService.deleteTestByIds(new Long[] { 10L }, 7L, "operator"));
        assertEquals("开始接收后不能删除测试", exception.getMessage());
        verify(testMapper, never()).deleteTestById(anyLong(), any(String.class));
        when(testMapper.deleteTestById(10L, "operator")).thenReturn(1);
        assertEquals(1, testService.deleteTestByIds(new Long[] { 10L }, 7L, "operator"));
        verify(testMapper).deleteTestWarningExceptionsByTestId(10L);
        verify(testMapper).deleteTestDataRevisionActionsByTestId(10L);
        verify(testMapper).deleteTestLogicStepMappingsByTestId(10L);
    }
    @Test
@@ -394,7 +350,6 @@
        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
@@ -417,9 +372,6 @@
        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
@@ -563,8 +515,6 @@
        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
@@ -582,8 +532,6 @@
        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
@@ -598,8 +546,6 @@
        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
@@ -613,10 +559,6 @@
        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()
@@ -635,28 +577,8 @@
        when(testMapper.countTestTypeEnabled(3L)).thenReturn(1);
    }
    private void mockGroupSnapshot()
    {
        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()
    {
        TTestPlanStep step = new TTestPlanStep();
        step.setCycleNo(1);
        step.setStepNo(1);
        step.setStepType("CONSTANT_CURRENT_CHARGE");
        step.setStepName("恒流充电");
        step.setSortOrder(1);
        LinkedHashMap<String, Object> parameters = new LinkedHashMap<>();
        parameters.put("currentA", new BigDecimal("1.5"));
        step.setParameters(parameters);
        TTestWarningThreshold threshold = new TTestWarningThreshold();
        threshold.setMetricCode("VOLTAGE");
        threshold.setMinValue(new BigDecimal("2.5"));
@@ -677,7 +599,6 @@
        test.setReceptionStatus("NOT_STARTED");
        test.setMonitorTimeoutMinutes(30);
        test.setBatteries(Collections.singletonList(validBattery("BAT-001", 1L, 100L)));
        test.setPlanSteps(Collections.singletonList(step));
        test.setThresholds(Collections.singletonList(threshold));
        return test;
    }