Commit cd656fad4b55c14ce5d49f2fc821f27883d3a102

Authored by puhui999
1 parent 2073ecb2

【代码优化】IoT: 数据桥梁的执行器单元测试优化

yudao-module-iot/yudao-module-iot-biz/src/test/java/cn/iocoder/yudao/module/iot/service/rule/action/databridge/IotDataBridgeExecuteTest.java
... ... @@ -22,7 +22,7 @@ import static org.mockito.ArgumentMatchers.anyString;
22 22 import static org.mockito.Mockito.when;
23 23  
24 24 /**
25   - * {@link IotDataBridgeExecute} 实现类的测试
  25 + * {@link IotDataBridgeExecute} 实现类的单元测试
26 26 *
27 27 * @author HUIHUI
28 28 */
... ... @@ -41,114 +41,125 @@ public class IotDataBridgeExecuteTest extends BaseMockitoUnitTest {
41 41 @BeforeEach
42 42 public void setUp() {
43 43 // 创建共享的测试消息
44   - message = IotDeviceMessage.builder().requestId("TEST-001").reportTime(LocalDateTime.now()).tenantId(1L)
45   - .productKey("testProduct").deviceName("testDevice").deviceKey("testDeviceKey")
46   - .type("property").identifier("temperature").data("{\"value\": 60}")
  44 + message = IotDeviceMessage.builder()
  45 + .requestId("TEST-001")
  46 + .reportTime(LocalDateTime.now())
  47 + .tenantId(1L)
  48 + .productKey("testProduct")
  49 + .deviceName("testDevice")
  50 + .deviceKey("testDeviceKey")
  51 + .type("property")
  52 + .identifier("temperature")
  53 + .data("{\"value\": 60}")
47 54 .build();
48   -
49   - // 配置 RestTemplate mock 返回成功响应
50   - // TODO @puhui999:这个应该放到 testHttpDataBridge 里
51   - when(restTemplate.exchange(anyString(), any(HttpMethod.class), any(), any(Class.class)))
52   - .thenReturn(new ResponseEntity<>("Success", HttpStatus.OK));
53 55 }
54 56  
55 57 @Test
56   - public void testKafkaMQDataBridge() {
  58 + public void testKafkaMQDataBridge() throws Exception {
57 59 // 1. 创建执行器实例
58 60 IotKafkaMQDataBridgeExecute action = new IotKafkaMQDataBridgeExecute();
59 61  
60 62 // 2. 创建配置
61   - // TODO @puhui999:可以改成链式哈。
62   - IotDataBridgeKafkaMQConfig config = new IotDataBridgeKafkaMQConfig();
63   - config.setBootstrapServers("127.0.0.1:9092");
64   - config.setTopic("test-topic");
65   - config.setSsl(false);
66   - config.setUsername(null);
67   - config.setPassword(null);
68   -
69   - // 3. 执行两次测试,验证缓存
70   - log.info("[testKafkaMQDataBridge][第一次执行,应该会创建新的 producer]");
71   - action.execute(message, new IotDataBridgeDO().setType(action.getType()).setConfig(config));
72   -
73   - log.info("[testKafkaMQDataBridge][第二次执行,应该会复用缓存的 producer]");
74   - action.execute(message, new IotDataBridgeDO().setType(action.getType()).setConfig(config));
  63 + IotDataBridgeKafkaMQConfig config = new IotDataBridgeKafkaMQConfig()
  64 + .setBootstrapServers("127.0.0.1:9092")
  65 + .setTopic("test-topic")
  66 + .setSsl(false)
  67 + .setUsername(null)
  68 + .setPassword(null);
  69 +
  70 + // 3. 执行测试并验证缓存
  71 + executeAndVerifyCache(action, config, "KafkaMQ");
75 72 }
76 73  
77 74 @Test
78   - public void testRabbitMQDataBridge() {
  75 + public void testRabbitMQDataBridge() throws Exception {
79 76 // 1. 创建执行器实例
80 77 IotRabbitMQDataBridgeExecute action = new IotRabbitMQDataBridgeExecute();
81 78  
82 79 // 2. 创建配置
83   - IotDataBridgeRabbitMQConfig config = new IotDataBridgeRabbitMQConfig();
84   - config.setHost("localhost");
85   - config.setPort(5672);
86   - config.setVirtualHost("/");
87   - config.setUsername("admin");
88   - config.setPassword("123456");
89   - config.setExchange("test-exchange");
90   - config.setRoutingKey("test-key");
91   - config.setQueue("test-queue");
92   -
93   - // 3. 执行两次测试,验证缓存
94   - log.info("[testRabbitMQDataBridge][第一次执行,应该会创建新的 producer]");
95   - action.execute(message, new IotDataBridgeDO().setType(action.getType()).setConfig(config));
96   -
97   - log.info("[testRabbitMQDataBridge][第二次执行,应该会复用缓存的 producer]");
98   - action.execute(message, new IotDataBridgeDO().setType(action.getType()).setConfig(config));
  80 + IotDataBridgeRabbitMQConfig config = new IotDataBridgeRabbitMQConfig()
  81 + .setHost("localhost")
  82 + .setPort(5672)
  83 + .setVirtualHost("/")
  84 + .setUsername("admin")
  85 + .setPassword("123456")
  86 + .setExchange("test-exchange")
  87 + .setRoutingKey("test-key")
  88 + .setQueue("test-queue");
  89 +
  90 + // 3. 执行测试并验证缓存
  91 + executeAndVerifyCache(action, config, "RabbitMQ");
99 92 }
100 93  
101 94 @Test
102   - public void testRedisStreamMQDataBridge() {
  95 + public void testRedisStreamMQDataBridge() throws Exception {
103 96 // 1. 创建执行器实例
104 97 IotRedisStreamMQDataBridgeExecute action = new IotRedisStreamMQDataBridgeExecute();
105 98  
106 99 // 2. 创建配置
107   - IotDataBridgeRedisStreamMQConfig config = new IotDataBridgeRedisStreamMQConfig();
108   - config.setHost("127.0.0.1");
109   - config.setPort(6379);
110   - config.setDatabase(0);
111   - config.setPassword("123456");
112   - config.setTopic("test-stream");
113   -
114   - // 3. 执行两次测试,验证缓存
115   - log.info("[testRedisStreamMQDataBridge][第一次执行,应该会创建新的 producer]");
116   - action.execute(message, new IotDataBridgeDO().setType(action.getType()).setConfig(config));
117   -
118   - log.info("[testRedisStreamMQDataBridge][第二次执行,应该会复用缓存的 producer]");
119   - action.execute(message, new IotDataBridgeDO().setType(action.getType()).setConfig(config));
  100 + IotDataBridgeRedisMQConfig config = new IotDataBridgeRedisMQConfig()
  101 + .setHost("127.0.0.1")
  102 + .setPort(6379)
  103 + .setDatabase(0)
  104 + .setPassword("123456")
  105 + .setTopic("test-stream");
  106 +
  107 + // 3. 执行测试并验证缓存
  108 + executeAndVerifyCache(action, config, "RedisStreamMQ");
120 109 }
121 110  
122 111 @Test
123   - public void testRocketMQDataBridge() {
  112 + public void testRocketMQDataBridge() throws Exception {
124 113 // 1. 创建执行器实例
125 114 IotRocketMQDataBridgeExecute action = new IotRocketMQDataBridgeExecute();
126 115  
127 116 // 2. 创建配置
128   - IotDataBridgeRocketMQConfig config = new IotDataBridgeRocketMQConfig();
129   - config.setNameServer("127.0.0.1:9876");
130   - config.setGroup("test-group");
131   - config.setTopic("test-topic");
132   - config.setTags("test-tag");
133   -
134   - // 3. 执行两次测试,验证缓存
135   - log.info("[testRocketMQDataBridge][第一次执行,应该会创建新的 producer]");
136   - action.execute(message, new IotDataBridgeDO().setType(action.getType()).setConfig(config));
137   -
138   - log.info("[testRocketMQDataBridge][第二次执行,应该会复用缓存的 producer]");
139   - action.execute(message, new IotDataBridgeDO().setType(action.getType()).setConfig(config));
  117 + IotDataBridgeRocketMQConfig config = new IotDataBridgeRocketMQConfig()
  118 + .setNameServer("127.0.0.1:9876")
  119 + .setGroup("test-group")
  120 + .setTopic("test-topic")
  121 + .setTags("test-tag");
  122 +
  123 + // 3. 执行测试并验证缓存
  124 + executeAndVerifyCache(action, config, "RocketMQ");
140 125 }
141 126  
142 127 @Test
143 128 public void testHttpDataBridge() throws Exception {
144   - // 创建配置
145   - IotDataBridgeHttpConfig config = new IotDataBridgeHttpConfig();
146   - config.setUrl("https://doc.iocoder.cn/");
147   - config.setMethod(HttpMethod.GET.name());
  129 + // 1. 配置 RestTemplate mock 返回成功响应
  130 + when(restTemplate.exchange(anyString(), any(HttpMethod.class), any(), any(Class.class)))
  131 + .thenReturn(new ResponseEntity<>("Success", HttpStatus.OK));
148 132  
149   - // 执行测试
  133 + // 2. 创建配置
  134 + IotDataBridgeHttpConfig config = new IotDataBridgeHttpConfig()
  135 + .setUrl("https://doc.iocoder.cn/")
  136 + .setMethod(HttpMethod.GET.name());
  137 +
  138 + // 3. 执行测试
150 139 log.info("[testHttpDataBridge][执行HTTP数据桥接测试]");
151   - httpDataBridgeExecute.execute(message, new IotDataBridgeDO().setType(httpDataBridgeExecute.getType()).setConfig(config));
  140 + httpDataBridgeExecute.execute(message, new IotDataBridgeDO()
  141 + .setType(httpDataBridgeExecute.getType())
  142 + .setConfig(config));
  143 + }
  144 +
  145 + /**
  146 + * 执行测试并验证缓存的通用方法
  147 + *
  148 + * @param action 执行器实例
  149 + * @param config 配置对象
  150 + * @param mqType MQ类型
  151 + * @throws Exception 如果执行过程中发生异常
  152 + */
  153 + private void executeAndVerifyCache(IotDataBridgeExecute<?> action, IotDataBridgeAbstractConfig config, String mqType) throws Exception {
  154 + log.info("[test{}DataBridge][第一次执行,应该会创建新的 producer]", mqType);
  155 + action.execute(message, new IotDataBridgeDO()
  156 + .setType(action.getType())
  157 + .setConfig(config));
  158 +
  159 + log.info("[test{}DataBridge][第二次执行,应该会复用缓存的 producer]", mqType);
  160 + action.execute(message, new IotDataBridgeDO()
  161 + .setType(action.getType())
  162 + .setConfig(config));
152 163 }
153 164  
154 165 }
... ...