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
package cn.iocoder.yudao.module.iot.service.rule.data.action.tcp;
 
import cn.hutool.core.util.ReflectUtil;
import cn.iocoder.yudao.module.iot.dal.dataobject.rule.config.IotDataSinkTcpConfig;
import org.junit.jupiter.api.Test;
 
import static org.junit.jupiter.api.Assertions.*;
 
/**
 * {@link IotTcpClient} 的单元测试
 * <p>
 * 测试 dataFormat 默认值行为
 * Property 1: TCP 客户端 dataFormat 默认值行为
 * Validates: Requirements 1.1, 1.2
 *
 * @author HUIHUI
 */
class IotTcpClientTest {
 
    @Test
    public void testConstructor_dataFormatNull() {
        // 准备参数
        String host = "localhost";
        Integer port = 8080;
 
        // 调用
        IotTcpClient client = new IotTcpClient(host, port, null, null, null, null);
 
        // 断言:dataFormat 为 null 时应使用默认值
        assertEquals(IotDataSinkTcpConfig.DEFAULT_DATA_FORMAT,
                ReflectUtil.getFieldValue(client, "dataFormat"));
    }
 
    @Test
    public void testConstructor_dataFormatEmpty() {
        // 准备参数
        String host = "localhost";
        Integer port = 8080;
 
        // 调用
        IotTcpClient client = new IotTcpClient(host, port, null, null, null, "");
 
        // 断言:dataFormat 为空字符串时应使用默认值
        assertEquals(IotDataSinkTcpConfig.DEFAULT_DATA_FORMAT,
                ReflectUtil.getFieldValue(client, "dataFormat"));
    }
 
    @Test
    public void testConstructor_dataFormatBlank() {
        // 准备参数
        String host = "localhost";
        Integer port = 8080;
 
        // 调用
        IotTcpClient client = new IotTcpClient(host, port, null, null, null, "   ");
 
        // 断言:dataFormat 为纯空白字符串时应使用默认值
        assertEquals(IotDataSinkTcpConfig.DEFAULT_DATA_FORMAT,
                ReflectUtil.getFieldValue(client, "dataFormat"));
    }
 
    @Test
    public void testConstructor_dataFormatValid() {
        // 准备参数
        String host = "localhost";
        Integer port = 8080;
        String dataFormat = "BINARY";
 
        // 调用
        IotTcpClient client = new IotTcpClient(host, port, null, null, null, dataFormat);
 
        // 断言:dataFormat 为有效值时应保持原值
        assertEquals(dataFormat, ReflectUtil.getFieldValue(client, "dataFormat"));
    }
 
    @Test
    public void testConstructor_defaultValues() {
        // 准备参数
        String host = "localhost";
        Integer port = 8080;
 
        // 调用
        IotTcpClient client = new IotTcpClient(host, port, null, null, null, null);
 
        // 断言:验证所有默认值
        assertEquals(host, ReflectUtil.getFieldValue(client, "host"));
        assertEquals(port, ReflectUtil.getFieldValue(client, "port"));
        assertEquals(IotDataSinkTcpConfig.DEFAULT_CONNECT_TIMEOUT_MS,
                ReflectUtil.getFieldValue(client, "connectTimeoutMs"));
        assertEquals(IotDataSinkTcpConfig.DEFAULT_READ_TIMEOUT_MS,
                ReflectUtil.getFieldValue(client, "readTimeoutMs"));
        assertEquals(IotDataSinkTcpConfig.DEFAULT_SSL,
                ReflectUtil.getFieldValue(client, "ssl"));
        assertEquals(IotDataSinkTcpConfig.DEFAULT_DATA_FORMAT,
                ReflectUtil.getFieldValue(client, "dataFormat"));
    }
 
    @Test
    public void testConstructor_customValues() {
        // 准备参数
        String host = "192.168.1.100";
        Integer port = 9090;
        Integer connectTimeoutMs = 3000;
        Integer readTimeoutMs = 8000;
        Boolean ssl = true;
        String dataFormat = "BINARY";
 
        // 调用
        IotTcpClient client = new IotTcpClient(host, port, connectTimeoutMs, readTimeoutMs, ssl, dataFormat);
 
        // 断言:验证自定义值
        assertEquals(host, ReflectUtil.getFieldValue(client, "host"));
        assertEquals(port, ReflectUtil.getFieldValue(client, "port"));
        assertEquals(connectTimeoutMs, ReflectUtil.getFieldValue(client, "connectTimeoutMs"));
        assertEquals(readTimeoutMs, ReflectUtil.getFieldValue(client, "readTimeoutMs"));
        assertEquals(ssl, ReflectUtil.getFieldValue(client, "ssl"));
        assertEquals(dataFormat, ReflectUtil.getFieldValue(client, "dataFormat"));
    }
 
    @Test
    public void testIsConnected_initialState() {
        // 准备参数
        String host = "localhost";
        Integer port = 8080;
 
        // 调用
        IotTcpClient client = new IotTcpClient(host, port, null, null, null, null);
 
        // 断言:初始状态应为未连接
        assertFalse(client.isConnected());
    }
 
    @Test
    public void testToString() {
        // 准备参数
        String host = "localhost";
        Integer port = 8080;
 
        // 调用
        IotTcpClient client = new IotTcpClient(host, port, null, null, null, null);
        String result = client.toString();
 
        // 断言
        assertNotNull(result);
        assertTrue(result.contains("host='localhost'"));
        assertTrue(result.contains("port=8080"));
        assertTrue(result.contains("dataFormat='JSON'"));
        assertTrue(result.contains("connected=false"));
    }
 
}