|
|
@@ -9,12 +9,14 @@ import org.junit.jupiter.api.extension.ExtendWith;
|
|
9
|
9
|
import org.mockito.Mock;
|
|
10
|
10
|
import org.mockito.junit.jupiter.MockitoExtension;
|
|
11
|
11
|
|
|
12
|
|
-import java.util.List;
|
|
|
12
|
+import java.lang.reflect.Method;
|
|
|
13
|
+import java.time.LocalDate;
|
|
|
14
|
+import java.util.*;
|
|
13
|
15
|
import java.util.concurrent.ExecutorService;
|
|
14
|
16
|
import java.util.concurrent.Executors;
|
|
15
|
17
|
import java.util.concurrent.ScheduledExecutorService;
|
|
16
|
18
|
|
|
17
|
|
-import static org.assertj.core.api.Assertions.assertThat;
|
|
|
19
|
+import static org.assertj.core.api.Assertions.*;
|
|
18
|
20
|
import static org.mockito.Mockito.*;
|
|
19
|
21
|
|
|
20
|
22
|
@ExtendWith(MockitoExtension.class)
|
|
|
@@ -81,4 +83,74 @@ class MqttChargeStationConsumerTest {
|
|
81
|
83
|
|
|
82
|
84
|
assertThat(result).containsExactly("station/test");
|
|
83
|
85
|
}
|
|
|
86
|
+
|
|
|
87
|
+ @Test
|
|
|
88
|
+ @DisplayName("processMessageAndWriteToTdEngine: 正常数据写入 TDengine")
|
|
|
89
|
+ void processMessage_normalData_callsInsertBatch() throws Exception {
|
|
|
90
|
+ Method method = MqttChargeStationConsumer.class.getDeclaredMethod(
|
|
|
91
|
+ "processMessageAndWriteToTdEngine", List.class, String.class);
|
|
|
92
|
+ method.setAccessible(true);
|
|
|
93
|
+
|
|
|
94
|
+ Map<String, Object> data1 = new HashMap<>();
|
|
|
95
|
+ data1.put("voltage", 220);
|
|
|
96
|
+ List<Map<String, Object>> dataList = Collections.singletonList(data1);
|
|
|
97
|
+ String topic = "prefix/myDb/suffix/mySuperTable/extra";
|
|
|
98
|
+
|
|
|
99
|
+ LocalDate now = LocalDate.now();
|
|
|
100
|
+ String expectedTable = "mySuperTable_" + now.getYear() + String.format("%02d", now.getMonthValue());
|
|
|
101
|
+
|
|
|
102
|
+ method.invoke(consumer, dataList, topic);
|
|
|
103
|
+
|
|
|
104
|
+ verify(tdengineService).insertBatch(eq("myDb"), eq(expectedTable), anyList());
|
|
|
105
|
+ }
|
|
|
106
|
+
|
|
|
107
|
+ @Test
|
|
|
108
|
+ @DisplayName("processMessageAndWriteToTdEngine: 空数据列表直接返回")
|
|
|
109
|
+ void processMessage_emptyDataList_skipsInsertBatch() throws Exception {
|
|
|
110
|
+ Method method = MqttChargeStationConsumer.class.getDeclaredMethod(
|
|
|
111
|
+ "processMessageAndWriteToTdEngine", List.class, String.class);
|
|
|
112
|
+ method.setAccessible(true);
|
|
|
113
|
+
|
|
|
114
|
+ method.invoke(consumer, Collections.emptyList(), "prefix/db/suffix/table/extra");
|
|
|
115
|
+
|
|
|
116
|
+ verify(tdengineService, never()).insertBatch(anyString(), anyString(), anyList());
|
|
|
117
|
+ }
|
|
|
118
|
+
|
|
|
119
|
+ @Test
|
|
|
120
|
+ @DisplayName("processMessageAndWriteToTdEngine: 无效 topic 格式抛出异常")
|
|
|
121
|
+ void processMessage_invalidTopic_throwsException() throws Exception {
|
|
|
122
|
+ Method method = MqttChargeStationConsumer.class.getDeclaredMethod(
|
|
|
123
|
+ "processMessageAndWriteToTdEngine", List.class, String.class);
|
|
|
124
|
+ method.setAccessible(true);
|
|
|
125
|
+
|
|
|
126
|
+ Map<String, Object> data = new HashMap<>();
|
|
|
127
|
+ data.put("voltage", 220);
|
|
|
128
|
+
|
|
|
129
|
+ assertThatThrownBy(() -> method.invoke(consumer, Collections.singletonList(data), "short/topic"))
|
|
|
130
|
+ .isInstanceOf(java.lang.reflect.InvocationTargetException.class)
|
|
|
131
|
+ .hasCauseInstanceOf(IllegalArgumentException.class);
|
|
|
132
|
+ }
|
|
|
133
|
+
|
|
|
134
|
+ @Test
|
|
|
135
|
+ @DisplayName("processMessageAndWriteToTdEngine: null 和空 map 被过滤")
|
|
|
136
|
+ void processMessage_nullAndEmptyMaps_filtered() throws Exception {
|
|
|
137
|
+ Method method = MqttChargeStationConsumer.class.getDeclaredMethod(
|
|
|
138
|
+ "processMessageAndWriteToTdEngine", List.class, String.class);
|
|
|
139
|
+ method.setAccessible(true);
|
|
|
140
|
+
|
|
|
141
|
+ List<Map<String, Object>> dataList = new ArrayList<>();
|
|
|
142
|
+ dataList.add(null);
|
|
|
143
|
+ dataList.add(new HashMap<>());
|
|
|
144
|
+ Map<String, Object> valid = new HashMap<>();
|
|
|
145
|
+ valid.put("current", 10);
|
|
|
146
|
+ dataList.add(valid);
|
|
|
147
|
+
|
|
|
148
|
+ String topic = "prefix/myDb/suffix/mySuperTable/extra";
|
|
|
149
|
+ LocalDate now = LocalDate.now();
|
|
|
150
|
+ String expectedTable = "mySuperTable_" + now.getYear() + String.format("%02d", now.getMonthValue());
|
|
|
151
|
+
|
|
|
152
|
+ method.invoke(consumer, dataList, topic);
|
|
|
153
|
+
|
|
|
154
|
+ verify(tdengineService).insertBatch(eq("myDb"), eq(expectedTable), argThat(list -> list.size() == 1));
|
|
|
155
|
+ }
|
|
84
|
156
|
}
|