2026-06-24 f4bd1f3c89d906131495a0aca5aaf82966378510
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
package cn.iocoder.yudao.module.iot.service.rule.data.action.websocket;
 
import cn.iocoder.yudao.module.iot.core.mq.message.IotDeviceMessage;
import okhttp3.Response;
import okhttp3.WebSocket;
import okhttp3.WebSocketListener;
import okhttp3.mockwebserver.MockResponse;
import okhttp3.mockwebserver.MockWebServer;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
 
import static org.junit.jupiter.api.Assertions.*;
 
/**
 * {@link IotWebSocketClient} 的单元测试
 *
 * @author HUIHUI
 */
class IotWebSocketClientTest {
 
    private MockWebServer mockWebServer;
 
    @BeforeEach
    public void setUp() throws Exception {
        mockWebServer = new MockWebServer();
        mockWebServer.start();
    }
 
    @AfterEach
    public void tearDown() throws Exception {
        if (mockWebServer != null) {
            mockWebServer.shutdown();
        }
    }
 
    /**
     * 简单的 WebSocket 监听器,用于测试
     */
    private static class TestWebSocketListener extends WebSocketListener {
        @Override
        public void onOpen(@NotNull WebSocket webSocket, @NotNull Response response) {
            // 连接打开
        }
 
        @Override
        public void onMessage(@NotNull WebSocket webSocket, @NotNull String text) {
            // 收到消息
        }
 
        @Override
        public void onClosing(@NotNull WebSocket webSocket, int code, @NotNull String reason) {
            webSocket.close(code, reason);
        }
 
        @Override
        public void onFailure(@NotNull WebSocket webSocket, @NotNull Throwable t, @Nullable Response response) {
            // 连接失败
        }
    }
 
    @Test
    public void testConstructor_defaultValues() {
        // 准备参数
        String serverUrl = "ws://localhost:8080";
 
        // 调用
        IotWebSocketClient client = new IotWebSocketClient(serverUrl, null, null, null);
 
        // 断言:验证默认值被正确设置
        assertNotNull(client);
        assertFalse(client.isConnected());
    }
 
    @Test
    public void testConstructor_customValues() {
        // 准备参数
        String serverUrl = "ws://localhost:8080";
        Integer connectTimeoutMs = 3000;
        Integer sendTimeoutMs = 5000;
        String dataFormat = "TEXT";
 
        // 调用
        IotWebSocketClient client = new IotWebSocketClient(serverUrl, connectTimeoutMs, sendTimeoutMs, dataFormat);
 
        // 断言
        assertNotNull(client);
        assertFalse(client.isConnected());
    }
 
    @Test
    public void testConnect_success() throws Exception {
        // 准备参数:使用 MockWebServer 的 WebSocket 端点
        String serverUrl = "ws://" + mockWebServer.getHostName() + ":" + mockWebServer.getPort();
        IotWebSocketClient client = new IotWebSocketClient(serverUrl, 5000, 5000, "JSON");
 
        // mock:设置 MockWebServer 响应 WebSocket 升级请求
        mockWebServer.enqueue(new MockResponse().withWebSocketUpgrade(new TestWebSocketListener()));
 
        // 调用
        client.connect();
 
        // 断言
        assertTrue(client.isConnected());
 
        // 清理
        client.close();
    }
 
    @Test
    public void testConnect_alreadyConnected() throws Exception {
        // 准备参数
        String serverUrl = "ws://" + mockWebServer.getHostName() + ":" + mockWebServer.getPort();
        IotWebSocketClient client = new IotWebSocketClient(serverUrl, 5000, 5000, "JSON");
 
        // mock
        mockWebServer.enqueue(new MockResponse().withWebSocketUpgrade(new TestWebSocketListener()));
 
        // 调用:第一次连接
        client.connect();
        assertTrue(client.isConnected());
 
        // 调用:第二次连接(应该不会重复连接)
        client.connect();
        assertTrue(client.isConnected());
 
        // 清理
        client.close();
    }
 
    @Test
    public void testSendMessage_success() throws Exception {
        // 准备参数
        String serverUrl = "ws://" + mockWebServer.getHostName() + ":" + mockWebServer.getPort();
        IotWebSocketClient client = new IotWebSocketClient(serverUrl, 5000, 5000, "JSON");
 
        IotDeviceMessage message = IotDeviceMessage.builder()
                .deviceId(123L)
                .method("thing.property.report")
                .params("{\"temperature\": 25.5}")
                .build();
 
        // mock
        mockWebServer.enqueue(new MockResponse().withWebSocketUpgrade(new TestWebSocketListener()));
 
        // 调用
        client.connect();
        client.sendMessage(message);
 
        // 断言:消息发送成功不抛异常
        assertTrue(client.isConnected());
 
        // 清理
        client.close();
    }
 
    @Test
    public void testSendMessage_notConnected() {
        // 准备参数
        String serverUrl = "ws://localhost:8080";
        IotWebSocketClient client = new IotWebSocketClient(serverUrl, 5000, 5000, "JSON");
 
        IotDeviceMessage message = IotDeviceMessage.builder()
                .deviceId(123L)
                .method("thing.property.report")
                .params("{\"temperature\": 25.5}")
                .build();
 
        // 调用 & 断言:未连接时发送消息应抛出异常
        assertThrows(IllegalStateException.class, () -> client.sendMessage(message));
    }
 
    @Test
    public void testClose_success() throws Exception {
        // 准备参数
        String serverUrl = "ws://" + mockWebServer.getHostName() + ":" + mockWebServer.getPort();
        IotWebSocketClient client = new IotWebSocketClient(serverUrl, 5000, 5000, "JSON");
 
        // mock
        mockWebServer.enqueue(new MockResponse().withWebSocketUpgrade(new TestWebSocketListener()));
 
        // 调用
        client.connect();
        assertTrue(client.isConnected());
 
        client.close();
 
        // 断言
        assertFalse(client.isConnected());
    }
 
    @Test
    public void testClose_notConnected() {
        // 准备参数
        String serverUrl = "ws://localhost:8080";
        IotWebSocketClient client = new IotWebSocketClient(serverUrl, 5000, 5000, "JSON");
 
        // 调用:关闭未连接的客户端不应抛异常
        assertDoesNotThrow(client::close);
        assertFalse(client.isConnected());
    }
 
    @Test
    public void testIsConnected_initialState() {
        // 准备参数
        String serverUrl = "ws://localhost:8080";
        IotWebSocketClient client = new IotWebSocketClient(serverUrl, 5000, 5000, "JSON");
 
        // 断言:初始状态应为未连接
        assertFalse(client.isConnected());
    }
 
    @Test
    public void testToString() {
        // 准备参数
        String serverUrl = "ws://localhost:8080";
        IotWebSocketClient client = new IotWebSocketClient(serverUrl, 5000, 5000, "JSON");
 
        // 调用
        String result = client.toString();
 
        // 断言
        assertNotNull(result);
        assertTrue(result.contains("serverUrl='ws://localhost:8080'"));
        assertTrue(result.contains("dataFormat='JSON'"));
        assertTrue(result.contains("connected=false"));
    }
 
    @Test
    public void testSendMessage_textFormat() throws Exception {
        // 准备参数
        String serverUrl = "ws://" + mockWebServer.getHostName() + ":" + mockWebServer.getPort();
        IotWebSocketClient client = new IotWebSocketClient(serverUrl, 5000, 5000, "TEXT");
 
        IotDeviceMessage message = IotDeviceMessage.builder()
                .deviceId(123L)
                .method("thing.property.report")
                .params("{\"temperature\": 25.5}")
                .build();
 
        // mock
        mockWebServer.enqueue(new MockResponse().withWebSocketUpgrade(new TestWebSocketListener()));
 
        // 调用
        client.connect();
        client.sendMessage(message);
 
        // 断言:消息发送成功不抛异常
        assertTrue(client.isConnected());
 
        // 清理
        client.close();
    }
 
}