|
|
@@ -0,0 +1,279 @@
|
|
|
1
|
+package com.water.iot.service;
|
|
|
2
|
+
|
|
|
3
|
+import com.water.iot.entity.DeviceCommand;
|
|
|
4
|
+import com.water.iot.entity.DeviceInfo;
|
|
|
5
|
+import org.junit.jupiter.api.BeforeEach;
|
|
|
6
|
+import org.junit.jupiter.api.Test;
|
|
|
7
|
+import org.junit.jupiter.api.extension.ExtendWith;
|
|
|
8
|
+import org.mockito.InjectMocks;
|
|
|
9
|
+import org.mockito.Mock;
|
|
|
10
|
+import org.mockito.junit.jupiter.MockitoExtension;
|
|
|
11
|
+import org.springframework.jdbc.core.BeanPropertyRowMapper;
|
|
|
12
|
+import org.springframework.jdbc.core.JdbcTemplate;
|
|
|
13
|
+import org.springframework.transaction.annotation.Transactional;
|
|
|
14
|
+
|
|
|
15
|
+import java.sql.Timestamp;
|
|
|
16
|
+import java.util.Arrays;
|
|
|
17
|
+import java.util.HashMap;
|
|
|
18
|
+import java.util.List;
|
|
|
19
|
+import java.util.Map;
|
|
|
20
|
+
|
|
|
21
|
+import static org.junit.jupiter.api.Assertions.*;
|
|
|
22
|
+import static org.mockito.ArgumentMatchers.*;
|
|
|
23
|
+import static org.mockito.Mockito.*;
|
|
|
24
|
+
|
|
|
25
|
+@ExtendWith(MockitoExtension.class)
|
|
|
26
|
+class DeviceServiceTest {
|
|
|
27
|
+
|
|
|
28
|
+ @Mock
|
|
|
29
|
+ private JdbcTemplate jdbcTemplate;
|
|
|
30
|
+
|
|
|
31
|
+ @InjectMocks
|
|
|
32
|
+ private DeviceService deviceService;
|
|
|
33
|
+
|
|
|
34
|
+ private DeviceInfo testDevice;
|
|
|
35
|
+ private DeviceCommand testCommand;
|
|
|
36
|
+
|
|
|
37
|
+ @BeforeEach
|
|
|
38
|
+ void setUp() {
|
|
|
39
|
+ testDevice = new DeviceInfo();
|
|
|
40
|
+ testDevice.setId(1L);
|
|
|
41
|
+ testDevice.setDeviceSn("DEV001");
|
|
|
42
|
+ testDevice.setDeviceName("测试设备");
|
|
|
43
|
+ testDevice.setDeviceType("sensor");
|
|
|
44
|
+ testDevice.setArea("区域1");
|
|
|
45
|
+ testDevice.setStatus("online");
|
|
|
46
|
+ testDevice.setLastReportTime(new Timestamp(System.currentTimeMillis()));
|
|
|
47
|
+ testDevice.setLocLng(116.4074);
|
|
|
48
|
+ testDevice.setLocLat(39.9042);
|
|
|
49
|
+
|
|
|
50
|
+ testCommand = new DeviceCommand();
|
|
|
51
|
+ testCommand.setDeviceId(1L);
|
|
|
52
|
+ testCommand.setCommand("restart");
|
|
|
53
|
+ testCommand.setParameters("{}");
|
|
|
54
|
+ testCommand.setTimestamp(new Timestamp(System.currentTimeMillis()));
|
|
|
55
|
+ }
|
|
|
56
|
+
|
|
|
57
|
+ @Test
|
|
|
58
|
+ void testListDevices_Success() {
|
|
|
59
|
+ DeviceInfo device2 = new DeviceInfo();
|
|
|
60
|
+ device2.setId(2L);
|
|
|
61
|
+ device2.setDeviceSn("DEV002");
|
|
|
62
|
+ device2.setDeviceName("测试设备2");
|
|
|
63
|
+ device2.setDeviceType("actuator");
|
|
|
64
|
+ device2.setArea("区域2");
|
|
|
65
|
+ device2.setStatus("online");
|
|
|
66
|
+
|
|
|
67
|
+ List<DeviceInfo> devices = Arrays.asList(testDevice, device2);
|
|
|
68
|
+
|
|
|
69
|
+ when(jdbcTemplate.query(anyString(), any(BeanPropertyRowMapper.class), anyInt(), anyInt()))
|
|
|
70
|
+ .thenReturn(devices);
|
|
|
71
|
+
|
|
|
72
|
+ List<DeviceInfo> result = deviceService.listDevices(1, 10);
|
|
|
73
|
+
|
|
|
74
|
+ assertNotNull(result);
|
|
|
75
|
+ assertEquals(2, result.size());
|
|
|
76
|
+ assertEquals("DEV001", result.get(0).getDeviceSn());
|
|
|
77
|
+ assertEquals("DEV002", result.get(1).getDeviceSn());
|
|
|
78
|
+
|
|
|
79
|
+ verify(jdbcTemplate).query(
|
|
|
80
|
+ "SELECT id, device_sn, device_name, device_type, area, status, last_report_time FROM iot_device ORDER BY id LIMIT ? OFFSET ?",
|
|
|
81
|
+ new BeanPropertyRowMapper<>(DeviceInfo.class), 10, 0);
|
|
|
82
|
+ }
|
|
|
83
|
+
|
|
|
84
|
+ @Test
|
|
|
85
|
+ void testGetDeviceById_Success() {
|
|
|
86
|
+ when(jdbcTemplate.queryForObject(anyString(), any(BeanPropertyRowMapper.class), anyLong()))
|
|
|
87
|
+ .thenReturn(testDevice);
|
|
|
88
|
+
|
|
|
89
|
+ DeviceInfo result = deviceService.getDeviceById(1L);
|
|
|
90
|
+
|
|
|
91
|
+ assertNotNull(result);
|
|
|
92
|
+ assertEquals(1L, result.getId());
|
|
|
93
|
+ assertEquals("DEV001", result.getDeviceSn());
|
|
|
94
|
+
|
|
|
95
|
+ verify(jdbcTemplate).queryForObject(
|
|
|
96
|
+ "SELECT * FROM iot_device WHERE id = ?",
|
|
|
97
|
+ new BeanPropertyRowMapper<>(DeviceInfo.class), 1L);
|
|
|
98
|
+ }
|
|
|
99
|
+
|
|
|
100
|
+ @Test
|
|
|
101
|
+ void testGetDeviceById_NotFound() {
|
|
|
102
|
+ when(jdbcTemplate.queryForObject(anyString(), any(BeanPropertyRowMapper.class), anyLong()))
|
|
|
103
|
+ .thenReturn(null);
|
|
|
104
|
+
|
|
|
105
|
+ DeviceInfo result = deviceService.getDeviceById(999L);
|
|
|
106
|
+
|
|
|
107
|
+ assertNull(result);
|
|
|
108
|
+ verify(jdbcTemplate).queryForObject(
|
|
|
109
|
+ "SELECT * FROM iot_device WHERE id = ?",
|
|
|
110
|
+ new BeanPropertyRowMapper<>(DeviceInfo.class), 999L);
|
|
|
111
|
+ }
|
|
|
112
|
+
|
|
|
113
|
+ @Test
|
|
|
114
|
+ @Transactional
|
|
|
115
|
+ void testRegisterDevice_Success() {
|
|
|
116
|
+ DeviceInfo newDevice = new DeviceInfo();
|
|
|
117
|
+ newDevice.setDeviceSn("DEV003");
|
|
|
118
|
+ newDevice.setDeviceName("新设备");
|
|
|
119
|
+ newDevice.setDeviceType("sensor");
|
|
|
120
|
+ newDevice.setArea("区域3");
|
|
|
121
|
+ newDevice.setLocLng(116.4075);
|
|
|
122
|
+ newDevice.setLocLat(39.9043);
|
|
|
123
|
+
|
|
|
124
|
+ when(jdbcTemplate.update(anyString(), any(), any(), any(), any(), any(), any()))
|
|
|
125
|
+ .thenReturn(1);
|
|
|
126
|
+
|
|
|
127
|
+ String result = deviceService.registerDevice(newDevice);
|
|
|
128
|
+
|
|
|
129
|
+ assertEquals("注册成功", result);
|
|
|
130
|
+ verify(jdbcTemplate).update(
|
|
|
131
|
+ "INSERT INTO iot_device (device_sn, device_name, device_type, area, loc_lng, loc_lat) VALUES (?,?,?,?,?,?)",
|
|
|
132
|
+ "DEV003", "新设备", "sensor", "区域3", 116.4075, 39.9043);
|
|
|
133
|
+ }
|
|
|
134
|
+
|
|
|
135
|
+ @Test
|
|
|
136
|
+ void testSendCommand_Success() throws Exception {
|
|
|
137
|
+ when(jdbcTemplate.queryForObject(anyString(), any(BeanPropertyRowMapper.class), anyLong()))
|
|
|
138
|
+ .thenReturn(testDevice);
|
|
|
139
|
+
|
|
|
140
|
+ String result = deviceService.sendCommand(testCommand);
|
|
|
141
|
+
|
|
|
142
|
+ assertEquals("指令已下发", result);
|
|
|
143
|
+ verify(jdbcTemplate).queryForObject(
|
|
|
144
|
+ "SELECT * FROM iot_device WHERE id = ?",
|
|
|
145
|
+ new BeanPropertyRowMapper<>(DeviceInfo.class), 1L);
|
|
|
146
|
+ }
|
|
|
147
|
+
|
|
|
148
|
+ @Test
|
|
|
149
|
+ void testSendCommand_DeviceNotFound() {
|
|
|
150
|
+ when(jdbcTemplate.queryForObject(anyString(), any(BeanPropertyRowMapper.class), anyLong()))
|
|
|
151
|
+ .thenReturn(null);
|
|
|
152
|
+
|
|
|
153
|
+ assertThrows(RuntimeException.class, () -> {
|
|
|
154
|
+ deviceService.sendCommand(testCommand);
|
|
|
155
|
+ });
|
|
|
156
|
+ }
|
|
|
157
|
+
|
|
|
158
|
+ @Test
|
|
|
159
|
+ void testSendCommand_DeviceOffline() {
|
|
|
160
|
+ testDevice.setStatus("offline");
|
|
|
161
|
+ when(jdbcTemplate.queryForObject(anyString(), any(BeanPropertyRowMapper.class), anyLong()))
|
|
|
162
|
+ .thenReturn(testDevice);
|
|
|
163
|
+
|
|
|
164
|
+ assertThrows(RuntimeException.class, () -> {
|
|
|
165
|
+ deviceService.sendCommand(testCommand);
|
|
|
166
|
+ });
|
|
|
167
|
+ }
|
|
|
168
|
+
|
|
|
169
|
+ @Test
|
|
|
170
|
+ void testGetDeviceStats_Success() {
|
|
|
171
|
+ Map<String, Object> stat1 = new HashMap<>();
|
|
|
172
|
+ stat1.put("status", "online");
|
|
|
173
|
+ stat1.put("count", 5);
|
|
|
174
|
+
|
|
|
175
|
+ Map<String, Object> stat2 = new HashMap<>();
|
|
|
176
|
+ stat2.put("status", "offline");
|
|
|
177
|
+ stat2.put("count", 2);
|
|
|
178
|
+
|
|
|
179
|
+ List<Map<String, Object>> stats = Arrays.asList(stat1, stat2);
|
|
|
180
|
+
|
|
|
181
|
+ when(jdbcTemplate.queryForList(anyString())).thenReturn(stats);
|
|
|
182
|
+
|
|
|
183
|
+ List<Map<String, Object>> result = deviceService.getDeviceStats();
|
|
|
184
|
+
|
|
|
185
|
+ assertNotNull(result);
|
|
|
186
|
+ assertEquals(2, result.size());
|
|
|
187
|
+ assertEquals("online", result.get(0).get("status"));
|
|
|
188
|
+ assertEquals(5, result.get(0).get("count"));
|
|
|
189
|
+
|
|
|
190
|
+ verify(jdbcTemplate).queryForList("SELECT status, COUNT(*) as count FROM iot_device GROUP BY status");
|
|
|
191
|
+ }
|
|
|
192
|
+
|
|
|
193
|
+ @Test
|
|
|
194
|
+ void testGetDeviceLocations_Success() {
|
|
|
195
|
+ Map<String, Object> location1 = new HashMap<>();
|
|
|
196
|
+ location1.put("id", 1L);
|
|
|
197
|
+ location1.put("deviceCode", "DEV001");
|
|
|
198
|
+ location1.put("deviceName", "测试设备");
|
|
|
199
|
+ location1.put("deviceType", "sensor");
|
|
|
200
|
+ location1.put("status", "online");
|
|
|
201
|
+ location1.put("latitude", 39.9042);
|
|
|
202
|
+ location1.put("longitude", 116.4074);
|
|
|
203
|
+ location1.put("location", "区域1");
|
|
|
204
|
+ location1.put("lastOnlineTime", new Timestamp(System.currentTimeMillis()));
|
|
|
205
|
+
|
|
|
206
|
+ List<Map<String, Object>> locations = Arrays.asList(location1);
|
|
|
207
|
+
|
|
|
208
|
+ when(jdbcTemplate.queryForList(anyString())).thenReturn(locations);
|
|
|
209
|
+
|
|
|
210
|
+ List<Map<String, Object>> result = deviceService.getDeviceLocations("sensor", "online");
|
|
|
211
|
+
|
|
|
212
|
+ assertNotNull(result);
|
|
|
213
|
+ assertEquals(1, result.size());
|
|
|
214
|
+ assertEquals("DEV001", result.get(0).get("deviceCode"));
|
|
|
215
|
+ assertEquals(39.9042, result.get(0).get("latitude"));
|
|
|
216
|
+
|
|
|
217
|
+ verify(jdbcTemplate).queryForList(
|
|
|
218
|
+ "SELECT id, device_sn as deviceCode, device_name as deviceName, " +
|
|
|
219
|
+ "device_type as deviceType, status, loc_lat as latitude, " +
|
|
|
220
|
+ "loc_lng as longitude, area as location, last_report_time as lastOnlineTime " +
|
|
|
221
|
+ "FROM iot_device WHERE loc_lat IS NOT NULL AND loc_lng IS NOT NULL " +
|
|
|
222
|
+ "AND device_type = 'sensor' AND status = 'online'");
|
|
|
223
|
+ }
|
|
|
224
|
+
|
|
|
225
|
+ @Test
|
|
|
226
|
+ void testBatchOperation_Enable() {
|
|
|
227
|
+ List<Long> ids = Arrays.asList(1L, 2L, 3L);
|
|
|
228
|
+ when(jdbcTemplate.update(anyString())).thenReturn(3);
|
|
|
229
|
+
|
|
|
230
|
+ String result = deviceService.batchOperation(ids, "enable");
|
|
|
231
|
+
|
|
|
232
|
+ assertEquals("操作成功", result);
|
|
|
233
|
+ verify(jdbcTemplate).update(
|
|
|
234
|
+ "UPDATE iot_device SET status = 'online' WHERE id IN (1,2,3)");
|
|
|
235
|
+ }
|
|
|
236
|
+
|
|
|
237
|
+ @Test
|
|
|
238
|
+ void testBatchOperation_Disable() {
|
|
|
239
|
+ List<Long> ids = Arrays.asList(1L, 2L);
|
|
|
240
|
+ when(jdbcTemplate.update(anyString())).thenReturn(2);
|
|
|
241
|
+
|
|
|
242
|
+ String result = deviceService.batchOperation(ids, "disable");
|
|
|
243
|
+
|
|
|
244
|
+ assertEquals("操作成功", result);
|
|
|
245
|
+ verify(jdbcTemplate).update(
|
|
|
246
|
+ "UPDATE iot_device SET status = 'disabled' WHERE id IN (1,2)");
|
|
|
247
|
+ }
|
|
|
248
|
+
|
|
|
249
|
+ @Test
|
|
|
250
|
+ void testBatchOperation_Delete() {
|
|
|
251
|
+ List<Long> ids = Arrays.asList(1L, 2L, 3L);
|
|
|
252
|
+ when(jdbcTemplate.update(anyString())).thenReturn(3);
|
|
|
253
|
+
|
|
|
254
|
+ String result = deviceService.batchOperation(ids, "delete");
|
|
|
255
|
+
|
|
|
256
|
+ assertEquals("操作成功", result);
|
|
|
257
|
+ verify(jdbcTemplate).update(
|
|
|
258
|
+ "DELETE FROM iot_device WHERE id IN (1,2,3)");
|
|
|
259
|
+ }
|
|
|
260
|
+
|
|
|
261
|
+ @Test
|
|
|
262
|
+ void testBatchOperation_InvalidAction() {
|
|
|
263
|
+ List<Long> ids = Arrays.asList(1L, 2L);
|
|
|
264
|
+
|
|
|
265
|
+ assertThrows(IllegalArgumentException.class, () -> {
|
|
|
266
|
+ deviceService.batchOperation(ids, "invalid_action");
|
|
|
267
|
+ });
|
|
|
268
|
+ }
|
|
|
269
|
+
|
|
|
270
|
+ @Test
|
|
|
271
|
+ void testBatchOperation_EmptyIds() {
|
|
|
272
|
+ List<Long> ids = Arrays.asList();
|
|
|
273
|
+
|
|
|
274
|
+ String result = deviceService.batchOperation(ids, "enable");
|
|
|
275
|
+
|
|
|
276
|
+ assertEquals("无设备ID", result);
|
|
|
277
|
+ verify(jdbcTemplate, never()).update(anyString());
|
|
|
278
|
+ }
|
|
|
279
|
+}
|