2026-04-09 15:36:11 +08:00
|
|
|
package com.label.integration;
|
|
|
|
|
|
|
|
|
|
import com.label.AbstractIntegrationTest;
|
2026-04-14 13:28:10 +08:00
|
|
|
import com.label.dto.LoginRequest;
|
2026-04-09 15:36:11 +08:00
|
|
|
import org.junit.jupiter.api.BeforeEach;
|
|
|
|
|
import org.junit.jupiter.api.DisplayName;
|
|
|
|
|
import org.junit.jupiter.api.Test;
|
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
|
import org.springframework.boot.test.web.client.TestRestTemplate;
|
|
|
|
|
import org.springframework.http.*;
|
|
|
|
|
|
|
|
|
|
import java.util.Map;
|
|
|
|
|
|
|
|
|
|
import static org.assertj.core.api.Assertions.assertThat;
|
|
|
|
|
|
|
|
|
|
/**
|
2026-04-14 13:28:10 +08:00
|
|
|
* 鎻愬彇闃舵瀹℃壒闆嗘垚娴嬭瘯锛圲S4锛夈€? *
|
|
|
|
|
* 娴嬭瘯鍦烘櫙锛? * 1. 瀹℃壒閫氳繃 鈫?QA_GENERATION 浠诲姟鑷姩鍒涘缓锛宻ource_data 鐘舵€佹洿鏂颁负 QA_REVIEW
|
|
|
|
|
* 2. 瀹℃壒浜轰笌鎻愪氦浜虹浉鍚岋紙鑷锛夆啋 403 SELF_REVIEW_FORBIDDEN
|
|
|
|
|
* 3. 椹冲洖鍚庢爣娉ㄥ憳鍙噸棰嗕换鍔″苟鍐嶆鎻愪氦
|
2026-04-09 15:36:11 +08:00
|
|
|
*/
|
|
|
|
|
public class ExtractionApprovalIntegrationTest extends AbstractIntegrationTest {
|
|
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
|
private TestRestTemplate restTemplate;
|
|
|
|
|
|
|
|
|
|
private Long sourceId;
|
|
|
|
|
private Long taskId;
|
|
|
|
|
private Long annotatorUserId;
|
|
|
|
|
private Long reviewerUserId;
|
|
|
|
|
|
|
|
|
|
@BeforeEach
|
|
|
|
|
void setup() {
|
2026-04-14 13:28:10 +08:00
|
|
|
// 鑾峰彇绉嶅瓙鐢ㄦ埛 ID锛坕nit.sql 涓凡鎻掑叆锛? annotatorUserId = jdbcTemplate.queryForObject(
|
2026-04-09 15:36:11 +08:00
|
|
|
"SELECT id FROM sys_user WHERE username = 'annotator01'", Long.class);
|
|
|
|
|
reviewerUserId = jdbcTemplate.queryForObject(
|
|
|
|
|
"SELECT id FROM sys_user WHERE username = 'reviewer01'", Long.class);
|
|
|
|
|
Long companyId = jdbcTemplate.queryForObject(
|
|
|
|
|
"SELECT id FROM sys_company WHERE company_code = 'DEMO'", Long.class);
|
|
|
|
|
|
2026-04-14 13:28:10 +08:00
|
|
|
// 鎻掑叆娴嬭瘯 source_data
|
2026-04-09 15:36:11 +08:00
|
|
|
jdbcTemplate.execute(
|
|
|
|
|
"INSERT INTO source_data (company_id, uploader_id, data_type, file_path, " +
|
|
|
|
|
"file_name, file_size, bucket_name, status) " +
|
|
|
|
|
"VALUES (" + companyId + ", " + annotatorUserId + ", 'TEXT', " +
|
|
|
|
|
"'test/approval-test/file.txt', 'file.txt', 100, 'test-bucket', 'PENDING')");
|
|
|
|
|
sourceId = jdbcTemplate.queryForObject(
|
|
|
|
|
"SELECT id FROM source_data ORDER BY id DESC LIMIT 1", Long.class);
|
|
|
|
|
|
2026-04-14 13:28:10 +08:00
|
|
|
// 鎻掑叆 UNCLAIMED EXTRACTION 浠诲姟
|
2026-04-09 15:36:11 +08:00
|
|
|
jdbcTemplate.execute(
|
|
|
|
|
"INSERT INTO annotation_task (company_id, source_id, task_type, status) " +
|
|
|
|
|
"VALUES (" + companyId + ", " + sourceId + ", 'EXTRACTION', 'UNCLAIMED')");
|
|
|
|
|
taskId = jdbcTemplate.queryForObject(
|
|
|
|
|
"SELECT id FROM annotation_task ORDER BY id DESC LIMIT 1", Long.class);
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-14 13:28:10 +08:00
|
|
|
// ------------------------------------------------------------------ 娴嬭瘯 1: 瀹℃壒閫氳繃 鈫?QA 浠诲姟鑷姩鍒涘缓 --
|
2026-04-09 15:36:11 +08:00
|
|
|
|
|
|
|
|
@Test
|
2026-04-14 13:28:10 +08:00
|
|
|
@DisplayName("瀹℃壒閫氳繃鍚庯紝QA_GENERATION 浠诲姟鑷姩鍒涘缓锛宻ource_data 鐘舵€佸彉涓?QA_REVIEW")
|
2026-04-09 15:36:11 +08:00
|
|
|
void approveTask_thenQaTaskAndSourceStatusUpdated() {
|
|
|
|
|
String annotatorToken = loginAndGetToken("DEMO", "annotator01", "annot123");
|
|
|
|
|
String reviewerToken = loginAndGetToken("DEMO", "reviewer01", "review123");
|
|
|
|
|
|
2026-04-14 13:28:10 +08:00
|
|
|
// 1. 鏍囨敞鍛橀鍙栦换鍔? ResponseEntity<Map> claimResp = restTemplate.exchange(
|
2026-04-09 15:36:11 +08:00
|
|
|
baseUrl("/api/tasks/" + taskId + "/claim"),
|
|
|
|
|
HttpMethod.POST, bearerRequest(annotatorToken), Map.class);
|
|
|
|
|
assertThat(claimResp.getStatusCode()).isEqualTo(HttpStatus.OK);
|
|
|
|
|
|
2026-04-14 13:28:10 +08:00
|
|
|
// 2. 鏍囨敞鍛樻彁浜ゆ爣娉? ResponseEntity<Map> submitResp = restTemplate.exchange(
|
2026-04-09 15:36:11 +08:00
|
|
|
baseUrl("/api/extraction/" + taskId + "/submit"),
|
|
|
|
|
HttpMethod.POST, bearerRequest(annotatorToken), Map.class);
|
|
|
|
|
assertThat(submitResp.getStatusCode()).isEqualTo(HttpStatus.OK);
|
|
|
|
|
|
2026-04-14 13:28:10 +08:00
|
|
|
// 3. 瀹℃牳鍛樺鎵归€氳繃
|
|
|
|
|
// 娉細ExtractionApprovedEventListener(@TransactionalEventListener AFTER_COMMIT)
|
|
|
|
|
// 鍦ㄥ悓涓€绾跨▼涓悓姝ユ墽琛岋紝HTTP 鍝嶅簲杩斿洖鍓嶅凡瀹屾垚鍚庣画澶勭悊
|
2026-04-09 15:36:11 +08:00
|
|
|
ResponseEntity<Map> approveResp = restTemplate.exchange(
|
|
|
|
|
baseUrl("/api/extraction/" + taskId + "/approve"),
|
|
|
|
|
HttpMethod.POST, bearerRequest(reviewerToken), Map.class);
|
|
|
|
|
assertThat(approveResp.getStatusCode()).isEqualTo(HttpStatus.OK);
|
|
|
|
|
|
2026-04-14 13:28:10 +08:00
|
|
|
// 楠岃瘉锛氬師浠诲姟鐘舵€佸彉涓?APPROVED锛宨s_final=true
|
2026-04-09 15:36:11 +08:00
|
|
|
Map<String, Object> taskRow = jdbcTemplate.queryForMap(
|
|
|
|
|
"SELECT status, is_final FROM annotation_task WHERE id = ?", taskId);
|
|
|
|
|
assertThat(taskRow.get("status")).isEqualTo("APPROVED");
|
|
|
|
|
assertThat(taskRow.get("is_final")).isEqualTo(Boolean.TRUE);
|
|
|
|
|
|
2026-04-14 13:28:10 +08:00
|
|
|
// 楠岃瘉锛歈A_GENERATION 浠诲姟宸茶嚜鍔ㄥ垱寤猴紙UNCLAIMED 鐘舵€侊級
|
2026-04-09 15:36:11 +08:00
|
|
|
Integer qaTaskCount = jdbcTemplate.queryForObject(
|
|
|
|
|
"SELECT COUNT(*) FROM annotation_task " +
|
|
|
|
|
"WHERE source_id = ? AND task_type = 'QA_GENERATION' AND status = 'UNCLAIMED'",
|
|
|
|
|
Integer.class, sourceId);
|
2026-04-14 13:28:10 +08:00
|
|
|
assertThat(qaTaskCount).as("QA_GENERATION 浠诲姟搴斿凡鍒涘缓").isEqualTo(1);
|
2026-04-09 15:36:11 +08:00
|
|
|
|
2026-04-14 13:28:10 +08:00
|
|
|
// 楠岃瘉锛歴ource_data 鐘舵€佸凡鏇存柊涓?QA_REVIEW
|
2026-04-09 15:36:11 +08:00
|
|
|
String sourceStatus = jdbcTemplate.queryForObject(
|
|
|
|
|
"SELECT status FROM source_data WHERE id = ?", String.class, sourceId);
|
2026-04-14 13:28:10 +08:00
|
|
|
assertThat(sourceStatus).as("source_data 鐘舵€佸簲涓?QA_REVIEW").isEqualTo("QA_REVIEW");
|
2026-04-09 15:36:11 +08:00
|
|
|
|
2026-04-14 13:28:10 +08:00
|
|
|
// 楠岃瘉锛歵raining_dataset 宸蹭互 PENDING_REVIEW 鐘舵€佸垱寤? Integer datasetCount = jdbcTemplate.queryForObject(
|
2026-04-09 15:36:11 +08:00
|
|
|
"SELECT COUNT(*) FROM training_dataset " +
|
|
|
|
|
"WHERE source_id = ? AND status = 'PENDING_REVIEW'",
|
|
|
|
|
Integer.class, sourceId);
|
2026-04-14 13:28:10 +08:00
|
|
|
assertThat(datasetCount).as("training_dataset 搴斿凡鍒涘缓").isEqualTo(1);
|
2026-04-09 15:36:11 +08:00
|
|
|
}
|
|
|
|
|
|
2026-04-14 13:28:10 +08:00
|
|
|
// ------------------------------------------------------------------ 娴嬭瘯 2: 鑷杩斿洖 403 --
|
2026-04-09 15:36:11 +08:00
|
|
|
|
|
|
|
|
@Test
|
2026-04-14 13:28:10 +08:00
|
|
|
@DisplayName("瀹℃壒浜轰笌浠诲姟棰嗗彇浜虹浉鍚岋紙鑷锛夆啋 403 SELF_REVIEW_FORBIDDEN")
|
2026-04-09 15:36:11 +08:00
|
|
|
void approveOwnSubmission_returnsForbidden() {
|
2026-04-14 13:28:10 +08:00
|
|
|
// 鐩存帴灏嗕换鍔$疆涓?SUBMITTED 骞惰 claimed_by = reviewer01锛堟ā鎷熻嚜瀹″満鏅級
|
2026-04-09 15:36:11 +08:00
|
|
|
jdbcTemplate.execute(
|
|
|
|
|
"UPDATE annotation_task " +
|
|
|
|
|
"SET status = 'SUBMITTED', claimed_by = " + reviewerUserId +
|
|
|
|
|
", claimed_at = NOW(), submitted_at = NOW() " +
|
|
|
|
|
"WHERE id = " + taskId);
|
|
|
|
|
|
|
|
|
|
String reviewerToken = loginAndGetToken("DEMO", "reviewer01", "review123");
|
|
|
|
|
|
|
|
|
|
ResponseEntity<Map> resp = restTemplate.exchange(
|
|
|
|
|
baseUrl("/api/extraction/" + taskId + "/approve"),
|
|
|
|
|
HttpMethod.POST, bearerRequest(reviewerToken), Map.class);
|
|
|
|
|
|
|
|
|
|
assertThat(resp.getStatusCode()).isEqualTo(HttpStatus.FORBIDDEN);
|
|
|
|
|
|
2026-04-14 13:28:10 +08:00
|
|
|
// 楠岃瘉浠诲姟鐘舵€佹湭鍙? String status = jdbcTemplate.queryForObject(
|
2026-04-09 15:36:11 +08:00
|
|
|
"SELECT status FROM annotation_task WHERE id = ?", String.class, taskId);
|
|
|
|
|
assertThat(status).isEqualTo("SUBMITTED");
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-14 13:28:10 +08:00
|
|
|
// ------------------------------------------------------------------ 娴嬭瘯 3: 椹冲洖 鈫?閲嶉 鈫?鍐嶆彁浜?--
|
2026-04-09 15:36:11 +08:00
|
|
|
|
|
|
|
|
@Test
|
2026-04-14 13:28:10 +08:00
|
|
|
@DisplayName("椹冲洖鍚庢爣娉ㄥ憳鍙噸棰嗕换鍔″苟鍐嶆鎻愪氦锛屼换鍔$姸鎬佹仮澶嶄负 SUBMITTED")
|
2026-04-09 15:36:11 +08:00
|
|
|
void rejectThenReclaimAndResubmit_succeeds() {
|
|
|
|
|
String annotatorToken = loginAndGetToken("DEMO", "annotator01", "annot123");
|
|
|
|
|
String reviewerToken = loginAndGetToken("DEMO", "reviewer01", "review123");
|
|
|
|
|
|
2026-04-14 13:28:10 +08:00
|
|
|
// 1. 鏍囨敞鍛橀鍙栧苟鎻愪氦
|
2026-04-09 15:36:11 +08:00
|
|
|
restTemplate.exchange(baseUrl("/api/tasks/" + taskId + "/claim"),
|
|
|
|
|
HttpMethod.POST, bearerRequest(annotatorToken), Map.class);
|
|
|
|
|
restTemplate.exchange(baseUrl("/api/extraction/" + taskId + "/submit"),
|
|
|
|
|
HttpMethod.POST, bearerRequest(annotatorToken), Map.class);
|
|
|
|
|
|
2026-04-14 13:28:10 +08:00
|
|
|
// 2. 瀹℃牳鍛橀┏鍥烇紙椹冲洖鍘熷洜蹇呭~锛? HttpHeaders rejectHeaders = new HttpHeaders();
|
2026-04-09 15:36:11 +08:00
|
|
|
rejectHeaders.set("Authorization", "Bearer " + reviewerToken);
|
|
|
|
|
rejectHeaders.setContentType(MediaType.APPLICATION_JSON);
|
|
|
|
|
HttpEntity<Map<String, String>> rejectReq = new HttpEntity<>(
|
2026-04-14 13:28:10 +08:00
|
|
|
Map.of("reason", "瀹炰綋璇嗗埆鏈夎锛岃閲嶆柊鏍囨敞"), rejectHeaders);
|
2026-04-09 15:36:11 +08:00
|
|
|
|
|
|
|
|
ResponseEntity<Map> rejectResp = restTemplate.exchange(
|
|
|
|
|
baseUrl("/api/extraction/" + taskId + "/reject"),
|
|
|
|
|
HttpMethod.POST, rejectReq, Map.class);
|
|
|
|
|
assertThat(rejectResp.getStatusCode()).isEqualTo(HttpStatus.OK);
|
|
|
|
|
|
2026-04-14 13:28:10 +08:00
|
|
|
// 楠岃瘉锛氫换鍔$姸鎬佸彉涓?REJECTED
|
2026-04-09 15:36:11 +08:00
|
|
|
String statusAfterReject = jdbcTemplate.queryForObject(
|
|
|
|
|
"SELECT status FROM annotation_task WHERE id = ?", String.class, taskId);
|
|
|
|
|
assertThat(statusAfterReject).isEqualTo("REJECTED");
|
|
|
|
|
|
2026-04-14 13:28:10 +08:00
|
|
|
// 3. 鏍囨敞鍛橀噸棰嗕换鍔★紙REJECTED 鈫?IN_PROGRESS锛? ResponseEntity<Map> reclaimResp = restTemplate.exchange(
|
2026-04-09 15:36:11 +08:00
|
|
|
baseUrl("/api/tasks/" + taskId + "/reclaim"),
|
|
|
|
|
HttpMethod.POST, bearerRequest(annotatorToken), Map.class);
|
|
|
|
|
assertThat(reclaimResp.getStatusCode()).isEqualTo(HttpStatus.OK);
|
|
|
|
|
|
2026-04-14 13:28:10 +08:00
|
|
|
// 楠岃瘉锛氫换鍔$姸鎬佹仮澶嶄负 IN_PROGRESS
|
2026-04-09 15:36:11 +08:00
|
|
|
String statusAfterReclaim = jdbcTemplate.queryForObject(
|
|
|
|
|
"SELECT status FROM annotation_task WHERE id = ?", String.class, taskId);
|
|
|
|
|
assertThat(statusAfterReclaim).isEqualTo("IN_PROGRESS");
|
|
|
|
|
|
2026-04-14 13:28:10 +08:00
|
|
|
// 4. 鏍囨敞鍛樺啀娆℃彁浜わ紙IN_PROGRESS 鈫?SUBMITTED锛? ResponseEntity<Map> resubmitResp = restTemplate.exchange(
|
2026-04-09 15:36:11 +08:00
|
|
|
baseUrl("/api/extraction/" + taskId + "/submit"),
|
|
|
|
|
HttpMethod.POST, bearerRequest(annotatorToken), Map.class);
|
|
|
|
|
assertThat(resubmitResp.getStatusCode()).isEqualTo(HttpStatus.OK);
|
|
|
|
|
|
2026-04-14 13:28:10 +08:00
|
|
|
// 楠岃瘉锛氫换鍔$姸鎬佸彉涓?SUBMITTED
|
2026-04-09 15:36:11 +08:00
|
|
|
String finalStatus = jdbcTemplate.queryForObject(
|
|
|
|
|
"SELECT status FROM annotation_task WHERE id = ?", String.class, taskId);
|
|
|
|
|
assertThat(finalStatus).isEqualTo("SUBMITTED");
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-14 13:28:10 +08:00
|
|
|
// ------------------------------------------------------------------ 宸ュ叿鏂规硶 --
|
2026-04-09 15:36:11 +08:00
|
|
|
|
|
|
|
|
private String loginAndGetToken(String companyCode, String username, String password) {
|
|
|
|
|
LoginRequest req = new LoginRequest();
|
|
|
|
|
req.setCompanyCode(companyCode);
|
|
|
|
|
req.setUsername(username);
|
|
|
|
|
req.setPassword(password);
|
|
|
|
|
ResponseEntity<Map> response = restTemplate.postForEntity(
|
|
|
|
|
baseUrl("/api/auth/login"), req, Map.class);
|
|
|
|
|
if (!response.getStatusCode().is2xxSuccessful()) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
@SuppressWarnings("unchecked")
|
|
|
|
|
Map<String, Object> data = (Map<String, Object>) response.getBody().get("data");
|
|
|
|
|
return (String) data.get("token");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private HttpEntity<Void> bearerRequest(String token) {
|
|
|
|
|
HttpHeaders headers = new HttpHeaders();
|
|
|
|
|
headers.set("Authorization", "Bearer " + token);
|
|
|
|
|
return new HttpEntity<>(headers);
|
|
|
|
|
}
|
|
|
|
|
}
|