Commit 42d4112bd9aa6a48f29dc95c3e9b6ee3ba5ed576
1 parent
e03e3c1a
fix(iot):单测报错问题
Showing
2 changed files
with
30 additions
and
2 deletions
yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/service/rule/scene/matcher/IotSceneRuleMatcherHelper.java
| ... | ... | @@ -261,9 +261,17 @@ public final class IotSceneRuleMatcherHelper { |
| 261 | 261 | * @return 校验结果 |
| 262 | 262 | */ |
| 263 | 263 | public static boolean productAndDeviceNotMatched(IotDeviceMessage message, Long productId, Long deviceId) { |
| 264 | + if (message == null || message.getDeviceId() == null) { | |
| 265 | + return false; | |
| 266 | + } | |
| 267 | + if (deviceId != null && !IotDeviceDO.DEVICE_ID_ALL.equals(deviceId)) { | |
| 268 | + return ObjectUtil.notEqual(message.getDeviceId(), deviceId); | |
| 269 | + } | |
| 270 | + if (productId == null) { | |
| 271 | + return false; | |
| 272 | + } | |
| 264 | 273 | IotDeviceDO device = SpringUtils.getBean(IotDeviceService.class).getDeviceFromCache(message.getDeviceId()); |
| 265 | - return IotDeviceDO.DEVICE_ID_ALL.equals(deviceId) ? ObjectUtil.notEqual(device.getProductId(), productId) | |
| 266 | - : ObjectUtil.notEqual(message.getDeviceId(), deviceId); | |
| 274 | + return device == null || ObjectUtil.notEqual(device.getProductId(), productId); | |
| 267 | 275 | } |
| 268 | 276 | |
| 269 | 277 | } | ... | ... |
yudao-module-iot/yudao-module-iot-biz/src/test/java/cn/iocoder/yudao/module/iot/service/rule/scene/IotSceneRuleTimerConditionIntegrationTest.java
| 1 | 1 | package cn.iocoder.yudao.module.iot.service.rule.scene; |
| 2 | 2 | |
| 3 | 3 | import cn.hutool.core.collection.ListUtil; |
| 4 | +import cn.hutool.extra.spring.SpringUtil; | |
| 4 | 5 | import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum; |
| 6 | +import cn.iocoder.yudao.framework.common.util.spring.SpringExpressionUtils; | |
| 5 | 7 | import cn.iocoder.yudao.framework.test.core.ut.BaseMockitoUnitTest; |
| 6 | 8 | import cn.iocoder.yudao.module.iot.core.enums.device.IotDeviceStateEnum; |
| 7 | 9 | import cn.iocoder.yudao.module.iot.dal.dataobject.device.IotDeviceDO; |
| ... | ... | @@ -21,6 +23,9 @@ import cn.iocoder.yudao.module.iot.service.product.IotProductService; |
| 21 | 23 | import org.junit.jupiter.api.*; |
| 22 | 24 | import org.mockito.InjectMocks; |
| 23 | 25 | import org.mockito.Mock; |
| 26 | +import org.springframework.context.annotation.Bean; | |
| 27 | +import org.springframework.context.annotation.Configuration; | |
| 28 | +import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; | |
| 24 | 29 | |
| 25 | 30 | import java.lang.reflect.Field; |
| 26 | 31 | import java.util.*; |
| ... | ... | @@ -43,8 +48,23 @@ import static org.mockito.Mockito.*; |
| 43 | 48 | * |
| 44 | 49 | * @author HUIHUI |
| 45 | 50 | */ |
| 51 | +@SpringJUnitConfig(classes = IotSceneRuleTimerConditionIntegrationTest.TestConfig.class) | |
| 46 | 52 | public class IotSceneRuleTimerConditionIntegrationTest extends BaseMockitoUnitTest { |
| 47 | 53 | |
| 54 | + /** | |
| 55 | + * 注入一下 SpringUtil,解析 EL 表达式时需要 | |
| 56 | + * {@link SpringExpressionUtils#parseExpression} | |
| 57 | + */ | |
| 58 | + @Configuration | |
| 59 | + static class TestConfig { | |
| 60 | + | |
| 61 | + @Bean | |
| 62 | + public SpringUtil springUtil() { | |
| 63 | + return new SpringUtil(); | |
| 64 | + } | |
| 65 | + | |
| 66 | + } | |
| 67 | + | |
| 48 | 68 | @InjectMocks |
| 49 | 69 | private IotSceneRuleServiceImpl sceneRuleService; |
| 50 | 70 | ... | ... |