Commit 5bc8a4e487be9d63c27c63901975579d8f46fa0b

Authored by puhui999
1 parent f320569f

perf:【iot】添加 IotDeviceMessageUtils.notContainsIdentifier 方法

yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/service/rule/scene/matcher/trigger/IotDevicePropertyPostTriggerMatcher.java
... ... @@ -38,8 +38,7 @@ public class IotDevicePropertyPostTriggerMatcher implements IotSceneRuleTriggerM
38 38  
39 39 // 1.3 检查消息中是否包含触发器指定的属性标识符
40 40 // 注意:属性上报可能同时上报多个属性,所以需要判断 trigger.getIdentifier() 是否在 message 的 params 中
41   - // TODO @puhui999:可以考虑 notXXX 方法,简化代码(尽量取反)
42   - if (!IotDeviceMessageUtils.containsIdentifier(message, trigger.getIdentifier())) {
  41 + if (IotDeviceMessageUtils.notContainsIdentifier(message, trigger.getIdentifier())) {
43 42 IotSceneRuleMatcherHelper.logTriggerMatchFailure(message, trigger, "消息中不包含属性: " +
44 43 trigger.getIdentifier());
45 44 return false;
... ...
yudao-module-iot/yudao-module-iot-biz/src/test/java/cn/iocoder/yudao/module/iot/service/rule/data/action/tcp/IotTcpClientTest.java 0 → 100644
  1 +package cn.iocoder.yudao.module.iot.service.rule.data.action.tcp;
  2 +
  3 +import cn.hutool.core.util.ReflectUtil;
  4 +import cn.iocoder.yudao.module.iot.dal.dataobject.rule.config.IotDataSinkTcpConfig;
  5 +import org.junit.jupiter.api.Test;
  6 +
  7 +import static org.junit.jupiter.api.Assertions.*;
  8 +
  9 +/**
  10 + * {@link IotTcpClient} 的单元测试
  11 + * <p>
  12 + * 测试 dataFormat 默认值行为
  13 + * Property 1: TCP 客户端 dataFormat 默认值行为
  14 + * Validates: Requirements 1.1, 1.2
  15 + *
  16 + * @author HUIHUI
  17 + */
  18 +class IotTcpClientTest {
  19 +
  20 + @Test
  21 + public void testConstructor_dataFormatNull() {
  22 + // 准备参数
  23 + String host = "localhost";
  24 + Integer port = 8080;
  25 +
  26 + // 调用
  27 + IotTcpClient client = new IotTcpClient(host, port, null, null, null, null);
  28 +
  29 + // 断言:dataFormat 为 null 时应使用默认值
  30 + assertEquals(IotDataSinkTcpConfig.DEFAULT_DATA_FORMAT,
  31 + ReflectUtil.getFieldValue(client, "dataFormat"));
  32 + }
  33 +
  34 + @Test
  35 + public void testConstructor_dataFormatEmpty() {
  36 + // 准备参数
  37 + String host = "localhost";
  38 + Integer port = 8080;
  39 +
  40 + // 调用
  41 + IotTcpClient client = new IotTcpClient(host, port, null, null, null, "");
  42 +
  43 + // 断言:dataFormat 为空字符串时应使用默认值
  44 + assertEquals(IotDataSinkTcpConfig.DEFAULT_DATA_FORMAT,
  45 + ReflectUtil.getFieldValue(client, "dataFormat"));
  46 + }
  47 +
  48 + @Test
  49 + public void testConstructor_dataFormatBlank() {
  50 + // 准备参数
  51 + String host = "localhost";
  52 + Integer port = 8080;
  53 +
  54 + // 调用
  55 + IotTcpClient client = new IotTcpClient(host, port, null, null, null, " ");
  56 +
  57 + // 断言:dataFormat 为纯空白字符串时应使用默认值
  58 + assertEquals(IotDataSinkTcpConfig.DEFAULT_DATA_FORMAT,
  59 + ReflectUtil.getFieldValue(client, "dataFormat"));
  60 + }
  61 +
  62 + @Test
  63 + public void testConstructor_dataFormatValid() {
  64 + // 准备参数
  65 + String host = "localhost";
  66 + Integer port = 8080;
  67 + String dataFormat = "BINARY";
  68 +
  69 + // 调用
  70 + IotTcpClient client = new IotTcpClient(host, port, null, null, null, dataFormat);
  71 +
  72 + // 断言:dataFormat 为有效值时应保持原值
  73 + assertEquals(dataFormat, ReflectUtil.getFieldValue(client, "dataFormat"));
  74 + }
  75 +
  76 + @Test
  77 + public void testConstructor_defaultValues() {
  78 + // 准备参数
  79 + String host = "localhost";
  80 + Integer port = 8080;
  81 +
  82 + // 调用
  83 + IotTcpClient client = new IotTcpClient(host, port, null, null, null, null);
  84 +
  85 + // 断言:验证所有默认值
  86 + assertEquals(host, ReflectUtil.getFieldValue(client, "host"));
  87 + assertEquals(port, ReflectUtil.getFieldValue(client, "port"));
  88 + assertEquals(IotDataSinkTcpConfig.DEFAULT_CONNECT_TIMEOUT_MS,
  89 + ReflectUtil.getFieldValue(client, "connectTimeoutMs"));
  90 + assertEquals(IotDataSinkTcpConfig.DEFAULT_READ_TIMEOUT_MS,
  91 + ReflectUtil.getFieldValue(client, "readTimeoutMs"));
  92 + assertEquals(IotDataSinkTcpConfig.DEFAULT_SSL,
  93 + ReflectUtil.getFieldValue(client, "ssl"));
  94 + assertEquals(IotDataSinkTcpConfig.DEFAULT_DATA_FORMAT,
  95 + ReflectUtil.getFieldValue(client, "dataFormat"));
  96 + }
  97 +
  98 + @Test
  99 + public void testConstructor_customValues() {
  100 + // 准备参数
  101 + String host = "192.168.1.100";
  102 + Integer port = 9090;
  103 + Integer connectTimeoutMs = 3000;
  104 + Integer readTimeoutMs = 8000;
  105 + Boolean ssl = true;
  106 + String dataFormat = "BINARY";
  107 +
  108 + // 调用
  109 + IotTcpClient client = new IotTcpClient(host, port, connectTimeoutMs, readTimeoutMs, ssl, dataFormat);
  110 +
  111 + // 断言:验证自定义值
  112 + assertEquals(host, ReflectUtil.getFieldValue(client, "host"));
  113 + assertEquals(port, ReflectUtil.getFieldValue(client, "port"));
  114 + assertEquals(connectTimeoutMs, ReflectUtil.getFieldValue(client, "connectTimeoutMs"));
  115 + assertEquals(readTimeoutMs, ReflectUtil.getFieldValue(client, "readTimeoutMs"));
  116 + assertEquals(ssl, ReflectUtil.getFieldValue(client, "ssl"));
  117 + assertEquals(dataFormat, ReflectUtil.getFieldValue(client, "dataFormat"));
  118 + }
  119 +
  120 + @Test
  121 + public void testIsConnected_initialState() {
  122 + // 准备参数
  123 + String host = "localhost";
  124 + Integer port = 8080;
  125 +
  126 + // 调用
  127 + IotTcpClient client = new IotTcpClient(host, port, null, null, null, null);
  128 +
  129 + // 断言:初始状态应为未连接
  130 + assertFalse(client.isConnected());
  131 + }
  132 +
  133 + @Test
  134 + public void testToString() {
  135 + // 准备参数
  136 + String host = "localhost";
  137 + Integer port = 8080;
  138 +
  139 + // 调用
  140 + IotTcpClient client = new IotTcpClient(host, port, null, null, null, null);
  141 + String result = client.toString();
  142 +
  143 + // 断言
  144 + assertNotNull(result);
  145 + assertTrue(result.contains("host='localhost'"));
  146 + assertTrue(result.contains("port=8080"));
  147 + assertTrue(result.contains("dataFormat='JSON'"));
  148 + assertTrue(result.contains("connected=false"));
  149 + }
  150 +
  151 +}
... ...
yudao-module-iot/yudao-module-iot-core/src/main/java/cn/iocoder/yudao/module/iot/core/util/IotDeviceMessageUtils.java
... ... @@ -100,6 +100,17 @@ public class IotDeviceMessageUtils {
100 100 }
101 101  
102 102 /**
  103 + * 判断消息中是否不包含指定的标识符
  104 + *
  105 + * @param message 消息
  106 + * @param identifier 要检查的标识符
  107 + * @return 是否不包含
  108 + */
  109 + public static boolean notContainsIdentifier(IotDeviceMessage message, String identifier) {
  110 + return !containsIdentifier(message, identifier);
  111 + }
  112 +
  113 + /**
103 114 * 将 params 解析为 Map
104 115 *
105 116 * @param params 参数(可能是 Map 或 JSON 字符串)
... ...
yudao-module-iot/yudao-module-iot-core/src/test/java/cn/iocoder/yudao/module/iot/core/util/IotDeviceMessageUtilsTest.java
1 1 package cn.iocoder.yudao.module.iot.core.util;
2 2  
  3 +import cn.iocoder.yudao.module.iot.core.enums.IotDeviceMessageMethodEnum;
3 4 import cn.iocoder.yudao.module.iot.core.mq.message.IotDeviceMessage;
4 5 import org.junit.jupiter.api.Test;
5 6  
6 7 import java.util.HashMap;
7 8 import java.util.Map;
8 9  
9   -import static org.junit.jupiter.api.Assertions.assertEquals;
10   -import static org.junit.jupiter.api.Assertions.assertNull;
  10 +import static org.junit.jupiter.api.Assertions.*;
11 11  
12 12 /**
13 13 * {@link IotDeviceMessageUtils} 的单元测试
... ... @@ -138,4 +138,72 @@ public class IotDeviceMessageUtilsTest {
138 138 Object result = IotDeviceMessageUtils.extractPropertyValue(message, "temperature");
139 139 assertEquals(25.5, result); // 应该返回直接标识符的值
140 140 }
  141 +
  142 + // ========== notContainsIdentifier 测试 ==========
  143 +
  144 + /**
  145 + * 测试 notContainsIdentifier 与 containsIdentifier 的互补性
  146 + * **Property 2: notContainsIdentifier 与 containsIdentifier 互补性**
  147 + * **Validates: Requirements 4.1**
  148 + */
  149 + @Test
  150 + public void testNotContainsIdentifier_complementary_whenContains() {
  151 + // 准备参数:消息包含指定标识符
  152 + IotDeviceMessage message = new IotDeviceMessage();
  153 + message.setMethod(IotDeviceMessageMethodEnum.PROPERTY_POST.getMethod());
  154 + Map<String, Object> params = new HashMap<>();
  155 + params.put("temperature", 25);
  156 + message.setParams(params);
  157 + String identifier = "temperature";
  158 +
  159 + // 调用 & 断言:验证互补性
  160 + boolean containsResult = IotDeviceMessageUtils.containsIdentifier(message, identifier);
  161 + boolean notContainsResult = IotDeviceMessageUtils.notContainsIdentifier(message, identifier);
  162 + assertTrue(containsResult);
  163 + assertFalse(notContainsResult);
  164 + assertEquals(!containsResult, notContainsResult);
  165 + }
  166 +
  167 + /**
  168 + * 测试 notContainsIdentifier 与 containsIdentifier 的互补性
  169 + * **Property 2: notContainsIdentifier 与 containsIdentifier 互补性**
  170 + * **Validates: Requirements 4.1**
  171 + */
  172 + @Test
  173 + public void testNotContainsIdentifier_complementary_whenNotContains() {
  174 + // 准备参数:消息不包含指定标识符
  175 + IotDeviceMessage message = new IotDeviceMessage();
  176 + message.setMethod(IotDeviceMessageMethodEnum.PROPERTY_POST.getMethod());
  177 + Map<String, Object> params = new HashMap<>();
  178 + params.put("temperature", 25);
  179 + message.setParams(params);
  180 + String identifier = "humidity";
  181 +
  182 + // 调用 & 断言:验证互补性
  183 + boolean containsResult = IotDeviceMessageUtils.containsIdentifier(message, identifier);
  184 + boolean notContainsResult = IotDeviceMessageUtils.notContainsIdentifier(message, identifier);
  185 + assertFalse(containsResult);
  186 + assertTrue(notContainsResult);
  187 + assertEquals(!containsResult, notContainsResult);
  188 + }
  189 +
  190 + /**
  191 + * 测试 notContainsIdentifier 与 containsIdentifier 的互补性 - 空参数场景
  192 + * **Property 2: notContainsIdentifier 与 containsIdentifier 互补性**
  193 + * **Validates: Requirements 4.1**
  194 + */
  195 + @Test
  196 + public void testNotContainsIdentifier_complementary_nullParams() {
  197 + // 准备参数:params 为 null
  198 + IotDeviceMessage message = new IotDeviceMessage();
  199 + message.setParams(null);
  200 + String identifier = "temperature";
  201 +
  202 + // 调用 & 断言:验证互补性
  203 + boolean containsResult = IotDeviceMessageUtils.containsIdentifier(message, identifier);
  204 + boolean notContainsResult = IotDeviceMessageUtils.notContainsIdentifier(message, identifier);
  205 + assertFalse(containsResult);
  206 + assertTrue(notContainsResult);
  207 + assertEquals(!containsResult, notContainsResult);
  208 + }
141 209 }
... ...