【代码优化】IoT: 数据桥梁测试代码抽离到测试类
This commit is contained in:
parent
34453a3f70
commit
966357b44e
|
@ -15,12 +15,12 @@ import lombok.Data;
|
|||
@Data
|
||||
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, property = "type", visible = true)
|
||||
@JsonSubTypes({
|
||||
@JsonSubTypes.Type(value = IotDataBridgeHttpConfig.class, name = "HTTP"),
|
||||
@JsonSubTypes.Type(value = IotDataBridgeKafkaMQConfig.class, name = "KAFKA"),
|
||||
@JsonSubTypes.Type(value = IotDataBridgeMqttConfig.class, name = "MQTT"),
|
||||
@JsonSubTypes.Type(value = IotDataBridgeRabbitMQConfig.class, name = "RABBITMQ"),
|
||||
@JsonSubTypes.Type(value = IotDataBridgeRedisStreamMQConfig.class, name = "REDIS_STREAM"),
|
||||
@JsonSubTypes.Type(value = IotDataBridgeRocketMQConfig.class, name = "ROCKETMQ"),
|
||||
@JsonSubTypes.Type(value = IotDataBridgeHttpConfig.class, name = "1"),
|
||||
@JsonSubTypes.Type(value = IotDataBridgeMqttConfig.class, name = "10"),
|
||||
@JsonSubTypes.Type(value = IotDataBridgeRedisStreamMQConfig.class, name = "21"),
|
||||
@JsonSubTypes.Type(value = IotDataBridgeRocketMQConfig.class, name = "30"),
|
||||
@JsonSubTypes.Type(value = IotDataBridgeRabbitMQConfig.class, name = "31"),
|
||||
@JsonSubTypes.Type(value = IotDataBridgeKafkaMQConfig.class, name = "32"),
|
||||
})
|
||||
public abstract class IotDataBridgeAbstractConfig {
|
||||
|
||||
|
|
|
@ -1,6 +1,5 @@
|
|||
package cn.iocoder.yudao.module.iot.service.rule.action.databridge;
|
||||
|
||||
import cn.iocoder.yudao.module.iot.controller.admin.rule.vo.databridge.config.IotDataBridgeAbstractConfig;
|
||||
import cn.iocoder.yudao.module.iot.dal.dataobject.rule.IotDataBridgeDO;
|
||||
import cn.iocoder.yudao.module.iot.mq.message.IotDeviceMessage;
|
||||
|
||||
|
|
|
@ -1,7 +1,6 @@
|
|||
package cn.iocoder.yudao.module.iot.service.rule.action.databridge;
|
||||
|
||||
import cn.iocoder.yudao.module.iot.controller.admin.rule.vo.databridge.config.IotDataBridgeKafkaMQConfig;
|
||||
import cn.iocoder.yudao.module.iot.dal.dataobject.rule.IotDataBridgeDO;
|
||||
import cn.iocoder.yudao.module.iot.enums.rule.IotDataBridgeTypeEnum;
|
||||
import cn.iocoder.yudao.module.iot.mq.message.IotDeviceMessage;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
@ -13,7 +12,6 @@ import org.springframework.kafka.core.KafkaTemplate;
|
|||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.time.Duration;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
@ -77,38 +75,4 @@ public class IotKafkaMQDataBridgeExecute extends
|
|||
producer.destroy();
|
||||
}
|
||||
|
||||
// TODO @芋艿:测试代码,后续清理
|
||||
public static void main(String[] args) {
|
||||
// 1. 创建一个共享的实例
|
||||
IotKafkaMQDataBridgeExecute action = new IotKafkaMQDataBridgeExecute();
|
||||
|
||||
// 2. 创建共享的配置
|
||||
IotDataBridgeKafkaMQConfig config = new IotDataBridgeKafkaMQConfig();
|
||||
config.setBootstrapServers("127.0.0.1:9092");
|
||||
config.setTopic("test-topic");
|
||||
config.setSsl(false);
|
||||
config.setUsername(null);
|
||||
config.setPassword(null);
|
||||
|
||||
// 3. 创建共享的消息
|
||||
IotDeviceMessage message = IotDeviceMessage.builder()
|
||||
.requestId("TEST-001")
|
||||
.productKey("testProduct")
|
||||
.deviceName("testDevice")
|
||||
.deviceKey("testDeviceKey")
|
||||
.type("property")
|
||||
.identifier("temperature")
|
||||
.data("{\"value\": 60}")
|
||||
.reportTime(LocalDateTime.now())
|
||||
.tenantId(1L)
|
||||
.build();
|
||||
|
||||
// 4. 执行两次测试,验证缓存
|
||||
log.info("[main][第一次执行,应该会创建新的 producer]");
|
||||
action.execute(message, new IotDataBridgeDO().setType(action.getType()).setConfig(config));
|
||||
|
||||
log.info("[main][第二次执行,应该会复用缓存的 producer]");
|
||||
action.execute(message, new IotDataBridgeDO().setType(action.getType()).setConfig(config));
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -1,7 +1,6 @@
|
|||
package cn.iocoder.yudao.module.iot.service.rule.action.databridge;
|
||||
|
||||
import cn.iocoder.yudao.module.iot.controller.admin.rule.vo.databridge.config.IotDataBridgeRabbitMQConfig;
|
||||
import cn.iocoder.yudao.module.iot.dal.dataobject.rule.IotDataBridgeDO;
|
||||
import cn.iocoder.yudao.module.iot.enums.rule.IotDataBridgeTypeEnum;
|
||||
import cn.iocoder.yudao.module.iot.mq.message.IotDeviceMessage;
|
||||
import com.rabbitmq.client.Channel;
|
||||
|
@ -12,7 +11,6 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
|
|||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
/**
|
||||
* RabbitMQ 的 {@link IotDataBridgeExecute} 实现类
|
||||
|
@ -76,42 +74,4 @@ public class IotRabbitMQDataBridgeExecute extends
|
|||
}
|
||||
}
|
||||
|
||||
// TODO @芋艿:测试代码,后续清理
|
||||
public static void main(String[] args) {
|
||||
// 1. 创建一个共享的实例
|
||||
IotRabbitMQDataBridgeExecute action = new IotRabbitMQDataBridgeExecute();
|
||||
|
||||
// 2. 创建共享的配置
|
||||
IotDataBridgeRabbitMQConfig config = new IotDataBridgeRabbitMQConfig();
|
||||
config.setHost("localhost");
|
||||
config.setPort(5672);
|
||||
config.setVirtualHost("/");
|
||||
config.setUsername("admin");
|
||||
config.setPassword("123456");
|
||||
config.setExchange("test-exchange");
|
||||
config.setRoutingKey("test-key");
|
||||
config.setQueue("test-queue");
|
||||
|
||||
// 3. 创建共享的消息
|
||||
IotDeviceMessage message = IotDeviceMessage.builder()
|
||||
.requestId("TEST-001")
|
||||
.productKey("testProduct")
|
||||
.deviceName("testDevice")
|
||||
.deviceKey("testDeviceKey")
|
||||
.type("property")
|
||||
.identifier("temperature")
|
||||
.data("{\"value\": 60}")
|
||||
.reportTime(LocalDateTime.now())
|
||||
.tenantId(1L)
|
||||
.build();
|
||||
|
||||
// 4. 执行两次测试,验证缓存
|
||||
// 4. 执行两次测试,验证缓存
|
||||
log.info("[main][第一次执行,应该会创建新的 producer]");
|
||||
action.execute(message, new IotDataBridgeDO().setType(action.getType()).setConfig(config));
|
||||
|
||||
log.info("[main][第二次执行,应该会复用缓存的 producer]");
|
||||
action.execute(message, new IotDataBridgeDO().setType(action.getType()).setConfig(config));
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -3,7 +3,6 @@ package cn.iocoder.yudao.module.iot.service.rule.action.databridge;
|
|||
import cn.hutool.core.util.ReflectUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import cn.iocoder.yudao.module.iot.controller.admin.rule.vo.databridge.config.IotDataBridgeRedisStreamMQConfig;
|
||||
import cn.iocoder.yudao.module.iot.dal.dataobject.rule.IotDataBridgeDO;
|
||||
import cn.iocoder.yudao.module.iot.enums.rule.IotDataBridgeTypeEnum;
|
||||
import cn.iocoder.yudao.module.iot.mq.message.IotDeviceMessage;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
|
@ -21,8 +20,6 @@ import org.springframework.data.redis.core.RedisTemplate;
|
|||
import org.springframework.data.redis.serializer.RedisSerializer;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
/**
|
||||
* Redis Stream MQ 的 {@link IotDataBridgeExecute} 实现类
|
||||
*
|
||||
|
@ -96,38 +93,4 @@ public class IotRedisStreamMQDataBridgeExecute extends
|
|||
return json;
|
||||
}
|
||||
|
||||
// TODO @芋艿:测试代码,后续清理
|
||||
public static void main(String[] args) {
|
||||
// 1. 创建一个共享的实例
|
||||
IotRedisStreamMQDataBridgeExecute action = new IotRedisStreamMQDataBridgeExecute();
|
||||
|
||||
// 2. 创建共享的配置
|
||||
IotDataBridgeRedisStreamMQConfig config = new IotDataBridgeRedisStreamMQConfig();
|
||||
config.setHost("127.0.0.1");
|
||||
config.setPort(6379);
|
||||
config.setDatabase(0);
|
||||
config.setPassword("123456");
|
||||
config.setTopic("test-stream");
|
||||
|
||||
// 3. 创建共享的消息
|
||||
IotDeviceMessage message = IotDeviceMessage.builder()
|
||||
.requestId("TEST-001")
|
||||
.productKey("testProduct")
|
||||
.deviceName("testDevice")
|
||||
.deviceKey("testDeviceKey")
|
||||
.type("property")
|
||||
.identifier("temperature")
|
||||
.data("{\"value\": 60}")
|
||||
.reportTime(LocalDateTime.now())
|
||||
.tenantId(1L)
|
||||
.build();
|
||||
|
||||
// 4. 执行两次测试,验证缓存
|
||||
log.info("[main][第一次执行,应该会创建新的 producer]");
|
||||
action.execute(message, new IotDataBridgeDO().setType(action.getType()).setConfig(config));
|
||||
|
||||
log.info("[main][第二次执行,应该会复用缓存的 producer]");
|
||||
action.execute(message, new IotDataBridgeDO().setType(action.getType()).setConfig(config));
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,7 +1,6 @@
|
|||
package cn.iocoder.yudao.module.iot.service.rule.action.databridge;
|
||||
|
||||
import cn.iocoder.yudao.module.iot.controller.admin.rule.vo.databridge.config.IotDataBridgeRocketMQConfig;
|
||||
import cn.iocoder.yudao.module.iot.dal.dataobject.rule.IotDataBridgeDO;
|
||||
import cn.iocoder.yudao.module.iot.enums.rule.IotDataBridgeTypeEnum;
|
||||
import cn.iocoder.yudao.module.iot.mq.message.IotDeviceMessage;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
@ -13,8 +12,6 @@ import org.apache.rocketmq.remoting.common.RemotingHelper;
|
|||
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
/**
|
||||
* RocketMQ 的 {@link IotDataBridgeExecute} 实现类
|
||||
*
|
||||
|
@ -65,38 +62,4 @@ public class IotRocketMQDataBridgeExecute extends
|
|||
producer.shutdown();
|
||||
}
|
||||
|
||||
// TODO @芋艿:测试代码,后续清理
|
||||
// TODO @puhui999:搞到测试类里。
|
||||
public static void main(String[] args) {
|
||||
// 1. 创建一个共享的实例
|
||||
IotRocketMQDataBridgeExecute action = new IotRocketMQDataBridgeExecute();
|
||||
|
||||
// 2. 创建共享的配置
|
||||
IotDataBridgeRocketMQConfig config = new IotDataBridgeRocketMQConfig();
|
||||
config.setNameServer("127.0.0.1:9876");
|
||||
config.setGroup("test-group");
|
||||
config.setTopic("test-topic");
|
||||
config.setTags("test-tag");
|
||||
|
||||
// 3. 创建共享的消息
|
||||
IotDeviceMessage message = IotDeviceMessage.builder()
|
||||
.requestId("TEST-001")
|
||||
.productKey("testProduct")
|
||||
.deviceName("testDevice")
|
||||
.deviceKey("testDeviceKey")
|
||||
.type("property")
|
||||
.identifier("temperature")
|
||||
.data("{\"value\": 60}")
|
||||
.reportTime(LocalDateTime.now())
|
||||
.tenantId(1L)
|
||||
.build();
|
||||
|
||||
// 4. 执行两次测试,验证缓存
|
||||
log.info("[main][第一次执行,应该会创建新的 producer]");
|
||||
action.execute(message, new IotDataBridgeDO().setType(action.getType()).setConfig(config));
|
||||
|
||||
log.info("[main][第二次执行,应该会复用缓存的 producer]");
|
||||
action.execute(message, new IotDataBridgeDO().setType(action.getType()).setConfig(config));
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -0,0 +1,159 @@
|
|||
package cn.iocoder.yudao.module.iot.service.rule.action.databridge;
|
||||
|
||||
import cn.iocoder.yudao.framework.test.core.ut.BaseMockitoUnitTest;
|
||||
import cn.iocoder.yudao.module.iot.controller.admin.rule.vo.databridge.config.*;
|
||||
import cn.iocoder.yudao.module.iot.dal.dataobject.rule.IotDataBridgeDO;
|
||||
import cn.iocoder.yudao.module.iot.mq.message.IotDeviceMessage;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Disabled;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.mockito.InjectMocks;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.Mockito;
|
||||
import org.springframework.http.HttpMethod;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.anyString;
|
||||
|
||||
/**
|
||||
* {@link IotDataBridgeExecute} 实现类的测试
|
||||
*
|
||||
* @author HUIHUI
|
||||
*/
|
||||
@Disabled // 默认禁用,需要手动启用测试
|
||||
@Slf4j
|
||||
public class IotDataBridgeExecuteTest extends BaseMockitoUnitTest {
|
||||
|
||||
private IotDeviceMessage message;
|
||||
|
||||
@Mock
|
||||
private RestTemplate restTemplate;
|
||||
|
||||
@InjectMocks
|
||||
private IotHttpDataBridgeExecute httpDataBridgeExecute;
|
||||
|
||||
@BeforeEach
|
||||
public void setUp() {
|
||||
// 创建共享的测试消息
|
||||
message = IotDeviceMessage.builder()
|
||||
.requestId("TEST-001")
|
||||
.productKey("testProduct")
|
||||
.deviceName("testDevice")
|
||||
.deviceKey("testDeviceKey")
|
||||
.type("property")
|
||||
.identifier("temperature")
|
||||
.data("{\"value\": 60}")
|
||||
.reportTime(LocalDateTime.now())
|
||||
.tenantId(1L)
|
||||
.build();
|
||||
|
||||
// 配置 RestTemplate mock 返回成功响应
|
||||
Mockito.when(restTemplate.exchange(anyString(), any(HttpMethod.class), any(), any(Class.class)))
|
||||
.thenReturn(new ResponseEntity<>("Success", HttpStatus.OK));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testKafkaMQDataBridge() {
|
||||
// 1. 创建执行器实例
|
||||
IotKafkaMQDataBridgeExecute action = new IotKafkaMQDataBridgeExecute();
|
||||
|
||||
// 2. 创建配置
|
||||
IotDataBridgeKafkaMQConfig config = new IotDataBridgeKafkaMQConfig();
|
||||
config.setBootstrapServers("127.0.0.1:9092");
|
||||
config.setTopic("test-topic");
|
||||
config.setSsl(false);
|
||||
config.setUsername(null);
|
||||
config.setPassword(null);
|
||||
|
||||
// 3. 执行两次测试,验证缓存
|
||||
log.info("[testKafkaMQDataBridge][第一次执行,应该会创建新的 producer]");
|
||||
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
|
||||
public void testRabbitMQDataBridge() {
|
||||
// 1. 创建执行器实例
|
||||
IotRabbitMQDataBridgeExecute action = new IotRabbitMQDataBridgeExecute();
|
||||
|
||||
// 2. 创建配置
|
||||
IotDataBridgeRabbitMQConfig config = new IotDataBridgeRabbitMQConfig();
|
||||
config.setHost("localhost");
|
||||
config.setPort(5672);
|
||||
config.setVirtualHost("/");
|
||||
config.setUsername("admin");
|
||||
config.setPassword("123456");
|
||||
config.setExchange("test-exchange");
|
||||
config.setRoutingKey("test-key");
|
||||
config.setQueue("test-queue");
|
||||
|
||||
// 3. 执行两次测试,验证缓存
|
||||
log.info("[testRabbitMQDataBridge][第一次执行,应该会创建新的 producer]");
|
||||
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
|
||||
public void testRedisStreamMQDataBridge() {
|
||||
// 1. 创建执行器实例
|
||||
IotRedisStreamMQDataBridgeExecute action = new IotRedisStreamMQDataBridgeExecute();
|
||||
|
||||
// 2. 创建配置
|
||||
IotDataBridgeRedisStreamMQConfig config = new IotDataBridgeRedisStreamMQConfig();
|
||||
config.setHost("127.0.0.1");
|
||||
config.setPort(6379);
|
||||
config.setDatabase(0);
|
||||
config.setPassword("123456");
|
||||
config.setTopic("test-stream");
|
||||
|
||||
// 3. 执行两次测试,验证缓存
|
||||
log.info("[testRedisStreamMQDataBridge][第一次执行,应该会创建新的 producer]");
|
||||
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
|
||||
public void testRocketMQDataBridge() {
|
||||
// 1. 创建执行器实例
|
||||
IotRocketMQDataBridgeExecute action = new IotRocketMQDataBridgeExecute();
|
||||
|
||||
// 2. 创建配置
|
||||
IotDataBridgeRocketMQConfig config = new IotDataBridgeRocketMQConfig();
|
||||
config.setNameServer("127.0.0.1:9876");
|
||||
config.setGroup("test-group");
|
||||
config.setTopic("test-topic");
|
||||
config.setTags("test-tag");
|
||||
|
||||
// 3. 执行两次测试,验证缓存
|
||||
log.info("[testRocketMQDataBridge][第一次执行,应该会创建新的 producer]");
|
||||
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
|
||||
public void testHttpDataBridge() throws Exception {
|
||||
// 创建配置
|
||||
IotDataBridgeHttpConfig config = new IotDataBridgeHttpConfig();
|
||||
config.setUrl("https://doc.iocoder.cn/");
|
||||
config.setMethod(HttpMethod.GET.name());
|
||||
|
||||
// 执行测试
|
||||
log.info("[testHttpDataBridge][执行HTTP数据桥接测试]");
|
||||
httpDataBridgeExecute.execute(message, new IotDataBridgeDO().setType(httpDataBridgeExecute.getType()).setConfig(config));
|
||||
}
|
||||
|
||||
}
|
|
@ -115,19 +115,18 @@
|
|||
<version>${revision}</version>
|
||||
</dependency>
|
||||
<!-- IoT 数据桥梁的执行器所需消息队列。如果您只需要使用 rocketmq 那么则注释掉其它消息队列即可 -->
|
||||
<!-- TODO @puhui999:默认不使用哈。可以在 iot biz 那,作为 optional 进行引入 -->
|
||||
<dependency>
|
||||
<groupId>org.apache.rocketmq</groupId>
|
||||
<artifactId>rocketmq-spring-boot-starter</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.kafka</groupId>
|
||||
<artifactId>spring-kafka</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-amqp</artifactId>
|
||||
</dependency>
|
||||
<!-- <dependency>-->
|
||||
<!-- <groupId>org.apache.rocketmq</groupId>-->
|
||||
<!-- <artifactId>rocketmq-spring-boot-starter</artifactId>-->
|
||||
<!-- </dependency>-->
|
||||
<!-- <dependency>-->
|
||||
<!-- <groupId>org.springframework.kafka</groupId>-->
|
||||
<!-- <artifactId>spring-kafka</artifactId>-->
|
||||
<!-- </dependency>-->
|
||||
<!-- <dependency>-->
|
||||
<!-- <groupId>org.springframework.boot</groupId>-->
|
||||
<!-- <artifactId>spring-boot-starter-amqp</artifactId>-->
|
||||
<!-- </dependency>-->
|
||||
|
||||
<!-- spring boot 配置所需依赖 -->
|
||||
<dependency>
|
||||
|
|
Loading…
Reference in New Issue