2025-03-29 21:52:06 +08:00
|
|
|
package xiaozhi.modules.device;
|
|
|
|
|
|
|
|
|
|
import org.junit.jupiter.api.Assertions;
|
|
|
|
|
import org.junit.jupiter.api.DisplayName;
|
|
|
|
|
import org.junit.jupiter.api.Test;
|
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
|
import org.springframework.boot.test.context.SpringBootTest;
|
|
|
|
|
import org.springframework.test.context.ActiveProfiles;
|
|
|
|
|
|
|
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
|
|
import xiaozhi.common.redis.RedisKeys;
|
|
|
|
|
import xiaozhi.common.redis.RedisUtils;
|
2025-04-30 09:41:38 +08:00
|
|
|
import xiaozhi.modules.sys.dto.SysUserDTO;
|
|
|
|
|
import xiaozhi.modules.sys.service.SysUserService;
|
|
|
|
|
|
|
|
|
|
import java.util.HashMap;
|
2025-03-29 21:52:06 +08:00
|
|
|
|
|
|
|
|
@Slf4j
|
|
|
|
|
@SpringBootTest
|
|
|
|
|
@ActiveProfiles("dev")
|
|
|
|
|
@DisplayName("设备测试")
|
|
|
|
|
public class DeviceTest {
|
|
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
|
private RedisUtils redisUtils;
|
2025-04-30 09:41:38 +08:00
|
|
|
@Autowired
|
|
|
|
|
private SysUserService sysUserService;
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void testSaveUser() {
|
|
|
|
|
SysUserDTO userDTO = new SysUserDTO();
|
|
|
|
|
userDTO.setUsername("13536468486");
|
|
|
|
|
userDTO.setPassword("0218jianyuQ!");
|
|
|
|
|
sysUserService.save(userDTO);
|
|
|
|
|
}
|
2025-03-29 21:52:06 +08:00
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
@DisplayName("测试写入设备信息")
|
|
|
|
|
public void testWriteDeviceInfo() {
|
|
|
|
|
log.info("开始测试写入设备信息...");
|
|
|
|
|
// 模拟设备MAC地址
|
2025-04-30 09:41:38 +08:00
|
|
|
String macAddress = "00:11:22:33:44:66";
|
2025-03-29 21:52:06 +08:00
|
|
|
// 模拟设备验证码
|
|
|
|
|
String deviceCode = "123456";
|
|
|
|
|
|
2025-04-30 09:41:38 +08:00
|
|
|
HashMap<String,Object> map = new HashMap<>();
|
|
|
|
|
map.put("mac_address",macAddress);
|
|
|
|
|
map.put("activation_code",deviceCode);
|
|
|
|
|
map.put("board","硬件型号");
|
|
|
|
|
map.put("app_version","0.3.13");
|
|
|
|
|
|
|
|
|
|
String safeDeviceId = macAddress.replace(":", "_").toLowerCase();
|
|
|
|
|
String cacheDeviceKey = String.format("ota:activation:data:%s", safeDeviceId);
|
|
|
|
|
redisUtils.set(cacheDeviceKey, map, 300);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
String redisKey = "ota:activation:code:"+deviceCode;
|
2025-03-29 21:52:06 +08:00
|
|
|
log.info("Redis Key: {}", redisKey);
|
|
|
|
|
|
|
|
|
|
// 将设备信息写入Redis
|
|
|
|
|
redisUtils.set(redisKey, macAddress, 300);
|
|
|
|
|
log.info("设备信息已写入Redis");
|
|
|
|
|
|
|
|
|
|
// 验证是否写入成功
|
|
|
|
|
String savedMacAddress = (String) redisUtils.get(redisKey);
|
|
|
|
|
log.info("从Redis读取的MAC地址: {}", savedMacAddress);
|
|
|
|
|
|
|
|
|
|
// 使用断言验证
|
|
|
|
|
Assertions.assertNotNull(savedMacAddress, "从Redis读取的MAC地址不应为空");
|
|
|
|
|
Assertions.assertEquals(macAddress, savedMacAddress, "保存的MAC地址与原始MAC地址不匹配");
|
|
|
|
|
|
|
|
|
|
log.info("测试完成");
|
|
|
|
|
}
|
|
|
|
|
}
|