package com.ruoyi.system.service.impl; 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.anyLong; import static org.mockito.ArgumentMatchers.anyString; import static org.mockito.Mockito.doAnswer; import static org.mockito.Mockito.never; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; import java.math.BigDecimal; import java.util.Collections; 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.common.exception.ServiceException; import com.ruoyi.system.domain.TTest; import com.ruoyi.system.domain.TTestBattery; import com.ruoyi.system.domain.TTestBatteryField; import com.ruoyi.system.domain.TTestWarningThreshold; import com.ruoyi.system.mapper.TTestMapper; /** 测试管理 Service 核心业务单元测试。 */ @ExtendWith(MockitoExtension.class) class TTestServiceImplTest { @Mock private TTestMapper testMapper; @InjectMocks private TTestServiceImpl testService; @Test void insertShouldBackfillRelationIdBeforeSavingFields() { TTest test = validTest(null, "0"); mockEnabledResources(); when(testMapper.insertTest(any(TTest.class))).thenAnswer(invocation -> { invocation.getArgument(0).setTestId(10L); return 1; }); doAnswer(invocation -> { invocation.getArgument(0).setRelationId(20L); return 1; }).when(testMapper).insertTestBattery(any(TTestBattery.class)); assertEquals(1, testService.insertTest(test)); verify(testMapper).insertTestBatteryField(any(TTestBatteryField.class)); assertEquals(20L, test.getBatteries().get(0).getFields().get(0).getRelationId()); } @Test void insertShouldRejectOccupiedBattery() { TTest test = validTest(null, "0"); mockEnabledResources(); when(testMapper.countTestingByBatteryNo("BAT-001", null)).thenReturn(1); ServiceException exception = assertThrows(ServiceException.class, () -> testService.insertTest(test)); assertEquals("电池号或检测柜测试通道已被检测中的任务占用", exception.getMessage()); verify(testMapper, never()).insertTest(any(TTest.class)); } @Test void updateShouldRejectNonWaitingStatus() { TTest request = validTest(10L, "0"); TTest current = validTest(10L, "1"); when(testMapper.countUserEnabled(1L)).thenReturn(1); when(testMapper.countTestTypeEnabled(1L)).thenReturn(1); when(testMapper.selectTestById(10L)).thenReturn(current); when(testMapper.selectTestBatteries(10L)).thenReturn(current.getBatteries()); when(testMapper.selectTestBatteryFields(10L)).thenReturn(current.getBatteries().get(0).getFields()); when(testMapper.selectTestThresholds(10L)).thenReturn(current.getThresholds()); ServiceException exception = assertThrows(ServiceException.class, () -> testService.updateTest(request)); assertEquals("仅待检测状态允许编辑草稿", exception.getMessage()); verify(testMapper, never()).updateTest(any(TTest.class)); } @Test void startShouldTransitionWaitingToTesting() { TTest current = validTest(10L, "0"); when(testMapper.selectTestById(10L)).thenReturn(current); when(testMapper.selectTestBatteries(10L)).thenReturn(current.getBatteries()); when(testMapper.selectTestBatteryFields(10L)).thenReturn(current.getBatteries().get(0).getFields()); when(testMapper.selectTestThresholds(10L)).thenReturn(current.getThresholds()); when(testMapper.countBatteryInEnabledGroup(1L, "BAT-001")).thenReturn(1); when(testMapper.countCabinetEnabled(1L)).thenReturn(1); when(testMapper.countCollectorEnabled(1L, 1L)).thenReturn(1); when(testMapper.updateTestStatus(10L, "1", "admin")).thenReturn(1); assertEquals(1, testService.startTest(10L, "admin")); verify(testMapper).updateTestStatus(10L, "1", "admin"); } @Test void deleteShouldRejectTestingTask() { TTest current = validTest(10L, "1"); when(testMapper.selectTestById(10L)).thenReturn(current); when(testMapper.selectTestBatteries(10L)).thenReturn(current.getBatteries()); when(testMapper.selectTestBatteryFields(10L)).thenReturn(current.getBatteries().get(0).getFields()); when(testMapper.selectTestThresholds(10L)).thenReturn(current.getThresholds()); ServiceException exception = assertThrows(ServiceException.class, () -> testService.deleteTestByIds(new Long[] {10L}, "admin")); assertEquals("检测中或异常测试不能删除", exception.getMessage()); verify(testMapper, never()).deleteTestById(anyLong(), anyString()); } private void mockEnabledResources() { when(testMapper.countUserEnabled(1L)).thenReturn(1); when(testMapper.countTestTypeEnabled(1L)).thenReturn(1); when(testMapper.countBatteryInEnabledGroup(1L, "BAT-001")).thenReturn(1); when(testMapper.countCabinetEnabled(1L)).thenReturn(1); when(testMapper.countCollectorEnabled(1L, 1L)).thenReturn(1); } private TTest validTest(Long testId, String status) { TTestBatteryField field = new TTestBatteryField(); field.setRelationId(20L); field.setFieldName("容量"); field.setFieldValue("100"); TTestBattery battery = new TTestBattery(); battery.setRelationId(20L); battery.setGroupId(1L); battery.setBatteryNo("BAT-001"); battery.setCabinetId(1L); battery.setTestChannelNo(0); battery.setCollectorId(1L); battery.setTemperatureChannelNo(1); battery.setPositiveChannelNo(2); battery.setNegativeChannelNo(3); battery.setFields(Collections.singletonList(field)); TTestWarningThreshold threshold = new TTestWarningThreshold(); threshold.setThresholdType("VOLTAGE:2"); threshold.setMinValue(BigDecimal.ZERO); threshold.setMaxValue(BigDecimal.TEN); TTest test = new TTest(); test.setTestId(testId); test.setTestName("测试任务"); test.setPrincipalId(1L); test.setTestTypeId(1L); test.setTestPurpose("验证容量"); test.setStatus(status); test.setBatteries(Collections.singletonList(battery)); test.setThresholds(Collections.singletonList(threshold)); return test; } }