|
|
@@ -9,14 +9,17 @@ import org.junit.jupiter.api.Test;
|
|
9
|
9
|
import org.junit.jupiter.api.extension.ExtendWith;
|
|
10
|
10
|
import org.mockito.Mock;
|
|
11
|
11
|
import org.mockito.junit.jupiter.MockitoExtension;
|
|
|
12
|
+import org.springframework.data.redis.core.HashOperations;
|
|
12
|
13
|
import org.springframework.data.redis.core.StringRedisTemplate;
|
|
13
|
|
-import org.springframework.data.redis.core.ValueOperations;
|
|
14
|
14
|
|
|
|
15
|
+import java.util.Collections;
|
|
15
|
16
|
import java.util.concurrent.ExecutorService;
|
|
16
|
17
|
import java.util.concurrent.Executors;
|
|
17
|
18
|
|
|
18
|
19
|
import static org.assertj.core.api.Assertions.assertThat;
|
|
|
20
|
+import static org.mockito.ArgumentMatchers.any;
|
|
19
|
21
|
import static org.mockito.ArgumentMatchers.anyString;
|
|
|
22
|
+import static org.mockito.ArgumentMatchers.eq;
|
|
20
|
23
|
import static org.mockito.Mockito.lenient;
|
|
21
|
24
|
import static org.mockito.Mockito.never;
|
|
22
|
25
|
import static org.mockito.Mockito.verify;
|
|
|
@@ -29,7 +32,7 @@ class MqttStatusConsumerTest {
|
|
29
|
32
|
private StringRedisTemplate stringRedisTemplate;
|
|
30
|
33
|
|
|
31
|
34
|
@Mock
|
|
32
|
|
- private ValueOperations<String, String> valueOperations;
|
|
|
35
|
+ private HashOperations<String, Object, Object> hashOperations;
|
|
33
|
36
|
|
|
34
|
37
|
@Mock
|
|
35
|
38
|
private IotProperties iotProperties;
|
|
|
@@ -44,7 +47,8 @@ class MqttStatusConsumerTest {
|
|
44
|
47
|
@BeforeEach
|
|
45
|
48
|
void setUp() {
|
|
46
|
49
|
mqttStatusConsumer = new MqttStatusConsumer(executorService, iotProperties, stringRedisTemplate, sysStatusService);
|
|
47
|
|
- lenient().when(stringRedisTemplate.opsForValue()).thenReturn(valueOperations);
|
|
|
50
|
+ lenient().when(stringRedisTemplate.opsForHash()).thenReturn(hashOperations);
|
|
|
51
|
+ lenient().when(hashOperations.entries(anyString())).thenReturn(Collections.emptyMap());
|
|
48
|
52
|
}
|
|
49
|
53
|
|
|
50
|
54
|
@Test
|
|
|
@@ -68,11 +72,11 @@ class MqttStatusConsumerTest {
|
|
68
|
72
|
@Test
|
|
69
|
73
|
@DisplayName("handleMessage with valid JSON writes status to Redis")
|
|
70
|
74
|
void handleMessage_validJson_writesStatusToRedis() throws Exception {
|
|
71
|
|
- String json = "{\"controller_id\":\"CTRL_001\",\"fleet_id\":\"FLEET_001\",\"status\":\"online\"}";
|
|
|
75
|
+ String json = "{\"controller_id\":\"CTRL_001\",\"status\":\"online\"}";
|
|
72
|
76
|
|
|
73
|
77
|
mqttStatusConsumer.handleMessage("+/status", json);
|
|
74
|
78
|
|
|
75
|
|
- verify(valueOperations).set("CTRL_001_FLEET_001", "online");
|
|
|
79
|
+ verify(hashOperations).putAll(eq("status_CTRL_001"), any());
|
|
76
|
80
|
}
|
|
77
|
81
|
|
|
78
|
82
|
@Test
|
|
|
@@ -82,39 +86,36 @@ class MqttStatusConsumerTest {
|
|
82
|
86
|
|
|
83
|
87
|
mqttStatusConsumer.handleMessage("+/status", invalidJson);
|
|
84
|
88
|
|
|
85
|
|
- verify(valueOperations, never()).set(anyString(), anyString());
|
|
|
89
|
+ verify(hashOperations, never()).putAll(anyString(), any());
|
|
86
|
90
|
}
|
|
87
|
91
|
|
|
88
|
92
|
@Test
|
|
89
|
93
|
@DisplayName("handleMessage with blank controllerId skips processing")
|
|
90
|
94
|
void handleMessage_blankControllerId_skipsProcessing() throws Exception {
|
|
91
|
|
- String json = "{\"controller_id\":\"\",\"fleet_id\":\"FLEET_001\",\"status\":\"online\"}";
|
|
|
95
|
+ String json = "{\"controller_id\":\"\",\"status\":\"online\"}";
|
|
92
|
96
|
|
|
93
|
97
|
mqttStatusConsumer.handleMessage("+/status", json);
|
|
94
|
98
|
|
|
95
|
|
- verify(valueOperations, never()).set(anyString(), anyString());
|
|
|
99
|
+ verify(hashOperations, never()).putAll(anyString(), any());
|
|
96
|
100
|
}
|
|
97
|
101
|
|
|
98
|
102
|
@Test
|
|
99
|
|
- @DisplayName("handleMessage with null fleetId skips processing")
|
|
100
|
|
- void handleMessage_nullFleetId_skipsProcessing() throws Exception {
|
|
101
|
|
- JSONObject payload = new JSONObject();
|
|
102
|
|
- payload.put("controller_id", "CTRL_001");
|
|
103
|
|
- payload.put("fleet_id", null);
|
|
104
|
|
- payload.put("status", "online");
|
|
|
103
|
+ @DisplayName("handleMessage without fleetId still processes normally")
|
|
|
104
|
+ void handleMessage_withoutFleetId_stillProcesses() throws Exception {
|
|
|
105
|
+ String json = "{\"controller_id\":\"CTRL_001\",\"status\":\"online\"}";
|
|
105
|
106
|
|
|
106
|
|
- mqttStatusConsumer.handleMessage("+/status", payload.toJSONString());
|
|
|
107
|
+ mqttStatusConsumer.handleMessage("+/status", json);
|
|
107
|
108
|
|
|
108
|
|
- verify(valueOperations, never()).set(anyString(), anyString());
|
|
|
109
|
+ verify(hashOperations).putAll(eq("status_CTRL_001"), any());
|
|
109
|
110
|
}
|
|
110
|
111
|
|
|
111
|
112
|
@Test
|
|
112
|
113
|
@DisplayName("handleMessage with blank status skips processing")
|
|
113
|
114
|
void handleMessage_blankStatus_skipsProcessing() throws Exception {
|
|
114
|
|
- String json = "{\"controller_id\":\"CTRL_001\",\"fleet_id\":\"FLEET_001\",\"status\":\" \"}";
|
|
|
115
|
+ String json = "{\"controller_id\":\"CTRL_001\",\"status\":\" \"}";
|
|
115
|
116
|
|
|
116
|
117
|
mqttStatusConsumer.handleMessage("+/status", json);
|
|
117
|
118
|
|
|
118
|
|
- verify(valueOperations, never()).set(anyString(), anyString());
|
|
|
119
|
+ verify(hashOperations, never()).putAll(anyString(), any());
|
|
119
|
120
|
}
|
|
120
|
121
|
}
|