|
|
@@ -1,209 +0,0 @@
|
|
1
|
|
-package com.iot.platform.task;
|
|
2
|
|
-
|
|
3
|
|
-import com.iot.platform.config.IotProperties;
|
|
4
|
|
-import com.iot.platform.domain.SysCar;
|
|
5
|
|
-import com.iot.platform.domain.SysDevice;
|
|
6
|
|
-import com.iot.platform.service.*;
|
|
7
|
|
-import org.junit.jupiter.api.BeforeEach;
|
|
8
|
|
-import org.junit.jupiter.api.DisplayName;
|
|
9
|
|
-import org.junit.jupiter.api.Test;
|
|
10
|
|
-import org.junit.jupiter.api.extension.ExtendWith;
|
|
11
|
|
-import org.mockito.InjectMocks;
|
|
12
|
|
-import org.mockito.Mock;
|
|
13
|
|
-import org.mockito.junit.jupiter.MockitoExtension;
|
|
14
|
|
-import org.mockito.junit.jupiter.MockitoSettings;
|
|
15
|
|
-import org.mockito.quality.Strictness;
|
|
16
|
|
-import org.springframework.dao.DataAccessException;
|
|
17
|
|
-import org.springframework.data.redis.RedisConnectionFailureException;
|
|
18
|
|
-import org.springframework.data.redis.core.StringRedisTemplate;
|
|
19
|
|
-import org.springframework.data.redis.core.ValueOperations;
|
|
20
|
|
-import org.springframework.web.client.RestClientException;
|
|
21
|
|
-import org.springframework.web.client.RestTemplate;
|
|
22
|
|
-
|
|
23
|
|
-import java.util.*;
|
|
24
|
|
-
|
|
25
|
|
-import static org.assertj.core.api.Assertions.assertThat;
|
|
26
|
|
-import static org.mockito.ArgumentMatchers.*;
|
|
27
|
|
-import static org.mockito.Mockito.*;
|
|
28
|
|
-
|
|
29
|
|
-@ExtendWith(MockitoExtension.class)
|
|
30
|
|
-@MockitoSettings(strictness = Strictness.LENIENT)
|
|
31
|
|
-class VehicleSyncTaskTest {
|
|
32
|
|
-
|
|
33
|
|
- @Mock
|
|
34
|
|
- private SysCarService sysCarService;
|
|
35
|
|
- @Mock
|
|
36
|
|
- private SysDeviceService sysDeviceService;
|
|
37
|
|
- @Mock
|
|
38
|
|
- private StringRedisTemplate stringRedisTemplate;
|
|
39
|
|
- @Mock
|
|
40
|
|
- private SysrealtimeService sysrealtimeService;
|
|
41
|
|
- @Mock
|
|
42
|
|
- private SysDeviceVoService sysDeviceVoService;
|
|
43
|
|
- @Mock
|
|
44
|
|
- private SysDeviceControlService sysDeviceControlService;
|
|
45
|
|
- @Mock
|
|
46
|
|
- private SysWorkorderService sysWorkorderService;
|
|
47
|
|
- @Mock
|
|
48
|
|
- private SysIndicatorsService sysIndicatorsService;
|
|
49
|
|
-
|
|
50
|
|
- @Mock
|
|
51
|
|
- private RestTemplate restTemplate;
|
|
52
|
|
- @Mock
|
|
53
|
|
- private IotProperties iotProperties;
|
|
54
|
|
- @Mock
|
|
55
|
|
- private IotProperties.Mqtt mqttConfig;
|
|
56
|
|
-
|
|
57
|
|
- @InjectMocks
|
|
58
|
|
- private VehicleSyncTask task;
|
|
59
|
|
-
|
|
60
|
|
- @Mock
|
|
61
|
|
- private ValueOperations<String, String> valueOps;
|
|
62
|
|
-
|
|
63
|
|
- @BeforeEach
|
|
64
|
|
- void setUp() {
|
|
65
|
|
- when(stringRedisTemplate.opsForValue()).thenReturn(valueOps);
|
|
66
|
|
- when(iotProperties.getMqtt()).thenReturn(mqttConfig);
|
|
67
|
|
- when(mqttConfig.getVehicleTriggerUrl()).thenReturn("https://esos-iot.com:9443/syscar/trigger");
|
|
68
|
|
- }
|
|
69
|
|
-
|
|
70
|
|
- @Test
|
|
71
|
|
- @DisplayName("updateSysCar: 获取锁失败时应跳过执行")
|
|
72
|
|
- void updateSysCar_lockFail_skipsExecution() {
|
|
73
|
|
- when(valueOps.setIfAbsent(anyString(), eq("1"), anyLong(), any())).thenReturn(false);
|
|
74
|
|
-
|
|
75
|
|
- task.updateSysCar();
|
|
76
|
|
-
|
|
77
|
|
- verify(sysCarService, never()).selectcontrollerId();
|
|
78
|
|
- }
|
|
79
|
|
-
|
|
80
|
|
- @Test
|
|
81
|
|
- @DisplayName("updateSysCar: 获取锁成功时应执行车辆位置更新")
|
|
82
|
|
- void updateSysCar_lockSuccess_executesUpdate() {
|
|
83
|
|
- when(valueOps.setIfAbsent(anyString(), eq("1"), anyLong(), any())).thenReturn(true);
|
|
84
|
|
-
|
|
85
|
|
- SysCar car = new SysCar();
|
|
86
|
|
- car.setCarId("1");
|
|
87
|
|
- car.setControllerId("CTRL001");
|
|
88
|
|
- when(sysCarService.selectcontrollerId()).thenReturn(Collections.singletonList(car));
|
|
89
|
|
-
|
|
90
|
|
- SysDevice lat = new SysDevice();
|
|
91
|
|
- lat.setV("31.2304");
|
|
92
|
|
- SysDevice lon = new SysDevice();
|
|
93
|
|
- lon.setV("121.4737");
|
|
94
|
|
- when(sysDeviceService.selectsysdevice("CTRL001", "纬度")).thenReturn(lat);
|
|
95
|
|
- when(sysDeviceService.selectsysdevice("CTRL001", "经度")).thenReturn(lon);
|
|
96
|
|
-
|
|
97
|
|
- when(stringRedisTemplate.opsForHash()).thenReturn(mock(org.springframework.data.redis.core.HashOperations.class));
|
|
98
|
|
- when(stringRedisTemplate.delete(anyString())).thenReturn(true);
|
|
99
|
|
-
|
|
100
|
|
- task.updateSysCar();
|
|
101
|
|
-
|
|
102
|
|
- verify(sysCarService).selectcontrollerId();
|
|
103
|
|
- }
|
|
104
|
|
-
|
|
105
|
|
- @Test
|
|
106
|
|
- @DisplayName("updateSysCar: 单条记录异常时不应中断整个批次")
|
|
107
|
|
- void updateSysCar_singleRecordException_continuesBatch() {
|
|
108
|
|
- when(valueOps.setIfAbsent(anyString(), eq("1"), anyLong(), any())).thenReturn(true);
|
|
109
|
|
-
|
|
110
|
|
- SysCar car1 = new SysCar();
|
|
111
|
|
- car1.setCarId("1");
|
|
112
|
|
- car1.setControllerId("CTRL001");
|
|
113
|
|
- SysCar car2 = new SysCar();
|
|
114
|
|
- car2.setCarId("2");
|
|
115
|
|
- car2.setControllerId("CTRL002");
|
|
116
|
|
- when(sysCarService.selectcontrollerId()).thenReturn(Arrays.asList(car1, car2));
|
|
117
|
|
-
|
|
118
|
|
- // car1 正常
|
|
119
|
|
- SysDevice lat1 = new SysDevice();
|
|
120
|
|
- lat1.setV("31.2304");
|
|
121
|
|
- SysDevice lon1 = new SysDevice();
|
|
122
|
|
- lon1.setV("121.4737");
|
|
123
|
|
- when(sysDeviceService.selectsysdevice("CTRL001", "纬度")).thenReturn(lat1);
|
|
124
|
|
- when(sysDeviceService.selectsysdevice("CTRL001", "经度")).thenReturn(lon1);
|
|
125
|
|
-
|
|
126
|
|
- // car2 抛异常
|
|
127
|
|
- when(sysDeviceService.selectsysdevice("CTRL002", "纬度"))
|
|
128
|
|
- .thenThrow(new DataAccessException("DB error") {});
|
|
129
|
|
-
|
|
130
|
|
- when(stringRedisTemplate.opsForHash()).thenReturn(mock(org.springframework.data.redis.core.HashOperations.class));
|
|
131
|
|
- when(stringRedisTemplate.delete(anyString())).thenReturn(true);
|
|
132
|
|
-
|
|
133
|
|
- task.updateSysCar();
|
|
134
|
|
-
|
|
135
|
|
- // car1 正常执行,car2 异常被捕获,两者都应该尝试
|
|
136
|
|
- verify(sysDeviceService).selectsysdevice("CTRL001", "纬度");
|
|
137
|
|
- verify(sysDeviceService).selectsysdevice("CTRL002", "纬度");
|
|
138
|
|
- }
|
|
139
|
|
-
|
|
140
|
|
- @Test
|
|
141
|
|
- @DisplayName("insertDevice: Redis 连接失败时应跳过执行")
|
|
142
|
|
- void insertDevice_redisConnectionFailure_skipsGracefully() {
|
|
143
|
|
- when(valueOps.setIfAbsent(anyString(), eq("1"), anyLong(), any())).thenReturn(true);
|
|
144
|
|
- when(stringRedisTemplate.opsForSet()).thenThrow(
|
|
145
|
|
- new RedisConnectionFailureException("Redis down"));
|
|
146
|
|
- when(stringRedisTemplate.delete(anyString())).thenReturn(true);
|
|
147
|
|
-
|
|
148
|
|
- task.insertDevice();
|
|
149
|
|
-
|
|
150
|
|
- verify(sysDeviceControlService, never()).selectdevice(anyString());
|
|
151
|
|
- }
|
|
152
|
|
-
|
|
153
|
|
- @Test
|
|
154
|
|
- @DisplayName("insertDevice: 空数据时应直接返回")
|
|
155
|
|
- void insertDevice_emptyData_returnsEarly() {
|
|
156
|
|
- when(valueOps.setIfAbsent(anyString(), eq("1"), anyLong(), any())).thenReturn(true);
|
|
157
|
|
-
|
|
158
|
|
- org.springframework.data.redis.core.SetOperations setOps = mock(org.springframework.data.redis.core.SetOperations.class);
|
|
159
|
|
- when(stringRedisTemplate.opsForSet()).thenReturn(setOps);
|
|
160
|
|
- when(setOps.members("DSB:active:devices")).thenReturn(Collections.emptySet());
|
|
161
|
|
- when(stringRedisTemplate.delete(anyString())).thenReturn(true);
|
|
162
|
|
-
|
|
163
|
|
- task.insertDevice();
|
|
164
|
|
-
|
|
165
|
|
- verify(sysDeviceControlService, never()).selectdevice(anyString());
|
|
166
|
|
- }
|
|
167
|
|
-
|
|
168
|
|
- @Test
|
|
169
|
|
- @DisplayName("syncRedisToMySQL: 空活跃 key 时应直接返回")
|
|
170
|
|
- void syncRedisToMySQL_emptyKeys_returnsEarly() {
|
|
171
|
|
- when(valueOps.setIfAbsent(anyString(), eq("1"), anyLong(), any())).thenReturn(true);
|
|
172
|
|
-
|
|
173
|
|
- org.springframework.data.redis.core.SetOperations setOps = mock(org.springframework.data.redis.core.SetOperations.class);
|
|
174
|
|
- when(stringRedisTemplate.opsForSet()).thenReturn(setOps);
|
|
175
|
|
- when(setOps.members("DSB:active:devices")).thenReturn(null);
|
|
176
|
|
- when(stringRedisTemplate.delete(anyString())).thenReturn(true);
|
|
177
|
|
-
|
|
178
|
|
- task.syncRedisToMySQL();
|
|
179
|
|
-
|
|
180
|
|
- verify(sysrealtimeService, never()).createrealtime(anyString());
|
|
181
|
|
- }
|
|
182
|
|
-
|
|
183
|
|
-
|
|
184
|
|
- @Test
|
|
185
|
|
- @DisplayName("webhook 调用失败时不应中断主流程")
|
|
186
|
|
- void updateCarPosition_webhookFailure_continues() {
|
|
187
|
|
- when(valueOps.setIfAbsent(anyString(), eq("1"), anyLong(), any())).thenReturn(true);
|
|
188
|
|
-
|
|
189
|
|
- SysCar car = new SysCar();
|
|
190
|
|
- car.setCarId("1");
|
|
191
|
|
- car.setControllerId("CTRL001");
|
|
192
|
|
- when(sysCarService.selectcontrollerId()).thenReturn(Collections.singletonList(car));
|
|
193
|
|
-
|
|
194
|
|
- SysDevice lat = new SysDevice();
|
|
195
|
|
- lat.setV("31.2304");
|
|
196
|
|
- SysDevice lon = new SysDevice();
|
|
197
|
|
- lon.setV("121.4737");
|
|
198
|
|
- when(sysDeviceService.selectsysdevice("CTRL001", "纬度")).thenReturn(lat);
|
|
199
|
|
- when(sysDeviceService.selectsysdevice("CTRL001", "经度")).thenReturn(lon);
|
|
200
|
|
-
|
|
201
|
|
- when(stringRedisTemplate.opsForHash()).thenReturn(mock(org.springframework.data.redis.core.HashOperations.class));
|
|
202
|
|
- when(stringRedisTemplate.delete(anyString())).thenReturn(true);
|
|
203
|
|
- when(restTemplate.postForObject(anyString(), isNull(), eq(String.class)))
|
|
204
|
|
- .thenThrow(new RestClientException("Connection refused"));
|
|
205
|
|
-
|
|
206
|
|
- // 不应抛异常
|
|
207
|
|
- task.updateSysCar();
|
|
208
|
|
- }
|
|
209
|
|
-}
|