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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
package cn.iocoder.yudao.module.iot.service.rule.scene.matcher.trigger;
 
import cn.hutool.core.map.MapUtil;
import cn.iocoder.yudao.module.iot.core.enums.IotDeviceMessageMethodEnum;
import cn.iocoder.yudao.module.iot.core.mq.message.IotDeviceMessage;
import cn.iocoder.yudao.module.iot.dal.dataobject.rule.IotSceneRuleDO;
import cn.iocoder.yudao.module.iot.enums.rule.IotSceneRuleConditionOperatorEnum;
import cn.iocoder.yudao.module.iot.enums.rule.IotSceneRuleTriggerTypeEnum;
import cn.iocoder.yudao.module.iot.service.rule.scene.matcher.IotBaseConditionMatcherTest;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
 
import java.util.HashMap;
import java.util.Map;
 
import static cn.hutool.core.util.RandomUtil.randomDouble;
import static cn.hutool.core.util.RandomUtil.randomInt;
import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.randomLongId;
import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.randomString;
import static org.junit.jupiter.api.Assertions.*;
 
/**
 * {@link IotDevicePropertyPostTriggerMatcher} 的单元测试
 *
 * @author HUIHUI
 */
public class IotDevicePropertyPostTriggerMatcherTest extends IotBaseConditionMatcherTest {
 
    private IotDevicePropertyPostTriggerMatcher matcher;
 
    @BeforeEach
    public void setUp() {
        matcher = new IotDevicePropertyPostTriggerMatcher();
    }
 
    @Test
    public void testGetSupportedTriggerType_success() {
        // 准备参数
        // 无需准备参数
 
        // 调用
        IotSceneRuleTriggerTypeEnum result = matcher.getSupportedTriggerType();
 
        // 断言
        assertEquals(IotSceneRuleTriggerTypeEnum.DEVICE_PROPERTY_POST, result);
    }
 
    @Test
    public void testGetPriority_success() {
        // 准备参数
        // 无需准备参数
 
        // 调用
        int result = matcher.getPriority();
 
        // 断言
        assertEquals(20, result);
    }
 
    @Test
    public void testIsEnabled_success() {
        // 准备参数
        // 无需准备参数
 
        // 调用
        boolean result = matcher.isEnabled();
 
        // 断言
        assertTrue(result);
    }
 
    @Test
    public void testMatches_numericPropertyGreaterThanSuccess() {
        // 准备参数
        String propertyName = randomString();
        Double propertyValue = 25.5;
        Integer compareValue = 20;
        Map<String, Object> properties = MapUtil.builder(new HashMap<String, Object>())
                .put(propertyName, propertyValue)
                .build();
        IotDeviceMessage message = createPropertyPostMessage(properties);
        IotSceneRuleDO.Trigger trigger = createValidTrigger(
                propertyName,
                IotSceneRuleConditionOperatorEnum.GREATER_THAN.getOperator(),
                String.valueOf(compareValue)
        );
 
        // 调用
        boolean result = matcher.matches(message, trigger);
 
        // 断言
        assertTrue(result);
    }
 
    @Test
    public void testMatches_integerPropertyEqualsSuccess() {
        // 准备参数
        String propertyName = randomString();
        Integer propertyValue = randomInt();
        Map<String, Object> properties = MapUtil.builder(new HashMap<String, Object>())
                .put(propertyName, propertyValue)
                .build();
        IotDeviceMessage message = createPropertyPostMessage(properties);
        IotSceneRuleDO.Trigger trigger = createValidTrigger(
                propertyName,
                IotSceneRuleConditionOperatorEnum.EQUALS.getOperator(),
                String.valueOf(propertyValue)
        );
 
        // 调用
        boolean result = matcher.matches(message, trigger);
 
        // 断言
        assertTrue(result);
    }
 
    @Test
    public void testMatches_propertyValueNotMeetCondition() {
        // 准备参数
        String propertyName = randomString();
        Double propertyValue = 15.0;
        Integer compareValue = 20;
        Map<String, Object> properties = MapUtil.builder(new HashMap<String, Object>())
                .put(propertyName, propertyValue)
                .build();
        IotDeviceMessage message = createPropertyPostMessage(properties);
        IotSceneRuleDO.Trigger trigger = createValidTrigger(
                propertyName,
                IotSceneRuleConditionOperatorEnum.GREATER_THAN.getOperator(),
                String.valueOf(compareValue)
        );
 
        // 调用
        boolean result = matcher.matches(message, trigger);
 
        // 断言
        assertFalse(result);
    }
 
    @Test
    public void testMatches_propertyNotFound() {
        // 准备参数
        String existingProperty = randomString();
        String missingProperty = randomString();
        Map<String, Object> properties = MapUtil.builder(new HashMap<String, Object>())
                .put(existingProperty, randomDouble())
                .build();
        IotDeviceMessage message = createPropertyPostMessage(properties);
        IotSceneRuleDO.Trigger trigger = createValidTrigger(
                missingProperty, // 不存在的属性
                IotSceneRuleConditionOperatorEnum.GREATER_THAN.getOperator(),
                String.valueOf(randomInt())
        );
 
        // 调用
        boolean result = matcher.matches(message, trigger);
 
        // 断言
        assertFalse(result);
    }
 
    @Test
    public void testMatches_wrongMessageMethod() {
        // 准备参数
        String propertyName = randomString();
        Map<String, Object> properties = MapUtil.builder(new HashMap<String, Object>())
                .put(propertyName, randomDouble())
                .build();
        IotDeviceMessage message = new IotDeviceMessage();
        message.setDeviceId(randomLongId());
        message.setMethod(IotDeviceMessageMethodEnum.STATE_UPDATE.getMethod());
        message.setParams(properties);
        IotSceneRuleDO.Trigger trigger = createValidTrigger(
                propertyName,
                IotSceneRuleConditionOperatorEnum.GREATER_THAN.getOperator(),
                String.valueOf(randomInt())
        );
 
        // 调用
        boolean result = matcher.matches(message, trigger);
 
        // 断言
        assertFalse(result);
    }
 
    @Test
    public void testMatches_nullTriggerIdentifier() {
        // 准备参数
        String propertyName = randomString();
        Map<String, Object> properties = MapUtil.builder(new HashMap<String, Object>())
                .put(propertyName, randomDouble())
                .build();
        IotDeviceMessage message = createPropertyPostMessage(properties);
        IotSceneRuleDO.Trigger trigger = new IotSceneRuleDO.Trigger();
        trigger.setType(IotSceneRuleTriggerTypeEnum.DEVICE_PROPERTY_POST.getType());
        trigger.setIdentifier(null); // 缺少标识符
        trigger.setOperator(IotSceneRuleConditionOperatorEnum.GREATER_THAN.getOperator());
        trigger.setValue(String.valueOf(randomInt()));
 
        // 调用
        boolean result = matcher.matches(message, trigger);
 
        // 断言
        assertFalse(result);
    }
 
    @Test
    public void testMatches_nullMessageParams() {
        // 准备参数
        String propertyName = randomString();
        IotDeviceMessage message = new IotDeviceMessage();
        message.setDeviceId(randomLongId());
        message.setMethod(IotDeviceMessageMethodEnum.PROPERTY_POST.getMethod());
        message.setParams(null);
        IotSceneRuleDO.Trigger trigger = createValidTrigger(
                propertyName,
                IotSceneRuleConditionOperatorEnum.GREATER_THAN.getOperator(),
                String.valueOf(randomInt())
        );
 
        // 调用
        boolean result = matcher.matches(message, trigger);
 
        // 断言
        assertFalse(result);
    }
 
    @Test
    public void testMatches_invalidMessageParams() {
        // 准备参数
        String propertyName = randomString();
        IotDeviceMessage message = new IotDeviceMessage();
        message.setDeviceId(randomLongId());
        message.setMethod(IotDeviceMessageMethodEnum.PROPERTY_POST.getMethod());
        message.setParams(randomString()); // 不是 Map 类型
        IotSceneRuleDO.Trigger trigger = createValidTrigger(
                propertyName,
                IotSceneRuleConditionOperatorEnum.GREATER_THAN.getOperator(),
                String.valueOf(randomInt())
        );
 
        // 调用
        boolean result = matcher.matches(message, trigger);
 
        // 断言
        assertFalse(result);
    }
 
    @Test
    public void testMatches_lessThanOperatorSuccess() {
        // 准备参数
        String propertyName = randomString();
        Double propertyValue = 15.0;
        Integer compareValue = 20;
        Map<String, Object> properties = MapUtil.builder(new HashMap<String, Object>())
                .put(propertyName, propertyValue)
                .build();
        IotDeviceMessage message = createPropertyPostMessage(properties);
        IotSceneRuleDO.Trigger trigger = createValidTrigger(
                propertyName,
                IotSceneRuleConditionOperatorEnum.LESS_THAN.getOperator(),
                String.valueOf(compareValue)
        );
 
        // 调用
        boolean result = matcher.matches(message, trigger);
 
        // 断言
        assertTrue(result);
    }
 
    @Test
    public void testMatches_notEqualsOperatorSuccess() {
        // 准备参数
        String propertyName = randomString();
        String propertyValue = randomString();
        String compareValue = randomString();
        Map<String, Object> properties = MapUtil.builder(new HashMap<String, Object>())
                .put(propertyName, propertyValue)
                .build();
        IotDeviceMessage message = createPropertyPostMessage(properties);
        IotSceneRuleDO.Trigger trigger = createValidTrigger(
                propertyName,
                IotSceneRuleConditionOperatorEnum.NOT_EQUALS.getOperator(),
                compareValue
        );
 
        // 调用
        boolean result = matcher.matches(message, trigger);
 
        // 断言
        assertTrue(result);
    }
 
    @Test
    public void testMatches_multiplePropertiesTargetPropertySuccess() {
        // 准备参数
        String targetProperty = randomString();
        Integer targetValue = randomInt();
        Map<String, Object> properties = MapUtil.builder(new HashMap<String, Object>())
                .put(randomString(), randomDouble())
                .put(targetProperty, targetValue)
                .put(randomString(), randomString())
                .build();
        IotDeviceMessage message = createPropertyPostMessage(properties);
        IotSceneRuleDO.Trigger trigger = createValidTrigger(
                targetProperty,
                IotSceneRuleConditionOperatorEnum.EQUALS.getOperator(),
                String.valueOf(targetValue)
        );
 
        // 调用
        boolean result = matcher.matches(message, trigger);
 
        // 断言
        assertTrue(result);
    }
 
    // ========== 辅助方法 ==========
 
    /**
     * 创建属性上报消息
     */
    private IotDeviceMessage createPropertyPostMessage(Map<String, Object> properties) {
        IotDeviceMessage message = new IotDeviceMessage();
        message.setDeviceId(randomLongId());
        message.setMethod(IotDeviceMessageMethodEnum.PROPERTY_POST.getMethod());
        message.setParams(properties);
        return message;
    }
 
    /**
     * 创建有效的触发器
     */
    private IotSceneRuleDO.Trigger createValidTrigger(String identifier, String operator, String value) {
        IotSceneRuleDO.Trigger trigger = new IotSceneRuleDO.Trigger();
        trigger.setType(IotSceneRuleTriggerTypeEnum.DEVICE_PROPERTY_POST.getType());
        trigger.setIdentifier(identifier);
        trigger.setOperator(operator);
        trigger.setValue(value);
        return trigger;
    }
 
}