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
package cn.iocoder.yudao.module.iot.service.rule.data.action;
 
import cn.hutool.core.map.MapUtil;
import cn.iocoder.yudao.framework.common.util.json.JsonUtils;
import cn.iocoder.yudao.module.iot.core.mq.message.IotDeviceMessage;
import cn.iocoder.yudao.module.iot.dal.dataobject.rule.config.IotDataSinkTcpConfig;
import cn.iocoder.yudao.module.iot.service.rule.data.action.tcp.IotTcpClient;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
 
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.*;
 
/**
 * {@link IotTcpDataRuleAction} 的单元测试
 *
 * @author HUIHUI
 */
class IotTcpDataRuleActionTest {
 
    private IotTcpDataRuleAction tcpDataRuleAction;
 
    @Mock
    private IotTcpClient mockTcpClient;
 
    @BeforeEach
    public void setUp() {
        MockitoAnnotations.openMocks(this);
        tcpDataRuleAction = new IotTcpDataRuleAction();
    }
 
    @Test
    public void testGetType() {
        // 准备参数
        Integer expectedType = 2; // 数据接收类型枚举中 TCP 类型的值
 
        // 调用方法
        Integer actualType = tcpDataRuleAction.getType();
 
        // 断言结果
        assertEquals(expectedType, actualType);
    }
 
    @Test
    public void testInitProducer_success() throws Exception {
        // 准备参数
        IotDataSinkTcpConfig config = new IotDataSinkTcpConfig();
        config.setHost("localhost");
        config.setPort(8080);
        config.setDataFormat("JSON");
        config.setSsl(false);
 
        // 调用方法 & 断言结果
        // 此测试需要实际的 TCP 连接,在单元测试中可能不可用
        // 目前我们只验证配置是否有效
        assertNotNull(config.getHost());
        assertTrue(config.getPort() > 0 && config.getPort() <= 65535);
    }
 
    @Test
    public void testInitProducer_invalidHost() {
        // 准备参数
        IotDataSinkTcpConfig config = new IotDataSinkTcpConfig();
        config.setHost("");
        config.setPort(8080);
 
        // 调用方法 & 断言结果
        IotTcpDataRuleAction action = new IotTcpDataRuleAction();
 
        // 测试验证逻辑(通常在 initProducer 方法中)
        assertThrows(IllegalArgumentException.class, () -> {
            if (config.getHost() == null || config.getHost().trim().isEmpty()) {
                throw new IllegalArgumentException("TCP 服务器地址不能为空");
            }
        });
    }
 
    @Test
    public void testInitProducer_invalidPort() {
        // 准备参数
        IotDataSinkTcpConfig config = new IotDataSinkTcpConfig();
        config.setHost("localhost");
        config.setPort(-1);
 
        // 调用方法 & 断言结果
        assertThrows(IllegalArgumentException.class, () -> {
            if (config.getPort() == null || config.getPort() <= 0 || config.getPort() > 65535) {
                throw new IllegalArgumentException("TCP 服务器端口无效");
            }
        });
    }
 
    @Test
    public void testCloseProducer() throws Exception {
        // 准备参数
        IotTcpClient client = mock(IotTcpClient.class);
 
        // 调用方法
        tcpDataRuleAction.closeProducer(client);
 
        // 断言结果
        verify(client, times(1)).close();
    }
 
    @Test
    public void testExecute_withValidConfig() {
        // 准备参数
        IotDeviceMessage message = IotDeviceMessage.requestOf("thing.property.report",
                "{\"temperature\": 25.5, \"humidity\": 60}");
 
        IotDataSinkTcpConfig config = new IotDataSinkTcpConfig();
        config.setHost("localhost");
        config.setPort(8080);
        config.setDataFormat("JSON");
 
        // 调用方法 & 断言结果
        // 通常这需要实际的 TCP 连接
        // 在单元测试中,我们只验证输入参数
        assertNotNull(message);
        assertNotNull(config);
        assertNotNull(config.getHost());
        assertTrue(config.getPort() > 0);
    }
 
    @Test
    public void testConfig_defaultValues() {
        // 准备参数
        IotDataSinkTcpConfig config = new IotDataSinkTcpConfig();
 
        // 调用方法 & 断言结果
        // 验证默认值
        assertEquals("JSON", config.getDataFormat());
        assertEquals(5000, config.getConnectTimeoutMs());
        assertEquals(10000, config.getReadTimeoutMs());
        assertEquals(false, config.getSsl());
        assertEquals(30000L, config.getHeartbeatIntervalMs());
        assertEquals(5000L, config.getReconnectIntervalMs());
        assertEquals(3, config.getMaxReconnectAttempts());
    }
 
    @Test
    public void testMessageSerialization() {
        // 准备参数
        IotDeviceMessage message = IotDeviceMessage.builder()
                .deviceId(123L)
                .method("thing.property.report")
                .params(MapUtil.of("temperature", 25.5))
                .build();
 
        // 调用方法
        String json = JsonUtils.toJsonString(message);
 
        // 断言结果
        assertNotNull(json);
        assertTrue(json.contains("\"deviceId\":123"));
        assertTrue(json.contains("\"method\":\"thing.property.report\""));
        assertTrue(json.contains("\"temperature\":25.5"));
    }
 
}