lrj
2 天以前 c61d4fe27c97d2ecc907756aa571a4ef14a7b9b6
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
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端点工作正常");
    }
}