yeg
2026-08-27 a4f06a9095a2eab0bb48b406c4609be6e70b8854
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
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.<TTest>getArgument(0).setTestId(10L);
            return 1;
        });
        doAnswer(invocation -> {
            invocation.<TTestBattery>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;
    }
}