yeg
2026-08-29 795c16bd3aa31f4faa4fe8a8e2155718cac0e5d1
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
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.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
 
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.ArgumentCaptor;
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.TBatteryGroupField;
import com.ruoyi.system.mapper.TBatteryGroupFieldMapper;
 
/**
 * 电池组字段 Service 单元测试。
 */
@ExtendWith(MockitoExtension.class)
class TBatteryGroupFieldServiceImplTest
{
    @Mock
    private TBatteryGroupFieldMapper batteryGroupFieldMapper;
 
    @InjectMocks
    private TBatteryGroupFieldServiceImpl batteryGroupFieldService;
 
    private TBatteryGroupField field;
 
    @BeforeEach
    void setUp()
    {
        field = batteryGroupField(1L, "电流", "text", "0");
    }
 
    @Test
    void insertShouldNormalizeFieldsAndInitializeStatus()
    {
        field.setFieldId(null);
        field.setFieldName("  电流  ");
        field.setFieldType("  text  ");
        field.setRemark("  备注  ");
        field.setCreateBy("admin");
        when(batteryGroupFieldMapper.insertBatteryGroupField(any(TBatteryGroupField.class))).thenReturn(1);
 
        int rows = batteryGroupFieldService.insertBatteryGroupField(field);
 
        ArgumentCaptor<TBatteryGroupField> captor = ArgumentCaptor.forClass(TBatteryGroupField.class);
        verify(batteryGroupFieldMapper).insertBatteryGroupField(captor.capture());
        TBatteryGroupField saved = captor.getValue();
        assertEquals(1, rows);
        assertEquals("电流", saved.getFieldName());
        assertEquals("text", saved.getFieldType());
        assertEquals("备注", saved.getRemark());
        assertEquals("0", saved.getStatus());
        assertEquals(0L, saved.getDelFlag());
        assertEquals("admin", saved.getUpdateBy());
    }
 
    @Test
    void insertShouldRejectBlankName()
    {
        field.setFieldId(null);
        field.setFieldName("   ");
 
        ServiceException exception = assertThrows(ServiceException.class,
                () -> batteryGroupFieldService.insertBatteryGroupField(field));
 
        assertEquals("字段名称不能为空", exception.getMessage());
        verify(batteryGroupFieldMapper, never()).insertBatteryGroupField(any(TBatteryGroupField.class));
    }
 
    @Test
    void insertShouldAcceptExistingFieldTypes()
    {
        field.setFieldId(null);
        field.setFieldType("number");
        when(batteryGroupFieldMapper.insertBatteryGroupField(any(TBatteryGroupField.class))).thenReturn(1);
 
        assertEquals(1, batteryGroupFieldService.insertBatteryGroupField(field));
 
        verify(batteryGroupFieldMapper).insertBatteryGroupField(field);
    }
 
    @Test
    void insertShouldRejectUnsupportedFieldType()
    {
        field.setFieldId(null);
        field.setFieldType("boolean");
 
        ServiceException exception = assertThrows(ServiceException.class,
                () -> batteryGroupFieldService.insertBatteryGroupField(field));
 
        assertEquals("字段类型仅支持text、number、time或remark", exception.getMessage());
        verify(batteryGroupFieldMapper, never()).insertBatteryGroupField(any(TBatteryGroupField.class));
    }
 
    @Test
    void insertShouldRejectDuplicateName()
    {
        field.setFieldId(null);
        when(batteryGroupFieldMapper.selectBatteryGroupFieldByName("电流"))
                .thenReturn(batteryGroupField(2L, "电流", "text", "0"));
 
        ServiceException exception = assertThrows(ServiceException.class,
                () -> batteryGroupFieldService.insertBatteryGroupField(field));
 
        assertEquals("字段名称已存在", exception.getMessage());
        verify(batteryGroupFieldMapper, never()).insertBatteryGroupField(any(TBatteryGroupField.class));
    }
 
    @Test
    void updateShouldAllowCurrentName()
    {
        when(batteryGroupFieldMapper.selectBatteryGroupFieldById(1L)).thenReturn(field);
        when(batteryGroupFieldMapper.selectBatteryGroupFieldByName("电流")).thenReturn(field);
        when(batteryGroupFieldMapper.updateBatteryGroupField(any(TBatteryGroupField.class))).thenReturn(1);
 
        assertEquals(1, batteryGroupFieldService.updateBatteryGroupField(field));
 
        verify(batteryGroupFieldMapper).updateBatteryGroupField(field);
    }
 
    @Test
    void updateStatusShouldRejectInvalidStatus()
    {
        TBatteryGroupField command = statusCommand("2");
 
        ServiceException exception = assertThrows(ServiceException.class,
                () -> batteryGroupFieldService.updateBatteryGroupFieldStatus(command));
 
        assertEquals("状态只能为0或1", exception.getMessage());
        verify(batteryGroupFieldMapper, never()).updateBatteryGroupFieldStatus(any(TBatteryGroupField.class));
    }
 
    @Test
    void updateStatusShouldDisableField()
    {
        when(batteryGroupFieldMapper.selectBatteryGroupFieldById(1L)).thenReturn(field);
        when(batteryGroupFieldMapper.updateBatteryGroupFieldStatus(any(TBatteryGroupField.class))).thenReturn(1);
        TBatteryGroupField command = statusCommand("1");
 
        assertEquals(1, batteryGroupFieldService.updateBatteryGroupFieldStatus(command));
 
        verify(batteryGroupFieldMapper).updateBatteryGroupFieldStatus(command);
    }
 
    @Test
    void updateStatusShouldBeIdempotent()
    {
        when(batteryGroupFieldMapper.selectBatteryGroupFieldById(1L)).thenReturn(field);
        TBatteryGroupField command = statusCommand("0");
 
        assertEquals(1, batteryGroupFieldService.updateBatteryGroupFieldStatus(command));
 
        verify(batteryGroupFieldMapper, never()).updateBatteryGroupFieldStatus(any(TBatteryGroupField.class));
    }
 
    @Test
    void deleteByIdsShouldValidateAllBeforeAnyDelete()
    {
        when(batteryGroupFieldMapper.selectBatteryGroupFieldById(1L)).thenReturn(field);
        when(batteryGroupFieldMapper.selectBatteryGroupFieldById(2L)).thenReturn(null);
 
        ServiceException exception = assertThrows(ServiceException.class,
                () -> batteryGroupFieldService.deleteBatteryGroupFieldByIds(new Long[] { 1L, 2L }, "admin"));
 
        assertEquals("电池组字段不存在", exception.getMessage());
        verify(batteryGroupFieldMapper, never()).deleteBatteryGroupFieldById(anyLong(), any(String.class));
    }
 
    @Test
    void deleteByIdsShouldDeleteAllValidatedFields()
    {
        when(batteryGroupFieldMapper.selectBatteryGroupFieldById(1L)).thenReturn(field);
        when(batteryGroupFieldMapper.selectBatteryGroupFieldById(2L))
                .thenReturn(batteryGroupField(2L, "电压", "text", "0"));
        when(batteryGroupFieldMapper.deleteBatteryGroupFieldById(anyLong(), any(String.class))).thenReturn(1);
 
        assertEquals(2, batteryGroupFieldService.deleteBatteryGroupFieldByIds(new Long[] { 1L, 2L }, "admin"));
 
        verify(batteryGroupFieldMapper).deleteBatteryGroupFieldById(1L, "admin");
        verify(batteryGroupFieldMapper).deleteBatteryGroupFieldById(2L, "admin");
    }
 
    @Test
    void deleteByIdsShouldRejectReferencedField()
    {
        when(batteryGroupFieldMapper.selectBatteryGroupFieldById(1L)).thenReturn(field);
        when(batteryGroupFieldMapper.countFieldReferences(1L)).thenReturn(1);
 
        ServiceException exception = assertThrows(ServiceException.class,
                () -> batteryGroupFieldService.deleteBatteryGroupFieldByIds(new Long[] { 1L }, "admin"));
 
        assertEquals("字段已被模板或电池组引用,不能删除", exception.getMessage());
        verify(batteryGroupFieldMapper, never()).deleteBatteryGroupFieldById(anyLong(), any(String.class));
    }
 
    private TBatteryGroupField statusCommand(String status)
    {
        TBatteryGroupField command = new TBatteryGroupField();
        command.setFieldId(1L);
        command.setStatus(status);
        command.setUpdateBy("admin");
        return command;
    }
 
    private TBatteryGroupField batteryGroupField(Long id, String name, String type, String status)
    {
        TBatteryGroupField value = new TBatteryGroupField();
        value.setFieldId(id);
        value.setFieldName(name);
        value.setFieldType(type);
        value.setStatus(status);
        return value;
    }
}