完善:同月多文件导入与当前用户离线迁移更新

This commit is contained in:
2026-11-04 11:35:24 +08:00
parent 147b56d0fd
commit f04d4e96e3
89 changed files with 6634 additions and 201 deletions
@@ -88,7 +88,9 @@ public class AnalysisApiSettingsService {
Files.createDirectories(target.getParent());
if (Files.isSymbolicLink(target.getParent()) || Files.isSymbolicLink(target)) throw new IOException();
temp = Files.createTempFile(target.getParent(), ".nao-model-", ".tmp");
Files.setPosixFilePermissions(temp, PosixFilePermissions.fromString("rw-------"));
if (Files.getFileStore(temp).supportsFileAttributeView("posix")) {
Files.setPosixFilePermissions(temp, PosixFilePermissions.fromString("rw-------"));
}
List<Map<String,Object>> storedProfiles = updated.stream().map(p -> Map.<String,Object>of(
"id",p.id(),"name",p.name(),"baseUrl",p.config().baseUrl(),"apiKey",p.config().apiKey(),"model",p.config().model(),"thinkingEnabled",p.config().thinkingEnabled(),"thinkingEffort",p.config().thinkingEffort())).toList();
Files.writeString(temp, json.writeValueAsString(Map.of("schemaVersion", 3, "baseUrl", next.baseUrl(),
@@ -19,8 +19,18 @@ import org.springframework.stereotype.Service;
}
static boolean overlaps(Coverage a,Coverage b) {
if(!Objects.equals(a.stage(),b.stage())||a.year()!=b.year()||a.month()!=b.month()||!SourceIdentity.intersects(a.scope(),b.scope()))return false;
return Objects.equals(SourceIdentity.canonicalSlot(a.slot()),SourceIdentity.canonicalSlot(b.slot()))
||!"SHARD_SNAPSHOT".equals(a.mode())||!"SHARD_SNAPSHOT".equals(b.mode())||Objects.equals(a.sha(),b.sha());
if(Objects.equals(SourceIdentity.canonicalSlot(a.slot()),SourceIdentity.canonicalSlot(b.slot()))||Objects.equals(a.sha(),b.sha()))return true;
if("SHARD_SNAPSHOT".equals(a.mode())&&"SHARD_SNAPSHOT".equals(b.mode()))return false;
// APPEND_SNAPSHOT is created only by the explicit upload workflow. Its scope must match
// exactly before it can augment a complete monthly file; intersecting declarations still conflict.
if(isUploadAppend(a.mode())||isUploadAppend(b.mode())){
boolean supportedPair=(isUploadAppend(a.mode())&&isUploadAppend(b.mode()))
||("MONTHLY_SNAPSHOT".equals(a.mode())&&isUploadAppend(b.mode()))
||("MONTHLY_SNAPSHOT".equals(b.mode())&&isUploadAppend(a.mode()));
return !supportedPair||!Objects.equals(SourceIdentity.scope(a.scope()),SourceIdentity.scope(b.scope()));
}
return true;
}
private static boolean isUploadAppend(String mode){return "APPEND_SNAPSHOT".equals(mode);}
record Coverage(String stage,int year,int month,String scope,String mode,String sha,String slot) {}
}
@@ -255,8 +255,7 @@ public class ImportWorker {
issues.add(new Issue(candidate, row, "INVALID_NUMBER", field, "数值字段不能转换为重量数值,原值已保留在raw中", true));
}
if (value.unmappedCategory()) {
blocking[0]++;
issues.add(new Issue(candidate, row, "CATEGORY_MAPPING_MISSING", "PDT代码/销售片区", "非空来源键未命中已验证的产业部-地区映射", true));
issues.add(new Issue(candidate, row, "CATEGORY_MAPPING_MISSING", "PDT代码/销售片区", "未命中业务分组对应关系,已保留为未分组,可在对应关系中补充归类", false));
} else if ("source_key_blank".equals(value.components().get("categoryMappingStatus"))) {
blankCategory[0]++;
issues.add(new Issue(candidate, row, "SOURCE_CATEGORY_KEY_BLANK", "PDT代码/销售片区", "原始匹配键为空,按源模型趋势上下文默认排除;原始行保留", false));
@@ -19,6 +19,8 @@ final class TransformBuildKey {
Integer.toString(source.year()), Integer.toString(source.month()), parserVersion,
rulesVersion, mappingVersion);
if(parserConfigHash!=null&&!"legacy".equals(parserConfigHash))identity+="\n"+parserConfigHash;
// Validation policy is part of cache identity; old mapping rejections must not survive a retry.
identity += "\nunassigned-mapping-nonblocking-v1";
try {
return HexFormat.of().formatHex(MessageDigest.getInstance("SHA-256").digest(identity.getBytes(StandardCharsets.UTF_8)));
} catch (NoSuchAlgorithmException e) {
@@ -32,8 +32,8 @@ public class SourceUploadService {
}
public Map<String,Object> options(AppUserPrincipal original){
access.requireUserActivePolicy(original);AppUserPrincipal actor=access.resolvePrincipal(original);String current=current(false);List<Map<String,Object>> targets=new ArrayList<>();
if(current!=null)for(Map<String,Object> row:g.rows("SELECT d.id AS document_id,r.id AS revision_id,d.source_slot,d.stage,d.source_year AS year,d.source_month AS month,d.scope_code,r.source_file,r.sha256,r.parser_config_hash FROM release_member m JOIN source_document d ON d.id=m.source_document_id JOIN source_revision r ON r.id=m.source_revision_id WHERE m.release_id=? ORDER BY d.source_year,d.source_month,d.stage",current)){
try{sourceAccess.requireSource((String)row.get("documentId"),(String)row.get("revisionId"),actor);targets.add(row);}catch(ResponseStatusException denied){if(denied.getStatusCode()!=HttpStatus.FORBIDDEN)throw denied;}
if(current!=null)for(Map<String,Object> row:g.rows("SELECT d.id AS document_id,r.id AS revision_id,d.source_slot,d.source_mode,d.stage,d.source_year AS year,d.source_month AS month,d.scope_code,r.source_file,r.sha256,r.parser_config_hash FROM release_member m JOIN source_document d ON d.id=m.source_document_id JOIN source_revision r ON r.id=m.source_revision_id WHERE m.release_id=? ORDER BY d.source_year,d.source_month,d.stage,d.source_slot",current)){
try{sourceAccess.requireSource((String)row.get("documentId"),(String)row.get("revisionId"),actor);row.put("fileId",fileId(row.get("sourceSlot")));targets.add(row);}catch(ResponseStatusException denied){if(denied.getStatusCode()!=HttpStatus.FORBIDDEN)throw denied;}
}
List<String> currentSourceIds=new ArrayList<>();
for(var row:targets){var ids=jdbc.query("SELECT id FROM source_candidate WHERE "+SourceIdentity.canonicalSlotSql("source_slot")+"=? AND sha256=? AND parser_config_hash=? AND deleted_at IS NULL AND status IN('DISCOVERED','IMPORTED') AND NOT "+SourceDeletionService.pendingDeletionSql("source_candidate")+" ORDER BY discovered_at DESC,id DESC LIMIT 1",(rs,n)->rs.getString(1),SourceIdentity.canonicalSlot((String)row.get("sourceSlot")),row.get("sha256"),row.get("parserConfigHash"));if(!ids.isEmpty())currentSourceIds.add(ids.getFirst());}
@@ -41,39 +41,90 @@ public class SourceUploadService {
}
@Transactional public Map<String,Object> configure(ConfigureRequest r,AppUserPrincipal original)throws IOException {
requireActor(original);AppUserPrincipal actor=access.resolvePrincipal(original);
if(r.year()<1900||r.year()>2100||r.month()<1||r.month()>12||!Set.of("ADD","REPLACE").contains(r.mode()==null?"":r.mode()))throw new IllegalArgumentException("请选择有效年月和新增/替换模式");
if(r.year()<1900||r.year()>2100||r.month()<1||r.month()>12||!Set.of("ADD","REPLACE","APPEND").contains(r.mode()==null?"":r.mode()))throw new IllegalArgumentException("请选择有效年月和新增、追加或替换模式");
boolean append="APPEND".equals(r.mode()),appendReplacement="REPLACE".equals(r.mode())&&r.fileId()!=null&&!r.fileId().isBlank();
if((append||appendReplacement)&&!validFileId(r.fileId()))throw new IllegalArgumentException("追加或替换追加文件时必须提供有效fileId");
if(!append&&!appendReplacement&&r.fileId()!=null&&!r.fileId().isBlank())throw new IllegalArgumentException("仅追加文件替换可提供fileId");
if(r.templateId()!=null&&!r.templateId().isBlank())throw new IllegalArgumentException("表头模板功能已停用,请直接提交工作表、表头行和字段映射");
String reason=g.required(r.reason(),"操作理由");String scope=SourceIdentity.scope(r.scopeCode()==null?"ALL":r.scopeCode());
if(scope==null||scope.isBlank()||scope.length()>160||scope.contains("|")||!SourceIdentity.fullOrganizationScope(actor)&&(!actor.getDepartments().stream().map(SourceIdentity::scope).toList().contains(scope)||!actor.getGroups().isEmpty()&&!actor.getGroups().contains("*")))throw new ResponseStatusException(HttpStatus.FORBIDDEN,"来源范围须完整属于当前组织及分组授权");
Map<String,Object> draft=draft(r.uploadId(),actor,true);if(draft.get("candidateId")!=null)throw new ResponseStatusException(HttpStatus.CONFLICT,"此上传已确认,请刷新来源列表或重新上传");
Map<String,Object> draft=draft(r.uploadId(),actor,true);String existingCandidateId=(String)draft.get("candidateId");Map<String,Object> priorCandidate=existingCandidateId==null?null:reconfigurableCandidate(existingCandidateId,actor,draft);
Path file=verified(draft);SourceParserConfig config=new SourceParserConfig(r.stage(),r.sheetName(),r.headerRow(),r.fieldMap(),null);
Map<String,Object> actual=reader.preview(file,r.sheetName(),r.headerRow());Map<?,?> preview=(Map<?,?>)actual.get("preview");config.validate((List<String>)preview.get("headers"));
String current=current(true);if(!Objects.equals(current,r.expectedCurrentReleaseId()))throw new ResponseStatusException(HttpStatus.CONFLICT,"当前正式版本已变化,请刷新后重新确认");
String slot=SourceIdentity.slot(r.stage(),r.year(),r.month(),scope,"MAIN");
String shard=(append||appendReplacement)?"UPLOAD_"+r.fileId():"MAIN";
String slot=SourceIdentity.slot(r.stage(),r.year(),r.month(),scope,shard);
if(slot.length()>190)throw new IllegalArgumentException("来源身份过长");
var documents=g.rows("SELECT id FROM source_document WHERE "+SourceIdentity.canonicalSlotSql("source_slot")+"=? FOR UPDATE",slot);
String replacementRevision=null,replacementDocument=null;
if("REPLACE".equals(r.mode())){
if(r.targetDocumentId()==null||r.targetRevisionId()==null||current==null)throw new IllegalArgumentException("请选择当前正式版中的替换来源");
var old=g.rows("SELECT d.id,r.source_revision_id FROM release_member r JOIN source_document d ON d.id=r.source_document_id WHERE r.release_id=? AND d.id=? AND r.source_revision_id=? AND "+SourceIdentity.canonicalSlotSql("d.source_slot")+"=? FOR UPDATE",current,r.targetDocumentId(),r.targetRevisionId(),slot);
if(old.size()!=1)throw new ResponseStatusException(HttpStatus.CONFLICT,"替换目标的月份、阶段、范围或旧修订已变化");sourceAccess.requireSource(r.targetDocumentId(),r.targetRevisionId(),actor);replacementDocument=r.targetDocumentId();replacementRevision=r.targetRevisionId();
}else if(!documents.isEmpty())throw new ResponseStatusException(HttpStatus.CONFLICT,"该月份、阶段及范围已有来源,请选择替换旧数据");
var old=g.rows("SELECT d.id,r.source_revision_id,d.source_mode FROM release_member r JOIN source_document d ON d.id=r.source_document_id WHERE r.release_id=? AND d.id=? AND r.source_revision_id=? AND "+SourceIdentity.canonicalSlotSql("d.source_slot")+"=? FOR UPDATE",current,r.targetDocumentId(),r.targetRevisionId(),slot);
if(old.size()!=1)throw new ResponseStatusException(HttpStatus.CONFLICT,"替换目标的月份、阶段、范围、追加文件ID或旧修订已变化");
if(appendReplacement&&!("APPEND_SNAPSHOT".equals(old.getFirst().get("sourceMode"))))throw new ResponseStatusException(HttpStatus.CONFLICT,"替换目标不是由追加上传创建的文件批次");
sourceAccess.requireSource(r.targetDocumentId(),r.targetRevisionId(),actor);replacementDocument=r.targetDocumentId();replacementRevision=r.targetRevisionId();
}else if(!documents.isEmpty())throw new ResponseStatusException(HttpStatus.CONFLICT,append?"该fileId已有来源,请选择这份追加文件并替换其当前修订":"该月份、阶段及范围已有来源,请选择替换旧数据");
if(append){
var prior=g.rows("SELECT id FROM source_candidate WHERE "+SourceIdentity.canonicalSlotSql("source_slot")+"=? AND deleted_at IS NULL AND status IN('DISCOVERED','QUEUED','RUNNING','IMPORTED') FOR UPDATE",slot);
if(prior.stream().anyMatch(row->!Objects.equals(existingCandidateId,row.get("id"))))throw new ResponseStatusException(HttpStatus.CONFLICT,"该fileId已登记来源,请选择已有来源替换,或为新的独立文件批次生成fileId");
}
Integer pending=jdbc.queryForObject("SELECT COUNT(*) FROM source_candidate WHERE "+SourceIdentity.canonicalSlotSql("source_slot")+"=? AND status IN('QUEUED','RUNNING')",Integer.class,slot);if(pending!=null&&pending>0)throw new ResponseStatusException(HttpStatus.CONFLICT,"该来源正在构建,请稍后重试");
String protocol=protocol(slot,r,scope,actor,reason);String key=GovernanceSupport.sha(slot+"\n"+draft.get("sha256")+"\n"+config.hash()+"\n"+r.uploadId());String id=UUID.randomUUID().toString();
Map<String,Object> frozenConfiguration=new LinkedHashMap<>(config.snapshot());frozenConfiguration.put("sourceSlot",slot);
jdbc.update("INSERT INTO source_candidate(id,candidate_key,source_slot,stage,source_year,source_month,scope_code,source_file,source_path,sha256,file_size,status,protocol_id,resolution,resolution_reason,parser_config,parser_config_hash,expected_current_release_id,replacement_document_id,replacement_revision_id,configured_by) VALUES(?,?,?,?,?,?,?,?,?,?,?,'DISCOVERED',?,?,?,CAST(? AS JSON),?,?,?,?,?)",id,key,slot,r.stage(),r.year(),r.month(),scope,draft.get("sourceFile"),file.toString(),draft.get("sha256"),draft.get("fileSize"),protocol,"REPLACE".equals(r.mode())?"REPLACE":"CONFIRMED",reason,json.writeValueAsString(frozenConfiguration),config.hash(),current,replacementDocument,replacementRevision,actor.getId().toString());
jdbc.update("UPDATE source_upload_draft SET candidate_id=? WHERE id=?",id,r.uploadId());g.audit(actor.getId().toString(),"SOURCE_UPLOAD_CONFIGURED","source_candidate",id,Map.of("mode",r.mode(),"slot",slot,"reason",reason,"configuration",frozenConfiguration));
Map<String,Object> result=new LinkedHashMap<>();result.put("source",new SourceCatalogService.SourceView(id,(String)draft.get("sourceFile"),r.stage(),r.year(),r.month(),((Number)draft.get("fileSize")).longValue(),(String)draft.get("sha256"),"DISCOVERED"));result.put("configuration",frozenConfiguration);result.put("currentReleaseId",current);return result;
ensureUniqueContent((String)draft.get("sha256"),r.stage(),r.year(),r.month(),scope,current,replacementDocument,existingCandidateId);
String protocol=protocol(slot,r,scope,actor,reason,shard);String key=GovernanceSupport.sha(slot+"\n"+draft.get("sha256")+"\n"+config.hash()+"\n"+r.uploadId());String id=existingCandidateId==null?UUID.randomUUID().toString():existingCandidateId;
Map<String,Object> frozenConfiguration=new LinkedHashMap<>(config.snapshot());frozenConfiguration.put("sourceSlot",slot);if(append||appendReplacement)frozenConfiguration.put("fileId",r.fileId());
String resolution=append?"APPEND_CONFIRMED":"REPLACE".equals(r.mode())?"REPLACE":"CONFIRMED";
if(priorCandidate==null){
jdbc.update("INSERT INTO source_candidate(id,candidate_key,source_slot,stage,source_year,source_month,scope_code,source_file,source_path,sha256,file_size,status,protocol_id,resolution,resolution_reason,parser_config,parser_config_hash,expected_current_release_id,replacement_document_id,replacement_revision_id,configured_by) VALUES(?,?,?,?,?,?,?,?,?,?,?,'DISCOVERED',?,?,?,CAST(? AS JSON),?,?,?,?,?)",id,key,slot,r.stage(),r.year(),r.month(),scope,draft.get("sourceFile"),file.toString(),draft.get("sha256"),draft.get("fileSize"),protocol,resolution,reason,json.writeValueAsString(frozenConfiguration),config.hash(),current,replacementDocument,replacementRevision,actor.getId().toString());
}else{
String oldSlot=SourceIdentity.canonicalSlot((String)priorCandidate.get("sourceSlot"));String oldProtocol=Objects.toString(priorCandidate.get("protocolId"),null);
if(!Objects.equals(oldSlot,SourceIdentity.canonicalSlot(slot))&&oldProtocol!=null)deactivateUnusedUploadProtocol(oldProtocol,id);
int changed=jdbc.update("UPDATE source_candidate SET candidate_key=?,source_slot=?,stage=?,source_year=?,source_month=?,scope_code=?,source_file=?,source_path=?,sha256=?,file_size=?,protocol_id=?,resolution=?,resolution_reason=?,parser_config=CAST(? AS JSON),parser_config_hash=?,expected_current_release_id=?,replacement_document_id=?,replacement_revision_id=?,configured_by=? WHERE id=? AND status='DISCOVERED' AND deleted_at IS NULL",key,slot,r.stage(),r.year(),r.month(),scope,draft.get("sourceFile"),file.toString(),draft.get("sha256"),draft.get("fileSize"),protocol,resolution,reason,json.writeValueAsString(frozenConfiguration),config.hash(),current,replacementDocument,replacementRevision,actor.getId().toString(),id);
if(changed!=1)throw new ResponseStatusException(HttpStatus.CONFLICT,"来源已排队或状态变化,请刷新来源列表后重试");
}
jdbc.update("UPDATE source_upload_draft SET candidate_id=? WHERE id=?",id,r.uploadId());g.audit(actor.getId().toString(),priorCandidate==null?"SOURCE_UPLOAD_CONFIGURED":"SOURCE_UPLOAD_RECONFIGURED","source_candidate",id,Map.of("mode",r.mode(),"slot",slot,"reason",reason,"configuration",frozenConfiguration));
Map<String,Object> result=new LinkedHashMap<>();result.put("source",new SourceCatalogService.SourceView(id,(String)draft.get("sourceFile"),r.stage(),r.year(),r.month(),((Number)draft.get("fileSize")).longValue(),(String)draft.get("sha256"),"DISCOVERED"));result.put("configuration",frozenConfiguration);result.put("currentReleaseId",current);result.put("reconfigured",priorCandidate!=null);return result;
}
private String protocol(String slot,ConfigureRequest r,String scope,AppUserPrincipal actor,String reason){
private String protocol(String slot,ConfigureRequest r,String scope,AppUserPrincipal actor,String reason,String shard){
String mode="MAIN".equals(shard)?"MONTHLY_SNAPSHOT":"APPEND_SNAPSHOT";
var existing=g.rows("SELECT id,source_mode,applicable FROM source_protocol WHERE "+SourceIdentity.canonicalSlotSql("source_slot")+"=? AND active=TRUE FOR UPDATE",slot);
if(existing.size()>1)throw new IllegalArgumentException("来源协议存在重复,请先完成来源治理");
if(!existing.isEmpty()){if(!"MONTHLY_SNAPSHOT".equals(existing.getFirst().get("sourceMode"))||!Boolean.TRUE.equals(existing.getFirst().get("applicable")))throw new IllegalArgumentException("此向导仅接收适用阶段的完整月度快照");return (String)existing.getFirst().get("id");}
String id=UUID.randomUUID().toString();Integer version=jdbc.queryForObject("SELECT COALESCE(MAX(version_no),0)+1 FROM source_protocol WHERE "+SourceIdentity.canonicalSlotSql("source_slot")+"=?",Integer.class,slot);jdbc.update("INSERT INTO source_protocol(id,source_slot,version_no,stage,source_year,source_month,scope_code,source_mode,required_sheet,applicable,expected,reason,created_by) VALUES(?,?,?,?,?,?,?,'MONTHLY_SNAPSHOT',?,TRUE,TRUE,?,?)",id,slot,version,r.stage(),r.year(),r.month(),scope,r.sheetName(),reason,actor.getId().toString());return id;
if(!existing.isEmpty()){if(!mode.equals(existing.getFirst().get("sourceMode"))||!Boolean.TRUE.equals(existing.getFirst().get("applicable")))throw new IllegalArgumentException("来源协议与完整快照/追加批次身份不匹配,须先完成来源治理");return (String)existing.getFirst().get("id");}
var versions=g.rows("SELECT version_no FROM source_protocol WHERE "+SourceIdentity.canonicalSlotSql("source_slot")+"=? ORDER BY version_no DESC LIMIT 1 FOR UPDATE",slot);int version=versions.isEmpty()?1:((Number)versions.getFirst().get("versionNo")).intValue()+1;
String id=UUID.randomUUID().toString();jdbc.update("INSERT INTO source_protocol(id,source_slot,version_no,stage,source_year,source_month,scope_code,shard_code,source_mode,required_sheet,applicable,expected,reason,created_by) VALUES(?,?,?,?,?,?,?,?,?,?,TRUE,TRUE,?,?)",id,slot,version,r.stage(),r.year(),r.month(),scope,shard,mode,r.sheetName(),reason,actor.getId().toString());return id;
}
private Map<String,Object> reconfigurableCandidate(String id,AppUserPrincipal actor,Map<String,Object> draft){
var rows=g.rows("SELECT * FROM source_candidate WHERE id=? AND configured_by=? FOR UPDATE",id,actor.getId().toString());
if(rows.isEmpty())throw new ResponseStatusException(HttpStatus.CONFLICT,"已确认来源不属于当前上传人,请重新上传");Map<String,Object> candidate=rows.getFirst();
if(!"DISCOVERED".equals(candidate.get("status"))||!Objects.equals(candidate.get("sha256"),draft.get("sha256"))||!Objects.equals(candidate.get("sourcePath"),draft.get("sourcePath")))throw new ResponseStatusException(HttpStatus.CONFLICT,"此来源已排队、处理或内容变化,不能重新确认");
Integer runs=jdbc.queryForObject("SELECT COUNT(*) FROM import_run WHERE JSON_CONTAINS(requested_sources,JSON_QUOTE(?))",Integer.class,id);
String sameSlot=SourceIdentity.canonicalSlotSql("d.source_slot")+"="+SourceIdentity.canonicalSlotSql("c.source_slot");
Integer documents=jdbc.queryForObject("SELECT COUNT(*) FROM source_candidate c JOIN source_document d ON "+sameSlot+" JOIN source_revision r ON r.source_document_id=d.id AND r.sha256=c.sha256 AND r.parser_config_hash=c.parser_config_hash JOIN release_member m ON m.source_document_id=d.id AND m.source_revision_id=r.id WHERE c.id=?",Integer.class,id);
if((runs!=null&&runs>0)||(documents!=null&&documents>0))throw new ResponseStatusException(HttpStatus.CONFLICT,"来源已被导入任务或版本引用,不能重新确认");
return candidate;
}
private void deactivateUnusedUploadProtocol(String protocolId,String candidateId){
var protocol=g.rows("SELECT source_slot,active FROM source_protocol WHERE id=? AND active=TRUE FOR UPDATE",protocolId);if(protocol.isEmpty())return;
Integer others=jdbc.queryForObject("SELECT COUNT(*) FROM source_candidate WHERE protocol_id=? AND id<>? AND deleted_at IS NULL AND status NOT IN('RETURNED','SUPERSEDED')",Integer.class,protocolId,candidateId);
Integer documents=jdbc.queryForObject("SELECT COUNT(*) FROM source_document WHERE "+SourceIdentity.canonicalSlotSql("source_slot")+"=?",Integer.class,SourceIdentity.canonicalSlot((String)protocol.getFirst().get("sourceSlot")));
if((others==null||others==0)&&(documents==null||documents==0))jdbc.update("UPDATE source_protocol SET active=FALSE WHERE id=? AND active=TRUE",protocolId);
}
private void ensureUniqueContent(String sha,String stage,int year,int month,String scope,String releaseId,String excludedDocument,String excludedCandidate){
if(releaseId!=null){
List<Map<String,Object>> currentRows=g.rows("SELECT d.id,d.scope_code FROM release_member m JOIN source_document d ON d.id=m.source_document_id JOIN source_revision r ON r.id=m.source_revision_id WHERE m.release_id=? AND r.sha256=? AND d.stage=? AND d.source_year=? AND d.source_month=? FOR UPDATE",releaseId,sha,stage,year,month);
if(currentRows.stream().anyMatch(row->!Objects.equals(excludedDocument,row.get("id"))&&SourceIdentity.intersects(scope,(String)row.get("scopeCode"))))throw duplicateContent();
}
List<Map<String,Object>> pending=g.rows("SELECT id,source_slot,scope_code FROM source_candidate WHERE sha256=? AND stage=? AND source_year=? AND source_month=? AND deleted_at IS NULL AND status IN('DISCOVERED','QUEUED','RUNNING') AND NOT "+SourceDeletionService.pendingDeletionSql("source_candidate")+" FOR UPDATE",sha,stage,year,month);
if(pending.stream().anyMatch(row->!Objects.equals(excludedCandidate,row.get("id"))&&SourceIdentity.intersects(scope,(String)row.get("scopeCode"))))throw duplicateContent();
}
private ResponseStatusException duplicateContent(){return new ResponseStatusException(HttpStatus.CONFLICT,"该工作簿内容已用于同一年月、阶段及重叠范围的另一份来源,请勿重复计入;更正版请精确选择原来源替换");}
private static boolean validFileId(String value){return value!=null&&value.length()<=80&&value.matches("[A-Za-z0-9_-]{1,80}");}
private static String fileId(Object slotValue){if(!(slotValue instanceof String slot))return null;String[] parts=SourceIdentity.canonicalSlot(slot).split("\\|",-1);if(parts.length!=5||!parts[4].startsWith("UPLOAD_"))return null;String value=parts[4].substring("UPLOAD_".length());return validFileId(value)?value:null;}
private Map<String,Object> draft(String id,AppUserPrincipal actor,boolean lock){var rows=g.rows("SELECT * FROM source_upload_draft WHERE id=? AND created_by=? AND expires_at>CURRENT_TIMESTAMP(6)"+(lock?" FOR UPDATE":""),id,actor.getId().toString());if(rows.isEmpty())throw new ResponseStatusException(HttpStatus.NOT_FOUND,"上传预览已过期或不属于当前用户,请重新上传");return rows.getFirst();}
private Path verified(Map<String,Object> draft)throws IOException{Path path=Path.of((String)draft.get("sourcePath"));if(Files.isSymbolicLink(path)||!Files.isRegularFile(path)||!path.toRealPath().startsWith(scanner.archiveRoot())||Files.size(path)!=((Number)draft.get("fileSize")).longValue()||!SourceScanner.sha256(path).equals(draft.get("sha256")))throw new IllegalArgumentException("上传文件已变化,请重新上传");return path.toRealPath();}
private String current(boolean lock){return jdbc.queryForObject("SELECT release_id FROM current_release WHERE dataset_id='quality'"+(lock?" FOR UPDATE":""),String.class);}
private void requireActor(AppUserPrincipal actor){access.requireUserActivePolicy(actor);access.requireBoundAction("RECEIVER","source.receive");}
/** templateId remains an explicitly disabled compatibility field for older clients. */
public record ConfigureRequest(String uploadId,String stage,int year,int month,String sheetName,int headerRow,Map<String,String> fieldMap,String templateId,String mode,String expectedCurrentReleaseId,String targetDocumentId,String targetRevisionId,String reason,String scopeCode){}
public record ConfigureRequest(String uploadId,String stage,int year,int month,String sheetName,int headerRow,Map<String,String> fieldMap,String templateId,String mode,String expectedCurrentReleaseId,String targetDocumentId,String targetRevisionId,String reason,String scopeCode,String fileId){
public ConfigureRequest(String uploadId,String stage,int year,int month,String sheetName,int headerRow,Map<String,String> fieldMap,String templateId,String mode,String expectedCurrentReleaseId,String targetDocumentId,String targetRevisionId,String reason,String scopeCode){this(uploadId,stage,year,month,sheetName,headerRow,fieldMap,templateId,mode,expectedCurrentReleaseId,targetDocumentId,targetRevisionId,reason,scopeCode,null);}
}
}
@@ -0,0 +1,27 @@
package cn.yunsheng.quality.update;
import java.util.Map;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/api/system-update")
@PreAuthorize("hasRole('SYSTEM_ADMIN')")
public class SystemUpdateController {
private final SystemUpdateService updates;
public SystemUpdateController(SystemUpdateService updates) { this.updates = updates; }
@GetMapping
public Map<String, Object> status() { return updates.status(); }
// Both mutations use the application's existing session + CSRF filter.
@PostMapping("/check")
public Map<String, Object> check() { return updates.check(); }
@PostMapping("/apply")
public Map<String, Object> apply() { return updates.apply(); }
}
@@ -0,0 +1,39 @@
package cn.yunsheng.quality.update;
import jakarta.servlet.FilterChain;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Set;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import org.springframework.web.filter.OncePerRequestFilter;
/** Keeps the startup-to-update-commit interval free of business writes. */
@Component
@Order(Ordered.HIGHEST_PRECEDENCE + 20)
public class SystemUpdateMaintenanceFilter extends OncePerRequestFilter {
private static final Set<String> READ_METHODS = Set.of("GET", "HEAD", "OPTIONS", "TRACE");
private static final Set<String> AUTH_ROUTES = Set.of("/api/auth/login", "/api/auth/logout", "/api/auth/csrf", "/api/auth/me");
private final SystemUpdateService updates;
public SystemUpdateMaintenanceFilter(SystemUpdateService updates) { this.updates = updates; }
@Override
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain chain)
throws ServletException, IOException {
String path = request.getRequestURI().substring(request.getContextPath().length());
if (!READ_METHODS.contains(request.getMethod()) && path.startsWith("/api/")
&& !path.equals("/api/system-update") && !path.startsWith("/api/system-update/")
&& !AUTH_ROUTES.contains(path) && updates.maintenanceActive()) {
response.setStatus(HttpServletResponse.SC_SERVICE_UNAVAILABLE);
response.setContentType("application/problem+json;charset=UTF-8");
response.setHeader("Retry-After", "10");
response.getWriter().write("{\"title\":\"Service Unavailable\",\"status\":503,\"detail\":\"系统正在更新,请稍后重试。\"}");
return;
}
chain.doFilter(request, response);
}
}
@@ -0,0 +1,138 @@
package cn.yunsheng.quality.update;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;
import org.springframework.web.server.ResponseStatusException;
import tools.jackson.databind.json.JsonMapper;
/** Restricted bridge to the packaged updater; neither URLs nor paths are input. */
@Service
public class SystemUpdateService {
private static final Set<String> RESPONSE_FIELDS = Set.of("enabled", "currentVersion", "state", "message",
"checkedAt", "version", "available", "startedAt", "finishedAt");
private final JsonMapper json;
private final Path root;
private final boolean enabled;
public SystemUpdateService(JsonMapper json,
@Value("${QUALITY_PROJECT_ROOT:}") String root,
@Value("${QUALITY_UPDATE_ENABLED:false}") boolean requested) {
this.json = json;
this.root = root.isBlank() ? null : Path.of(root).toAbsolutePath().normalize();
this.enabled = requested && System.getProperty("os.name", "").startsWith("Windows")
&& this.root != null && Files.isRegularFile(this.root.resolve("manifest.json"))
&& Files.isRegularFile(this.root.resolve("runtimes/python/python.exe"))
&& Files.isRegularFile(this.root.resolve("deploy/offline/update_manager.py"))
&& Files.isRegularFile(this.root.resolve("deploy/offline/update-public-key.pem"));
}
public synchronized Map<String, Object> status() {
if (!enabled) return Map.of("enabled", false, "state", "disabled", "message", "此部署不支持软件内更新。");
return invoke("status");
}
public synchronized Map<String, Object> check() {
requireEnabled();
return invoke("check");
}
public synchronized Map<String, Object> apply() {
requireEnabled();
return invoke("launch");
}
/** Fail closed during installation/recovery, including a process crash. */
public boolean maintenanceActive() {
if (!enabled) return false;
try {
Map<?, ?> state = readMetadata("status.json");
if (state.get("state") instanceof String phase
&& Set.of("backingUp", "installing", "restarting", "rollingBack").contains(phase)) return true;
Map<?, ?> journal = readMetadata("journal.json");
return journal.get("phase") instanceof String phase && Set.of("stopping", "installing").contains(phase);
} catch (Exception failure) {
return true;
}
}
private Map<?, ?> readMetadata(String name) throws IOException {
Path path = root.resolve(".runtime/update/" + name);
if (!Files.exists(path)) return Map.of();
try (var stream = Files.newInputStream(path)) {
byte[] raw = stream.readNBytes(8193);
if (raw.length > 8192) throw new IOException("Oversized update state");
Object decoded = json.readValue(raw, Object.class);
if (!(decoded instanceof Map<?, ?> value)) throw new IOException("Invalid update state");
return value;
}
}
private void requireEnabled() {
if (!enabled) throw new ResponseStatusException(HttpStatus.CONFLICT, "此部署不支持软件内更新。");
}
private Map<String, Object> invoke(String action) {
Process process = null;
try {
ProcessBuilder builder = new ProcessBuilder(root.resolve("runtimes/python/python.exe").toString(),
root.resolve("deploy/offline/update_manager.py").toString(), action);
builder.directory(root.toFile());
builder.redirectError(ProcessBuilder.Redirect.DISCARD);
builder.environment().put("PYTHONUTF8", "1");
builder.environment().put("PYTHONIOENCODING", "utf-8");
builder.environment().put("PYTHONDONTWRITEBYTECODE", "1");
process = builder.start();
process.getOutputStream().close();
// The CLI prints one small, allowlisted JSON receipt. Waiting first
// also bounds hangs caused by a failed DNS/TLS/network operation.
if (!process.waitFor(45, TimeUnit.SECONDS)) {
process.destroyForcibly();
throw new ResponseStatusException(HttpStatus.GATEWAY_TIMEOUT, "更新服务器暂未响应,请稍后重试。");
}
byte[] raw = process.getInputStream().readNBytes(8193);
if (raw.length > 8192) throw new IOException("Oversized updater receipt");
Object decoded = json.readValue(new String(raw, StandardCharsets.UTF_8), Object.class);
if (!(decoded instanceof Map<?, ?> value)) throw new IOException("Invalid updater receipt");
if (process.exitValue() != 0) {
Object detail = value.get("error");
String message = detail instanceof String s && s.length() <= 300 ? s : "更新操作未完成,请稍后重试。";
throw new ResponseStatusException(HttpStatus.CONFLICT, message);
}
return safeResponse(value);
} catch (ResponseStatusException failure) {
throw failure;
} catch (InterruptedException failure) {
Thread.currentThread().interrupt();
throw new ResponseStatusException(HttpStatus.SERVICE_UNAVAILABLE, "更新操作已中断。");
} catch (Exception failure) {
throw new ResponseStatusException(HttpStatus.SERVICE_UNAVAILABLE, "更新服务暂不可用,请稍后重试。");
} finally {
if (process != null && process.isAlive()) process.destroyForcibly();
}
}
static Map<String, Object> safeResponse(Map<?, ?> value) throws IOException {
Map<String, Object> result = new LinkedHashMap<>();
for (String field : RESPONSE_FIELDS) {
Object item = value.get(field);
if (item == null) continue;
if ((field.equals("enabled") || field.equals("available")) && !(item instanceof Boolean))
throw new IOException("Invalid updater boolean");
if (!(item instanceof Boolean) && !(item instanceof String s && s.length() <= 300))
throw new IOException("Invalid updater field");
result.put(field, item);
}
if (!(result.get("state") instanceof String) || !(result.get("enabled") instanceof Boolean))
throw new IOException("Incomplete updater receipt");
return result;
}
}
@@ -0,0 +1,97 @@
package cn.yunsheng.quality.update;
import jakarta.annotation.PreDestroy;
import java.lang.reflect.Method;
import java.util.Set;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import org.aopalliance.intercept.MethodInterceptor;
import org.springframework.aop.framework.AopProxyUtils;
import org.springframework.aop.framework.ProxyFactory;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.AnnotatedElementUtils;
import org.springframework.core.env.Environment;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.util.ReflectionUtils;
/** Defers business recovery and suppresses schedules until the update commits. */
@Component
public class SystemUpdateStartupBarrier implements BeanPostProcessor, Ordered {
private static final Set<String> RECOVERY_METHODS = Set.of(
"cn.yunsheng.quality.ingestion.ImportRecoveryRunner#run",
"cn.yunsheng.quality.analysis.AnalysisTurnService#recoverQueuedTurns",
"cn.yunsheng.quality.analysis.AnalysisReportService#recoverPendingReports",
"cn.yunsheng.quality.analytics.InspectionService#recoverPendingDrilldowns");
private static final org.slf4j.Logger LOG = org.slf4j.LoggerFactory.getLogger(SystemUpdateStartupBarrier.class);
private final ObjectProvider<SystemUpdateService> updates;
private final Environment environment;
private final ConcurrentLinkedQueue<Runnable> pending = new ConcurrentLinkedQueue<>();
private final ScheduledExecutorService release = Executors.newSingleThreadScheduledExecutor(task -> {
Thread thread = new Thread(task, "quality-update-recovery");
thread.setDaemon(true);
return thread;
});
public SystemUpdateStartupBarrier(ObjectProvider<SystemUpdateService> updates, Environment environment) {
this.updates = updates;
this.environment = environment;
release.scheduleWithFixedDelay(this::drain, 500, 500, TimeUnit.MILLISECONDS);
}
@Override public int getOrder() { return Ordered.HIGHEST_PRECEDENCE + 50; }
@Override
public Object postProcessAfterInitialization(Object bean, String name) {
if (!System.getProperty("os.name", "").startsWith("Windows")
|| !environment.getProperty("QUALITY_UPDATE_ENABLED", Boolean.class, false)) return bean;
Class<?> type = AopProxyUtils.ultimateTargetClass(bean);
if (!type.getName().startsWith("cn.yunsheng.quality.") || type.getName().contains(".update.")) return bean;
boolean candidate = false;
for (Method method : ReflectionUtils.getAllDeclaredMethods(type))
if (isRecovery(type, method) || AnnotatedElementUtils.hasAnnotation(method, Scheduled.class)) candidate = true;
return candidate ? wrap(bean, type) : bean;
}
Object wrap(Object bean, Class<?> type) {
ProxyFactory proxy = new ProxyFactory(bean);
proxy.setProxyTargetClass(true);
proxy.addAdvice((MethodInterceptor) invocation -> {
Method method = ReflectionUtils.findMethod(type, invocation.getMethod().getName(), invocation.getMethod().getParameterTypes());
if (method == null || !updates.getObject().maintenanceActive()) return invocation.proceed();
if (isRecovery(type, method)) {
pending.add(() -> {
try { invocation.proceed(); }
catch (Throwable failure) { LOG.error("Deferred application recovery failed", failure); }
});
return null;
}
if (AnnotatedElementUtils.hasAnnotation(method, Scheduled.class)) return null;
return invocation.proceed();
});
return proxy.getProxy(type.getClassLoader());
}
private static boolean isRecovery(Class<?> type, Method method) {
return RECOVERY_METHODS.contains(type.getName() + "#" + method.getName());
}
void drain() {
if (pending.isEmpty()) return;
try {
while (!updates.getObject().maintenanceActive()) {
Runnable task = pending.poll();
if (task == null) return;
task.run();
}
} catch (Exception failure) {
LOG.warn("Waiting for application update to complete");
}
}
@PreDestroy public void close() { release.shutdownNow(); pending.clear(); }
}
@@ -506,10 +506,40 @@ public class DataWorkflowService {
throw failure("CANDIDATE_VERSION_MISMATCH", "候选版本未使用本次工作流冻结的映射和规则,候选不会发布");
Integer invalidBuilds = jdbc.queryForObject("""
SELECT COUNT(*) FROM release_member rm JOIN transform_build b ON b.id=rm.transform_build_id
WHERE rm.release_id=? AND (b.mapping_version<>? OR b.rule_version<>? OR b.status<>'COMPLETED' OR b.rejected_count>0)
WHERE rm.release_id=? AND (b.mapping_version<>? OR b.rule_version<>?)
""", Integer.class, releaseId, mappingId, ruleId);
if (invalidBuilds != null && invalidBuilds > 0)
throw failure("CANDIDATE_BUILD_VERSION_MISMATCH", "候选中存在未使用本次冻结版本的转换产物,候选不会发布");
Integer unfinishedBuilds = jdbc.queryForObject("""
SELECT COUNT(*) FROM release_member rm JOIN transform_build b ON b.id=rm.transform_build_id
WHERE rm.release_id=? AND b.status<>'COMPLETED'
""", Integer.class, releaseId);
if (unfinishedBuilds != null && unfinishedBuilds > 0)
throw failure("CANDIDATE_BUILD_INCOMPLETE", "有 " + unfinishedBuilds + " 个来源尚未完成转换,候选不会发布");
Integer rejected = jdbc.queryForObject("""
SELECT COALESCE(SUM(b.rejected_count),0) FROM release_member rm JOIN transform_build b ON b.id=rm.transform_build_id
WHERE rm.release_id=?
""", Integer.class, releaseId);
if (rejected != null && rejected > 0) {
List<Map<String,Object>> issues = jdbc.queryForList("""
SELECT issue_code AS issueCode,field_name AS fieldName,COUNT(*) AS issueCount FROM import_issue
WHERE import_run_id=(SELECT id FROM import_run WHERE result_release_id=? ORDER BY created_at DESC LIMIT 1)
AND JSON_EXTRACT(details,'$.blocking')=TRUE
GROUP BY issue_code,field_name ORDER BY issueCount DESC,issue_code,field_name LIMIT 3
""", releaseId);
String detail = issues.stream().map(issue -> {
String reason = switch (Objects.toString(issue.get("issueCode"), "")) {
case "INVALID_NUMBER" -> "数值格式无效";
case "REQUIRED_HEADER_MISSING" -> "缺少必需列";
case "NO_DETAIL_ROWS" -> "没有可导入明细";
default -> "来源校验未通过";
};
String field = Objects.toString(issue.get("fieldName"), "");
return (field.isBlank() ? "" : field + ":") + reason + "(" + issue.get("issueCount") + "项)";
}).collect(java.util.stream.Collectors.joining(";"));
throw failure("CANDIDATE_SOURCE_VALIDATION_FAILED", "来源数据有 " + rejected + " 项阻断问题,候选不会发布。"
+ (detail.isBlank() ? "请查看导入记录中的问题明细" : detail));
}
}
private void publishExisting(WorkflowOperation operation, AppUserPrincipal actor, String token) {
@@ -132,7 +132,7 @@ class AnalysisAgentLoopTest {
private static final class Fixture {
final AppUserPrincipal actor = new AppUserPrincipal(UUID.fromString("29000000-0000-0000-0000-000000000001"),
"agent-test", "unused", "Agent Test", true, false, List.of("BUSINESS_ANALYST"),
"agent-test", "unused", "Agent Test", true, false, List.of("USER"),
List.of("质量中心"), List.of("锻造"), List.of());
final JdbcTemplate jdbc = mock(JdbcTemplate.class);
final JsonMapper json = JsonMapper.builder().build();
@@ -184,7 +184,7 @@ class AnalysisApiSettingsTest {
context.registerBean(AnalysisApiSettingsService.class, this::service);
context.register(AnalysisApiSettingsController.class); context.refresh();
var c = context.getBean(AnalysisApiSettingsController.class);
SecurityContextHolder.getContext().setAuthentication(new UsernamePasswordAuthenticationToken("viewer", "", List.of(new SimpleGrantedAuthority("ROLE_QUALITY_VIEWER"))));
SecurityContextHolder.getContext().setAuthentication(new UsernamePasswordAuthenticationToken("viewer", "", List.of(new SimpleGrantedAuthority("ROLE_USER"))));
assertThrows(AccessDeniedException.class, c::get);
assertThrows(AccessDeniedException.class, () -> c.test(null));
assertThrows(AccessDeniedException.class, () -> c.save(null));
@@ -27,7 +27,7 @@ class AnalysisChangesServiceTest {
private final GovernanceAccessService governance = mock(GovernanceAccessService.class);
private final OptionsController options = mock(OptionsController.class);
private final AppUserPrincipal actor = new AppUserPrincipal(UUID.randomUUID(), "analyst", "unused", "Analyst", true,
false, List.of("BUSINESS_ANALYST"), List.of("消费电子产业部"), List.of("消费电子-3C"), List.of());
false, List.of("USER"), List.of("消费电子产业部"), List.of("消费电子-3C"), List.of());
private final AnalysisChangesService service = new AnalysisChangesService(metrics, responseAccess, governance, options);
@Test
@@ -56,7 +56,7 @@ class AnalysisControllerHistoryAccessTest {
}
private static final class Fixture {
final String session=UUID.randomUUID().toString(),turn=UUID.randomUUID().toString(),release=UUID.randomUUID().toString();
final AppUserPrincipal actor=new AppUserPrincipal(UUID.randomUUID(),"history-viewer","unused","测试",true,false,List.of("QUALITY_VIEWER"),List.of(),List.of(),List.of());
final AppUserPrincipal actor=new AppUserPrincipal(UUID.randomUUID(),"history-viewer","unused","测试",true,false,List.of("USER"),List.of(),List.of(),List.of());
final AnalysisProtocolService protocol=mock(AnalysisProtocolService.class);
final MetricsService metrics=mock(MetricsService.class);
final AnalysisController controller;
@@ -43,7 +43,7 @@ class AnalysisExplorationToolsTest {
private final JdbcTemplate jdbc = mock(JdbcTemplate.class);
private final AnalysisExplorationTools tools = new AnalysisExplorationTools(metrics, dictionary, governance, responseAccess, jdbc);
private final AppUserPrincipal actor = new AppUserPrincipal(UUID.randomUUID(), "analyst", "unused", "Analyst", true,
false, List.of("BUSINESS_ANALYST"), List.of("质量中心"), List.of("锻造"), List.of());
false, List.of("USER"), List.of("质量中心"), List.of("锻造"), List.of());
@Test
void detailsReturnsOnlyAuthorizedFieldsFromFilteredPublishedReleaseSample() throws Exception {
@@ -157,7 +157,7 @@ class AnalysisNativeAgentContextTest {
JdbcTemplate jdbc = mock(JdbcTemplate.class);
GovernanceAccessService governance = mock(GovernanceAccessService.class);
AppUserPrincipal actor = new AppUserPrincipal(UUID.randomUUID(), "agent-context-test", "unused", "测试", true, false,
List.of("QUALITY_VIEWER"), List.of(), List.of(), List.of());
List.of("USER"), List.of(), List.of(), List.of());
when(governance.allowedFields(actor)).thenReturn(Set.of("black_good_weight", "black_total_weight"));
doReturn(List.of()).when(jdbc).query(org.mockito.ArgumentMatchers.anyString(),
org.mockito.ArgumentMatchers.any(RowMapper.class));
@@ -112,7 +112,7 @@ class AnalysisNativeRuntimeRecoveryTest {
final GovernanceAccessService governance = mock(GovernanceAccessService.class);
final NaoAnalysisRunner runner = mock(NaoAnalysisRunner.class);
final AppUserPrincipal actor = new AppUserPrincipal(UUID.fromString("32cd8848-cfa1-4b10-8d2f-91c7bf433103"),
"runtime-review", "unused", "隔离测试", true, false, List.of("QUALITY_VIEWER"), List.of("D"), List.of("G"), List.of());
"runtime-review", "unused", "隔离测试", true, false, List.of("USER"), List.of("D"), List.of("G"), List.of());
final JsonMapper json = JsonMapper.builder().build();
final AnalysisTurnService service;
final Map<String, Long> firstEvents = new LinkedHashMap<>();
@@ -25,7 +25,7 @@ class AnalysisProtocolServiceMySqlTest {
private String database;
private boolean created;
private final String session=UUID.randomUUID().toString(),release=UUID.randomUUID().toString();
private final AppUserPrincipal actor=new AppUserPrincipal(UUID.randomUUID(),"protocol-test","unused","测试",true,false,List.of("QUALITY_VIEWER"),List.of(),List.of(),List.of());
private final AppUserPrincipal actor=new AppUserPrincipal(UUID.randomUUID(),"protocol-test","unused","测试",true,false,List.of("USER"),List.of(),List.of(),List.of());
private final GovernanceAccessService governance=mock(GovernanceAccessService.class);
private final OptionsController options=mock(OptionsController.class);
private final AnalysisToolsController tools=mock(AnalysisToolsController.class);
@@ -67,7 +67,7 @@ class AnalysisSemanticsTest {
@Test void modelIntentResolvesAutomotiveAliasToAllAuthorizedGroupsAndDefaultsGenericRateToComposite() throws Exception {
AppUserPrincipal actor = new AppUserPrincipal(UUID.randomUUID(), "semantic-test", "unused", "Semantic Test", true,
false, List.of("BUSINESS_ANALYST"), List.of("工业电机产业部", "新能源汽车磁钢", "欧洲磁钢", "消费电子产业部", "美洲产业部"),
false, List.of("USER"), List.of("工业电机产业部", "新能源汽车磁钢", "欧洲磁钢", "消费电子产业部", "美洲产业部"),
List.of("汽车交通-国内", "汽车交通-海外"), List.of());
List<Map<String,Object>> catalog = List.of(Map.of("code", "COMPOSITE_RATE", "name", "综合合格率", "enabled", true),
Map.of("code", "BLACK_RATE", "name", "黑片合格率", "enabled", true),
@@ -110,7 +110,7 @@ class AnalysisSemanticsTest {
@Test void modelCannotSilentlyDropAnUnmappedDimension() throws Exception {
AppUserPrincipal actor = new AppUserPrincipal(UUID.randomUUID(), "semantic-test", "unused", "Semantic Test", true,
false, List.of("BUSINESS_ANALYST"), List.of("工业电机产业部"), List.of("汽车交通-国内"), List.of());
false, List.of("USER"), List.of("工业电机产业部"), List.of("汽车交通-国内"), List.of());
List<Map<String,Object>> catalog = List.of(Map.of("code", "COMPOSITE_RATE", "name", "综合合格率", "enabled", true));
JsonNode parsed = mapper.readTree("{\"intent\":\"metric\",\"metricCode\":\"COMPOSITE_RATE\",\"group\":\"其他业务组\"}");
assertThrows(IllegalArgumentException.class, () -> AnalysisModelGateway.decodeIntent(parsed,
@@ -12,7 +12,7 @@ import org.springframework.web.server.ResponseStatusException;
class AnalysisSourceQueryControllerTest {
@Test void aPolicyChangeWhileScanningPreventsReturningTheProjectedData() {
var user=new AppUserPrincipal(UUID.randomUUID(),"source-reader","unused","测试",true,false,List.of("QUALITY_VIEWER"),List.of(),List.of(),List.of());
var user=new AppUserPrincipal(UUID.randomUUID(),"source-reader","unused","测试",true,false,List.of("USER"),List.of(),List.of(),List.of());
var governance=mock(cn.yunsheng.quality.governance.GovernanceAccessService.class);
var users=mock(cn.yunsheng.quality.identity.JdbcAppUserDetailsService.class);
when(governance.allowedFields(user)).thenReturn(Set.of("*"));
@@ -222,7 +222,7 @@ class AnalysisTurnSnapshotTest {
private final MetricsService metrics = mock(MetricsService.class);
private final JdbcAppUserDetailsService users = mock(JdbcAppUserDetailsService.class);
private final GovernanceAccessService governance = mock(GovernanceAccessService.class);
private final AppUserPrincipal submittedActor = actor("submitted-user", "QUALITY_VIEWER");
private final AppUserPrincipal submittedActor = actor("submitted-user", "USER");
private final AppUserPrincipal freshActor = actor("fresh-user", "SYSTEM_ADMIN");
private final String fingerprint;
private final AnalysisTurnService service;
@@ -643,7 +643,7 @@ class AnalysisVisualSnapshotAccessTest {
JdbcTemplate jdbc = mock(JdbcTemplate.class);
GovernanceAccessService governance = mock(GovernanceAccessService.class);
AppUserPrincipal actor = new AppUserPrincipal(UUID.randomUUID(), "visual-test", "unused", "测试", true, false,
List.of("QUALITY_VIEWER"), List.of(), List.of(), List.of());
List.of("USER"), List.of(), List.of(), List.of());
when(governance.allowedFields(actor)).thenReturn(RATE_FIELDS);
doReturn(List.of()).when(jdbc).query(anyString(), any(RowMapper.class));
AnalysisTurnService service = new AnalysisTurnService(jdbc, JsonMapper.builder().build(), null, null,
@@ -43,7 +43,7 @@ class FineDefectRollupsMySqlTest {
jdbc.execute("CREATE TABLE data_release(id CHAR(36) PRIMARY KEY,name VARCHAR(100),status VARCHAR(32),published_at TIMESTAMP NULL,rule_version VARCHAR(100),mapping_version VARCHAR(100))");
jdbc.execute("CREATE TABLE release_member(release_id CHAR(36),source_revision_id CHAR(36),transform_build_id CHAR(36))");
for (String stage : List.of("black", "bright")) jdbc.execute("CREATE TABLE fact_" + stage + "(id BIGINT AUTO_INCREMENT PRIMARY KEY,build_id CHAR(36),source_revision_id CHAR(36),month_key CHAR(7),department VARCHAR(190),group_name VARCHAR(190),project_group VARCHAR(190),product VARCHAR(190),qualified_weight DECIMAL(38,8),scrap_weight DECIMAL(38,8),total_weight DECIMAL(38,8),raw_values JSON,components JSON,KEY ix_fact_" + stage + "_month_dims(build_id,month_key))");
for (String migration : List.of("V16__metric_rollups.sql", "V36__fine_defect_rollups.sql")) {
for (String migration : List.of("V16__metric_rollups.sql", "V36__fine_defect_rollups.sql", "V39__sales_order_types.sql", "V40__sales_order_fact_indexes.sql", "V41__sales_order_metric_rollups.sql")) {
try (var stream = getClass().getResourceAsStream("/db/migration/" + migration)) {
for (String sql : new String(stream.readAllBytes(), java.nio.charset.StandardCharsets.UTF_8).split(";")) if (!sql.isBlank()) jdbc.execute(sql);
}
@@ -108,7 +108,7 @@ class FineDefectRollupsMySqlTest {
@Test void fieldAndOrganizationPermissionsRemainEffective() {
var viewer = new AppUserPrincipal(UUID.randomUUID(), "viewer", "", "Viewer", true, false,
List.of("QUALITY_VIEWER"), List.of("A"), List.of("G"), List.of());
List.of("USER"), List.of("A"), List.of("G"), List.of());
when(governance.allowedFields(viewer)).thenReturn(Set.of("责废尺寸重量"));
equivalent(request("black", Map.of()), viewer);
when(governance.allowedFields(actor)).thenReturn(Set.of("镀废缺角重量", "bright_plating_scrap"));
@@ -139,6 +139,27 @@ class FineDefectRollupsMySqlTest {
equivalent(request("bright", Map.of()), actor);
}
@Test void excludingASmallTypeSubtractsExactCountsAndWeightsAndLargeExclusionsKeepSourceFallback() {
String version=UUID.randomUUID().toString();
jdbc.update("INSERT INTO sales_order_type_version(id,source_file,source_sha256,import_mode,row_count,created_by) VALUES(?,'fixture',?,'REPLACE',2,?)",version,"0".repeat(64),actor.getId().toString());
jdbc.update("INSERT INTO sales_order_type_entry VALUES(?,'sample','样品销售订单'),(?,'normal','标准销售订单')",version,version);
jdbc.update("UPDATE sales_order_type_current SET version_id=?",version);
jdbc.execute("CREATE TABLE query_filter_field(field_key VARCHAR(64),label VARCHAR(80),black_column VARCHAR(160),bright_column VARCHAR(160),enabled BOOLEAN,allow_empty BOOLEAN,sort_order INT)");
for(String stage:List.of("black","bright")) jdbc.update("UPDATE fact_"+stage+" SET raw_values=JSON_SET(raw_values,?,CASE WHEN id=1 THEN 'sample' WHEN id=2 THEN NULL ELSE 'normal' END)","$.\""+cn.yunsheng.quality.filters.SalesOrderTypes.column(stage)+"\"");
var typed=new cn.yunsheng.quality.metrics.SalesOrderMetricRollups(jdbc);typed.prepareBuild(version,build);
for(String stage:List.of("black","bright")) for(var selection:List.of(List.of("样品销售订单"),List.of("标准销售订单"),List.of("__EMPTY__"),List.of("样品销售订单","__EMPTY__"))) {
for(Map<String,Object> scope:List.<Map<String,Object>>of(Map.of(),Map.of("includeUnassigned",false),Map.of("department","A"),Map.of("productSearch","P0"),Map.of("month",1))) {
var input=new LinkedHashMap<>(scope);input.put("extraFilters",Map.of("salesOrderTypeNotIn",selection));
var query=request(stage,input);
assertEquals(canonical(source.query(query,actor)),canonical(fast.query(query,actor)),stage+selection+scope);
}
}
// An unavailable fine-defect snapshot must use the original complete query.
jdbc.update("DELETE FROM fine_defect_rollup_build");
var query=request("black",Map.of("extraFilters",Map.of("salesOrderTypeNotIn",List.of("样品销售订单"))));
assertEquals(canonical(source.query(query,actor)),canonical(fast.query(query,actor)));
}
@AfterEach void cleanup() {
if (admin != null && database != null && database.matches("quality_fine_rollup_[a-f0-9]{32}")) admin.execute("DROP DATABASE " + database);
}
@@ -40,7 +40,7 @@ class NaoAnalysisRunnerRevocationTest {
return null;
}).when(access).authorizeModel("offline-delegation");
AppUserPrincipal actor = new AppUserPrincipal(UUID.randomUUID(), "offline", "unused", "测试", true, false,
List.of("QUALITY_VIEWER"), List.of("D"), List.of(), List.of());
List.of("USER"), List.of("D"), List.of(), List.of());
NaoAnalysisRunner runner = new NaoAnalysisRunner(JsonMapper.builder().build(), access);
try {
System.setProperty("quality.project.root", root.toString());
@@ -23,7 +23,7 @@ class NaoProtocolRunnerTest {
print(json.dumps({'type':'result','status':'COMPLETED','answer':'verified','stopReason':'stop'}),flush=True)
""");
var access=mock(NaoRuntimeAccessService.class);when(access.issueProtocol(any(),anyString(),anyString(),anyString())).thenReturn("synthetic-token");
var actor=new AppUserPrincipal(UUID.randomUUID(),"test","unused","测试",true,false,List.of("QUALITY_VIEWER"),List.of(),List.of(),List.of());
var actor=new AppUserPrincipal(UUID.randomUUID(),"test","unused","测试",true,false,List.of("USER"),List.of(),List.of(),List.of());
var runner=new NaoAnalysisRunner(JsonMapper.builder().build(),access);
String prior=System.getProperty("quality.project.root"),command=System.getProperty("quality.analysis.nao.command");
try {
@@ -100,8 +100,8 @@ class NaoRuntimeAccessServiceTest {
private final AtomicInteger policyVersion = new AtomicInteger(1);
private final MutableClock clock = new MutableClock(Instant.parse("2026-10-22T04:00:00Z"));
private final AppUserPrincipal actor = new AppUserPrincipal(USER_ID, "quality-user", "hash", "质量用户", true,
false, List.of("QUALITY_VIEWER"), List.of("D-1"), List.of("G-1"),
List.of(new SimpleGrantedAuthority("ROLE_QUALITY_VIEWER")));
false, List.of("USER"), List.of("D-1"), List.of("G-1"),
List.of(new SimpleGrantedAuthority("ROLE_USER")));
private final JdbcTemplate jdbc = mock(JdbcTemplate.class);
private final JdbcAppUserDetailsService users = mock(JdbcAppUserDetailsService.class);
private final GovernanceAccessService governance = mock(GovernanceAccessService.class);
@@ -55,8 +55,8 @@ class NaoRuntimeQueryControllerTest {
org.mockito.Mockito.verify(protocol,org.mockito.Mockito.never()).recordQuery(org.mockito.ArgumentMatchers.anyString(),org.mockito.ArgumentMatchers.anyString(),org.mockito.ArgumentMatchers.anyString(),org.mockito.ArgumentMatchers.anyMap(),org.mockito.ArgumentMatchers.any(),org.mockito.ArgumentMatchers.any(),org.mockito.ArgumentMatchers.any(),org.mockito.ArgumentMatchers.any());
}
private static final AppUserPrincipal ACTOR = new AppUserPrincipal(UUID.randomUUID(), "viewer", "hash", "查看者",
true, false, List.of("QUALITY_VIEWER"), List.of(), List.of(),
List.of(new SimpleGrantedAuthority("ROLE_QUALITY_VIEWER")));
true, false, List.of("USER"), List.of(), List.of(),
List.of(new SimpleGrantedAuthority("ROLE_USER")));
@Test void delegatedModelAuthorizationRechecksTheBoundModelService() {
Fixture fixture = new Fixture();
@@ -9,8 +9,10 @@ import org.junit.jupiter.api.Test;
class NaoSemanticCompletionTest {
@Test void historyCompletionNeedsCurrentImportAndCompleteCoverage() {
var message=new LinkedHashMap<String,Object>(Map.of("type","result","status","COMPLETED","answer","","stopReason","history_report_complete","finishReason","stop","completionMethod","confirmed_history_report","agentTask",Map.of("kind","report"),"historyEvidence",Map.of("importId","current"),"historyCoverage",Map.of("complete",true)));
var message=new LinkedHashMap<String,Object>(Map.of("type","result","status","COMPLETED","answer","历史报告证据已完整核对。","stopReason","history_report_complete","finishReason","stop","completionMethod","confirmed_history_report","agentTask",Map.of("kind","report"),"historyEvidence",Map.of("importId","current"),"historyCoverage",Map.of("complete",true)));
assertEquals("COMPLETED",NaoAnalysisRunner.Result.from(message,"",false).status());
var blank = new LinkedHashMap<>(message); blank.put("answer", "");
assertEquals("PARTIAL", NaoAnalysisRunner.Result.from(blank,"",false).status());
message.put("historyCoverage",Map.of("complete",false));assertEquals("PARTIAL",NaoAnalysisRunner.Result.from(message,"",false).status());
message.put("historyCoverage",Map.of("complete",true));message.remove("historyEvidence");assertEquals("PARTIAL",NaoAnalysisRunner.Result.from(message,"",false).status());
}
@@ -0,0 +1,45 @@
package cn.yunsheng.quality.analysis;
import cn.yunsheng.quality.governance.GovernanceAccessService;
import cn.yunsheng.quality.identity.AppUserPrincipal;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import org.junit.jupiter.api.Test;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import tools.jackson.databind.json.JsonMapper;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.*;
import static org.mockito.ArgumentMatchers.*;
class RoleMigrationFingerprintTest {
@Test @SuppressWarnings("unchecked") void roleRenamePreservesHistoryWhileRealGrantChangesStillInvalidateIt() {
JdbcTemplate jdbc = mock(JdbcTemplate.class);
List<String> oldRoles = List.of("SYSTEM_ADMIN","QUALITY_VIEWER","BUSINESS_ANALYST","DATA_MAINTAINER","DATA_PUBLISHER","RULE_MAINTAINER","SYSTEM_OPERATOR","AUDIT_VIEWER");
when(jdbc.query(contains("user_role_migration_anchor"),any(RowMapper.class),any(Object[].class)))
.thenReturn(List.of(Map.of("role","SYSTEM_ADMIN","previous",oldRoles)));
when(jdbc.query(contains("FROM user_access_policy"),any(RowMapper.class),any(Object[].class))).thenReturn(List.of());
GovernanceAccessService access = new GovernanceAccessService(jdbc,JsonMapper.builder().build());
UUID id=UUID.randomUUID();
String original=AuthorizationFingerprint.of(user(id,oldRoles,List.of("A"),List.of("G")),access);
assertEquals(original,AuthorizationFingerprint.of(user(id,List.of("SYSTEM_ADMIN"),List.of("A"),List.of("G")),access));
assertNotEquals(original,AuthorizationFingerprint.of(user(id,List.of("USER"),List.of("A"),List.of("G")),access));
assertNotEquals(original,AuthorizationFingerprint.of(user(id,List.of("SYSTEM_ADMIN"),List.of("B"),List.of("G")),access));
assertNotEquals(original,AuthorizationFingerprint.of(user(id,List.of("SYSTEM_ADMIN"),List.of("A"),List.of("H")),access));
when(jdbc.query(contains("FROM user_access_policy"),any(RowMapper.class),any(Object[].class)))
.thenReturn(List.of(Map.of("detailAllowed",false,"evidenceAllowed",false,"originalDownloadAllowed",false,"allowedFields",List.of("product"),"rowVersion",1L)));
assertNotEquals(original,AuthorizationFingerprint.of(user(id,List.of("SYSTEM_ADMIN"),List.of("A"),List.of("G")),access));
}
@Test @SuppressWarnings("unchecked") void inconsistentAnchorCannotKeepAnOldAdministratorFingerprintForAUser() {
JdbcTemplate jdbc=mock(JdbcTemplate.class);
when(jdbc.query(anyString(),any(RowMapper.class),any(Object[].class))).thenReturn(List.of(Map.of("role","USER","previous",List.of("SYSTEM_ADMIN"))));
GovernanceAccessService access=new GovernanceAccessService(jdbc,JsonMapper.builder().build());
assertEquals(List.of("USER"),access.fingerprintRoles(user(UUID.randomUUID(),List.of("USER"),List.of("A"),List.of())));
}
private AppUserPrincipal user(UUID id,List<String> roles,List<String> departments,List<String> groups) {
return new AppUserPrincipal(id,"test","unused","test",true,false,roles,departments,groups,List.of());
}
}
@@ -16,15 +16,15 @@ class ComparisonViewSharingServiceTest {
void legacyDepartmentSpellingUsesTheSameSharingScope() {
var policy = Map.<String,Object>of("detailAllowed", true, "evidenceAllowed", false,
"originalDownloadAllowed", false, "inheritDepartmentChildren", false);
var oldName = principal(List.of("QUALITY_VIEWER"), List.of("质量管理部"), List.of());
var currentName = principal(List.of("QUALITY_VIEWER"), List.of("质量中心"), List.of());
var oldName = principal(List.of("USER"), List.of("质量管理部"), List.of());
var currentName = principal(List.of("USER"), List.of("质量中心"), List.of());
assertEquals(ComparisonViewSharingService.semanticFingerprint(oldName, Set.of("*"), policy),
ComparisonViewSharingService.semanticFingerprint(currentName, Set.of("*"), policy));
}
@Test
void equivalentAuthorizationSemanticsIgnorePolicyEditCountAndCollectionOrder() {
AppUserPrincipal first = principal(List.of("QUALITY_VIEWER", "BUSINESS_ANALYST"), List.of("部二", "部一"), List.of("组B", "组A"));
AppUserPrincipal second = principal(List.of("BUSINESS_ANALYST", "QUALITY_VIEWER"), List.of("部一", "部二"), List.of("组A", "组B"));
AppUserPrincipal first = principal(List.of("USER"), List.of("部二", "部一"), List.of("组B", "组A"));
AppUserPrincipal second = principal(List.of("USER"), List.of("部一", "部二"), List.of("组A", "组B"));
String firstFingerprint = ComparisonViewSharingService.semanticFingerprint(first, Set.of("序号", "批次"),
Map.of("detailAllowed", true, "evidenceAllowed", false, "originalDownloadAllowed", false,
@@ -38,8 +38,8 @@ class ComparisonViewSharingServiceTest {
@Test
void changedOrganizationFieldOrPolicySemanticsCannotShare() {
AppUserPrincipal owner = principal(List.of("QUALITY_VIEWER"), List.of("部一"), List.of("组A"));
AppUserPrincipal narrowed = principal(List.of("QUALITY_VIEWER"), List.of("部二"), List.of("组A"));
AppUserPrincipal owner = principal(List.of("USER"), List.of("部一"), List.of("组A"));
AppUserPrincipal narrowed = principal(List.of("USER"), List.of("部二"), List.of("组A"));
Map<String, Object> policy = Map.of("detailAllowed", true, "evidenceAllowed", false,
"originalDownloadAllowed", false, "inheritDepartmentChildren", false, "validUntil", "2026-12-31T00:00:00Z");
String original = ComparisonViewSharingService.semanticFingerprint(owner, Set.of("序号"), policy);
@@ -14,7 +14,7 @@ class WorkspaceStateControllerTest {
final WorkspaceStateController controller = new WorkspaceStateController(jdbc, access);
final UUID id = UUID.randomUUID();
AppUserPrincipal actor(List<String> departments) {
return new AppUserPrincipal(id,"test","","test",true,false,List.of("QUALITY_VIEWER"),departments,List.of("*"),List.of());
return new AppUserPrincipal(id,"test","","test",true,false,List.of("USER"),departments,List.of("*"),List.of());
}
@Test void stableDigestIgnoresScopeOrderAndDoesNotExposePolicy() {
when(jdbc.queryForList(contains("user_access_policy"), any(Object[].class))).thenReturn(List.of(Map.of("allowed_fields","private-field")));
@@ -33,7 +33,7 @@ class SourceColumnQueriesMySqlTest {
private boolean created;
private final GovernanceAccessService governance = mock(GovernanceAccessService.class);
private final AppUserPrincipal actor = new AppUserPrincipal(UUID.randomUUID(), "source-column-test", "", "测试",
true, false, List.of("QUALITY_VIEWER"), List.of("部门A"), List.of("组A"), List.of());
true, false, List.of("USER"), List.of("部门A"), List.of("组A"), List.of());
private SourceColumnQueries queries;
@BeforeEach void setup() {
@@ -19,7 +19,7 @@ class SourceColumnQueriesTest {
private static final String SOURCE = "src_0123456789abcdef";
private final GovernanceAccessService governance = mock(GovernanceAccessService.class);
private final AppUserPrincipal actor = new AppUserPrincipal(UUID.randomUUID(), "source-test", "", "test",
true, false, List.of("QUALITY_VIEWER"), List.of(), List.of(), List.of());
true, false, List.of("USER"), List.of(), List.of(), List.of());
@Test void parsesJsonStringAndKeepsLeadingZeroAndInjectionTextAsExactStrings() {
List<Map<String, Object>> parsed = SourceColumnQueries.parse(
@@ -37,7 +37,7 @@ class GovernanceAccessServiceTest {
when(jdbc.query(anyString(),any(RowMapper.class),any(Object[].class))).thenAnswer(call->call.getArgument(0,String.class).startsWith("SELECT detail_allowed")?List.of(Map.of("inheritDepartmentChildren",true)):List.of(definition));
when(jdbc.query(anyString(),any(RowMapper.class))).thenReturn(List.of(definition));
GovernanceAccessService service=new GovernanceAccessService(jdbc,JsonMapper.builder().build());
AppUserPrincipal original=new AppUserPrincipal(UUID.randomUUID(),"reader","unchanged","Reader",true,false,List.of("QUALITY_VIEWER"),List.of("parent"),List.of("G1"),List.of());
AppUserPrincipal original=new AppUserPrincipal(UUID.randomUUID(),"reader","unchanged","Reader",true,false,List.of("USER"),List.of("parent"),List.of("G1"),List.of());
AppUserPrincipal expanded=service.resolvePrincipal(original);
assertEquals(List.of("parent"),original.getDepartments());assertEquals(Set.of("parent","child"),new HashSet<>(expanded.getDepartments()));
assertEquals(original.getId(),expanded.getId());assertEquals(original.getRoleCodes(),expanded.getRoleCodes());assertEquals(original.getGroups(),expanded.getGroups());
@@ -47,7 +47,7 @@ class GovernanceAccessServiceTest {
when(jdbc.query(anyString(),any(RowMapper.class),any(Object[].class))).thenReturn(List.of(Map.of("inheritDepartmentChildren",true)));
when(jdbc.query(anyString(),any(RowMapper.class))).thenReturn(List.of(definition));
GovernanceAccessService service=new GovernanceAccessService(jdbc,JsonMapper.builder().build());
AppUserPrincipal reader=new AppUserPrincipal(UUID.randomUUID(),"reader","unused","Reader",true,false,List.of("QUALITY_VIEWER"),List.of("p"),List.of(),List.of());
AppUserPrincipal reader=new AppUserPrincipal(UUID.randomUUID(),"reader","unused","Reader",true,false,List.of("USER"),List.of("p"),List.of(),List.of());
assertThrows(ResponseStatusException.class,()->service.resolvePrincipal(reader));
}
}
@@ -18,7 +18,7 @@ import static org.mockito.ArgumentMatchers.*;
import static org.mockito.Mockito.*;
class SourceChangeAuthorizationBoundaryTest {
private AppUserPrincipal actor(){return new AppUserPrincipal(UUID.randomUUID(),"a","unused","A",true,false,List.of("DATA_MAINTAINER","DATA_PUBLISHER"),List.of("A"),List.of(),List.of());}
private AppUserPrincipal actor(){return new AppUserPrincipal(UUID.randomUUID(),"a","unused","A",true,false,List.of("DATA_ADMIN"),List.of("A"),List.of(),List.of());}
private SourceAuthorizationService denyingAccess(){SourceAuthorizationService access=mock(SourceAuthorizationService.class);doThrow(new ResponseStatusException(HttpStatus.FORBIDDEN,"B is outside scope")).when(access).requireSource(eq("doc-b"),eq("rev-b"),any(AppUserPrincipal.class));return access;}
private GovernanceSupport support(JdbcTemplate jdbc){return new GovernanceSupport(jdbc,JsonMapper.builder().build());}
@@ -13,7 +13,7 @@ import static org.mockito.ArgumentMatchers.*;
import static org.mockito.Mockito.*;
class SourceSecurityRegressionTest {
private AppUserPrincipal reader(String department){return new AppUserPrincipal(UUID.randomUUID(),"reader","unused","Reader",true,false,List.of("DATA_MAINTAINER","DATA_PUBLISHER"),List.of(department),List.of(),List.of());}
private AppUserPrincipal reader(String department){return new AppUserPrincipal(UUID.randomUUID(),"reader","unused","Reader",true,false,List.of("DATA_ADMIN"),List.of(department),List.of(),List.of());}
@Test @SuppressWarnings("unchecked") void parsedAuthorizedRowsCannotGrantWholeWorkbookDownload(){
JdbcTemplate jdbc=mock(JdbcTemplate.class);
@@ -35,6 +35,17 @@ class SourceSecurityRegressionTest {
assertFalse(SourceCoverageGuard.overlaps(old,nextMonth));
}
@Test void uploadAppendOnlyCoexistsWithExactScopeMonthlySnapshotAndOtherAppendBatches(){
var monthly=new SourceCoverageGuard.Coverage("black",2026,2,"质量管理部","MONTHLY_SNAPSHOT","sha-month","black|2026|02|质量管理部");
var appendA=new SourceCoverageGuard.Coverage("black",2026,2,"质量中心","APPEND_SNAPSHOT","sha-a","black|2026|02|质量管理部|UPLOAD_A");
var appendB=new SourceCoverageGuard.Coverage("black",2026,2,"质量管理部","APPEND_SNAPSHOT","sha-b","black|2026|02|质量管理部|UPLOAD_B");
assertFalse(SourceCoverageGuard.overlaps(monthly,appendA));
assertFalse(SourceCoverageGuard.overlaps(appendA,appendB));
assertTrue(SourceCoverageGuard.overlaps(monthly,new SourceCoverageGuard.Coverage("black",2026,2,"ALL","APPEND_SNAPSHOT","sha-c","black|2026|02|ALL|UPLOAD_C")));
assertTrue(SourceCoverageGuard.overlaps(monthly,new SourceCoverageGuard.Coverage("black",2026,2,"质量管理部","APPEND_SNAPSHOT","sha-month","black|2026|02|质量管理部|UPLOAD_D")));
assertTrue(SourceCoverageGuard.overlaps(monthly,new SourceCoverageGuard.Coverage("black",2026,2,"质量管理部","SHARD_SNAPSHOT","sha-shard","black|2026|02|质量管理部|P1")));
}
@Test @SuppressWarnings("unchecked") void localMaintainerCannotRemoveAnotherDepartmentsSource(){
JdbcTemplate jdbc=mock(JdbcTemplate.class);
when(jdbc.query(anyString(),any(RowMapper.class),any(Object[].class))).thenReturn(List.of("B"));
@@ -0,0 +1,34 @@
package cn.yunsheng.quality.identity;
import java.util.Arrays;
import java.util.List;
import org.junit.jupiter.api.Test;
import org.springframework.web.server.ResponseStatusException;
import static org.junit.jupiter.api.Assertions.*;
class AppRolesTest {
@Test void exactlyOneOfThreeRolesCanBeAssigned() {
for (String role : AppRoles.CODES) assertEquals(List.of(role), AppRoles.validate(List.of(role)));
for (List<String> roles : List.of(List.<String>of(), List.of("USER", "DATA_ADMIN"),
List.of("USER", "USER"), List.of("QUALITY_VIEWER"), Arrays.asList((String) null)))
assertEquals(400, assertThrows(ResponseStatusException.class, () -> AppRoles.validate(roles)).getStatusCode().value());
assertThrows(ResponseStatusException.class, () -> AppRoles.validate(null));
}
@Test void operationAuthoritiesAreInheritedWhileAssignedRoleStaysSingle() {
assertEquals(List.of("ROLE_USER"), codes("USER"));
assertEquals(List.of("ROLE_DATA_ADMIN", "ROLE_USER"), codes("DATA_ADMIN"));
assertEquals(List.of("ROLE_SYSTEM_ADMIN", "ROLE_DATA_ADMIN", "ROLE_USER"), codes("SYSTEM_ADMIN"));
assertEquals(List.of("DATA_ADMIN"), AppRoles.validate(List.of("DATA_ADMIN")));
}
@Test void unknownOrMultipleAssignmentsCannotGainAuthorities() {
assertTrue(AppRoles.authorities(List.of("QUALITY_VIEWER")).isEmpty());
assertTrue(AppRoles.authorities(List.of("USER", "SYSTEM_ADMIN")).isEmpty());
assertTrue(AppRoles.authorities(Arrays.asList((String) null)).isEmpty());
}
private List<String> codes(String role) {
return AppRoles.authorities(List.of(role)).stream().map(a -> a.getAuthority()).toList();
}
}
@@ -45,7 +45,7 @@ class CurrentUserRefreshFilterTest {
@Test void refreshesRolesAndScopesFromTheCurrentUserRecord() throws Exception {
UUID id = UUID.randomUUID();
AppUserPrincipal old = principal(id, List.of("SYSTEM_ADMIN"), List.of("*"), List.of("*"), true);
AppUserPrincipal current = principal(id, List.of("QUALITY_VIEWER"), List.of("质量管理部"), List.of("VCM"), true);
AppUserPrincipal current = principal(id, List.of("USER"), List.of("质量管理部"), List.of("VCM"), true);
JdbcAppUserDetailsService users = mock(JdbcAppUserDetailsService.class);
when(users.loadUserByUsername("alice")).thenReturn(current);
SecurityContextRepository repository = mock(SecurityContextRepository.class);
@@ -60,14 +60,14 @@ class CurrentUserRefreshFilterTest {
assertTrue(continued.get());
AppUserPrincipal refreshed = (AppUserPrincipal) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
assertEquals(List.of("QUALITY_VIEWER"), refreshed.getRoleCodes());
assertEquals(List.of("USER"), refreshed.getRoleCodes());
assertEquals(List.of("质量管理部"), refreshed.getDepartments());
assertEquals(List.of("VCM"), refreshed.getGroups());
verify(repository).saveContext(any(), eq(request), eq(response));
}
@Test void rejectsAndInvalidatesARevokedSessionBeforeTheRequestRuns() throws Exception {
AppUserPrincipal old = principal(UUID.randomUUID(), List.of("QUALITY_VIEWER"), List.of("*"), List.of("*"), true);
AppUserPrincipal old = principal(UUID.randomUUID(), List.of("USER"), List.of("*"), List.of("*"), true);
JdbcAppUserDetailsService users = mock(JdbcAppUserDetailsService.class);
when(users.loadUserByUsername("alice")).thenThrow(new UsernameNotFoundException("inactive"));
SecurityContextHolder.getContext().setAuthentication(UsernamePasswordAuthenticationToken.authenticated(
@@ -87,7 +87,7 @@ class CurrentUserRefreshFilterTest {
}
@Test void expiresAnExistingSessionBeforeAnyBusinessRequestRuns() throws Exception {
AppUserPrincipal current = principal(UUID.randomUUID(), List.of("QUALITY_VIEWER"), List.of("*"), List.of("*"), true);
AppUserPrincipal current = principal(UUID.randomUUID(), List.of("USER"), List.of("*"), List.of("*"), true);
JdbcAppUserDetailsService users = mock(JdbcAppUserDetailsService.class);
when(users.loadUserByUsername("alice")).thenReturn(current);
GovernanceAccessService access = mock(GovernanceAccessService.class);
@@ -111,8 +111,8 @@ class CurrentUserRefreshFilterTest {
@Test void usesTheServerResolvedHierarchyForTheRefreshedIdentity() throws Exception {
UUID id = UUID.randomUUID();
AppUserPrincipal current = principal(id, List.of("QUALITY_VIEWER"), List.of("parent"), List.of(), true);
AppUserPrincipal expanded = principal(id, List.of("QUALITY_VIEWER"), List.of("parent", "child"), List.of(), true);
AppUserPrincipal current = principal(id, List.of("USER"), List.of("parent"), List.of(), true);
AppUserPrincipal expanded = principal(id, List.of("USER"), List.of("parent", "child"), List.of(), true);
JdbcAppUserDetailsService users = mock(JdbcAppUserDetailsService.class);
when(users.loadUserByUsername("alice")).thenReturn(current);
GovernanceAccessService access = mock(GovernanceAccessService.class);
@@ -26,12 +26,12 @@ class ImportBeforeImageAuthorizationTest {
}
private GovernanceAccessService access(){GovernanceAccessService access=mock(GovernanceAccessService.class);when(access.resolvePrincipal(any())).thenAnswer(call->call.getArgument(0));return access;}
@Test void historicalCandidateCannotReplaceCurrentBeforeImageOutsideMaintainerScope(){
JdbcTemplate jdbc=jdbc("B","DATA_MAINTAINER");
JdbcTemplate jdbc=jdbc("B","DATA_ADMIN");
var denied=assertThrows(ResponseStatusException.class,()->ImportRunService.requireBeforeImages(jdbc,access(),List.of("historical-candidate-with-only-A-rows"),actor,"current-release"));
assertEquals(HttpStatus.FORBIDDEN,denied.getStatusCode());verify(jdbc,never()).update(anyString(),any(Object[].class));
}
@Test void currentRevisionIsCheckedInsteadOfHistoricalCandidateRevision(){
JdbcTemplate jdbc=jdbc("A","DATA_MAINTAINER");SourceAuthorizationService sourceAccess=mock(SourceAuthorizationService.class);
JdbcTemplate jdbc=jdbc("A","DATA_ADMIN");SourceAuthorizationService sourceAccess=mock(SourceAuthorizationService.class);
ImportRunService.requireBeforeImages(jdbc,access(),sourceAccess,List.of("historical-candidate"),actor,"current-release");
verify(sourceAccess).requireSource(eq("current-doc"),eq("current-revision"),argThat(user->user.getDepartments().equals(List.of("A"))));
}
@@ -0,0 +1,126 @@
package cn.yunsheng.quality.ingestion;
import cn.yunsheng.quality.analytics.TargetVersionService;
import cn.yunsheng.quality.governance.*;
import cn.yunsheng.quality.metrics.MetricsRollupService;
import cn.yunsheng.quality.source.*;
import java.nio.file.Path;
import java.util.*;
import java.util.function.Consumer;
import org.junit.jupiter.api.Test;
import org.springframework.jdbc.core.*;
import org.springframework.test.util.ReflectionTestUtils;
import tools.jackson.databind.json.JsonMapper;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.*;
import static org.mockito.ArgumentMatchers.*;
class ImportUnassignedRowsTest {
@Test
@org.junit.jupiter.api.condition.EnabledIfEnvironmentVariable(named="QUALITY_UNASSIGNED_REPLAY_INPUT", matches=".+")
@SuppressWarnings("unchecked")
void failedDecemberWorkbookKeepsAllRowsAndItsTwelveUnassignedMappings() throws Exception {
JsonMapper json = JsonMapper.builder().build();
Map<String,Object> input = json.readValue(java.nio.file.Files.readString(Path.of(System.getenv("QUALITY_UNASSIGNED_REPLAY_INPUT"))), Map.class);
Map<String,Object> mapping = (Map<String,Object>) input.get("mapping");
Map<String,Object> config = (Map<String,Object>) input.get("parserConfig");
JdbcTemplate jdbc = mock(JdbcTemplate.class);
SourceScanner scanner = mock(SourceScanner.class);
Path file = Path.of((String) input.get("archivePath"));
when(scanner.archiveRevision(any())).thenReturn(file);
when(jdbc.queryForObject(anyString(), eq(String.class), any(Object[].class))).thenAnswer(call -> {
String sql = call.getArgument(0);
return sql.contains("CAST(parser_config") ? json.writeValueAsString(config) : sql.contains("parser_config_hash") ? input.get("parserConfigHash") : sql.contains("scope_code") ? "ALL" : "actor";
});
when(jdbc.query(anyString(), any(RowMapper.class), any(Object[].class))).thenAnswer(call -> {
String sql = call.getArgument(0);
return sql.startsWith("SELECT id FROM source_document") ? List.of("doc") : sql.startsWith("SELECT id FROM source_revision") ? List.of("revision") : List.of();
});
int[] counters = {0, 0, -1};
Set<Integer> unmappedRows = new TreeSet<>();
doAnswer(call -> {
String sql = call.getArgument(0);
if (sql.contains("INSERT INTO fact_bright")) {
counters[0]++;
if (call.getArgument(7) == null) counters[1]++;
Map<String,Object> components = json.readValue(call.getArgument(14, String.class), Map.class);
if ("unmapped".equals(components.get("categoryMappingStatus"))) unmappedRows.add(call.getArgument(4, Integer.class));
}
if (sql.contains("UPDATE transform_build SET status='COMPLETED'")) counters[2] = call.getArgument(2, Integer.class);
return 1;
}).when(jdbc).update(anyString(), any(Object[].class));
var rules = ConfigurableRuleEngine.compile((Map<String,Object>) input.get("rules"));
var mapper = new BusinessRowMapper(lookup(mapping,"categoryLookup"), lookup(mapping,"projectGroupLookup"), (String)input.get("mappingVersion"), lookup(mapping,"departmentLookup"), lookup(mapping,"productLookup"), rules, mapping);
var worker = new ImportWorker(jdbc, mock(SourceCatalogService.class), scanner, new XlsxStreamingReader(), json, mock(GovernanceAccessService.class), mock(TargetVersionService.class), mock(SourceCoverageGuard.class), mock(TaskProgressService.class), mock(CandidateAcceptanceService.class), mock(MappingIssueService.class), mock(MetricsRollupService.class));
var candidate = new SourceFileCandidate(UUID.randomUUID(), "replay", "2025:bright:12", "bright", 2025, 12, "12月亮片.xlsx", file, (String)input.get("sha256"), java.nio.file.Files.size(file), "DISCOVERED");
ReflectionTestUtils.invokeMethod(worker, "build", candidate, mapper, input.get("ruleVersion"), "replay-run");
assertArrayEquals(new int[]{18627,13,0}, counters, "all original rows survive, including unassigned rows; no blocking data errors");
assertEquals(Set.of(9104,11838,12576,12682,13205,13682,13773,13774,13945,14347,15419,17290), unmappedRows);
}
@SuppressWarnings("unchecked")
private Map<String,String> lookup(Map<String,Object> definition, String name) {
Map<String,String> result = new HashMap<>();
((Map<String,Object>)definition.getOrDefault(name, Map.of())).forEach((key,value) -> result.put(key, value == null ? null : MappingValueCodec.fromStorage(String.valueOf(value))));
return result;
}
@Test void missingBusinessGroupIsStoredWithoutBlockingEitherStage() throws Exception {
for (String stage : List.of("black", "bright")) checkBuild(stage, false, false);
}
@Test void unassignedRowsStillRejectInvalidNumbersAndMissingHeaders() throws Exception {
checkBuild("black", true, false);
checkBuild("bright", false, true);
}
@SuppressWarnings("unchecked")
private void checkBuild(String stage, boolean invalidNumber, boolean missingHeader) throws Exception {
JdbcTemplate jdbc = mock(JdbcTemplate.class);
SourceScanner scanner = mock(SourceScanner.class);
XlsxStreamingReader reader = mock(XlsxStreamingReader.class);
when(jdbc.queryForObject(anyString(), eq(String.class), any(Object[].class))).thenAnswer(call -> {
String sql = call.getArgument(0);
return sql.contains("CAST(parser_config") ? "{}" : sql.contains("parser_config_hash") ? "legacy" : sql.contains("scope_code") ? "ALL" : "actor";
});
when(jdbc.query(anyString(), any(RowMapper.class), any(Object[].class))).thenAnswer(call -> {
String sql = call.getArgument(0);
return sql.startsWith("SELECT id FROM source_document") ? List.of("doc") : sql.startsWith("SELECT id FROM source_revision") ? List.of("revision") : List.of();
});
when(scanner.archiveRevision(any())).thenReturn(Path.of("/unused"));
BusinessRowMapper mapper = new BusinessRowMapper(Map.of(), Map.of(), "mapping");
Map<String, XlsxStreamingReader.CellValue> cells = new LinkedHashMap<>();
for (String header : mapper.requiredHeaders(stage)) cells.put(header, cell("0"));
cells.put("PDT代码", cell("P001")); cells.put("销售片区", cell("欧洲事业部"));
cells.put("事业部", cell("新能源汽车磁钢")); cells.put("成品规格", cell("32.05X18X3.7+T"));
if (invalidNumber) cells.put("送电镀重量", cell("bad"));
if (missingHeader) cells.remove("晶界扩散报废合计重量");
String sheet = stage.equals("black") ? "报废明细" : "明细";
doAnswer(call -> {
Consumer<XlsxStreamingReader.RawRow> consumer = call.getArgument(3);
consumer.accept(new XlsxStreamingReader.RawRow(sheet, 9104, cells, Map.of("PDT代码", "P001", "销售片区", "欧洲事业部")));
return new XlsxStreamingReader.ReadSummary(sheet, 1);
}).when(reader).read(any(Path.class), anyString(), isNull(), any(Consumer.class));
List<List<Object>> facts = new ArrayList<>(), issues = new ArrayList<>();
int[] rejected = {-1};
doAnswer(call -> {
String sql = call.getArgument(0);
List<Object> args = Arrays.asList(call.getArguments()).subList(1, call.getArguments().length);
if (sql.contains("INSERT INTO fact_")) facts.add(new ArrayList<>(args));
if (sql.contains("INSERT INTO import_issue")) issues.add(new ArrayList<>(args));
if (sql.contains("UPDATE transform_build SET status='COMPLETED'")) rejected[0] = ((Number) args.get(1)).intValue();
return 1;
}).when(jdbc).update(anyString(), any(Object[].class));
ImportWorker worker = new ImportWorker(jdbc, mock(SourceCatalogService.class), scanner, reader, JsonMapper.builder().build(), mock(GovernanceAccessService.class), mock(TargetVersionService.class), mock(SourceCoverageGuard.class), mock(TaskProgressService.class), mock(CandidateAcceptanceService.class), mock(MappingIssueService.class), mock(MetricsRollupService.class));
var candidate = new SourceFileCandidate(UUID.randomUUID(), "key", "2025:" + stage + ":12", stage, 2025, 12, "synthetic.xlsx", Path.of("/unused"), "a".repeat(64), 1, "DISCOVERED");
ReflectionTestUtils.invokeMethod(worker, "build", candidate, mapper, "pq-dax-v1", "run");
assertEquals(1, facts.size());
assertNull(facts.getFirst().get(6), "unmatched business group must remain unassigned");
assertTrue(String.valueOf(facts.getFirst().get(12)).contains("欧洲事业部"), "original values are retained");
var unassigned = issues.stream().filter(issue -> "CATEGORY_MAPPING_MISSING".equals(issue.get(5))).findFirst().orElseThrow();
assertTrue(String.valueOf(unassigned.get(8)).contains("\"blocking\":false"));
assertEquals(invalidNumber || missingHeader ? 1 : 0, rejected[0]);
}
private XlsxStreamingReader.CellValue cell(String value) { return new XlsxStreamingReader.CellValue(value, value, "s"); }
}
@@ -11,6 +11,14 @@ import org.junit.jupiter.api.Test;
class TransformBuildKeyTest {
private static final String SAME_FILE_HASH = "a".repeat(64);
@Test
void previousBlockingMappingCacheIsNotReusedAfterUnassignedPolicyChange() throws Exception {
var source = source("2026:black:01", "black", 1);
String oldIdentity = String.join("\n", source.sha256(), source.sourceSlot(), "revision", source.stage(), "2026", "1", "parser", "rule", "mapping");
String oldKey = java.util.HexFormat.of().formatHex(java.security.MessageDigest.getInstance("SHA-256").digest(oldIdentity.getBytes(java.nio.charset.StandardCharsets.UTF_8)));
assertNotEquals(oldKey, TransformBuildKey.create(source, "revision", "parser", "rule", "mapping"));
}
@Test
void identicalWorkbookBytesAreSeparatedBySourcePeriodAndRevision() {
SourceFileCandidate january = source("2026:black:01", "black", 1);
@@ -30,6 +30,9 @@ class UnclassifiedMappingRebuildMySqlTest {
jdbc.execute("CREATE TABLE defect_component(id BIGINT AUTO_INCREMENT PRIMARY KEY,build_id CHAR(36),stage VARCHAR(16),sheet_name VARCHAR(100),physical_row_no INT,defect_name VARCHAR(100),weight DECIMAL(24,8),source_field VARCHAR(100))");
try(var stream=getClass().getResourceAsStream("/db/migration/V16__metric_rollups.sql")){for(String sql:new String(stream.readAllBytes(),java.nio.charset.StandardCharsets.UTF_8).split(";"))if(!sql.isBlank())jdbc.execute(sql);}
try(var stream=getClass().getResourceAsStream("/db/migration/V36__fine_defect_rollups.sql")){for(String sql:new String(stream.readAllBytes(),java.nio.charset.StandardCharsets.UTF_8).split(";"))if(!sql.isBlank())jdbc.execute(sql);}
for(String migration:List.of("V39__sales_order_types.sql","V40__sales_order_fact_indexes.sql","V41__sales_order_metric_rollups.sql")) {
try(var stream=getClass().getResourceAsStream("/db/migration/"+migration)){for(String sql:new String(stream.readAllBytes(),java.nio.charset.StandardCharsets.UTF_8).split(";"))if(!sql.isBlank())jdbc.execute(sql);}
}
jdbc.execute("CREATE TABLE import_run(id CHAR(36) PRIMARY KEY,idempotency_key VARCHAR(190),status VARCHAR(32),stage VARCHAR(80),requested_sources JSON,expected_current_release_id CHAR(36),created_by CHAR(36),processed BIGINT,total BIGINT,result_release_id CHAR(36),expected_mapping_version VARCHAR(100),expected_rule_version VARCHAR(100),workflow_operation_id CHAR(36),validation JSON,started_at TIMESTAMP(6),completed_at TIMESTAMP(6))");
jdbc.execute("CREATE TABLE release_protocol_dependency(release_id CHAR(36),protocol_id VARCHAR(100),PRIMARY KEY(release_id,protocol_id))");jdbc.execute("CREATE TABLE release_source_exclusion(release_id CHAR(36),source_document_id VARCHAR(100),request_id VARCHAR(100))");
jdbc.update("INSERT INTO data_release VALUES(?,'parent','PUBLISHED',NULL,NULL,'rule','old-map',NULL,TRUE,JSON_OBJECT(),?)",parent,UUID.randomUUID().toString());
@@ -15,7 +15,7 @@ import org.junit.jupiter.api.Test;
class MetricsResponseAccessTest {
final GovernanceAccessService governance = mock(GovernanceAccessService.class);
final AppUserPrincipal actor = new AppUserPrincipal(UUID.randomUUID(), "scope", "unused", "测试", true, false,
List.of("QUALITY_VIEWER"), List.of("D"), List.of(), List.of());
List.of("USER"), List.of("D"), List.of(), List.of());
final MetricStage stage = new MetricStage(new BigDecimal("90"), new BigDecimal("10"), new BigDecimal("100"),
new BigDecimal("0.9"), List.of(new DefectRow("责废", BigDecimal.TEN, BigDecimal.ONE)), BigDecimal.ONE, new BigDecimal("0.91"));
final MetricRow row = new MetricRow("x", "x", "D", "G", "P", null, new BigDecimal("0.81"), stage, stage, Map.of(), new BigDecimal("0.82"));
@@ -19,7 +19,7 @@ class MetricsTrendControllerTest {
MetricsService metrics = mock(MetricsService.class);
MetricsResponseAccess access = mock(MetricsResponseAccess.class);
AppUserPrincipal actor = new AppUserPrincipal(UUID.randomUUID(), "trend", "unused", "趋势", true, false,
List.of("QUALITY_VIEWER"), List.of("D"), List.of("G"), List.of());
List.of("USER"), List.of("D"), List.of("G"), List.of());
MetricsService.MetricTrendSnapshot response = new MetricsService.MetricTrendSnapshot(
Map.of(), Map.of(), List.of(), Map.of());
when(metrics.calculateTrend(any(), eq(actor))).thenReturn(response);
@@ -62,7 +62,7 @@ class SourceDeletionServiceTest {
verify(jdbc,never()).update(anyString(),any(Object[].class));verify(governance,never()).request(any(),any());
}
@Test void restrictedMaintainerCannotHideARevisionOutsideTheirOrganization(){
currentUsed=true;var service=service();var restricted=new AppUserPrincipal(actor.getId(),"A","","",true,false,List.of("DATA_MAINTAINER"),List.of("A"),List.of(),List.of());
currentUsed=true;var service=service();var restricted=new AppUserPrincipal(actor.getId(),"A","","",true,false,List.of("DATA_ADMIN"),List.of("A"),List.of(),List.of());
doThrow(new ResponseStatusException(HttpStatus.FORBIDDEN,"B outside scope")).when(sourceAccess).requireSource(eq("document"),eq("current-revision"),any());
assertEquals(HttpStatus.FORBIDDEN,assertThrows(ResponseStatusException.class,()->service.delete("candidate",new SourceDeletionService.DeleteRequest(pointer,0L,"current-revision","理由","key"),restricted)).getStatusCode());
verify(jdbc,never()).update(anyString(),any(Object[].class));
@@ -0,0 +1,284 @@
package cn.yunsheng.quality.source;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.mock;
import cn.yunsheng.quality.analytics.TargetVersionService;
import cn.yunsheng.quality.config.QualityProperties;
import cn.yunsheng.quality.governance.*;
import cn.yunsheng.quality.identity.AppRoles;
import cn.yunsheng.quality.identity.AppUserPrincipal;
import cn.yunsheng.quality.ingestion.*;
import cn.yunsheng.quality.metrics.MetricsRollupService;
import cn.yunsheng.quality.metrics.MetricsService;
import cn.yunsheng.quality.release.ReleaseService;
import java.io.ByteArrayOutputStream;
import java.math.BigDecimal;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.*;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.flywaydb.core.Flyway;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.condition.EnabledIfEnvironmentVariable;
import org.junit.jupiter.api.io.TempDir;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.http.HttpStatus;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.test.util.ReflectionTestUtils;
import org.springframework.transaction.support.TransactionTemplate;
import org.springframework.web.server.ResponseStatusException;
import tools.jackson.databind.json.JsonMapper;
/** Exercises the upload, import, release, and metric paths against a real isolated MySQL schema. */
@EnabledIfEnvironmentVariable(named="QUALITY_MULTIFILE_TEST_CREATE_DATABASE",matches="true")
class SourceUploadAppendMySqlTest {
@TempDir Path tempDir;
private JdbcTemplate admin,jdbc;
private DriverManagerDataSource dataSource;
private TransactionTemplate tx;
private String database;
private Path archiveRoot;
private UUID adminId;
private AppUserPrincipal adminActor;
private JsonMapper json;
private GovernanceAccessService access;
private SourceAuthorizationService sourceAccess;
private SourceScanner scanner;
private XlsxStreamingReader reader;
private SourceUploadService uploads;
private SourceCatalogService sources;
private ImportRunService imports;
private ImportWorker worker;
private SourceCoverageGuard coverage;
private ReleaseService releases;
private SourceGovernanceService governance;
private MetricsService metrics;
@BeforeEach void setup() throws Exception {
String serverUrl=System.getenv("QUALITY_MULTIFILE_TEST_MYSQL_SERVER_URL");
assertNotNull(serverUrl,"set the isolated local MySQL server URL");
assertTrue(serverUrl.startsWith("jdbc:mysql://127.0.0.1:")&&serverUrl.contains("/?"));
java.util.regex.Matcher port=java.util.regex.Pattern.compile("jdbc:mysql://127\\.0\\.0\\.1:(\\d+)/\\?").matcher(serverUrl);
assertTrue(port.find());assertNotEquals("53306",port.group(1),"tests must never connect to the formal MySQL port");
String user=System.getenv("QUALITY_MULTIFILE_TEST_MYSQL_USER"),password=System.getenv("QUALITY_MULTIFILE_TEST_MYSQL_PASSWORD");
admin=new JdbcTemplate(new DriverManagerDataSource(serverUrl,user,password));
database="quality_verify_multifile_"+UUID.randomUUID().toString().replace("-","");
admin.execute("CREATE DATABASE "+database+" CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci");
dataSource=new DriverManagerDataSource(serverUrl.replaceFirst("/\\?","/"+database+"?"),user,password);
Flyway.configure().dataSource(dataSource).locations("classpath:db/migration")
.placeholders(Map.of("legacyTimestampOffsetSeconds","0")).load().migrate();
jdbc=new JdbcTemplate(dataSource);tx=new TransactionTemplate(new DataSourceTransactionManager(dataSource));
archiveRoot=tempDir.resolve("archive");Files.createDirectories(archiveRoot);
adminId=UUID.randomUUID();
jdbc.update("INSERT INTO app_user(id,username,display_name,password_hash,active) VALUES(?,?,?,?,TRUE)",adminId.toString(),"multifile-test-admin","Multifile test admin","fixture-hash");
jdbc.update("INSERT INTO app_user_role(user_id,role_code) VALUES(?,'SYSTEM_ADMIN')",adminId.toString());
adminActor=new AppUserPrincipal(adminId,"multifile-test-admin","fixture-hash","Multifile test admin",true,false,List.of("SYSTEM_ADMIN"),List.of("*"),List.of("*"),AppRoles.authorities(List.of("SYSTEM_ADMIN")));
QualityProperties properties=new QualityProperties();properties.setArchiveRoot(archiveRoot);scanner=new SourceScanner(properties);reader=new XlsxStreamingReader();
json=JsonMapper.builder().build();access=new GovernanceAccessService(jdbc,json);sourceAccess=new SourceAuthorizationService(jdbc);
GovernanceSupport support=new GovernanceSupport(jdbc,json);sources=new SourceCatalogService(jdbc,scanner);coverage=new SourceCoverageGuard(jdbc);
uploads=new SourceUploadService(jdbc,scanner,reader,sourceAccess,access,json,support);
imports=new ImportRunService(jdbc,sources,json,access);
MetricsRollupService rollups=new MetricsRollupService(jdbc);
worker=new ImportWorker(jdbc,sources,scanner,reader,json,access,new TargetVersionService(jdbc),coverage,new TaskProgressService(jdbc),mock(CandidateAcceptanceService.class),mock(MappingIssueService.class),rollups);
ReflectionTestUtils.setField(worker,"testWorkflowMode",true);
releases=new ReleaseService(jdbc,json,mock(ApplicationEventPublisher.class),scanner,coverage,sourceAccess,mock(CandidateAcceptanceService.class));
ReflectionTestUtils.setField(releases,"testWorkflowMode",true);
governance=new SourceGovernanceService(support,imports,worker,coverage,sourceAccess,mock(CandidateAcceptanceService.class));
ReflectionTestUtils.setField(governance,"testWorkflowMode",true);
metrics=new MetricsService(jdbc,access);
}
@AfterEach void cleanup(){
if(admin!=null&&database!=null&&database.matches("quality_verify_multifile_[a-f0-9]{32}"))admin.execute("DROP DATABASE "+database);
}
@Test void mainAndTwoAppendFilesImportPublishReplaceWithdrawAndKeepHistory() throws Exception {
ConfiguredSource mainBlack=configure("main-black.xlsx","black","ADD",null,11,"main black",null);
ConfiguredSource mainBright=configure("main-bright.xlsx","bright","ADD",null,5,"main bright",null);
String initialCandidate=importSources(List.of(mainBlack.sourceId(),mainBright.sourceId()),null);
publish(initialCandidate,null,"publish-main");
String mainRelease=currentRelease();
Map<String,Member> originalMembers=members(mainRelease);
assertEquals(2,originalMembers.size());
assertWeight(mainRelease,11);
ConfiguredSource appendA=configure("append-a.xlsx","black","APPEND","batch-A",4,"append batch A",mainRelease);
Map<String,Object> retry=configureAgain(appendA,"black","APPEND","batch-A",4,"retry same file identity",mainRelease);
assertEquals(true,retry.get("reconfigured"));
assertEquals(appendA.sourceId(),sourceId(retry));
ConfiguredSource appendB=configure("append-b.xlsx","black","APPEND","batch-B",6,"append batch B",mainRelease);
String appendCandidate=importSources(List.of(appendA.sourceId(),appendB.sourceId()),mainRelease);
assertTrue(Boolean.TRUE.equals(jdbc.queryForObject("SELECT complete FROM data_release WHERE id=?",Boolean.class,appendCandidate)));
assertEquals(0,coverage.overlapCount(appendCandidate));
publish(appendCandidate,mainRelease,"publish-appends");
String appendRelease=currentRelease();
Map<String,Member> appendedMembers=members(appendRelease);
assertEquals(4,appendedMembers.size());
assertEquals(originalMembers.get("black|2026|02|ALL"),appendedMembers.get("black|2026|02|ALL"));
assertEquals(originalMembers.get("bright|2026|02|ALL"),appendedMembers.get("bright|2026|02|ALL"));
assertEquals("APPEND_SNAPSHOT",sourceMode("black|2026|02|ALL|UPLOAD_batch-A"));
assertEquals("APPEND_SNAPSHOT",sourceMode("black|2026|02|ALL|UPLOAD_batch-B"));
assertWeight(appendRelease,21);
assertThrows(ResponseStatusException.class,()->configure("same-content.xlsx","black","APPEND","batch-C",0,"duplicate bytes",appendRelease,appendB.bytes()));
AppUserPrincipal limitedActor=limitedActor();
var unauthorized=new SourceUploadService.ConfigureRequest("unused","black",2026,2,"报废明细",1,fieldMap("black"),null,"APPEND",appendRelease,null,null,"out of range","ALL","limited-batch");
ResponseStatusException forbidden=assertThrows(ResponseStatusException.class,()->uploads.configure(unauthorized,limitedActor));
assertEquals(HttpStatus.FORBIDDEN,forbidden.getStatusCode());
Map<String,Object> target=targets().stream().filter(t->Objects.equals("batch-A",t.get("fileId"))).findFirst().orElseThrow();
assertEquals("APPEND_SNAPSHOT",target.get("sourceMode"));
String aDocument=String.valueOf(target.get("documentId")),aRevision=String.valueOf(target.get("revisionId"));
ConfiguredSource correctedA=configureReplace("append-a-corrected.xlsx","black","batch-A",8,"replace batch A only",appendRelease,aDocument,aRevision,blackWorkbook(8));
String replacementCandidate=importSources(List.of(correctedA.sourceId()),appendRelease);
publish(replacementCandidate,appendRelease,"publish-replace-a");
String replacementRelease=currentRelease();
Map<String,Member> replacedMembers=members(replacementRelease);
assertEquals(4,replacedMembers.size());
assertEquals(appendedMembers.get("black|2026|02|ALL"),replacedMembers.get("black|2026|02|ALL"));
assertEquals(appendedMembers.get("bright|2026|02|ALL"),replacedMembers.get("bright|2026|02|ALL"));
assertEquals(appendedMembers.get("black|2026|02|ALL|UPLOAD_batch-B"),replacedMembers.get("black|2026|02|ALL|UPLOAD_batch-B"));
assertEquals(aDocument,replacedMembers.get("black|2026|02|ALL|UPLOAD_batch-A").documentId());
assertNotEquals(aRevision,replacedMembers.get("black|2026|02|ALL|UPLOAD_batch-A").revisionId());
assertWeight(replacementRelease,25);
assertWeight(appendRelease,21);
assertEquals(originalMembers, members(mainRelease));
Map<String,Object> requested=tx.execute(status->governance.request(new SourceGovernanceService.ChangeRequest("WITHDRAW",aDocument,null,"withdraw appended batch A","withdraw-batch-a"),adminActor));
String requestId=String.valueOf(((Map<?,?>)requested.get("request")).get("id"));
tx.executeWithoutResult(status->governance.review(requestId,new SourceGovernanceService.ReviewRequest(true,"approve batch A withdrawal",0L),adminActor));
Map<String,Object> built=tx.execute(status->governance.build(requestId,new SourceGovernanceService.BuildRequest(replacementRelease,"build-withdraw-a",null),adminActor));
String withdrawalCandidate=String.valueOf(built.get("releaseId"));
assertTrue(Boolean.TRUE.equals(jdbc.queryForObject("SELECT complete FROM data_release WHERE id=?",Boolean.class,withdrawalCandidate)));
publish(withdrawalCandidate,replacementRelease,"publish-withdraw-a");
String withdrawalRelease=currentRelease();
Map<String,Member> withdrawnMembers=members(withdrawalRelease);
assertEquals(3,withdrawnMembers.size());
assertFalse(withdrawnMembers.containsKey("black|2026|02|ALL|UPLOAD_batch-A"));
assertEquals(replacedMembers.get("black|2026|02|ALL"),withdrawnMembers.get("black|2026|02|ALL"));
assertEquals(replacedMembers.get("bright|2026|02|ALL"),withdrawnMembers.get("bright|2026|02|ALL"));
assertEquals(replacedMembers.get("black|2026|02|ALL|UPLOAD_batch-B"),withdrawnMembers.get("black|2026|02|ALL|UPLOAD_batch-B"));
assertWeight(withdrawalRelease,17);
assertWeight(replacementRelease,25);
assertWeight(appendRelease,21);
assertEquals(originalMembers,members(mainRelease));
ConfiguredSource departmentAppend=configureScoped("department-append.xlsx","black","APPEND","dept-batch",7,"scope overlap regression",withdrawalRelease,"质量管理部",blackWorkbook(7));
String scopeOverlapCandidate=importSources(List.of(departmentAppend.sourceId()),withdrawalRelease);
assertFalse(Boolean.TRUE.equals(jdbc.queryForObject("SELECT complete FROM data_release WHERE id=?",Boolean.class,scopeOverlapCandidate)));
assertTrue(coverage.overlapCount(scopeOverlapCandidate)>0);
}
@Test void concurrentSameShaUploadsUseCurrentLockingReadsAndOnlyOneIsConfirmed() throws Exception {
byte[] bytes=blackWorkbook(11);
Map<String,Object> firstPreview=uploads.previewUpload(multipart("same-one.xlsx",bytes),adminActor);
Map<String,Object> secondPreview=uploads.previewUpload(multipart("same-two.xlsx",bytes),adminActor);
var first=request(firstPreview,"black","APPEND","concurrent-A",null,"same content A");
var second=request(secondPreview,"black","APPEND","concurrent-B",null,"same content B");
CountDownLatch ready=new CountDownLatch(2),start=new CountDownLatch(1);
try(var pool=Executors.newFixedThreadPool(2)){
var a=pool.submit(()->concurrentConfigure(first,ready,start));
var b=pool.submit(()->concurrentConfigure(second,ready,start));
assertTrue(ready.await(10,TimeUnit.SECONDS));start.countDown();
List<String> outcomes=List.of(a.get(20,TimeUnit.SECONDS),b.get(20,TimeUnit.SECONDS));
assertEquals(1,outcomes.stream().filter("CONFIRMED"::equals).count());
assertEquals(1,outcomes.stream().filter("DUPLICATE_SHA"::equals).count());
}
assertEquals(1,jdbc.queryForObject("SELECT COUNT(*) FROM source_candidate WHERE status='DISCOVERED' AND resolution='APPEND_CONFIRMED'",Integer.class));
}
private String concurrentConfigure(SourceUploadService.ConfigureRequest request,CountDownLatch ready,CountDownLatch start){
try{return tx.execute(status->{
jdbc.queryForObject("SELECT COUNT(*) FROM source_candidate",Integer.class); // Establish a repeatable-read snapshot before the shared pointer lock.
ready.countDown();try{if(!start.await(10,TimeUnit.SECONDS))throw new IllegalStateException("barrier timeout");}catch(InterruptedException e){Thread.currentThread().interrupt();throw new IllegalStateException(e);}
try{uploads.configure(request,adminActor);return "CONFIRMED";}catch(ResponseStatusException duplicate){if(duplicate.getReason()!=null&&duplicate.getReason().contains("请勿重复计入"))return "DUPLICATE_SHA";throw duplicate;}catch(java.io.IOException e){throw new IllegalStateException(e);}
});}catch(RuntimeException failure){throw failure;}
}
private ConfiguredSource configure(String filename,String stage,String mode,String fileId,int weight,String reason,String expected)throws Exception{
return configure(filename,stage,mode,fileId,weight,reason,expected,"black".equals(stage)?blackWorkbook(weight):brightWorkbook(weight));
}
private ConfiguredSource configure(String filename,String stage,String mode,String fileId,int weight,String reason,String expected,byte[] bytes)throws Exception{
return configureScoped(filename,stage,mode,fileId,weight,reason,expected,"ALL",bytes);
}
private ConfiguredSource configureScoped(String filename,String stage,String mode,String fileId,int weight,String reason,String expected,String scope,byte[] bytes)throws Exception{
Map<String,Object> preview=uploads.previewUpload(multipart(filename,bytes),adminActor);
var request=request(preview,stage,mode,fileId,expected,reason,scope);
Map<String,Object> configured=tx.execute(status->{try{return uploads.configure(request,adminActor);}catch(java.io.IOException e){throw new IllegalStateException(e);}});
return new ConfiguredSource(((SourceCatalogService.SourceView)configured.get("source")).id(),String.valueOf(preview.get("uploadId")),bytes);
}
private ConfiguredSource configureReplace(String filename,String stage,String fileId,int weight,String reason,String expected,String targetDocument,String targetRevision,byte[] bytes)throws Exception{
Map<String,Object> preview=uploads.previewUpload(multipart(filename,bytes),adminActor);
var request=new SourceUploadService.ConfigureRequest(String.valueOf(preview.get("uploadId")),stage,2026,2,"black".equals(stage)?"报废明细":"明细",1,fieldMap(stage),null,"REPLACE",expected,targetDocument,targetRevision,reason,"ALL",fileId);
Map<String,Object> configured=tx.execute(status->{try{return uploads.configure(request,adminActor);}catch(java.io.IOException e){throw new IllegalStateException(e);}});
return new ConfiguredSource(((SourceCatalogService.SourceView)configured.get("source")).id(),String.valueOf(preview.get("uploadId")),bytes);
}
private Map<String,Object> configureAgain(ConfiguredSource source,String stage,String mode,String fileId,int weight,String reason,String expected)throws Exception{
var request=new SourceUploadService.ConfigureRequest(source.uploadId(),stage,2026,2,"black".equals(stage)?"报废明细":"明细",1,fieldMap(stage),null,mode,expected,null,null,reason,"ALL",fileId);
return tx.execute(status->{try{return uploads.configure(request,adminActor);}catch(java.io.IOException e){throw new IllegalStateException(e);}});
}
private SourceUploadService.ConfigureRequest request(Map<String,Object> preview,String stage,String mode,String fileId,String expected,String reason){
return request(preview,stage,mode,fileId,expected,reason,"ALL");
}
private SourceUploadService.ConfigureRequest request(Map<String,Object> preview,String stage,String mode,String fileId,String expected,String reason,String scope){
return new SourceUploadService.ConfigureRequest(String.valueOf(preview.get("uploadId")),stage,2026,2,"black".equals(stage)?"报废明细":"明细",1,fieldMap(stage),null,mode,expected,null,null,reason,scope,fileId);
}
private Map<String,String> fieldMap(String stage){Map<String,String> result=new LinkedHashMap<>();SourceParserConfig.required(stage).forEach(header->result.put(header,header));return result;}
private String importSources(List<String> ids,String expected){
var run=tx.execute(status->imports.create(new ImportRunService.CreateImportRequest(ids,UUID.randomUUID().toString(),expected),adminId));
Map<String,Object> result=tx.execute(status->worker.execute(run.id()));
assertEquals("COMPLETED",result.get("status"),String.valueOf(result.get("errorMessage")));
return String.valueOf(result.get("resultReleaseId"));
}
private void publish(String releaseId,String expected,String key){
tx.execute(status->releases.transitionInTestWorkflow(releaseId,"PUBLISH",new ReleaseService.ReleaseActionRequest(expected,key,"multifile integration acceptance"),adminActor,false));
}
private String currentRelease(){return jdbc.query("SELECT release_id FROM current_release WHERE dataset_id='quality'",(rs,n)->rs.getString(1)).getFirst();}
private Map<String,Member> members(String releaseId){
return jdbc.query("SELECT d.source_slot,d.id,m.source_revision_id,m.transform_build_id FROM release_member m JOIN source_document d ON d.id=m.source_document_id WHERE m.release_id=? ORDER BY d.source_slot",(rs,n)->Map.entry(rs.getString(1),new Member(rs.getString(2),rs.getString(3),rs.getString(4))),releaseId).stream().collect(java.util.stream.Collectors.toMap(Map.Entry::getKey,Map.Entry::getValue, (a,b)->a,LinkedHashMap::new));
}
private String sourceMode(String slot){return jdbc.queryForObject("SELECT source_mode FROM source_document WHERE source_slot=?",String.class,slot);}
private void assertWeight(String releaseId,int expected){
var query=new MetricsService.MetricsQuery(2026,2,null,null,null,null,null,"overall",false,releaseId,"month",Map.of());
BigDecimal total=metrics.calculate(query,adminActor).summary().black().totalWeight();
assertNotNull(total);assertEquals(0,new BigDecimal(expected).compareTo(total),"black-stage accumulated total for "+releaseId);
}
private List<Map<String,Object>> targets(){return (List<Map<String,Object>>)uploads.options(adminActor).get("targets");}
private String sourceId(Map<String,Object> response){return ((SourceCatalogService.SourceView)response.get("source")).id();}
private AppUserPrincipal limitedActor(){
UUID id=UUID.randomUUID();jdbc.update("INSERT INTO app_user(id,username,display_name,password_hash,active) VALUES(?,?,?,?,TRUE)",id.toString(),"limited-upload-user","Limited upload user","fixture-hash");
jdbc.update("INSERT INTO app_user_role(user_id,role_code) VALUES(?,'DATA_ADMIN')",id.toString());
jdbc.update("INSERT INTO app_user_scope(user_id,scope_type,scope_value) VALUES(?,'DEPARTMENT','DEPT_A')",id.toString());
return new AppUserPrincipal(id,"limited-upload-user","fixture-hash","Limited upload user",true,false,List.of("DATA_ADMIN"),List.of("DEPT_A"),List.of(),AppRoles.authorities(List.of("DATA_ADMIN")));
}
private MockMultipartFile multipart(String filename,byte[] bytes){return new MockMultipartFile("file",filename,"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",bytes);}
private byte[] blackWorkbook(int weight)throws Exception{return workbook("报废明细","black",weight);}
private byte[] brightWorkbook(int weight)throws Exception{return workbook("明细","bright",weight);}
private byte[] workbook(String sheetName,String stage,int weight)throws Exception{
List<String> headers=SourceParserConfig.required(stage);Map<String,String> values=new HashMap<>();
if("black".equals(stage)){
values.put("序号",Integer.toString(weight));values.put("PDT代码","P002");values.put("销售片区","中国区");values.put("事业部","质量管理部");values.put("成品规格","P");values.put("送电镀重量",Integer.toString(weight));values.put("黑片出货重量","0");values.put("责废总重量","0");values.put("料废重量","0");values.put("外废重量","0");values.put("发料地点","常规");values.put("检验备注","");
}else{
values.put("序号",Integer.toString(weight));values.put("PDT代码","P002");values.put("销售片区","中国区");values.put("事业部","质量管理部");values.put("成品规格","P");values.put("是否电镀来料/二等品库来料","是");values.put("合格重量",Integer.toString(weight));values.put("检包入废品库重量","1");values.put("本部报废重量","0");values.put("镀废重量合计","1");values.put("材料废重量合计","0");values.put("加工废重量合计","0");values.put("晶界扩散报废合计重量","0");
}
try(XSSFWorkbook workbook=new XSSFWorkbook();ByteArrayOutputStream out=new ByteArrayOutputStream()){
Sheet sheet=workbook.createSheet(sheetName);Row header=sheet.createRow(0);for(int i=0;i<headers.size();i++)header.createCell(i).setCellValue(headers.get(i));
Row row=sheet.createRow(1);for(int i=0;i<headers.size();i++)row.createCell(i).setCellValue(values.getOrDefault(headers.get(i),""));
workbook.write(out);return out.toByteArray();
}
}
private record ConfiguredSource(String sourceId,String uploadId,byte[] bytes){}
private record Member(String documentId,String revisionId,String buildId){}
}
@@ -27,6 +27,59 @@ class SourceUploadServiceTest {
assertConfiguredWithoutTemplate("REPLACE");
}
@Test void appendUsesStablePerFileSlotAndAppendProtocol() throws Exception {
Fixture f=fixture("APPEND");
f.service.configure(f.request(null),f.actor);
ArgumentCaptor<Object[]> candidate=ArgumentCaptor.forClass(Object[].class);
verify(f.jdbc).update(contains("INSERT INTO source_candidate"),candidate.capture());
assertEquals("black|2026|02|ALL|UPLOAD_file-1",candidate.getValue()[2]);
assertEquals("APPEND_CONFIRMED",candidate.getValue()[12]);
ArgumentCaptor<Object[]> protocol=ArgumentCaptor.forClass(Object[].class);
verify(f.jdbc).update(contains("INSERT INTO source_protocol"),protocol.capture());
assertEquals("UPLOAD_file-1",protocol.getValue()[7]);
assertEquals("APPEND_SNAPSHOT",protocol.getValue()[8]);
}
@Test void sameWorkbookHashInCurrentOverlappingScopeIsRejected() throws Exception {
Fixture f=fixture("APPEND");
when(f.jdbc.queryForList(startsWith("SELECT d.id,d.scope_code FROM release_member"),any(Object[].class)))
.thenReturn(List.of(Map.of("id","monthly-document","scope_code","ALL")));
var error=assertThrows(org.springframework.web.server.ResponseStatusException.class,()->f.service.configure(f.request(null),f.actor));
assertEquals(org.springframework.http.HttpStatus.CONFLICT,error.getStatusCode());
assertTrue(error.getReason().contains("请勿重复计入"));
verify(f.jdbc,never()).update(contains("INSERT INTO source_candidate"),any(Object[].class));
}
@Test void replacementTargetsOneAppendFileByItsStableFileIdAndRevision() throws Exception {
Fixture f=fixture("REPLACE");
when(f.jdbc.queryForList(startsWith("SELECT d.id,r.source_revision_id,d.source_mode"),any(Object[].class)))
.thenReturn(List.of(Map.of("id","document-current","source_revision_id","revision-current","source_mode","APPEND_SNAPSHOT")));
SourceUploadService.ConfigureRequest base=f.request(null);
var request=new SourceUploadService.ConfigureRequest(base.uploadId(),base.stage(),base.year(),base.month(),base.sheetName(),base.headerRow(),base.fieldMap(),base.templateId(),base.mode(),base.expectedCurrentReleaseId(),base.targetDocumentId(),base.targetRevisionId(),base.reason(),base.scopeCode(),"file-1");
f.service.configure(request,f.actor);
ArgumentCaptor<Object[]> candidate=ArgumentCaptor.forClass(Object[].class);
verify(f.jdbc).update(contains("INSERT INTO source_candidate"),candidate.capture());
assertEquals("black|2026|02|ALL|UPLOAD_file-1",candidate.getValue()[2]);
assertEquals("document-current",candidate.getValue()[17]);
assertEquals("revision-current",candidate.getValue()[18]);
}
@Test void sameAppendFileIdCannotBeRegisteredAsAnotherNewBatch() throws Exception {
Fixture f=fixture("APPEND");
when(f.jdbc.queryForList(startsWith("SELECT id FROM source_candidate WHERE"),any(Object[].class)))
.thenReturn(List.of(Map.of("id","prior-candidate")));
var error=assertThrows(org.springframework.web.server.ResponseStatusException.class,()->f.service.configure(f.request(null),f.actor));
assertTrue(error.getReason().contains("该fileId已登记"));
verify(f.jdbc,never()).update(contains("INSERT INTO source_candidate"),any(Object[].class));
}
@Test void uploadOptionsDoNotExposeOrReadTemplateCatalog() throws Exception {
Fixture f = fixture("ADD");
when(f.jdbc.queryForList(anyString(), any(Object[].class))).thenReturn(List.of());
@@ -134,7 +187,7 @@ class SourceUploadServiceTest {
boolean replace = "REPLACE".equals(mode);
return new SourceUploadService.ConfigureRequest("upload-draft", "black", 2026, 2, "工作表", 2,
fieldMap, templateId, mode, "release-current", replace ? "document-current" : null,
replace ? "revision-current" : null, "测试直接字段映射", "ALL");
replace ? "revision-current" : null, "测试直接字段映射", "ALL", "APPEND".equals(mode)?"file-1":null);
}
}
}
@@ -0,0 +1,49 @@
package cn.yunsheng.quality.update;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.*;
import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean;
import org.junit.jupiter.api.Test;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.mock.web.MockHttpServletResponse;
class SystemUpdateMaintenanceFilterTest {
@Test
void blocksBusinessWritesDuringUpdateButKeepsReadsAuthAndUpdateStatusAvailable() throws Exception {
var updates = mock(SystemUpdateService.class);
when(updates.maintenanceActive()).thenReturn(true);
var filter = new SystemUpdateMaintenanceFilter(updates);
for (String method : List.of("POST", "PUT", "PATCH", "DELETE")) {
var request = new MockHttpServletRequest(method, "/api/sources/test");
var response = new MockHttpServletResponse();
var continued = new AtomicBoolean();
filter.doFilter(request, response, (req, res) -> continued.set(true));
assertFalse(continued.get());
assertEquals(503, response.getStatus());
assertEquals("10", response.getHeader("Retry-After"));
}
var password = new MockHttpServletResponse();
filter.doFilter(new MockHttpServletRequest("POST", "/api/auth/change-password"), password,
(req, res) -> fail("Password changes must remain blocked during update"));
assertEquals(503, password.getStatus());
for (var route : List.of(new String[]{"GET", "/api/sources"}, new String[]{"GET", "/api/system-update"},
new String[]{"POST", "/api/system-update/check"}, new String[]{"POST", "/api/auth/login"},
new String[]{"GET", "/actuator/health"})) {
var continued = new AtomicBoolean();
filter.doFilter(new MockHttpServletRequest(route[0], route[1]), new MockHttpServletResponse(),
(req, res) -> continued.set(true));
assertTrue(continued.get());
}
}
@Test
void normalAndSourceDeploymentsContinueToAllowWrites() throws Exception {
var updates = mock(SystemUpdateService.class);
when(updates.maintenanceActive()).thenReturn(false);
var continued = new AtomicBoolean();
new SystemUpdateMaintenanceFilter(updates).doFilter(new MockHttpServletRequest("POST", "/api/sources"),
new MockHttpServletResponse(), (req, res) -> continued.set(true));
assertTrue(continued.get());
}
}
@@ -0,0 +1,88 @@
package cn.yunsheng.quality.update;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.*;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import cn.yunsheng.quality.config.SecurityConfig;
import cn.yunsheng.quality.governance.GovernanceAccessService;
import cn.yunsheng.quality.identity.JdbcAppUserDetailsService;
import jakarta.servlet.Filter;
import java.util.List;
import java.util.Map;
import org.junit.jupiter.api.Test;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.mock.web.MockHttpSession;
import org.springframework.mock.web.MockServletContext;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.web.csrf.CsrfToken;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
/** Exercises the real application security chain, including CSRF and method roles. */
class SystemUpdateSecurityTest {
@Configuration
@EnableWebMvc
@Import(SecurityConfig.class)
static class TestConfig {
@Bean JdbcAppUserDetailsService users() { return mock(JdbcAppUserDetailsService.class); }
@Bean GovernanceAccessService access() { return mock(GovernanceAccessService.class); }
@Bean SystemUpdateService updates() {
var service = mock(SystemUpdateService.class);
var result = Map.<String, Object>of("enabled", true, "state", "idle");
when(service.status()).thenReturn(result);
when(service.check()).thenReturn(result);
when(service.apply()).thenReturn(result);
return service;
}
@Bean SystemUpdateController controller(SystemUpdateService updates) { return new SystemUpdateController(updates); }
}
private MockHttpSession session(String role) {
var context = SecurityContextHolder.createEmptyContext();
context.setAuthentication(UsernamePasswordAuthenticationToken.authenticated("test-user", null,
List.of(new SimpleGrantedAuthority("ROLE_" + role))));
var session = new MockHttpSession();
session.setAttribute("SPRING_SECURITY_CONTEXT", context);
return session;
}
@Test
void requiresAdminAndCsrfForBothUpdateMutations() throws Exception {
try (var context = new AnnotationConfigWebApplicationContext()) {
context.setServletContext(new MockServletContext());
context.register(TestConfig.class);
context.refresh();
var mvc = MockMvcBuilders.webAppContextSetup(context)
.addFilters(context.getBean("springSecurityFilterChain", Filter.class)).build();
mvc.perform(get("/api/system-update")).andExpect(status().isUnauthorized());
mvc.perform(get("/api/system-update").session(session("USER"))).andExpect(status().isForbidden());
var admin = session("SYSTEM_ADMIN");
var initial = mvc.perform(get("/api/system-update").session(admin)).andExpect(status().isOk()).andReturn();
var csrf = (CsrfToken) initial.getRequest().getAttribute(CsrfToken.class.getName());
assertNotNull(csrf);
String token = csrf.getToken();
var cookie = initial.getResponse().getCookie("XSRF-TOKEN");
assertNotNull(cookie);
for (String path : List.of("/api/system-update/check", "/api/system-update/apply")) {
mvc.perform(post(path).session(admin)).andExpect(status().isForbidden());
mvc.perform(post(path).session(admin).cookie(cookie).header(csrf.getHeaderName(), "bad-token"))
.andExpect(status().isForbidden());
mvc.perform(post(path).session(session("DATA_ADMIN")).cookie(cookie).header(csrf.getHeaderName(), token))
.andExpect(status().isForbidden());
mvc.perform(post(path).session(admin).cookie(cookie).header(csrf.getHeaderName(), token))
.andExpect(status().isOk());
}
verify(context.getBean(SystemUpdateService.class), times(1)).check();
verify(context.getBean(SystemUpdateService.class), times(1)).apply();
} finally {
SecurityContextHolder.clearContext();
}
}
}
@@ -0,0 +1,63 @@
package cn.yunsheng.quality.update;
import static org.junit.jupiter.api.Assertions.*;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Map;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.server.ResponseStatusException;
import tools.jackson.databind.json.JsonMapper;
class SystemUpdateServiceTest {
@Test
void sourceDeploymentIsDisabledEvenIfFlagIsAccidentallyEnabled() {
var service = new SystemUpdateService(JsonMapper.builder().build(), "", true);
assertEquals(false, service.status().get("enabled"));
assertThrows(ResponseStatusException.class, service::check);
assertThrows(ResponseStatusException.class, service::apply);
}
@Test
void updaterReceiptsNeverExposePrivateConfigurationOrPaths() throws Exception {
var result = SystemUpdateService.safeResponse(Map.of("enabled", true, "state", "available",
"version", "2026.11.04", "rootPassword", "secret", "backupPath", "C:/private"));
assertEquals(Map.of("enabled", true, "state", "available", "version", "2026.11.04"), result);
assertThrows(IOException.class, () -> SystemUpdateService.safeResponse(Map.of("enabled", "true", "state", "idle")));
assertThrows(IOException.class, () -> SystemUpdateService.safeResponse(Map.of("enabled", true, "state", Map.of())));
}
@Test
void allUpdateRoutesRequireSystemAdministrator() {
assertEquals("hasRole('SYSTEM_ADMIN')", SystemUpdateController.class.getAnnotation(PreAuthorize.class).value());
}
@Test
void maintenanceStartsBeforeBackupAndPersistsAcrossAnInterruptedInstall(@TempDir Path root) throws Exception {
var service = new SystemUpdateService(JsonMapper.builder().build(), root.toString(), false);
// Exercise host-independent metadata handling without changing the
// production Windows/runtime enablement guard.
var enabled = SystemUpdateService.class.getDeclaredField("enabled");
enabled.setAccessible(true);
enabled.setBoolean(service, true);
assertFalse(service.maintenanceActive());
Path directory = Files.createDirectories(root.resolve(".runtime/update"));
for (String phase : new String[]{"queued", "downloading", "completed", "failed"}) {
Files.writeString(directory.resolve("status.json"), "{\"state\":\"" + phase + "\"}");
assertFalse(service.maintenanceActive(), phase);
}
for (String phase : new String[]{"backingUp", "installing", "restarting", "rollingBack"}) {
Files.writeString(directory.resolve("status.json"), "{\"state\":\"" + phase + "\"}");
assertTrue(service.maintenanceActive(), phase);
}
Files.writeString(directory.resolve("status.json"), "{\"state\":\"failed\"}");
Files.writeString(directory.resolve("journal.json"), "{\"phase\":\"installing\"}");
assertTrue(service.maintenanceActive());
Files.writeString(directory.resolve("journal.json"), "{\"phase\":\"rolledBack\"}");
assertFalse(service.maintenanceActive());
Files.writeString(directory.resolve("status.json"), "broken JSON");
assertTrue(service.maintenanceActive());
}
}
@@ -0,0 +1,79 @@
package cn.yunsheng.quality.update;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.*;
import cn.yunsheng.quality.analysis.AnalysisReportService;
import java.time.Duration;
import java.util.Map;
import java.util.concurrent.atomic.AtomicBoolean;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.core.env.MapPropertySource;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.ScheduledAnnotationBeanPostProcessor;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;
/** Actual Spring event/scheduling registration, including transaction proxies. */
class SystemUpdateSpringLifecycleTest {
private static final AtomicBoolean MAINTENANCE = new AtomicBoolean();
private static AnalysisReportService originalReports;
@Configuration
@EnableScheduling
@EnableTransactionManagement
static class Config {
@Bean static SystemUpdateStartupBarrier barrier(ObjectProvider<SystemUpdateService> updates, Environment environment) {
return new SystemUpdateStartupBarrier(updates, environment);
}
@Bean SystemUpdateService updates() {
var updates = mock(SystemUpdateService.class);
when(updates.maintenanceActive()).thenAnswer(call -> MAINTENANCE.get());
return updates;
}
@Bean AnalysisReportService reports() { originalReports = mock(AnalysisReportService.class); return originalReports; }
@Bean PlatformTransactionManager transactionManager() { return mock(PlatformTransactionManager.class); }
}
@Test
void actualSpringReadyEventAndScheduledTaskRemainPausedUntilCommit() {
// Initialize Mockito before temporarily simulating the target OS flag;
// no external process or filesystem/runtime guard is changed.
mock(SystemUpdateService.class);
String originalOs = System.getProperty("os.name");
MAINTENANCE.set(true);
try (var context = new AnnotationConfigApplicationContext()) {
context.getEnvironment().getPropertySources().addFirst(new MapPropertySource("update-test",
Map.of("QUALITY_UPDATE_ENABLED", "true")));
context.register(Config.class);
try {
System.setProperty("os.name", "Windows 11");
context.refresh();
} finally {
System.setProperty("os.name", originalOs);
}
var event = new ApplicationReadyEvent(new SpringApplication(Config.class), new String[0], context, Duration.ZERO);
context.publishEvent(event);
verify(originalReports, never()).recoverPendingReports(any(ApplicationReadyEvent.class));
var schedules = context.getBean(ScheduledAnnotationBeanPostProcessor.class).getScheduledTasks();
assertEquals(1, schedules.size());
var task = schedules.iterator().next().getTask().getRunnable();
task.run();
verify(originalReports, never()).scheduledReports();
MAINTENANCE.set(false);
context.getBean(SystemUpdateStartupBarrier.class).drain();
verify(originalReports, times(1)).recoverPendingReports(event);
task.run();
verify(originalReports, times(1)).scheduledReports();
} finally {
System.setProperty("os.name", originalOs);
MAINTENANCE.set(false);
}
}
}
@@ -0,0 +1,58 @@
package cn.yunsheng.quality.update;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.*;
import cn.yunsheng.quality.analysis.AnalysisTurnService;
import cn.yunsheng.quality.ingestion.ImportRecoveryRunner;
import java.util.concurrent.atomic.AtomicBoolean;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.core.env.Environment;
import org.springframework.scheduling.annotation.Scheduled;
class SystemUpdateStartupBarrierTest {
public static class ScheduledProbe {
int ticks;
@Scheduled(fixedDelay = 1000) public void tick() { ticks++; }
public void ordinaryRead() { ticks += 10; }
}
@Test
@SuppressWarnings("unchecked")
void defersBusinessStartupExactlyOnceUntilCommitAndSkipsScheduledWrites() {
var maintenance = new AtomicBoolean(true);
var updates = mock(SystemUpdateService.class);
when(updates.maintenanceActive()).thenAnswer(call -> maintenance.get());
var provider = (ObjectProvider<SystemUpdateService>) mock(ObjectProvider.class);
when(provider.getObject()).thenReturn(updates);
try (var scope = new BarrierScope(new SystemUpdateStartupBarrier(provider, mock(Environment.class)))) {
var barrier = scope.barrier;
var imports = mock(ImportRecoveryRunner.class);
var importProxy = (ImportRecoveryRunner) barrier.wrap(imports, ImportRecoveryRunner.class);
var turns = mock(AnalysisTurnService.class);
var turnProxy = (AnalysisTurnService) barrier.wrap(turns, AnalysisTurnService.class);
var raw = new ScheduledProbe();
var scheduled = (ScheduledProbe) barrier.wrap(raw, ScheduledProbe.class);
importProxy.run(null);
turnProxy.recoverQueuedTurns();
scheduled.tick();
scheduled.ordinaryRead();
assertEquals(10, raw.ticks);
verify(imports, never()).run(null);
verify(turns, never()).recoverQueuedTurns();
barrier.drain();
verify(imports, never()).run(null);
maintenance.set(false);
barrier.drain();
barrier.drain();
verify(imports, times(1)).run(null);
verify(turns, times(1)).recoverQueuedTurns();
scheduled.tick();
assertEquals(11, raw.ticks);
}
}
private record BarrierScope(SystemUpdateStartupBarrier barrier) implements AutoCloseable {
@Override public void close() { barrier.close(); }
}
}
@@ -62,6 +62,41 @@ class DataWorkflowServiceTest {
verifyNoMoreInteractions(jdbc);
}
@Test
void candidateDataErrorsAreNotReportedAsVersionMismatches() {
when(jdbc.queryForMap(contains("SELECT mapping_version"), eq("candidate")))
.thenReturn(Map.of("mappingVersion", "mapping", "ruleVersion", "rule"));
when(jdbc.queryForObject(contains("rejected_count"), eq(Integer.class), any(Object[].class))).thenReturn(2);
when(jdbc.queryForList(contains("FROM import_issue"), any(Object[].class))).thenReturn(List.of(
Map.of("issueCode", "INVALID_NUMBER", "fieldName", "合格重量", "issueCount", 2L)));
RuntimeException error = assertThrows(RuntimeException.class, () -> ReflectionTestUtils.invokeMethod(service,
"assertWorkflowCandidate", "candidate", "mapping", "rule"));
assertTrue(error.getMessage().contains("合格重量"), error.getMessage());
assertTrue(error.getMessage().contains("数值"), error.getMessage());
assertFalse(error.getMessage().contains("未使用本次冻结版本"));
}
@Test
void incompleteCandidateBuildHasItsOwnFailureReason() {
when(jdbc.queryForMap(contains("SELECT mapping_version"), eq("candidate")))
.thenReturn(Map.of("mappingVersion", "mapping", "ruleVersion", "rule"));
when(jdbc.queryForObject(contains("b.status<>'COMPLETED'"), eq(Integer.class), any(Object[].class))).thenReturn(1);
RuntimeException error = assertThrows(RuntimeException.class, () -> ReflectionTestUtils.invokeMethod(service,
"assertWorkflowCandidate", "candidate", "mapping", "rule"));
assertTrue(error.getMessage().contains("尚未完成"), error.getMessage());
assertFalse(error.getMessage().contains("未使用本次冻结版本"));
}
@Test
void actualCandidateVersionMismatchStillBlocksPublication() {
when(jdbc.queryForMap(contains("SELECT mapping_version"), eq("candidate")))
.thenReturn(Map.of("mappingVersion", "mapping", "ruleVersion", "rule"));
when(jdbc.queryForObject(contains("b.mapping_version<>? OR b.rule_version<>?"), eq(Integer.class), any(Object[].class))).thenReturn(1);
RuntimeException error = assertThrows(RuntimeException.class, () -> ReflectionTestUtils.invokeMethod(service,
"assertWorkflowCandidate", "candidate", "mapping", "rule"));
assertTrue(error.getMessage().contains("未使用本次冻结版本"));
}
@Test
void disabledModeKeepsTheLegacyPathAndDoesNotQueueMalformedRequests() {
ReflectionTestUtils.setField(service, "testMode", false);
@@ -72,55 +107,35 @@ class DataWorkflowServiceTest {
}
@Test
void writeActionsStillRequirePublisherEvenWhenApprovalsAreSkipped() {
void usersCannotQueueDataWrites() {
ResponseStatusException error = assertThrows(ResponseStatusException.class,
() -> service.recalculate(user("DATA_MAINTAINER")));
() -> service.recalculate(user("USER")));
assertEquals(HttpStatus.FORBIDDEN, error.getStatusCode());
verify(access).requireUserActivePolicy(any());
verifyNoInteractions(jdbc);
}
@Test
void sourceCapabilitiesReflectManagerAndPublisherIntersection() {
Map<String, Object> maintainer = DataWorkflowService.capabilities(true, user("DATA_MAINTAINER"));
assertEquals(true, maintainer.get("canDeleteSource"));
assertEquals(false, maintainer.get("canDeleteCurrentSource"));
assertEquals(false, maintainer.get("canRestoreSource"));
assertEquals(false, maintainer.get("canApplyMapping"));
assertEquals(false, maintainer.get("canImport"));
Map<String, Object> publisher = DataWorkflowService.capabilities(true, user("DATA_PUBLISHER"));
assertEquals(false, publisher.get("canDeleteSource"));
assertEquals(false, publisher.get("canRestoreSource"));
assertEquals(false, publisher.get("canApplyMapping"));
assertEquals(false, publisher.get("canApplyRule"));
assertEquals(false, publisher.get("canImport"));
assertEquals(true, publisher.get("canRecalculate"));
Map<String, Object> publisherAndDataMaintainer = DataWorkflowService.capabilities(true,
user("DATA_PUBLISHER", "DATA_MAINTAINER"));
assertEquals(true, publisherAndDataMaintainer.get("canApplyMapping"));
assertEquals(false, publisherAndDataMaintainer.get("canApplyRule"));
assertEquals(true, publisherAndDataMaintainer.get("canImport"));
Map<String, Object> publisherAndRuleMaintainer = DataWorkflowService.capabilities(true,
user("DATA_PUBLISHER", "RULE_MAINTAINER"));
assertEquals(true, publisherAndRuleMaintainer.get("canApplyMapping"));
assertEquals(true, publisherAndRuleMaintainer.get("canApplyRule"));
assertEquals(false, publisherAndRuleMaintainer.get("canImport"));
Map<String, Object> rulesMaintainer = DataWorkflowService.capabilities(true, user("RULE_MAINTAINER"));
assertEquals(false, rulesMaintainer.get("canApply"));
assertEquals(false, rulesMaintainer.get("canApplyRule"));
assertEquals(false, rulesMaintainer.get("canDeleteSource"));
assertEquals(false, rulesMaintainer.get("canRestoreSource"));
void threeRolesInheritDataCapabilitiesButKeepRuleEditingRestricted() {
Map<String, Object> reader = DataWorkflowService.capabilities(true, user("USER"));
for (String key : List.of("canApply", "canImport", "canPublish", "canDeleteSource", "canRestoreSource"))
assertEquals(false, reader.get(key), key);
Map<String, Object> dataAdmin = DataWorkflowService.capabilities(true, user("DATA_ADMIN"));
for (String key : List.of("canApplyMapping", "canImport", "canPublish", "canRecalculate", "canDeleteSource", "canDeleteCurrentSource", "canRestoreSource"))
assertEquals(true, dataAdmin.get(key), key);
assertEquals(false, dataAdmin.get("canApplyRule"));
assertEquals(true, DataWorkflowService.capabilities(true, admin()).get("canApplyRule"));
Map<String, Object> disabled = DataWorkflowService.capabilities(false, admin());
assertEquals(false, disabled.get("canApply"));
assertEquals(false, disabled.get("canApplyMapping"));
assertEquals(false, disabled.get("canApplyRule"));
assertEquals(false, disabled.get("canImport"));
assertEquals(false, disabled.get("canPublish"));
for (String key : List.of("canApply", "canApplyMapping", "canApplyRule", "canImport", "canPublish"))
assertEquals(false, disabled.get(key), key);
}
@Test
void dataAdministratorCannotQueueGlobalRuleChanges() {
var error = assertThrows(ResponseStatusException.class, () -> service.apply(
new DataWorkflowController.ApplyRequest("rule", "rules", Map.of(), null), user("DATA_ADMIN")));
assertEquals(HttpStatus.FORBIDDEN, error.getStatusCode());
verifyNoInteractions(jdbc);
}
@Test
@@ -192,13 +207,13 @@ class DataWorkflowServiceTest {
}
@Test
void sourceMutationEndpointsRequireTheUnderlyingManagerAndPublisherRoles() {
void usersCannotDeleteOrRestoreDataSources() {
ResponseStatusException deleteError = assertThrows(ResponseStatusException.class,
() -> service.deleteSource("candidate", user("DATA_PUBLISHER")));
() -> service.deleteSource("candidate", user("USER")));
assertEquals(HttpStatus.FORBIDDEN, deleteError.getStatusCode());
ResponseStatusException restoreError = assertThrows(ResponseStatusException.class,
() -> service.restoreSource("candidate", user("DATA_MAINTAINER")));
() -> service.restoreSource("candidate", user("USER")));
assertEquals(HttpStatus.FORBIDDEN, restoreError.getStatusCode());
verifyNoInteractions(jdbc);
}
@@ -333,18 +348,18 @@ class DataWorkflowServiceTest {
}
@Test
void batchDeletePreflightsAllSourcesAndEnforcesCurrentSourcePublishingPermission() {
void dataAdministratorPreflightsAllSourcesBeforeQueueingBatchDeletion() {
when(jdbc.query(eq("SELECT release_id FROM current_release WHERE dataset_id=?"), any(RowMapper.class), eq("quality")))
.thenReturn(List.of("current"));
when(deletion.preview(eq("a"), any())).thenReturn(Map.of("hidden", false, "currentUsed", false));
when(deletion.preview(eq("b"), any())).thenReturn(Map.of("hidden", false, "currentUsed", true));
var error = assertThrows(ResponseStatusException.class, () -> service.deleteSources(
new DataWorkflowController.DeleteSourcesRequest(List.of("a", "b"), "current"), user("DATA_MAINTAINER")));
assertEquals(HttpStatus.FORBIDDEN, error.getStatusCode());
var queued = service.deleteSources(
new DataWorkflowController.DeleteSourcesRequest(List.of("a", "b"), "current"), user("DATA_ADMIN"));
assertEquals("DELETE_SOURCES", queued.get("operationType"));
verify(deletion).preview(eq("a"), any());
verify(deletion).preview(eq("b"), any());
verify(deletion, never()).delete(anyString(), any(), any());
verify(jdbc, never()).update(anyString(), any(Object[].class));
verify(jdbc).update(anyString(), any(Object[].class));
}
@Test
@@ -357,13 +372,13 @@ class DataWorkflowServiceTest {
@Test
void workflowWriteEndpointsKeepRoleGuards() throws Exception {
assertPreAuthorize("apply", "SYSTEM_ADMIN", "DATA_MAINTAINER", "DATA_PUBLISHER");
assertPreAuthorize("recalculate", "SYSTEM_ADMIN", "DATA_MAINTAINER", "DATA_PUBLISHER");
assertPreAuthorize("importSources", "SYSTEM_ADMIN", "DATA_MAINTAINER", "DATA_PUBLISHER");
assertPreAuthorize("publish", "SYSTEM_ADMIN", "DATA_PUBLISHER");
assertPreAuthorize("deleteSource", "SYSTEM_ADMIN", "DATA_MAINTAINER", "DATA_PUBLISHER");
assertPreAuthorize("deleteSources", "SYSTEM_ADMIN", "DATA_MAINTAINER", "DATA_PUBLISHER");
assertPreAuthorize("restoreSource", "SYSTEM_ADMIN", "DATA_MAINTAINER", "DATA_PUBLISHER");
assertPreAuthorize("apply", "SYSTEM_ADMIN", "DATA_ADMIN");
assertPreAuthorize("recalculate", "SYSTEM_ADMIN", "DATA_ADMIN");
assertPreAuthorize("importSources", "SYSTEM_ADMIN", "DATA_ADMIN");
assertPreAuthorize("publish", "SYSTEM_ADMIN", "DATA_ADMIN");
assertPreAuthorize("deleteSource", "SYSTEM_ADMIN", "DATA_ADMIN");
assertPreAuthorize("deleteSources", "SYSTEM_ADMIN", "DATA_ADMIN");
assertPreAuthorize("restoreSource", "SYSTEM_ADMIN", "DATA_ADMIN");
}
private void assertPreAuthorize(String method, String... roles) throws Exception {