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

This commit is contained in:
puhui999 2025-03-24 15:45:14 +08:00
parent 2073ecb2f3
commit cd656fad4b
1 changed files with 81 additions and 70 deletions

View File

@ -22,7 +22,7 @@ import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.when; import static org.mockito.Mockito.when;
/** /**
* {@link IotDataBridgeExecute} 实现类的测试 * {@link IotDataBridgeExecute} 实现类的单元测试
* *
* @author HUIHUI * @author HUIHUI
*/ */
@ -41,114 +41,125 @@ public class IotDataBridgeExecuteTest extends BaseMockitoUnitTest {
@BeforeEach @BeforeEach
public void setUp() { public void setUp() {
// 创建共享的测试消息 // 创建共享的测试消息
message = IotDeviceMessage.builder().requestId("TEST-001").reportTime(LocalDateTime.now()).tenantId(1L) message = IotDeviceMessage.builder()
.productKey("testProduct").deviceName("testDevice").deviceKey("testDeviceKey") .requestId("TEST-001")
.type("property").identifier("temperature").data("{\"value\": 60}") .reportTime(LocalDateTime.now())
.tenantId(1L)
.productKey("testProduct")
.deviceName("testDevice")
.deviceKey("testDeviceKey")
.type("property")
.identifier("temperature")
.data("{\"value\": 60}")
.build(); .build();
// 配置 RestTemplate mock 返回成功响应
// TODO @puhui999这个应该放到 testHttpDataBridge
when(restTemplate.exchange(anyString(), any(HttpMethod.class), any(), any(Class.class)))
.thenReturn(new ResponseEntity<>("Success", HttpStatus.OK));
} }
@Test @Test
public void testKafkaMQDataBridge() { public void testKafkaMQDataBridge() throws Exception {
// 1. 创建执行器实例 // 1. 创建执行器实例
IotKafkaMQDataBridgeExecute action = new IotKafkaMQDataBridgeExecute(); IotKafkaMQDataBridgeExecute action = new IotKafkaMQDataBridgeExecute();
// 2. 创建配置 // 2. 创建配置
// TODO @puhui999可以改成链式哈 IotDataBridgeKafkaMQConfig config = new IotDataBridgeKafkaMQConfig()
IotDataBridgeKafkaMQConfig config = new IotDataBridgeKafkaMQConfig(); .setBootstrapServers("127.0.0.1:9092")
config.setBootstrapServers("127.0.0.1:9092"); .setTopic("test-topic")
config.setTopic("test-topic"); .setSsl(false)
config.setSsl(false); .setUsername(null)
config.setUsername(null); .setPassword(null);
config.setPassword(null);
// 3. 执行两次测试验证缓存 // 3. 执行测试并验证缓存
log.info("[testKafkaMQDataBridge][第一次执行,应该会创建新的 producer]"); executeAndVerifyCache(action, config, "KafkaMQ");
action.execute(message, new IotDataBridgeDO().setType(action.getType()).setConfig(config));
log.info("[testKafkaMQDataBridge][第二次执行,应该会复用缓存的 producer]");
action.execute(message, new IotDataBridgeDO().setType(action.getType()).setConfig(config));
} }
@Test @Test
public void testRabbitMQDataBridge() { public void testRabbitMQDataBridge() throws Exception {
// 1. 创建执行器实例 // 1. 创建执行器实例
IotRabbitMQDataBridgeExecute action = new IotRabbitMQDataBridgeExecute(); IotRabbitMQDataBridgeExecute action = new IotRabbitMQDataBridgeExecute();
// 2. 创建配置 // 2. 创建配置
IotDataBridgeRabbitMQConfig config = new IotDataBridgeRabbitMQConfig(); IotDataBridgeRabbitMQConfig config = new IotDataBridgeRabbitMQConfig()
config.setHost("localhost"); .setHost("localhost")
config.setPort(5672); .setPort(5672)
config.setVirtualHost("/"); .setVirtualHost("/")
config.setUsername("admin"); .setUsername("admin")
config.setPassword("123456"); .setPassword("123456")
config.setExchange("test-exchange"); .setExchange("test-exchange")
config.setRoutingKey("test-key"); .setRoutingKey("test-key")
config.setQueue("test-queue"); .setQueue("test-queue");
// 3. 执行两次测试验证缓存 // 3. 执行测试并验证缓存
log.info("[testRabbitMQDataBridge][第一次执行,应该会创建新的 producer]"); executeAndVerifyCache(action, config, "RabbitMQ");
action.execute(message, new IotDataBridgeDO().setType(action.getType()).setConfig(config));
log.info("[testRabbitMQDataBridge][第二次执行,应该会复用缓存的 producer]");
action.execute(message, new IotDataBridgeDO().setType(action.getType()).setConfig(config));
} }
@Test @Test
public void testRedisStreamMQDataBridge() { public void testRedisStreamMQDataBridge() throws Exception {
// 1. 创建执行器实例 // 1. 创建执行器实例
IotRedisStreamMQDataBridgeExecute action = new IotRedisStreamMQDataBridgeExecute(); IotRedisStreamMQDataBridgeExecute action = new IotRedisStreamMQDataBridgeExecute();
// 2. 创建配置 // 2. 创建配置
IotDataBridgeRedisStreamMQConfig config = new IotDataBridgeRedisStreamMQConfig(); IotDataBridgeRedisMQConfig config = new IotDataBridgeRedisMQConfig()
config.setHost("127.0.0.1"); .setHost("127.0.0.1")
config.setPort(6379); .setPort(6379)
config.setDatabase(0); .setDatabase(0)
config.setPassword("123456"); .setPassword("123456")
config.setTopic("test-stream"); .setTopic("test-stream");
// 3. 执行两次测试验证缓存 // 3. 执行测试并验证缓存
log.info("[testRedisStreamMQDataBridge][第一次执行,应该会创建新的 producer]"); executeAndVerifyCache(action, config, "RedisStreamMQ");
action.execute(message, new IotDataBridgeDO().setType(action.getType()).setConfig(config));
log.info("[testRedisStreamMQDataBridge][第二次执行,应该会复用缓存的 producer]");
action.execute(message, new IotDataBridgeDO().setType(action.getType()).setConfig(config));
} }
@Test @Test
public void testRocketMQDataBridge() { public void testRocketMQDataBridge() throws Exception {
// 1. 创建执行器实例 // 1. 创建执行器实例
IotRocketMQDataBridgeExecute action = new IotRocketMQDataBridgeExecute(); IotRocketMQDataBridgeExecute action = new IotRocketMQDataBridgeExecute();
// 2. 创建配置 // 2. 创建配置
IotDataBridgeRocketMQConfig config = new IotDataBridgeRocketMQConfig(); IotDataBridgeRocketMQConfig config = new IotDataBridgeRocketMQConfig()
config.setNameServer("127.0.0.1:9876"); .setNameServer("127.0.0.1:9876")
config.setGroup("test-group"); .setGroup("test-group")
config.setTopic("test-topic"); .setTopic("test-topic")
config.setTags("test-tag"); .setTags("test-tag");
// 3. 执行两次测试验证缓存 // 3. 执行测试并验证缓存
log.info("[testRocketMQDataBridge][第一次执行,应该会创建新的 producer]"); executeAndVerifyCache(action, config, "RocketMQ");
action.execute(message, new IotDataBridgeDO().setType(action.getType()).setConfig(config));
log.info("[testRocketMQDataBridge][第二次执行,应该会复用缓存的 producer]");
action.execute(message, new IotDataBridgeDO().setType(action.getType()).setConfig(config));
} }
@Test @Test
public void testHttpDataBridge() throws Exception { public void testHttpDataBridge() throws Exception {
// 创建配置 // 1. 配置 RestTemplate mock 返回成功响应
IotDataBridgeHttpConfig config = new IotDataBridgeHttpConfig(); when(restTemplate.exchange(anyString(), any(HttpMethod.class), any(), any(Class.class)))
config.setUrl("https://doc.iocoder.cn/"); .thenReturn(new ResponseEntity<>("Success", HttpStatus.OK));
config.setMethod(HttpMethod.GET.name());
// 执行测试 // 2. 创建配置
IotDataBridgeHttpConfig config = new IotDataBridgeHttpConfig()
.setUrl("https://doc.iocoder.cn/")
.setMethod(HttpMethod.GET.name());
// 3. 执行测试
log.info("[testHttpDataBridge][执行HTTP数据桥接测试]"); log.info("[testHttpDataBridge][执行HTTP数据桥接测试]");
httpDataBridgeExecute.execute(message, new IotDataBridgeDO().setType(httpDataBridgeExecute.getType()).setConfig(config)); httpDataBridgeExecute.execute(message, new IotDataBridgeDO()
.setType(httpDataBridgeExecute.getType())
.setConfig(config));
}
/**
* 执行测试并验证缓存的通用方法
*
* @param action 执行器实例
* @param config 配置对象
* @param mqType MQ类型
* @throws Exception 如果执行过程中发生异常
*/
private void executeAndVerifyCache(IotDataBridgeExecute<?> action, IotDataBridgeAbstractConfig config, String mqType) throws Exception {
log.info("[test{}DataBridge][第一次执行,应该会创建新的 producer]", mqType);
action.execute(message, new IotDataBridgeDO()
.setType(action.getType())
.setConfig(config));
log.info("[test{}DataBridge][第二次执行,应该会复用缓存的 producer]", mqType);
action.execute(message, new IotDataBridgeDO()
.setType(action.getType())
.setConfig(config));
} }
} }