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
package cn.iocoder.yudao.module.iot.service.rule.scene.matcher.condition;
 
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.IotSceneRuleConditionTypeEnum;
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.time.LocalDateTime;
import java.time.ZoneOffset;
 
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 IotCurrentTimeConditionMatcher} 的单元测试
 *
 * @author HUIHUI
 */
public class IotCurrentTimeConditionMatcherTest extends IotBaseConditionMatcherTest {
 
    private IotCurrentTimeConditionMatcher matcher;
 
    @BeforeEach
    public void setUp() {
        matcher = new IotCurrentTimeConditionMatcher();
    }
 
    @Test
    public void testGetSupportedConditionType() {
        // 调用
        IotSceneRuleConditionTypeEnum result = matcher.getSupportedConditionType();
 
        // 断言
        assertEquals(IotSceneRuleConditionTypeEnum.CURRENT_TIME, result);
    }
 
    @Test
    public void testGetPriority() {
        // 调用
        int result = matcher.getPriority();
 
        // 断言
        assertEquals(40, result);
    }
 
    @Test
    public void testIsEnabled() {
        // 调用
        boolean result = matcher.isEnabled();
 
        // 断言
        assertTrue(result);
    }
 
    // ========== 时间戳条件测试 ==========
 
    @Test
    public void testMatches_dateTimeGreaterThan_success() {
        // 准备参数
        IotDeviceMessage message = createDeviceMessage();
        long pastTimestamp = LocalDateTime.now().minusHours(1).toEpochSecond(ZoneOffset.of("+8"));
        IotSceneRuleDO.TriggerCondition condition = createDateTimeCondition(
                IotSceneRuleConditionOperatorEnum.DATE_TIME_GREATER_THAN.getOperator(),
                String.valueOf(pastTimestamp)
        );
 
        // 调用
        boolean result = matcher.matches(message, condition);
 
        // 断言
        assertTrue(result);
    }
 
    @Test
    public void testMatches_dateTimeGreaterThan_fail() {
        // 准备参数
        IotDeviceMessage message = createDeviceMessage();
        long futureTimestamp = LocalDateTime.now().plusHours(1).toEpochSecond(ZoneOffset.of("+8"));
        IotSceneRuleDO.TriggerCondition condition = createDateTimeCondition(
                IotSceneRuleConditionOperatorEnum.DATE_TIME_GREATER_THAN.getOperator(),
                String.valueOf(futureTimestamp)
        );
 
        // 调用
        boolean result = matcher.matches(message, condition);
 
        // 断言
        assertFalse(result);
    }
 
    @Test
    public void testMatches_dateTimeLessThan_success() {
        // 准备参数
        IotDeviceMessage message = createDeviceMessage();
        long futureTimestamp = LocalDateTime.now().plusHours(1).toEpochSecond(ZoneOffset.of("+8"));
        IotSceneRuleDO.TriggerCondition condition = createDateTimeCondition(
                IotSceneRuleConditionOperatorEnum.DATE_TIME_LESS_THAN.getOperator(),
                String.valueOf(futureTimestamp)
        );
 
        // 调用
        boolean result = matcher.matches(message, condition);
 
        // 断言
        assertTrue(result);
    }
 
    @Test
    public void testMatches_dateTimeBetween_success() {
        // 准备参数
        IotDeviceMessage message = createDeviceMessage();
        long startTimestamp = LocalDateTime.now().minusHours(1).toEpochSecond(ZoneOffset.of("+8"));
        long endTimestamp = LocalDateTime.now().plusHours(1).toEpochSecond(ZoneOffset.of("+8"));
        IotSceneRuleDO.TriggerCondition condition = createDateTimeCondition(
                IotSceneRuleConditionOperatorEnum.DATE_TIME_BETWEEN.getOperator(),
                startTimestamp + "," + endTimestamp
        );
 
        // 调用
        boolean result = matcher.matches(message, condition);
 
        // 断言
        assertTrue(result);
    }
 
    @Test
    public void testMatches_dateTimeBetween_fail() {
        // 准备参数
        IotDeviceMessage message = createDeviceMessage();
        long startTimestamp = LocalDateTime.now().plusHours(1).toEpochSecond(ZoneOffset.of("+8"));
        long endTimestamp = LocalDateTime.now().plusHours(2).toEpochSecond(ZoneOffset.of("+8"));
        IotSceneRuleDO.TriggerCondition condition = createDateTimeCondition(
                IotSceneRuleConditionOperatorEnum.DATE_TIME_BETWEEN.getOperator(),
                startTimestamp + "," + endTimestamp
        );
 
        // 调用
        boolean result = matcher.matches(message, condition);
 
        // 断言
        assertFalse(result);
    }
 
    // ========== 当日时间条件测试 ==========
 
    @Test
    public void testMatches_timeGreaterThan_earlyMorning() {
        // 准备参数
        IotDeviceMessage message = createDeviceMessage();
        IotSceneRuleDO.TriggerCondition condition = createTimeCondition(
                IotSceneRuleConditionOperatorEnum.TIME_GREATER_THAN.getOperator(),
                "06:00:00" // 早上6点
        );
 
        // 调用
        boolean result = matcher.matches(message, condition);
 
        // 断言
        // 结果取决于当前时间,如果当前时间大于6点则为true
        assertNotNull(result);
    }
 
    @Test
    public void testMatches_timeLessThan_lateNight() {
        // 准备参数
        IotDeviceMessage message = createDeviceMessage();
        IotSceneRuleDO.TriggerCondition condition = createTimeCondition(
                IotSceneRuleConditionOperatorEnum.TIME_LESS_THAN.getOperator(),
                "23:59:59" // 晚上11点59分59秒
        );
 
        // 调用
        boolean result = matcher.matches(message, condition);
 
        // 断言
        // 大部分情况下应该为true,除非在午夜前1秒运行测试
        assertNotNull(result);
    }
 
    @Test
    public void testMatches_timeBetween_allDay() {
        // 准备参数
        IotDeviceMessage message = createDeviceMessage();
        IotSceneRuleDO.TriggerCondition condition = createTimeCondition(
                IotSceneRuleConditionOperatorEnum.TIME_BETWEEN.getOperator(),
                "00:00:00,23:59:59" // 全天
        );
 
        // 调用
        boolean result = matcher.matches(message, condition);
 
        // 断言
        assertTrue(result); // 全天范围应该总是匹配
    }
 
    @Test
    public void testMatches_timeBetween_workingHours() {
        // 准备参数
        IotDeviceMessage message = createDeviceMessage();
        IotSceneRuleDO.TriggerCondition condition = createTimeCondition(
                IotSceneRuleConditionOperatorEnum.TIME_BETWEEN.getOperator(),
                "09:00:00,17:00:00" // 工作时间
        );
 
        // 调用
        boolean result = matcher.matches(message, condition);
 
        // 断言
        // 结果取决于当前时间是否在工作时间内
        assertNotNull(result);
    }
 
    // ========== 异常情况测试 ==========
 
    @Test
    public void testMatches_nullCondition() {
        // 准备参数
        IotDeviceMessage message = createDeviceMessage();
 
        // 调用
        boolean result = matcher.matches(message, null);
 
        // 断言
        assertFalse(result);
    }
 
    @Test
    public void testMatches_nullConditionType() {
        // 准备参数
        IotDeviceMessage message = createDeviceMessage();
        IotSceneRuleDO.TriggerCondition condition = new IotSceneRuleDO.TriggerCondition();
        condition.setType(null);
 
        // 调用
        boolean result = matcher.matches(message, condition);
 
        // 断言
        assertFalse(result);
    }
 
    @Test
    public void testMatches_invalidOperator() {
        // 准备参数
        IotDeviceMessage message = createDeviceMessage();
        IotSceneRuleDO.TriggerCondition condition = new IotSceneRuleDO.TriggerCondition();
        condition.setType(IotSceneRuleConditionTypeEnum.CURRENT_TIME.getType());
        condition.setOperator(randomString()); // 随机无效操作符
        condition.setParam("12:00:00");
 
        // 调用
        boolean result = matcher.matches(message, condition);
 
        // 断言
        assertFalse(result);
    }
 
    @Test
    public void testMatches_invalidTimeFormat() {
        // 准备参数
        IotDeviceMessage message = createDeviceMessage();
        IotSceneRuleDO.TriggerCondition condition = createTimeCondition(
                IotSceneRuleConditionOperatorEnum.TIME_GREATER_THAN.getOperator(),
                randomString() // 随机无效时间格式
        );
 
        // 调用
        boolean result = matcher.matches(message, condition);
 
        // 断言
        assertFalse(result);
    }
 
    @Test
    public void testMatches_invalidTimestampFormat() {
        // 准备参数
        IotDeviceMessage message = createDeviceMessage();
        IotSceneRuleDO.TriggerCondition condition = createDateTimeCondition(
                IotSceneRuleConditionOperatorEnum.DATE_TIME_GREATER_THAN.getOperator(),
                randomString() // 随机无效时间戳格式
        );
 
        // 调用
        boolean result = matcher.matches(message, condition);
 
        // 断言
        assertFalse(result);
    }
 
    @Test
    public void testMatches_invalidBetweenFormat() {
        // 准备参数
        IotDeviceMessage message = createDeviceMessage();
        IotSceneRuleDO.TriggerCondition condition = createTimeCondition(
                IotSceneRuleConditionOperatorEnum.TIME_BETWEEN.getOperator(),
                "09:00:00" // 缺少结束时间
        );
 
        // 调用
        boolean result = matcher.matches(message, condition);
 
        // 断言
        assertFalse(result);
    }
 
    // ========== 辅助方法 ==========
 
    /**
     * 创建设备消息
     */
    private IotDeviceMessage createDeviceMessage() {
        IotDeviceMessage message = new IotDeviceMessage();
        message.setDeviceId(randomLongId());
        return message;
    }
 
    /**
     * 创建日期时间条件
     */
    private IotSceneRuleDO.TriggerCondition createDateTimeCondition(String operator, String param) {
        IotSceneRuleDO.TriggerCondition condition = new IotSceneRuleDO.TriggerCondition();
        condition.setType(IotSceneRuleConditionTypeEnum.CURRENT_TIME.getType());
        condition.setOperator(operator);
        condition.setParam(param);
        return condition;
    }
 
    /**
     * 创建当日时间条件
     */
    private IotSceneRuleDO.TriggerCondition createTimeCondition(String operator, String param) {
        IotSceneRuleDO.TriggerCondition condition = new IotSceneRuleDO.TriggerCondition();
        condition.setType(IotSceneRuleConditionTypeEnum.CURRENT_TIME.getType());
        condition.setOperator(operator);
        condition.setParam(param);
        return condition;
    }
 
}