package com.gxwebsoft.hjm;

import com.gxwebsoft.hjm.service.MqttService;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ActiveProfiles;

import javax.annotation.Resource;

/**
 * MQTT服务测试类
 *
 * @author 科技小王子
 * @since 2025-07-02
 */
@SpringBootTest
@ActiveProfiles("dev")
public class MqttServiceTest {
    
    @Resource
    private MqttService mqttService;
    
    @Test
    public void testMqttConnection() {
        System.out.println("MQTT连接状态: " + mqttService.isConnected());
        System.out.println("MQTT客户端信息: " + mqttService.getClientInfo());
    }
    
    @Test
    public void testMqttReconnect() {
        try {
            mqttService.reconnect();
            System.out.println("MQTT重连测试完成");
        } catch (Exception e) {
            System.err.println("MQTT重连测试失败: " + e.getMessage());
        }
    }
    
    @Test
    public void testMqttPublish() {
        try {
            if (mqttService.isConnected()) {
                mqttService.publish("/test/topic", "测试消息");
                System.out.println("MQTT消息发布测试完成");
            } else {
                System.out.println("MQTT未连接，跳过发布测试");
            }
        } catch (Exception e) {
            System.err.println("MQTT消息发布测试失败: " + e.getMessage());
        }
    }
}
