package com.rongyichuang.judge;
|
|
import com.fasterxml.jackson.databind.JsonNode;
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
import org.junit.jupiter.api.Test;
|
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.boot.test.context.SpringBootTest;
|
import org.springframework.boot.test.web.client.TestRestTemplate;
|
import org.springframework.boot.test.web.server.LocalServerPort;
|
import org.springframework.http.*;
|
import org.springframework.jdbc.core.JdbcTemplate;
|
import org.springframework.test.context.ActiveProfiles;
|
|
import java.io.File;
|
import java.nio.file.Files;
|
import java.nio.file.Path;
|
import java.nio.file.Paths;
|
import java.util.HashMap;
|
import java.util.List;
|
import java.util.Map;
|
|
import static org.junit.jupiter.api.Assertions.*;
|
|
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
|
@ActiveProfiles("test")
|
public class JudgeE2ECompleteTest {
|
|
@LocalServerPort
|
private int port;
|
|
@Autowired
|
private TestRestTemplate restTemplate;
|
|
@Autowired
|
private JdbcTemplate jdbcTemplate;
|
|
@Autowired
|
private ObjectMapper objectMapper;
|
|
private String getGraphQLUrl() {
|
return "http://localhost:" + port + "/graphql";
|
}
|
|
@Test
|
public void testCompleteJudgeCreationFlow() throws Exception {
|
System.out.println("=== 开始完整的评委创建流程测试 ===");
|
|
// 1. 首先上传头像图片(模拟前端上传)
|
System.out.println("步骤1: 上传头像图片");
|
Long mediaId = uploadAvatarImage();
|
assertNotNull(mediaId, "头像上传应该成功");
|
System.out.println("头像上传成功,Media ID: " + mediaId);
|
|
// 2. 创建评委信息
|
System.out.println("步骤2: 创建评委信息");
|
Long judgeId = createJudge(mediaId);
|
assertNotNull(judgeId, "评委创建应该成功");
|
System.out.println("评委创建成功,Judge ID: " + judgeId);
|
|
// 3. 验证数据库中的数据
|
System.out.println("步骤3: 验证数据库数据");
|
verifyDatabaseData(mediaId, judgeId);
|
|
System.out.println("=== 完整流程测试成功 ===");
|
}
|
|
private Long uploadAvatarImage() throws Exception {
|
// 检查UI目录下的logo图片
|
String[] possiblePaths = {
|
"../UI/logo.jpg",
|
"UI/logo.jpg",
|
"../../UI/logo.jpg"
|
};
|
|
String logoPath = null;
|
for (String path : possiblePaths) {
|
File file = new File(path);
|
if (file.exists()) {
|
logoPath = path;
|
break;
|
}
|
}
|
|
assertNotNull(logoPath, "应该找到logo图片文件");
|
System.out.println("找到logo图片: " + logoPath);
|
|
// 模拟文件上传到COS后的路径
|
String cosPath = "avatars/judge_avatar_" + System.currentTimeMillis() + ".jpg";
|
|
// 调用GraphQL保存媒体信息
|
String mutation = """
|
mutation SaveMedia($fileName: String!, $filePath: String!, $fileSize: Long!,
|
$fileType: String!, $storageType: String!, $bucketName: String!,
|
$region: String!, $targetType: Int!, $targetId: Long!) {
|
saveMedia(fileName: $fileName, filePath: $filePath, fileSize: $fileSize,
|
fileType: $fileType, storageType: $storageType, bucketName: $bucketName,
|
region: $region, targetType: $targetType, targetId: $targetId) {
|
id
|
name
|
path
|
targetType
|
targetId
|
}
|
}
|
""";
|
|
Map<String, Object> variables = new HashMap<>();
|
variables.put("fileName", "judge_avatar.jpg");
|
variables.put("filePath", cosPath);
|
variables.put("fileSize", 1024L);
|
variables.put("fileType", "image/jpeg");
|
variables.put("storageType", "COS");
|
variables.put("bucketName", "ryc-1256886520");
|
variables.put("region", "ap-chengdu");
|
variables.put("targetType", 1); // 1表示评委头像
|
variables.put("targetId", 0L); // 临时ID,后续会更新
|
|
JsonNode response = executeGraphQL(mutation, variables);
|
JsonNode mediaData = response.path("data").path("saveMedia");
|
|
return mediaData.path("id").asLong();
|
}
|
|
private Long createJudge(Long avatarMediaId) throws Exception {
|
String mutation = """
|
mutation SaveJudge($input: JudgeInput!) {
|
saveJudge(input: $input) {
|
id
|
name
|
title
|
company
|
introduction
|
avatarUrl
|
tags {
|
id
|
name
|
}
|
}
|
}
|
""";
|
|
Map<String, Object> judgeInput = new HashMap<>();
|
judgeInput.put("name", "张三");
|
judgeInput.put("title", "高级技术专家");
|
judgeInput.put("company", "阿里巴巴");
|
judgeInput.put("introduction", "拥有10年以上的技术开发经验,专注于云计算和大数据领域。");
|
judgeInput.put("avatarUrl", "https://ryc-1256886520.cos.ap-chengdu.myqcloud.com/avatars/judge_avatar.jpg");
|
judgeInput.put("tagNames", List.of("云计算", "大数据", "架构设计"));
|
|
Map<String, Object> variables = new HashMap<>();
|
variables.put("input", judgeInput);
|
|
JsonNode response = executeGraphQL(mutation, variables);
|
JsonNode judgeData = response.path("data").path("saveJudge");
|
|
Long judgeId = judgeData.path("id").asLong();
|
|
// 更新媒体记录的targetId
|
updateMediaTargetId(avatarMediaId, judgeId);
|
|
return judgeId;
|
}
|
|
private void updateMediaTargetId(Long mediaId, Long judgeId) {
|
String sql = "UPDATE t_media SET target_id = ? WHERE id = ?";
|
int updated = jdbcTemplate.update(sql, judgeId, mediaId);
|
assertEquals(1, updated, "应该更新一条媒体记录");
|
System.out.println("更新媒体记录的target_id: " + judgeId);
|
}
|
|
private void verifyDatabaseData(Long mediaId, Long judgeId) {
|
System.out.println("=== 验证数据库数据 ===");
|
|
// 验证t_media表数据
|
System.out.println("验证t_media表数据...");
|
String mediaSql = "SELECT id, name, path, target_type, target_id FROM t_media WHERE id = ?";
|
Map<String, Object> mediaRecord = jdbcTemplate.queryForMap(mediaSql, mediaId);
|
|
assertNotNull(mediaRecord, "应该找到媒体记录");
|
assertEquals("judge_avatar.jpg", mediaRecord.get("name"), "媒体文件名应该正确");
|
assertTrue(mediaRecord.get("path").toString().contains("avatars/"), "媒体路径应该包含avatars目录");
|
assertEquals(1, mediaRecord.get("target_type"), "target_type应该是1(评委头像)");
|
assertEquals(judgeId.longValue(), ((Number)mediaRecord.get("target_id")).longValue(), "target_id应该是评委ID");
|
|
System.out.println("✓ t_media表数据验证通过");
|
System.out.println(" - 文件名: " + mediaRecord.get("name"));
|
System.out.println(" - 路径: " + mediaRecord.get("path"));
|
System.out.println(" - 目标类型: " + mediaRecord.get("target_type"));
|
System.out.println(" - 目标ID: " + mediaRecord.get("target_id"));
|
|
// 验证t_judge表数据
|
System.out.println("验证t_judge表数据...");
|
String judgeSql = "SELECT id, name, title, company, introduction, avatar_url FROM t_judge WHERE id = ?";
|
Map<String, Object> judgeRecord = jdbcTemplate.queryForMap(judgeSql, judgeId);
|
|
assertNotNull(judgeRecord, "应该找到评委记录");
|
assertEquals("张三", judgeRecord.get("name"), "评委姓名应该正确");
|
assertEquals("高级技术专家", judgeRecord.get("title"), "评委职位应该正确");
|
assertEquals("阿里巴巴", judgeRecord.get("company"), "评委公司应该正确");
|
assertTrue(judgeRecord.get("introduction").toString().contains("10年以上"), "评委介绍应该正确");
|
assertTrue(judgeRecord.get("avatar_url").toString().contains("cos.ap-chengdu"), "头像URL应该正确");
|
|
System.out.println("✓ t_judge表数据验证通过");
|
System.out.println(" - 姓名: " + judgeRecord.get("name"));
|
System.out.println(" - 职位: " + judgeRecord.get("title"));
|
System.out.println(" - 公司: " + judgeRecord.get("company"));
|
System.out.println(" - 头像URL: " + judgeRecord.get("avatar_url"));
|
|
// 验证t_judge_tag表数据
|
System.out.println("验证t_judge_tag表数据...");
|
String tagSql = """
|
SELECT jt.judge_id, t.name as tag_name
|
FROM t_judge_tag jt
|
JOIN t_tag t ON jt.tag_id = t.id
|
WHERE jt.judge_id = ?
|
""";
|
List<Map<String, Object>> tagRecords = jdbcTemplate.queryForList(tagSql, judgeId);
|
|
assertFalse(tagRecords.isEmpty(), "应该找到评委标签记录");
|
assertEquals(3, tagRecords.size(), "应该有3个标签");
|
|
System.out.println("✓ t_judge_tag表数据验证通过");
|
for (Map<String, Object> tagRecord : tagRecords) {
|
System.out.println(" - 标签: " + tagRecord.get("tag_name"));
|
}
|
|
System.out.println("=== 所有数据库数据验证通过 ===");
|
}
|
|
private JsonNode executeGraphQL(String query, Map<String, Object> variables) throws Exception {
|
HttpHeaders headers = new HttpHeaders();
|
headers.setContentType(MediaType.APPLICATION_JSON);
|
|
Map<String, Object> requestBody = new HashMap<>();
|
requestBody.put("query", query);
|
if (variables != null) {
|
requestBody.put("variables", variables);
|
}
|
|
HttpEntity<String> request = new HttpEntity<>(
|
objectMapper.writeValueAsString(requestBody),
|
headers
|
);
|
|
ResponseEntity<String> response = restTemplate.postForEntity(
|
getGraphQLUrl(),
|
request,
|
String.class
|
);
|
|
assertEquals(HttpStatus.OK, response.getStatusCode(), "GraphQL请求应该成功");
|
|
JsonNode jsonResponse = objectMapper.readTree(response.getBody());
|
|
// 检查是否有错误
|
if (jsonResponse.has("errors")) {
|
System.err.println("GraphQL错误: " + jsonResponse.get("errors"));
|
fail("GraphQL请求返回错误: " + jsonResponse.get("errors"));
|
}
|
|
return jsonResponse;
|
}
|
|
@Test
|
public void testGraphQLEndpoint() throws Exception {
|
System.out.println("=== 测试GraphQL端点是否正常工作 ===");
|
|
String query = """
|
query {
|
judges {
|
id
|
name
|
title
|
company
|
}
|
}
|
""";
|
|
JsonNode response = executeGraphQL(query, null);
|
assertNotNull(response.path("data"), "应该有data字段");
|
|
System.out.println("✓ GraphQL端点工作正常");
|
}
|
}
|