Commit 966357b44e4db079d2db207254148e62463c035c

Authored by puhui999
1 parent 34453a3f

【代码优化】IoT: 数据桥梁测试代码抽离到测试类

yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/controller/admin/rule/vo/databridge/config/IotDataBridgeAbstractConfig.java
... ... @@ -15,12 +15,12 @@ import lombok.Data;
15 15 @Data
16 16 @JsonTypeInfo(use = JsonTypeInfo.Id.NAME, property = "type", visible = true)
17 17 @JsonSubTypes({
18   - @JsonSubTypes.Type(value = IotDataBridgeHttpConfig.class, name = "HTTP"),
19   - @JsonSubTypes.Type(value = IotDataBridgeKafkaMQConfig.class, name = "KAFKA"),
20   - @JsonSubTypes.Type(value = IotDataBridgeMqttConfig.class, name = "MQTT"),
21   - @JsonSubTypes.Type(value = IotDataBridgeRabbitMQConfig.class, name = "RABBITMQ"),
22   - @JsonSubTypes.Type(value = IotDataBridgeRedisStreamMQConfig.class, name = "REDIS_STREAM"),
23   - @JsonSubTypes.Type(value = IotDataBridgeRocketMQConfig.class, name = "ROCKETMQ"),
  18 + @JsonSubTypes.Type(value = IotDataBridgeHttpConfig.class, name = "1"),
  19 + @JsonSubTypes.Type(value = IotDataBridgeMqttConfig.class, name = "10"),
  20 + @JsonSubTypes.Type(value = IotDataBridgeRedisStreamMQConfig.class, name = "21"),
  21 + @JsonSubTypes.Type(value = IotDataBridgeRocketMQConfig.class, name = "30"),
  22 + @JsonSubTypes.Type(value = IotDataBridgeRabbitMQConfig.class, name = "31"),
  23 + @JsonSubTypes.Type(value = IotDataBridgeKafkaMQConfig.class, name = "32"),
24 24 })
25 25 public abstract class IotDataBridgeAbstractConfig {
26 26  
... ...
yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/service/rule/action/databridge/IotDataBridgeExecute.java
1 1 package cn.iocoder.yudao.module.iot.service.rule.action.databridge;
2 2  
3   -import cn.iocoder.yudao.module.iot.controller.admin.rule.vo.databridge.config.IotDataBridgeAbstractConfig;
4 3 import cn.iocoder.yudao.module.iot.dal.dataobject.rule.IotDataBridgeDO;
5 4 import cn.iocoder.yudao.module.iot.mq.message.IotDeviceMessage;
6 5  
... ...
yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/service/rule/action/databridge/IotKafkaMQDataBridgeExecute.java
1 1 package cn.iocoder.yudao.module.iot.service.rule.action.databridge;
2 2  
3 3 import cn.iocoder.yudao.module.iot.controller.admin.rule.vo.databridge.config.IotDataBridgeKafkaMQConfig;
4   -import cn.iocoder.yudao.module.iot.dal.dataobject.rule.IotDataBridgeDO;
5 4 import cn.iocoder.yudao.module.iot.enums.rule.IotDataBridgeTypeEnum;
6 5 import cn.iocoder.yudao.module.iot.mq.message.IotDeviceMessage;
7 6 import lombok.extern.slf4j.Slf4j;
... ... @@ -13,7 +12,6 @@ import org.springframework.kafka.core.KafkaTemplate;
13 12 import org.springframework.stereotype.Component;
14 13  
15 14 import java.time.Duration;
16   -import java.time.LocalDateTime;
17 15 import java.util.HashMap;
18 16 import java.util.Map;
19 17 import java.util.concurrent.TimeUnit;
... ... @@ -77,38 +75,4 @@ public class IotKafkaMQDataBridgeExecute extends
77 75 producer.destroy();
78 76 }
79 77  
80   - // TODO @芋艿:测试代码,后续清理
81   - public static void main(String[] args) {
82   - // 1. 创建一个共享的实例
83   - IotKafkaMQDataBridgeExecute action = new IotKafkaMQDataBridgeExecute();
84   -
85   - // 2. 创建共享的配置
86   - IotDataBridgeKafkaMQConfig config = new IotDataBridgeKafkaMQConfig();
87   - config.setBootstrapServers("127.0.0.1:9092");
88   - config.setTopic("test-topic");
89   - config.setSsl(false);
90   - config.setUsername(null);
91   - config.setPassword(null);
92   -
93   - // 3. 创建共享的消息
94   - IotDeviceMessage message = IotDeviceMessage.builder()
95   - .requestId("TEST-001")
96   - .productKey("testProduct")
97   - .deviceName("testDevice")
98   - .deviceKey("testDeviceKey")
99   - .type("property")
100   - .identifier("temperature")
101   - .data("{\"value\": 60}")
102   - .reportTime(LocalDateTime.now())
103   - .tenantId(1L)
104   - .build();
105   -
106   - // 4. 执行两次测试,验证缓存
107   - log.info("[main][第一次执行,应该会创建新的 producer]");
108   - action.execute(message, new IotDataBridgeDO().setType(action.getType()).setConfig(config));
109   -
110   - log.info("[main][第二次执行,应该会复用缓存的 producer]");
111   - action.execute(message, new IotDataBridgeDO().setType(action.getType()).setConfig(config));
112   - }
113   -
114 78 }
... ...
yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/service/rule/action/databridge/IotRabbitMQDataBridgeExecute.java
1 1 package cn.iocoder.yudao.module.iot.service.rule.action.databridge;
2 2  
3 3 import cn.iocoder.yudao.module.iot.controller.admin.rule.vo.databridge.config.IotDataBridgeRabbitMQConfig;
4   -import cn.iocoder.yudao.module.iot.dal.dataobject.rule.IotDataBridgeDO;
5 4 import cn.iocoder.yudao.module.iot.enums.rule.IotDataBridgeTypeEnum;
6 5 import cn.iocoder.yudao.module.iot.mq.message.IotDeviceMessage;
7 6 import com.rabbitmq.client.Channel;
... ... @@ -12,7 +11,6 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
12 11 import org.springframework.stereotype.Component;
13 12  
14 13 import java.nio.charset.StandardCharsets;
15   -import java.time.LocalDateTime;
16 14  
17 15 /**
18 16 * RabbitMQ 的 {@link IotDataBridgeExecute} 实现类
... ... @@ -76,42 +74,4 @@ public class IotRabbitMQDataBridgeExecute extends
76 74 }
77 75 }
78 76  
79   - // TODO @芋艿:测试代码,后续清理
80   - public static void main(String[] args) {
81   - // 1. 创建一个共享的实例
82   - IotRabbitMQDataBridgeExecute action = new IotRabbitMQDataBridgeExecute();
83   -
84   - // 2. 创建共享的配置
85   - IotDataBridgeRabbitMQConfig config = new IotDataBridgeRabbitMQConfig();
86   - config.setHost("localhost");
87   - config.setPort(5672);
88   - config.setVirtualHost("/");
89   - config.setUsername("admin");
90   - config.setPassword("123456");
91   - config.setExchange("test-exchange");
92   - config.setRoutingKey("test-key");
93   - config.setQueue("test-queue");
94   -
95   - // 3. 创建共享的消息
96   - IotDeviceMessage message = IotDeviceMessage.builder()
97   - .requestId("TEST-001")
98   - .productKey("testProduct")
99   - .deviceName("testDevice")
100   - .deviceKey("testDeviceKey")
101   - .type("property")
102   - .identifier("temperature")
103   - .data("{\"value\": 60}")
104   - .reportTime(LocalDateTime.now())
105   - .tenantId(1L)
106   - .build();
107   -
108   - // 4. 执行两次测试,验证缓存
109   - // 4. 执行两次测试,验证缓存
110   - log.info("[main][第一次执行,应该会创建新的 producer]");
111   - action.execute(message, new IotDataBridgeDO().setType(action.getType()).setConfig(config));
112   -
113   - log.info("[main][第二次执行,应该会复用缓存的 producer]");
114   - action.execute(message, new IotDataBridgeDO().setType(action.getType()).setConfig(config));
115   - }
116   -
117 77 }
... ...
yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/service/rule/action/databridge/IotRedisStreamMQDataBridgeExecute.java
... ... @@ -3,7 +3,6 @@ package cn.iocoder.yudao.module.iot.service.rule.action.databridge;
3 3 import cn.hutool.core.util.ReflectUtil;
4 4 import cn.hutool.core.util.StrUtil;
5 5 import cn.iocoder.yudao.module.iot.controller.admin.rule.vo.databridge.config.IotDataBridgeRedisStreamMQConfig;
6   -import cn.iocoder.yudao.module.iot.dal.dataobject.rule.IotDataBridgeDO;
7 6 import cn.iocoder.yudao.module.iot.enums.rule.IotDataBridgeTypeEnum;
8 7 import cn.iocoder.yudao.module.iot.mq.message.IotDeviceMessage;
9 8 import com.fasterxml.jackson.databind.ObjectMapper;
... ... @@ -21,8 +20,6 @@ import org.springframework.data.redis.core.RedisTemplate;
21 20 import org.springframework.data.redis.serializer.RedisSerializer;
22 21 import org.springframework.stereotype.Component;
23 22  
24   -import java.time.LocalDateTime;
25   -
26 23 /**
27 24 * Redis Stream MQ 的 {@link IotDataBridgeExecute} 实现类
28 25 *
... ... @@ -96,38 +93,4 @@ public class IotRedisStreamMQDataBridgeExecute extends
96 93 return json;
97 94 }
98 95  
99   - // TODO @芋艿:测试代码,后续清理
100   - public static void main(String[] args) {
101   - // 1. 创建一个共享的实例
102   - IotRedisStreamMQDataBridgeExecute action = new IotRedisStreamMQDataBridgeExecute();
103   -
104   - // 2. 创建共享的配置
105   - IotDataBridgeRedisStreamMQConfig config = new IotDataBridgeRedisStreamMQConfig();
106   - config.setHost("127.0.0.1");
107   - config.setPort(6379);
108   - config.setDatabase(0);
109   - config.setPassword("123456");
110   - config.setTopic("test-stream");
111   -
112   - // 3. 创建共享的消息
113   - IotDeviceMessage message = IotDeviceMessage.builder()
114   - .requestId("TEST-001")
115   - .productKey("testProduct")
116   - .deviceName("testDevice")
117   - .deviceKey("testDeviceKey")
118   - .type("property")
119   - .identifier("temperature")
120   - .data("{\"value\": 60}")
121   - .reportTime(LocalDateTime.now())
122   - .tenantId(1L)
123   - .build();
124   -
125   - // 4. 执行两次测试,验证缓存
126   - log.info("[main][第一次执行,应该会创建新的 producer]");
127   - action.execute(message, new IotDataBridgeDO().setType(action.getType()).setConfig(config));
128   -
129   - log.info("[main][第二次执行,应该会复用缓存的 producer]");
130   - action.execute(message, new IotDataBridgeDO().setType(action.getType()).setConfig(config));
131   - }
132   -
133   -}
134 96 \ No newline at end of file
  97 +}
... ...
yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/service/rule/action/databridge/IotRocketMQDataBridgeExecute.java
1 1 package cn.iocoder.yudao.module.iot.service.rule.action.databridge;
2 2  
3 3 import cn.iocoder.yudao.module.iot.controller.admin.rule.vo.databridge.config.IotDataBridgeRocketMQConfig;
4   -import cn.iocoder.yudao.module.iot.dal.dataobject.rule.IotDataBridgeDO;
5 4 import cn.iocoder.yudao.module.iot.enums.rule.IotDataBridgeTypeEnum;
6 5 import cn.iocoder.yudao.module.iot.mq.message.IotDeviceMessage;
7 6 import lombok.extern.slf4j.Slf4j;
... ... @@ -13,8 +12,6 @@ import org.apache.rocketmq.remoting.common.RemotingHelper;
13 12 import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
14 13 import org.springframework.stereotype.Component;
15 14  
16   -import java.time.LocalDateTime;
17   -
18 15 /**
19 16 * RocketMQ 的 {@link IotDataBridgeExecute} 实现类
20 17 *
... ... @@ -65,38 +62,4 @@ public class IotRocketMQDataBridgeExecute extends
65 62 producer.shutdown();
66 63 }
67 64  
68   - // TODO @芋艿:测试代码,后续清理
69   - // TODO @puhui999:搞到测试类里。
70   - public static void main(String[] args) {
71   - // 1. 创建一个共享的实例
72   - IotRocketMQDataBridgeExecute action = new IotRocketMQDataBridgeExecute();
73   -
74   - // 2. 创建共享的配置
75   - IotDataBridgeRocketMQConfig config = new IotDataBridgeRocketMQConfig();
76   - config.setNameServer("127.0.0.1:9876");
77   - config.setGroup("test-group");
78   - config.setTopic("test-topic");
79   - config.setTags("test-tag");
80   -
81   - // 3. 创建共享的消息
82   - IotDeviceMessage message = IotDeviceMessage.builder()
83   - .requestId("TEST-001")
84   - .productKey("testProduct")
85   - .deviceName("testDevice")
86   - .deviceKey("testDeviceKey")
87   - .type("property")
88   - .identifier("temperature")
89   - .data("{\"value\": 60}")
90   - .reportTime(LocalDateTime.now())
91   - .tenantId(1L)
92   - .build();
93   -
94   - // 4. 执行两次测试,验证缓存
95   - log.info("[main][第一次执行,应该会创建新的 producer]");
96   - action.execute(message, new IotDataBridgeDO().setType(action.getType()).setConfig(config));
97   -
98   - log.info("[main][第二次执行,应该会复用缓存的 producer]");
99   - action.execute(message, new IotDataBridgeDO().setType(action.getType()).setConfig(config));
100   - }
101   -
102 65 }
... ...
yudao-module-iot/yudao-module-iot-biz/src/test/java/cn/iocoder/yudao/module/iot/service/rule/action/databridge/IotDataBridgeExecuteTest.java 0 → 100644
  1 +package cn.iocoder.yudao.module.iot.service.rule.action.databridge;
  2 +
  3 +import cn.iocoder.yudao.framework.test.core.ut.BaseMockitoUnitTest;
  4 +import cn.iocoder.yudao.module.iot.controller.admin.rule.vo.databridge.config.*;
  5 +import cn.iocoder.yudao.module.iot.dal.dataobject.rule.IotDataBridgeDO;
  6 +import cn.iocoder.yudao.module.iot.mq.message.IotDeviceMessage;
  7 +import lombok.extern.slf4j.Slf4j;
  8 +import org.junit.jupiter.api.BeforeEach;
  9 +import org.junit.jupiter.api.Disabled;
  10 +import org.junit.jupiter.api.Test;
  11 +import org.mockito.InjectMocks;
  12 +import org.mockito.Mock;
  13 +import org.mockito.Mockito;
  14 +import org.springframework.http.HttpMethod;
  15 +import org.springframework.http.HttpStatus;
  16 +import org.springframework.http.ResponseEntity;
  17 +import org.springframework.web.client.RestTemplate;
  18 +
  19 +import java.time.LocalDateTime;
  20 +
  21 +import static org.mockito.ArgumentMatchers.any;
  22 +import static org.mockito.ArgumentMatchers.anyString;
  23 +
  24 +/**
  25 + * {@link IotDataBridgeExecute} 实现类的测试
  26 + *
  27 + * @author HUIHUI
  28 + */
  29 +@Disabled // 默认禁用,需要手动启用测试
  30 +@Slf4j
  31 +public class IotDataBridgeExecuteTest extends BaseMockitoUnitTest {
  32 +
  33 + private IotDeviceMessage message;
  34 +
  35 + @Mock
  36 + private RestTemplate restTemplate;
  37 +
  38 + @InjectMocks
  39 + private IotHttpDataBridgeExecute httpDataBridgeExecute;
  40 +
  41 + @BeforeEach
  42 + public void setUp() {
  43 + // 创建共享的测试消息
  44 + message = IotDeviceMessage.builder()
  45 + .requestId("TEST-001")
  46 + .productKey("testProduct")
  47 + .deviceName("testDevice")
  48 + .deviceKey("testDeviceKey")
  49 + .type("property")
  50 + .identifier("temperature")
  51 + .data("{\"value\": 60}")
  52 + .reportTime(LocalDateTime.now())
  53 + .tenantId(1L)
  54 + .build();
  55 +
  56 + // 配置 RestTemplate mock 返回成功响应
  57 + Mockito.when(restTemplate.exchange(anyString(), any(HttpMethod.class), any(), any(Class.class)))
  58 + .thenReturn(new ResponseEntity<>("Success", HttpStatus.OK));
  59 + }
  60 +
  61 + @Test
  62 + public void testKafkaMQDataBridge() {
  63 + // 1. 创建执行器实例
  64 + IotKafkaMQDataBridgeExecute action = new IotKafkaMQDataBridgeExecute();
  65 +
  66 + // 2. 创建配置
  67 + IotDataBridgeKafkaMQConfig config = new IotDataBridgeKafkaMQConfig();
  68 + config.setBootstrapServers("127.0.0.1:9092");
  69 + config.setTopic("test-topic");
  70 + config.setSsl(false);
  71 + config.setUsername(null);
  72 + config.setPassword(null);
  73 +
  74 + // 3. 执行两次测试,验证缓存
  75 + log.info("[testKafkaMQDataBridge][第一次执行,应该会创建新的 producer]");
  76 + action.execute(message, new IotDataBridgeDO().setType(action.getType()).setConfig(config));
  77 +
  78 + log.info("[testKafkaMQDataBridge][第二次执行,应该会复用缓存的 producer]");
  79 + action.execute(message, new IotDataBridgeDO().setType(action.getType()).setConfig(config));
  80 + }
  81 +
  82 + @Test
  83 + public void testRabbitMQDataBridge() {
  84 + // 1. 创建执行器实例
  85 + IotRabbitMQDataBridgeExecute action = new IotRabbitMQDataBridgeExecute();
  86 +
  87 + // 2. 创建配置
  88 + IotDataBridgeRabbitMQConfig config = new IotDataBridgeRabbitMQConfig();
  89 + config.setHost("localhost");
  90 + config.setPort(5672);
  91 + config.setVirtualHost("/");
  92 + config.setUsername("admin");
  93 + config.setPassword("123456");
  94 + config.setExchange("test-exchange");
  95 + config.setRoutingKey("test-key");
  96 + config.setQueue("test-queue");
  97 +
  98 + // 3. 执行两次测试,验证缓存
  99 + log.info("[testRabbitMQDataBridge][第一次执行,应该会创建新的 producer]");
  100 + action.execute(message, new IotDataBridgeDO().setType(action.getType()).setConfig(config));
  101 +
  102 + log.info("[testRabbitMQDataBridge][第二次执行,应该会复用缓存的 producer]");
  103 + action.execute(message, new IotDataBridgeDO().setType(action.getType()).setConfig(config));
  104 + }
  105 +
  106 + @Test
  107 + public void testRedisStreamMQDataBridge() {
  108 + // 1. 创建执行器实例
  109 + IotRedisStreamMQDataBridgeExecute action = new IotRedisStreamMQDataBridgeExecute();
  110 +
  111 + // 2. 创建配置
  112 + IotDataBridgeRedisStreamMQConfig config = new IotDataBridgeRedisStreamMQConfig();
  113 + config.setHost("127.0.0.1");
  114 + config.setPort(6379);
  115 + config.setDatabase(0);
  116 + config.setPassword("123456");
  117 + config.setTopic("test-stream");
  118 +
  119 + // 3. 执行两次测试,验证缓存
  120 + log.info("[testRedisStreamMQDataBridge][第一次执行,应该会创建新的 producer]");
  121 + action.execute(message, new IotDataBridgeDO().setType(action.getType()).setConfig(config));
  122 +
  123 + log.info("[testRedisStreamMQDataBridge][第二次执行,应该会复用缓存的 producer]");
  124 + action.execute(message, new IotDataBridgeDO().setType(action.getType()).setConfig(config));
  125 + }
  126 +
  127 + @Test
  128 + public void testRocketMQDataBridge() {
  129 + // 1. 创建执行器实例
  130 + IotRocketMQDataBridgeExecute action = new IotRocketMQDataBridgeExecute();
  131 +
  132 + // 2. 创建配置
  133 + IotDataBridgeRocketMQConfig config = new IotDataBridgeRocketMQConfig();
  134 + config.setNameServer("127.0.0.1:9876");
  135 + config.setGroup("test-group");
  136 + config.setTopic("test-topic");
  137 + config.setTags("test-tag");
  138 +
  139 + // 3. 执行两次测试,验证缓存
  140 + log.info("[testRocketMQDataBridge][第一次执行,应该会创建新的 producer]");
  141 + action.execute(message, new IotDataBridgeDO().setType(action.getType()).setConfig(config));
  142 +
  143 + log.info("[testRocketMQDataBridge][第二次执行,应该会复用缓存的 producer]");
  144 + action.execute(message, new IotDataBridgeDO().setType(action.getType()).setConfig(config));
  145 + }
  146 +
  147 + @Test
  148 + public void testHttpDataBridge() throws Exception {
  149 + // 创建配置
  150 + IotDataBridgeHttpConfig config = new IotDataBridgeHttpConfig();
  151 + config.setUrl("https://doc.iocoder.cn/");
  152 + config.setMethod(HttpMethod.GET.name());
  153 +
  154 + // 执行测试
  155 + log.info("[testHttpDataBridge][执行HTTP数据桥接测试]");
  156 + httpDataBridgeExecute.execute(message, new IotDataBridgeDO().setType(httpDataBridgeExecute.getType()).setConfig(config));
  157 + }
  158 +
  159 +}
... ...
yudao-server/pom.xml
... ... @@ -115,19 +115,18 @@
115 115 <version>${revision}</version>
116 116 </dependency>
117 117 <!-- IoT 数据桥梁的执行器所需消息队列。如果您只需要使用 rocketmq 那么则注释掉其它消息队列即可 -->
118   - <!-- TODO @puhui999:默认不使用哈。可以在 iot biz 那,作为 optional 进行引入 -->
119   - <dependency>
120   - <groupId>org.apache.rocketmq</groupId>
121   - <artifactId>rocketmq-spring-boot-starter</artifactId>
122   - </dependency>
123   - <dependency>
124   - <groupId>org.springframework.kafka</groupId>
125   - <artifactId>spring-kafka</artifactId>
126   - </dependency>
127   - <dependency>
128   - <groupId>org.springframework.boot</groupId>
129   - <artifactId>spring-boot-starter-amqp</artifactId>
130   - </dependency>
  118 + <!-- <dependency>-->
  119 + <!-- <groupId>org.apache.rocketmq</groupId>-->
  120 + <!-- <artifactId>rocketmq-spring-boot-starter</artifactId>-->
  121 + <!-- </dependency>-->
  122 + <!-- <dependency>-->
  123 + <!-- <groupId>org.springframework.kafka</groupId>-->
  124 + <!-- <artifactId>spring-kafka</artifactId>-->
  125 + <!-- </dependency>-->
  126 + <!-- <dependency>-->
  127 + <!-- <groupId>org.springframework.boot</groupId>-->
  128 + <!-- <artifactId>spring-boot-starter-amqp</artifactId>-->
  129 + <!-- </dependency>-->
131 130  
132 131 <!-- spring boot 配置所需依赖 -->
133 132 <dependency>
... ...