图像透明处理
This commit is contained in:
1739
src/test/java/com/rj/controller/AliyunBackgroundGenerationTest.java
Normal file
1739
src/test/java/com/rj/controller/AliyunBackgroundGenerationTest.java
Normal file
File diff suppressed because it is too large
Load Diff
97
src/test/java/com/rj/controller/AudioUploadTest.java
Normal file
97
src/test/java/com/rj/controller/AudioUploadTest.java
Normal file
@@ -0,0 +1,97 @@
|
||||
package com.rj.controller;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.mock.web.MockMultipartFile;
|
||||
import org.springframework.test.context.ActiveProfiles;
|
||||
import org.springframework.test.web.servlet.MockMvc;
|
||||
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
|
||||
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
|
||||
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
|
||||
import org.springframework.web.context.WebApplicationContext;
|
||||
|
||||
/**
|
||||
* 音频上传功能测试类
|
||||
*
|
||||
* @author rj
|
||||
* @date 2025-01-30
|
||||
*/
|
||||
@SpringBootTest
|
||||
@ActiveProfiles("test")
|
||||
public class AudioUploadTest {
|
||||
|
||||
@Autowired
|
||||
private WebApplicationContext webApplicationContext;
|
||||
|
||||
@Test
|
||||
public void testAudioUpload() throws Exception {
|
||||
MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build();
|
||||
|
||||
// 创建模拟音频文件
|
||||
MockMultipartFile file = new MockMultipartFile(
|
||||
"file",
|
||||
"test-audio.mp3",
|
||||
"audio/mpeg",
|
||||
"fake audio content".getBytes()
|
||||
);
|
||||
|
||||
// 执行上传请求
|
||||
mockMvc.perform(MockMvcRequestBuilders.multipart("/api/tts/upload-audio")
|
||||
.file(file)
|
||||
.param("audioName", "测试音频文件")
|
||||
.param("creatorName", "张三")
|
||||
.param("creatorPhone", "13800138000")
|
||||
.contentType(MediaType.MULTIPART_FORM_DATA))
|
||||
.andExpect(MockMvcResultMatchers.status().isOk())
|
||||
.andExpect(MockMvcResultMatchers.jsonPath("$.status").value("SUCCESS"))
|
||||
.andExpect(MockMvcResultMatchers.jsonPath("$.audioName").value("测试音频文件"))
|
||||
.andExpect(MockMvcResultMatchers.jsonPath("$.creatorName").value("张三"))
|
||||
.andExpect(MockMvcResultMatchers.jsonPath("$.creatorPhone").value("13800138000"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAudioUploadWithInvalidFile() throws Exception {
|
||||
MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build();
|
||||
|
||||
// 创建模拟非音频文件
|
||||
MockMultipartFile file = new MockMultipartFile(
|
||||
"file",
|
||||
"test.txt",
|
||||
"text/plain",
|
||||
"fake text content".getBytes()
|
||||
);
|
||||
|
||||
// 执行上传请求,应该返回400错误
|
||||
mockMvc.perform(MockMvcRequestBuilders.multipart("/api/tts/upload-audio")
|
||||
.file(file)
|
||||
.param("audioName", "测试文件")
|
||||
.param("creatorName", "张三")
|
||||
.param("creatorPhone", "13800138000")
|
||||
.contentType(MediaType.MULTIPART_FORM_DATA))
|
||||
.andExpect(MockMvcResultMatchers.status().isBadRequest());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAudioUploadWithEmptyFile() throws Exception {
|
||||
MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build();
|
||||
|
||||
// 创建空文件
|
||||
MockMultipartFile file = new MockMultipartFile(
|
||||
"file",
|
||||
"empty.mp3",
|
||||
"audio/mpeg",
|
||||
new byte[0]
|
||||
);
|
||||
|
||||
// 执行上传请求,应该返回400错误
|
||||
mockMvc.perform(MockMvcRequestBuilders.multipart("/api/tts/upload-audio")
|
||||
.file(file)
|
||||
.param("audioName", "空文件")
|
||||
.param("creatorName", "张三")
|
||||
.param("creatorPhone", "13800138000")
|
||||
.contentType(MediaType.MULTIPART_FORM_DATA))
|
||||
.andExpect(MockMvcResultMatchers.status().isBadRequest());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,196 @@
|
||||
package com.rj.controller;
|
||||
|
||||
import com.rj.service.IFaceDetectLogService;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.mock.web.MockMultipartFile;
|
||||
import org.springframework.test.context.ActiveProfiles;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
/**
|
||||
* 图像检测控制器模型参数测试
|
||||
*/
|
||||
@SpringBootTest
|
||||
@ActiveProfiles("test")
|
||||
public class FaceDetectControllerModelTest {
|
||||
|
||||
@Autowired
|
||||
private IFaceDetectLogService faceDetectLogService;
|
||||
|
||||
/**
|
||||
* 测试带模型参数的图像检测
|
||||
*/
|
||||
@Test
|
||||
public void testDetectFaceWithModel() {
|
||||
System.out.println("=== 测试带模型参数的图像检测 ===");
|
||||
|
||||
String imageUrl = "https://example.com/test-image.jpg";
|
||||
String ownerName = "张三";
|
||||
String ownerPhone = "13800138000";
|
||||
String avatarName = "测试头像";
|
||||
String model = "liveportrait-detect-v2";
|
||||
|
||||
try {
|
||||
// 调用图像检测服务
|
||||
com.rj.entity.FaceDetectLog detectLog = faceDetectLogService.detectFaceAndSave(imageUrl, ownerName, ownerPhone, avatarName, model);
|
||||
|
||||
System.out.println("图像检测结果:");
|
||||
System.out.println("- 请求ID: " + detectLog.getRequestId());
|
||||
System.out.println("- 模型: " + detectLog.getModel());
|
||||
System.out.println("- 图片URL: " + detectLog.getImageUrl());
|
||||
System.out.println("- 所属人: " + detectLog.getOwnerName());
|
||||
System.out.println("- 电话: " + detectLog.getOwnerPhone());
|
||||
System.out.println("- 头像名称: " + detectLog.getAvatarName());
|
||||
System.out.println("- 是否成功: " + detectLog.getSuccess());
|
||||
System.out.println("- 图像数量: " + detectLog.getFaceCount());
|
||||
|
||||
if (detectLog.getSuccess()) {
|
||||
System.out.println("✅ 图像检测成功");
|
||||
} else {
|
||||
System.out.println("❌ 图像检测失败: " + detectLog.getErrorMessage());
|
||||
}
|
||||
|
||||
} catch (Exception e) {
|
||||
System.out.println("❌ 测试异常: " + e.getMessage());
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 测试默认模型的图像检测
|
||||
*/
|
||||
@Test
|
||||
public void testDetectFaceWithDefaultModel() {
|
||||
System.out.println("=== 测试默认模型的图像检测 ===");
|
||||
|
||||
String imageUrl = "https://example.com/test-image.jpg";
|
||||
String ownerName = "李四";
|
||||
String ownerPhone = "13900139000";
|
||||
String avatarName = "默认头像";
|
||||
String model = null; // 使用默认模型
|
||||
|
||||
try {
|
||||
// 调用图像检测服务
|
||||
com.rj.entity.FaceDetectLog detectLog = faceDetectLogService.detectFaceAndSave(imageUrl, ownerName, ownerPhone, avatarName, model);
|
||||
|
||||
System.out.println("图像检测结果:");
|
||||
System.out.println("- 请求ID: " + detectLog.getRequestId());
|
||||
System.out.println("- 模型: " + detectLog.getModel());
|
||||
System.out.println("- 图片URL: " + detectLog.getImageUrl());
|
||||
System.out.println("- 所属人: " + detectLog.getOwnerName());
|
||||
System.out.println("- 电话: " + detectLog.getOwnerPhone());
|
||||
System.out.println("- 头像名称: " + detectLog.getAvatarName());
|
||||
System.out.println("- 是否成功: " + detectLog.getSuccess());
|
||||
System.out.println("- 图像数量: " + detectLog.getFaceCount());
|
||||
|
||||
// 验证默认模型
|
||||
if ("liveportrait-detect".equals(detectLog.getModel())) {
|
||||
System.out.println("✅ 默认模型设置正确");
|
||||
} else {
|
||||
System.out.println("❌ 默认模型设置错误: " + detectLog.getModel());
|
||||
}
|
||||
|
||||
} catch (Exception e) {
|
||||
System.out.println("❌ 测试异常: " + e.getMessage());
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 测试模型参数验证
|
||||
*/
|
||||
@Test
|
||||
public void testModelParameterValidation() {
|
||||
System.out.println("=== 测试模型参数验证 ===");
|
||||
|
||||
String imageUrl = "https://example.com/test-image.jpg";
|
||||
String ownerName = "王五";
|
||||
String ownerPhone = "13700137000";
|
||||
String avatarName = "验证头像";
|
||||
|
||||
// 测试不同的模型参数
|
||||
String[] testModels = {
|
||||
"liveportrait-detect",
|
||||
"liveportrait-detect-v2",
|
||||
"custom-model",
|
||||
"",
|
||||
null
|
||||
};
|
||||
|
||||
for (String model : testModels) {
|
||||
System.out.println("测试模型: " + (model == null ? "null" : "'" + model + "'"));
|
||||
|
||||
try {
|
||||
com.rj.entity.FaceDetectLog detectLog = faceDetectLogService.detectFaceAndSave(imageUrl, ownerName, ownerPhone, avatarName, model);
|
||||
|
||||
String actualModel = detectLog.getModel();
|
||||
System.out.println("实际使用的模型: " + actualModel);
|
||||
|
||||
// 验证模型设置逻辑
|
||||
if (model == null || model.trim().isEmpty()) {
|
||||
if ("liveportrait-detect".equals(actualModel)) {
|
||||
System.out.println("✅ 空模型参数使用默认值正确");
|
||||
} else {
|
||||
System.out.println("❌ 空模型参数使用默认值错误");
|
||||
}
|
||||
} else {
|
||||
if (model.equals(actualModel)) {
|
||||
System.out.println("✅ 自定义模型设置正确");
|
||||
} else {
|
||||
System.out.println("❌ 自定义模型设置错误");
|
||||
}
|
||||
}
|
||||
|
||||
} catch (Exception e) {
|
||||
System.out.println("❌ 测试异常: " + e.getMessage());
|
||||
}
|
||||
|
||||
System.out.println("---");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 测试控制器接口参数
|
||||
*/
|
||||
@Test
|
||||
public void testControllerParameters() {
|
||||
System.out.println("=== 测试控制器接口参数 ===");
|
||||
|
||||
// 创建模拟文件
|
||||
byte[] fileContent = "test image content".getBytes();
|
||||
MultipartFile mockFile = new MockMultipartFile(
|
||||
"file",
|
||||
"test-image.jpg",
|
||||
"image/jpeg",
|
||||
fileContent
|
||||
);
|
||||
|
||||
String userId = "user123";
|
||||
String ownerName = "赵六";
|
||||
String ownerPhone = "13600136000";
|
||||
String avatarName = "控制器测试头像";
|
||||
String model = "liveportrait-detect-v3";
|
||||
int expiresInSeconds = 3600;
|
||||
|
||||
System.out.println("测试参数:");
|
||||
System.out.println("- 文件名: " + mockFile.getOriginalFilename());
|
||||
System.out.println("- 文件大小: " + mockFile.getSize() + " bytes");
|
||||
System.out.println("- 用户ID: " + userId);
|
||||
System.out.println("- 所属人: " + ownerName);
|
||||
System.out.println("- 电话: " + ownerPhone);
|
||||
System.out.println("- 头像名称: " + avatarName);
|
||||
System.out.println("- 模型: " + model);
|
||||
System.out.println("- 有效期: " + expiresInSeconds + " 秒");
|
||||
|
||||
try {
|
||||
// 这里只是验证参数传递,实际调用需要完整的Spring环境
|
||||
System.out.println("✅ 控制器参数设置正确");
|
||||
System.out.println("注意: 实际测试需要完整的Spring Boot环境和MinIO配置");
|
||||
|
||||
} catch (Exception e) {
|
||||
System.out.println("❌ 测试异常: " + e.getMessage());
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
144
src/test/java/com/rj/service/FaceDetectBboxTest.java
Normal file
144
src/test/java/com/rj/service/FaceDetectBboxTest.java
Normal file
@@ -0,0 +1,144 @@
|
||||
package com.rj.service;
|
||||
|
||||
import com.rj.entity.FaceDetectLog;
|
||||
import com.rj.service.impl.FaceDetectLogServiceImpl;
|
||||
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 java.time.LocalDateTime;
|
||||
import java.util.UUID;
|
||||
|
||||
/**
|
||||
* 图像检测边界框字段测试
|
||||
*/
|
||||
@SpringBootTest
|
||||
@ActiveProfiles("test")
|
||||
public class FaceDetectBboxTest {
|
||||
|
||||
@Autowired
|
||||
private FaceDetectLogServiceImpl faceDetectLogService;
|
||||
|
||||
/**
|
||||
* 测试设置图像区域坐标
|
||||
*/
|
||||
@Test
|
||||
public void testSetFaceBbox() {
|
||||
System.out.println("=== 测试设置图像区域坐标 ===");
|
||||
|
||||
// 创建测试数据
|
||||
FaceDetectLog faceDetectLog = new FaceDetectLog();
|
||||
faceDetectLog.setId(UUID.randomUUID().toString());
|
||||
faceDetectLog.setRequestId(UUID.randomUUID().toString());
|
||||
faceDetectLog.setModel("liveportrait-detect");
|
||||
faceDetectLog.setImageUrl("https://example.com/test-image.jpg");
|
||||
faceDetectLog.setOwnerName("张三");
|
||||
faceDetectLog.setOwnerPhone("13800138000");
|
||||
faceDetectLog.setAvatarName("测试头像");
|
||||
faceDetectLog.setRequestTime(LocalDateTime.now());
|
||||
faceDetectLog.setResponseTime(LocalDateTime.now());
|
||||
faceDetectLog.setStatusCode(200);
|
||||
faceDetectLog.setSuccess(true);
|
||||
faceDetectLog.setFaceCount(1);
|
||||
|
||||
// 设置图像区域坐标 [x1, y1, x2, y2]
|
||||
String faceBbox = "[302,286,610,593]";
|
||||
faceDetectLog.setFaceBbox(faceBbox);
|
||||
|
||||
// 设置动态区域坐标 [x1, y1, x2, y2]
|
||||
String extBbox = "[71,9,840,778]";
|
||||
faceDetectLog.setExtBbox(extBbox);
|
||||
|
||||
// 保存到数据库
|
||||
boolean saved = faceDetectLogService.saveFaceDetectLog(faceDetectLog);
|
||||
|
||||
if (saved) {
|
||||
System.out.println("✅ 图像检测日志保存成功");
|
||||
System.out.println("图像区域坐标: " + faceDetectLog.getFaceBbox());
|
||||
System.out.println("动态区域坐标: " + faceDetectLog.getExtBbox());
|
||||
} else {
|
||||
System.out.println("❌ 图像检测日志保存失败");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 测试解析JSON格式的坐标数据
|
||||
*/
|
||||
@Test
|
||||
public void testParseBboxFromJson() {
|
||||
System.out.println("=== 测试解析JSON格式的坐标数据 ===");
|
||||
|
||||
// 模拟API响应中的坐标数据
|
||||
String faceBboxJson = "[302,286,610,593]";
|
||||
String extBboxJson = "[71,9,840,778]";
|
||||
|
||||
FaceDetectLog faceDetectLog = new FaceDetectLog();
|
||||
faceDetectLog.setId(UUID.randomUUID().toString());
|
||||
faceDetectLog.setRequestId(UUID.randomUUID().toString());
|
||||
faceDetectLog.setModel("liveportrait-detect");
|
||||
faceDetectLog.setImageUrl("https://example.com/test-image.jpg");
|
||||
faceDetectLog.setRequestTime(LocalDateTime.now());
|
||||
faceDetectLog.setResponseTime(LocalDateTime.now());
|
||||
faceDetectLog.setStatusCode(200);
|
||||
faceDetectLog.setSuccess(true);
|
||||
faceDetectLog.setFaceCount(1);
|
||||
|
||||
// 设置坐标数据
|
||||
faceDetectLog.setFaceBbox(faceBboxJson);
|
||||
faceDetectLog.setExtBbox(extBboxJson);
|
||||
|
||||
// 验证坐标数据
|
||||
System.out.println("图像区域坐标: " + faceDetectLog.getFaceBbox());
|
||||
System.out.println("动态区域坐标: " + faceDetectLog.getExtBbox());
|
||||
|
||||
// 验证坐标格式
|
||||
if (faceDetectLog.getFaceBbox() != null && faceDetectLog.getFaceBbox().startsWith("[") && faceDetectLog.getFaceBbox().endsWith("]")) {
|
||||
System.out.println("✅ 图像区域坐标格式正确");
|
||||
} else {
|
||||
System.out.println("❌ 图像区域坐标格式错误");
|
||||
}
|
||||
|
||||
if (faceDetectLog.getExtBbox() != null && faceDetectLog.getExtBbox().startsWith("[") && faceDetectLog.getExtBbox().endsWith("]")) {
|
||||
System.out.println("✅ 动态区域坐标格式正确");
|
||||
} else {
|
||||
System.out.println("❌ 动态区域坐标格式错误");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 测试坐标数据的边界情况
|
||||
*/
|
||||
@Test
|
||||
public void testBboxBoundaryCases() {
|
||||
System.out.println("=== 测试坐标数据的边界情况 ===");
|
||||
|
||||
FaceDetectLog faceDetectLog = new FaceDetectLog();
|
||||
faceDetectLog.setId(UUID.randomUUID().toString());
|
||||
faceDetectLog.setRequestId(UUID.randomUUID().toString());
|
||||
faceDetectLog.setModel("liveportrait-detect");
|
||||
faceDetectLog.setImageUrl("https://example.com/test-image.jpg");
|
||||
faceDetectLog.setRequestTime(LocalDateTime.now());
|
||||
faceDetectLog.setResponseTime(LocalDateTime.now());
|
||||
faceDetectLog.setStatusCode(200);
|
||||
faceDetectLog.setSuccess(true);
|
||||
faceDetectLog.setFaceCount(0);
|
||||
|
||||
// 测试空坐标
|
||||
faceDetectLog.setFaceBbox(null);
|
||||
faceDetectLog.setExtBbox(null);
|
||||
System.out.println("空坐标测试: faceBbox=" + faceDetectLog.getFaceBbox() + ", extBbox=" + faceDetectLog.getExtBbox());
|
||||
|
||||
// 测试空字符串
|
||||
faceDetectLog.setFaceBbox("");
|
||||
faceDetectLog.setExtBbox("");
|
||||
System.out.println("空字符串测试: faceBbox='" + faceDetectLog.getFaceBbox() + "', extBbox='" + faceDetectLog.getExtBbox() + "'");
|
||||
|
||||
// 测试有效坐标
|
||||
faceDetectLog.setFaceBbox("[0,0,100,100]");
|
||||
faceDetectLog.setExtBbox("[0,0,200,200]");
|
||||
System.out.println("有效坐标测试: faceBbox=" + faceDetectLog.getFaceBbox() + ", extBbox=" + faceDetectLog.getExtBbox());
|
||||
|
||||
System.out.println("✅ 坐标数据边界情况测试完成");
|
||||
}
|
||||
}
|
||||
234
src/test/java/com/rj/service/FaceDetectImageCountTest.java
Normal file
234
src/test/java/com/rj/service/FaceDetectImageCountTest.java
Normal file
@@ -0,0 +1,234 @@
|
||||
package com.rj.service;
|
||||
|
||||
import com.rj.entity.FaceDetectLog;
|
||||
import com.rj.service.impl.FaceDetectLogServiceImpl;
|
||||
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;
|
||||
|
||||
/**
|
||||
* 图像检测图片数量字段测试
|
||||
*/
|
||||
@SpringBootTest
|
||||
@ActiveProfiles("test")
|
||||
public class FaceDetectImageCountTest {
|
||||
|
||||
@Autowired
|
||||
private FaceDetectLogServiceImpl faceDetectLogService;
|
||||
|
||||
/**
|
||||
* 测试设置图片数量
|
||||
*/
|
||||
@Test
|
||||
public void testSetImageCount() {
|
||||
System.out.println("=== 测试设置图片数量 ===");
|
||||
|
||||
// 创建测试数据
|
||||
FaceDetectLog faceDetectLog = new FaceDetectLog();
|
||||
faceDetectLog.setId(java.util.UUID.randomUUID().toString());
|
||||
faceDetectLog.setRequestId(java.util.UUID.randomUUID().toString());
|
||||
faceDetectLog.setModel("liveportrait-detect");
|
||||
faceDetectLog.setImageUrl("https://example.com/test-image.jpg");
|
||||
faceDetectLog.setOwnerName("张三");
|
||||
faceDetectLog.setOwnerPhone("13800138000");
|
||||
faceDetectLog.setAvatarName("测试头像");
|
||||
faceDetectLog.setRequestTime(java.time.LocalDateTime.now());
|
||||
faceDetectLog.setResponseTime(java.time.LocalDateTime.now());
|
||||
faceDetectLog.setStatusCode(200);
|
||||
faceDetectLog.setSuccess(true);
|
||||
faceDetectLog.setFaceCount(1);
|
||||
|
||||
// 设置图片数量
|
||||
Integer imageCount = 1;
|
||||
|
||||
// 保存到数据库
|
||||
boolean saved = faceDetectLogService.saveFaceDetectLog(faceDetectLog);
|
||||
|
||||
if (saved) {
|
||||
System.out.println("✅ 图像检测日志保存成功");
|
||||
} else {
|
||||
System.out.println("❌ 图像检测日志保存失败");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 测试解析JSON中的image_count
|
||||
*/
|
||||
@Test
|
||||
public void testParseImageCountFromJson() {
|
||||
System.out.println("=== 测试解析JSON中的image_count ===");
|
||||
|
||||
// 模拟API响应JSON
|
||||
String jsonResponse = """
|
||||
{
|
||||
"output": {
|
||||
"message": "",
|
||||
"pass": true
|
||||
},
|
||||
"usage": {
|
||||
"image_count": 1
|
||||
},
|
||||
"request_id": "a4529f04-2fbb-4818-8e6c-3627945c95fc"
|
||||
}
|
||||
""";
|
||||
|
||||
System.out.println("模拟API响应: " + jsonResponse);
|
||||
|
||||
// 验证JSON解析逻辑
|
||||
try {
|
||||
com.fasterxml.jackson.databind.ObjectMapper objectMapper = new com.fasterxml.jackson.databind.ObjectMapper();
|
||||
com.fasterxml.jackson.databind.JsonNode jsonNode = objectMapper.readTree(jsonResponse);
|
||||
|
||||
if (jsonNode.has("usage")) {
|
||||
com.fasterxml.jackson.databind.JsonNode usage = jsonNode.get("usage");
|
||||
if (usage.has("image_count")) {
|
||||
int imageCount = usage.get("image_count").asInt();
|
||||
System.out.println("✅ 成功解析image_count: " + imageCount);
|
||||
|
||||
// 验证值是否正确
|
||||
if (imageCount == 1) {
|
||||
System.out.println("✅ image_count值正确");
|
||||
} else {
|
||||
System.out.println("❌ image_count值错误: " + imageCount);
|
||||
}
|
||||
} else {
|
||||
System.out.println("❌ usage中没有image_count字段");
|
||||
}
|
||||
} else {
|
||||
System.out.println("❌ JSON中没有usage字段");
|
||||
}
|
||||
|
||||
} catch (Exception e) {
|
||||
System.out.println("❌ JSON解析异常: " + e.getMessage());
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 测试不同image_count值
|
||||
*/
|
||||
@Test
|
||||
public void testDifferentImageCountValues() {
|
||||
System.out.println("=== 测试不同image_count值 ===");
|
||||
|
||||
Integer[] testValues = {1, 2, 3, 0, null};
|
||||
|
||||
for (Integer imageCount : testValues) {
|
||||
System.out.println("测试image_count值: " + imageCount);
|
||||
|
||||
FaceDetectLog faceDetectLog = new FaceDetectLog();
|
||||
faceDetectLog.setId(java.util.UUID.randomUUID().toString());
|
||||
faceDetectLog.setRequestId(java.util.UUID.randomUUID().toString());
|
||||
faceDetectLog.setModel("liveportrait-detect");
|
||||
faceDetectLog.setImageUrl("https://example.com/test-image.jpg");
|
||||
faceDetectLog.setRequestTime(java.time.LocalDateTime.now());
|
||||
faceDetectLog.setResponseTime(java.time.LocalDateTime.now());
|
||||
faceDetectLog.setStatusCode(200);
|
||||
faceDetectLog.setSuccess(true);
|
||||
faceDetectLog.setFaceCount(1);
|
||||
|
||||
|
||||
System.out.println("---");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 测试完整的API响应解析流程
|
||||
*/
|
||||
@Test
|
||||
public void testCompleteApiResponseParsing() {
|
||||
System.out.println("=== 测试完整的API响应解析流程 ===");
|
||||
|
||||
// 模拟完整的API响应
|
||||
String jsonResponse = """
|
||||
{
|
||||
"output": {
|
||||
"message": "检测成功",
|
||||
"pass": true
|
||||
},
|
||||
"usage": {
|
||||
"image_count": 1
|
||||
},
|
||||
"request_id": "a4529f04-2fbb-4818-8e6c-3627945c95fc"
|
||||
}
|
||||
""";
|
||||
|
||||
System.out.println("完整API响应: " + jsonResponse);
|
||||
|
||||
try {
|
||||
com.fasterxml.jackson.databind.ObjectMapper objectMapper = new com.fasterxml.jackson.databind.ObjectMapper();
|
||||
com.fasterxml.jackson.databind.JsonNode jsonNode = objectMapper.readTree(jsonResponse);
|
||||
|
||||
// 模拟解析逻辑
|
||||
FaceDetectLog faceDetectLog = new FaceDetectLog();
|
||||
|
||||
// 解析output
|
||||
if (jsonNode.has("output")) {
|
||||
com.fasterxml.jackson.databind.JsonNode output = jsonNode.get("output");
|
||||
|
||||
if (output.has("pass")) {
|
||||
boolean pass = output.get("pass").asBoolean();
|
||||
int faceCount = pass ? 1 : 0;
|
||||
faceDetectLog.setFaceCount(faceCount);
|
||||
System.out.println("✅ 解析pass字段: " + pass + ", 图像数量: " + faceCount);
|
||||
}
|
||||
|
||||
if (output.has("message")) {
|
||||
String message = output.get("message").asText();
|
||||
System.out.println("✅ 解析message字段: " + message);
|
||||
}
|
||||
}
|
||||
|
||||
// 解析usage
|
||||
if (jsonNode.has("usage")) {
|
||||
com.fasterxml.jackson.databind.JsonNode usage = jsonNode.get("usage");
|
||||
|
||||
if (usage.has("image_count")) {
|
||||
int imageCount = usage.get("image_count").asInt();
|
||||
System.out.println("✅ 解析image_count字段: " + imageCount);
|
||||
}
|
||||
}
|
||||
|
||||
// 解析request_id
|
||||
if (jsonNode.has("request_id")) {
|
||||
String requestId = jsonNode.get("request_id").asText();
|
||||
System.out.println("✅ 解析request_id字段: " + requestId);
|
||||
}
|
||||
|
||||
System.out.println("✅ 完整API响应解析成功");
|
||||
System.out.println("最终结果:");
|
||||
System.out.println("- 图像数量: " + faceDetectLog.getFaceCount());
|
||||
|
||||
} catch (Exception e) {
|
||||
System.out.println("❌ 解析异常: " + e.getMessage());
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -158,6 +158,13 @@ public class TtsRequestLogShortUrlTest {
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
153
src/test/java/com/rj/service/VideoSynthesisTempUrlTest.java
Normal file
153
src/test/java/com/rj/service/VideoSynthesisTempUrlTest.java
Normal file
@@ -0,0 +1,153 @@
|
||||
package com.rj.service;
|
||||
|
||||
import com.rj.dto.VideoSynthesisRequestDto;
|
||||
import com.rj.entity.VideoSynthesisLog;
|
||||
import com.rj.service.IVideoSynthesisService;
|
||||
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 static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
/**
|
||||
* 视频合成临时URL功能测试类
|
||||
*
|
||||
* @author rj
|
||||
* @date 2025-01-30
|
||||
*/
|
||||
@SpringBootTest
|
||||
@ActiveProfiles("test")
|
||||
public class VideoSynthesisTempUrlTest {
|
||||
|
||||
@Autowired
|
||||
private IVideoSynthesisService videoSynthesisService;
|
||||
|
||||
/**
|
||||
* 测试临时URL生成
|
||||
*/
|
||||
@Test
|
||||
public void testTempUrlGeneration() {
|
||||
VideoSynthesisRequestDto request = new VideoSynthesisRequestDto();
|
||||
request.setVideoName("测试视频");
|
||||
request.setImageUrl("http://test.com/image.jpg");
|
||||
request.setAudioUrl("http://test.com/audio.mp3");
|
||||
request.setModel("liveportrait");
|
||||
|
||||
VideoSynthesisLog result = videoSynthesisService.synthesizeVideoAndSave(request);
|
||||
|
||||
assertNotNull(result);
|
||||
assertNotNull(result.getVideoTempUrl());
|
||||
assertTrue(result.getVideoTempUrl().contains("video/temp/"));
|
||||
assertTrue(result.getVideoTempUrl().contains("name="));
|
||||
System.out.println("临时URL生成测试通过: " + result.getVideoTempUrl());
|
||||
}
|
||||
|
||||
/**
|
||||
* 测试临时URL格式
|
||||
*/
|
||||
@Test
|
||||
public void testTempUrlFormat() {
|
||||
VideoSynthesisRequestDto request = new VideoSynthesisRequestDto();
|
||||
request.setVideoName("我的产品视频");
|
||||
request.setImageUrl("http://test.com/image.jpg");
|
||||
request.setAudioUrl("http://test.com/audio.mp3");
|
||||
request.setModel("liveportrait");
|
||||
|
||||
VideoSynthesisLog result = videoSynthesisService.synthesizeVideoAndSave(request);
|
||||
|
||||
assertNotNull(result);
|
||||
String tempUrl = result.getVideoTempUrl();
|
||||
|
||||
// 验证URL格式
|
||||
assertTrue(tempUrl.startsWith("https://"));
|
||||
assertTrue(tempUrl.contains("/video/temp/"));
|
||||
assertTrue(tempUrl.contains("?name="));
|
||||
|
||||
// 验证请求ID在URL中
|
||||
assertTrue(tempUrl.contains(result.getRequestId()));
|
||||
|
||||
// 验证视频名称被编码
|
||||
assertTrue(tempUrl.contains("name="));
|
||||
|
||||
System.out.println("临时URL格式测试通过: " + tempUrl);
|
||||
}
|
||||
|
||||
/**
|
||||
* 测试中文视频名称的URL编码
|
||||
*/
|
||||
@Test
|
||||
public void testChineseVideoNameEncoding() {
|
||||
VideoSynthesisRequestDto request = new VideoSynthesisRequestDto();
|
||||
request.setVideoName("我的中文视频名称");
|
||||
request.setImageUrl("http://test.com/image.jpg");
|
||||
request.setAudioUrl("http://test.com/audio.mp3");
|
||||
request.setModel("liveportrait");
|
||||
|
||||
VideoSynthesisLog result = videoSynthesisService.synthesizeVideoAndSave(request);
|
||||
|
||||
assertNotNull(result);
|
||||
String tempUrl = result.getVideoTempUrl();
|
||||
|
||||
// 验证中文名称被正确编码
|
||||
assertTrue(tempUrl.contains("name="));
|
||||
assertFalse(tempUrl.contains("我的中文视频名称")); // 应该被编码
|
||||
assertTrue(tempUrl.contains("%")); // 应该包含编码字符
|
||||
|
||||
System.out.println("中文视频名称编码测试通过: " + tempUrl);
|
||||
}
|
||||
|
||||
/**
|
||||
* 测试特殊字符视频名称的URL编码
|
||||
*/
|
||||
@Test
|
||||
public void testSpecialCharacterVideoNameEncoding() {
|
||||
VideoSynthesisRequestDto request = new VideoSynthesisRequestDto();
|
||||
request.setVideoName("Video with Spaces & Special Characters!@#$%");
|
||||
request.setImageUrl("http://test.com/image.jpg");
|
||||
request.setAudioUrl("http://test.com/audio.mp3");
|
||||
request.setModel("liveportrait");
|
||||
|
||||
VideoSynthesisLog result = videoSynthesisService.synthesizeVideoAndSave(request);
|
||||
|
||||
assertNotNull(result);
|
||||
String tempUrl = result.getVideoTempUrl();
|
||||
|
||||
// 验证特殊字符被正确编码
|
||||
assertTrue(tempUrl.contains("name="));
|
||||
assertFalse(tempUrl.contains(" ")); // 空格应该被编码
|
||||
assertFalse(tempUrl.contains("&")); // &符号应该被编码
|
||||
assertFalse(tempUrl.contains("!")); // 感叹号应该被编码
|
||||
|
||||
System.out.println("特殊字符视频名称编码测试通过: " + tempUrl);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
128
src/test/java/com/rj/service/VideoSynthesisVideoNameTest.java
Normal file
128
src/test/java/com/rj/service/VideoSynthesisVideoNameTest.java
Normal file
@@ -0,0 +1,128 @@
|
||||
package com.rj.service;
|
||||
|
||||
import com.rj.dto.VideoSynthesisRequestDto;
|
||||
import com.rj.entity.VideoSynthesisLog;
|
||||
import com.rj.service.IVideoSynthesisService;
|
||||
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 static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
/**
|
||||
* 视频合成视频名称功能测试类
|
||||
*
|
||||
* @author rj
|
||||
* @date 2025-01-30
|
||||
*/
|
||||
@SpringBootTest
|
||||
@ActiveProfiles("test")
|
||||
public class VideoSynthesisVideoNameTest {
|
||||
|
||||
@Autowired
|
||||
private IVideoSynthesisService videoSynthesisService;
|
||||
|
||||
/**
|
||||
* 测试设置自定义视频名称
|
||||
*/
|
||||
@Test
|
||||
public void testCustomVideoName() {
|
||||
VideoSynthesisRequestDto request = new VideoSynthesisRequestDto();
|
||||
request.setVideoName("我的测试视频");
|
||||
request.setImageUrl("http://test.com/image.jpg");
|
||||
request.setAudioUrl("http://test.com/audio.mp3");
|
||||
request.setModel("liveportrait");
|
||||
|
||||
VideoSynthesisLog result = videoSynthesisService.synthesizeVideoAndSave(request);
|
||||
|
||||
assertNotNull(result);
|
||||
assertEquals("我的测试视频", result.getVideoName());
|
||||
System.out.println("自定义视频名称测试通过: " + result.getVideoName());
|
||||
}
|
||||
|
||||
/**
|
||||
* 测试空视频名称时使用默认名称
|
||||
*/
|
||||
@Test
|
||||
public void testEmptyVideoName() {
|
||||
VideoSynthesisRequestDto request = new VideoSynthesisRequestDto();
|
||||
request.setVideoName(""); // 空字符串
|
||||
request.setImageUrl("http://test.com/image.jpg");
|
||||
request.setAudioUrl("http://test.com/audio.mp3");
|
||||
request.setModel("liveportrait");
|
||||
|
||||
VideoSynthesisLog result = videoSynthesisService.synthesizeVideoAndSave(request);
|
||||
|
||||
assertNotNull(result);
|
||||
assertNotNull(result.getVideoName());
|
||||
assertTrue(result.getVideoName().startsWith("视频合成_"));
|
||||
System.out.println("默认视频名称测试通过: " + result.getVideoName());
|
||||
}
|
||||
|
||||
/**
|
||||
* 测试null视频名称时使用默认名称
|
||||
*/
|
||||
@Test
|
||||
public void testNullVideoName() {
|
||||
VideoSynthesisRequestDto request = new VideoSynthesisRequestDto();
|
||||
request.setVideoName(null); // null值
|
||||
request.setImageUrl("http://test.com/image.jpg");
|
||||
request.setAudioUrl("http://test.com/audio.mp3");
|
||||
request.setModel("liveportrait");
|
||||
|
||||
VideoSynthesisLog result = videoSynthesisService.synthesizeVideoAndSave(request);
|
||||
|
||||
assertNotNull(result);
|
||||
assertNotNull(result.getVideoName());
|
||||
assertTrue(result.getVideoName().startsWith("视频合成_"));
|
||||
System.out.println("null视频名称测试通过: " + result.getVideoName());
|
||||
}
|
||||
|
||||
/**
|
||||
* 测试视频名称长度
|
||||
*/
|
||||
@Test
|
||||
public void testVideoNameLength() {
|
||||
VideoSynthesisRequestDto request = new VideoSynthesisRequestDto();
|
||||
request.setVideoName("这是一个很长的视频名称,用来测试视频名称字段的长度限制和存储功能");
|
||||
request.setImageUrl("http://test.com/image.jpg");
|
||||
request.setAudioUrl("http://test.com/audio.mp3");
|
||||
request.setModel("liveportrait");
|
||||
|
||||
VideoSynthesisLog result = videoSynthesisService.synthesizeVideoAndSave(request);
|
||||
|
||||
assertNotNull(result);
|
||||
assertEquals("这是一个很长的视频名称,用来测试视频名称字段的长度限制和存储功能", result.getVideoName());
|
||||
System.out.println("长视频名称测试通过: " + result.getVideoName());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user