feat(flow): 流程草稿 ProcessModelController(act_re_model,Model API 首次启用)
在线 Modeler 草稿仓:flowable 原生 Model API 存草稿于 act_re_model(全仓首次启用
Model API,表已有 143 行存量,databaseSchemaUpdate=false 不涉 DDL)。5 端点(@RequestMapping
"/api/flow"):GET /models 列表(orderByLastUpdateTime desc,时间转 ISO)、GET /models/{id}
取草稿(getModel+getModelEditorSource,不存在/源空→found:false)、POST /models 建草稿
(XML 过 BpmnXMLConverter 解析守卫,坏 XML→422 不落库)、PUT /models/{id} 更新
(不存在→404;name/metaInfo 仅当请求提供才更新,避免抹掉 eip4IdMap)、
POST /models/from-procdef 从流程定义建草稿(BpmnIdSanitizer 净化中文 id,
metaInfo={eip4IdMap,sourceProcDefId,originalKey} 供发布逆向还原)。
解析守卫就地 catch 转 422,不逸出到 FlowExceptionHandler(那里 FlowableException→400
语义不符)。from-procdef 复用本控制器自带 readProcessModel 小方法,零改动 ProcessDiagramController。
测试:ProcessModelControllerTest 11 例(列表 ISO 降序/取草稿含 metaInfo/model 不存在或
源空 found:false/建草稿合法 XML 落库/坏 XML 422 且 verify 零 saveModel/PUT 404/PUT 合法
更新 status:0/PUT 坏 XML 422/from-procdef 净化 XML 与 idMap 落 metaInfo(捕获 setMetaInfo
断言 JSON 含 eip4IdMap+中文 originalKey、捕获 editorSource 断言净化无中文 id)/procDef
不存在 found:false 不落库)。flowable-service 全模块 84 测试绿(新增 11,既有 73 零改动)。
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,281 @@
|
||||
package com.eip4.flowservice.api;
|
||||
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.anyString;
|
||||
import static org.mockito.ArgumentMatchers.eq;
|
||||
import static org.mockito.Mockito.never;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
|
||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
|
||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.put;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
|
||||
import org.flowable.engine.RepositoryService;
|
||||
import org.flowable.engine.repository.Model;
|
||||
import org.flowable.engine.repository.ModelQuery;
|
||||
import org.flowable.engine.repository.ProcessDefinition;
|
||||
import org.flowable.engine.repository.ProcessDefinitionQuery;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.mockito.ArgumentCaptor;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.Mockito;
|
||||
import org.mockito.junit.jupiter.MockitoExtension;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.test.web.servlet.MockMvc;
|
||||
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
|
||||
|
||||
/**
|
||||
* 流程草稿 CRUD 端点薄层单测:mock RepositoryService + Model API(newModel/saveModel/addModelEditorSource/
|
||||
* getModelEditorSource/createModelQuery),验证列表/取草稿/建草稿(坏 XML 不落库)/PUT 404/from-procdef
|
||||
* 净化 XML 与 idMap 落 metaInfo。standaloneSetup 不挂 FlowExceptionHandler——422/404 由控制器本地 ResponseEntity
|
||||
* 显式产出(不经引擎异常映射)。
|
||||
*/
|
||||
@ExtendWith(MockitoExtension.class)
|
||||
class ProcessModelControllerTest {
|
||||
|
||||
@Mock RepositoryService repositoryService;
|
||||
@Mock ModelQuery modelQuery;
|
||||
@Mock ProcessDefinitionQuery processDefinitionQuery;
|
||||
|
||||
MockMvc mvc;
|
||||
|
||||
// 最小 schema 合法 BPMN(default ns=BPMN20 MODEL),供 convertToBpmnModel(validateSchema=true) 过门。
|
||||
private static final String VALID_BPMN = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
|
||||
+ "<definitions xmlns=\"http://www.omg.org/spec/BPMN/20100524/MODEL\" targetNamespace=\"http://eip4\">"
|
||||
+ "<process id=\"p1\" name=\"p1\">"
|
||||
+ "<startEvent id=\"start\"/>"
|
||||
+ "<sequenceFlow id=\"f1\" sourceRef=\"start\" targetRef=\"end\"/>"
|
||||
+ "<endEvent id=\"end\"/>"
|
||||
+ "</process></definitions>";
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
ProcessModelController controller = new ProcessModelController(repositoryService);
|
||||
mvc = MockMvcBuilders.standaloneSetup(controller).build();
|
||||
}
|
||||
|
||||
@Test
|
||||
void 列表按最近更新降序返回id名key时间() throws Exception {
|
||||
Model m = Mockito.mock(Model.class);
|
||||
when(m.getId()).thenReturn("m1");
|
||||
when(m.getName()).thenReturn("总包合同交底");
|
||||
when(m.getKey()).thenReturn("总包合同交底");
|
||||
when(m.getLastUpdateTime()).thenReturn(new Date(0L)); // 1970-01-01T00:00:00Z
|
||||
when(repositoryService.createModelQuery()).thenReturn(modelQuery);
|
||||
when(modelQuery.orderByLastUpdateTime()).thenReturn(modelQuery);
|
||||
when(modelQuery.desc()).thenReturn(modelQuery);
|
||||
when(modelQuery.list()).thenReturn(List.of(m));
|
||||
|
||||
mvc.perform(get("/api/flow/models"))
|
||||
.andExpect(status().isOk())
|
||||
.andExpect(jsonPath("$.models[0].id").value("m1"))
|
||||
.andExpect(jsonPath("$.models[0].name").value("总包合同交底"))
|
||||
.andExpect(jsonPath("$.models[0].key").value("总包合同交底"))
|
||||
.andExpect(jsonPath("$.models[0].lastUpdateTime").value("1970-01-01T00:00:00Z"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void 取草稿返回XML与metaInfo() throws Exception {
|
||||
Model m = Mockito.mock(Model.class);
|
||||
when(m.getId()).thenReturn("m1");
|
||||
when(m.getName()).thenReturn("总包合同交底");
|
||||
when(m.getKey()).thenReturn("总包合同交底");
|
||||
when(m.getMetaInfo()).thenReturn("{\"eip4IdMap\":{\"总包合同交底\":\"n1\"}}");
|
||||
when(repositoryService.getModel("m1")).thenReturn(m);
|
||||
when(repositoryService.getModelEditorSource("m1"))
|
||||
.thenReturn("<definitions/>".getBytes(StandardCharsets.UTF_8));
|
||||
|
||||
mvc.perform(get("/api/flow/models/m1"))
|
||||
.andExpect(status().isOk())
|
||||
.andExpect(jsonPath("$.found").value(true))
|
||||
.andExpect(jsonPath("$.id").value("m1"))
|
||||
.andExpect(jsonPath("$.key").value("总包合同交底"))
|
||||
.andExpect(jsonPath("$.bpmnXml").value("<definitions/>"))
|
||||
.andExpect(jsonPath("$.metaInfo").value("{\"eip4IdMap\":{\"总包合同交底\":\"n1\"}}"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void 取草稿_model不存在返found_false() throws Exception {
|
||||
when(repositoryService.getModel("nope")).thenReturn(null);
|
||||
|
||||
mvc.perform(get("/api/flow/models/nope"))
|
||||
.andExpect(status().isOk())
|
||||
.andExpect(jsonPath("$.found").value(false));
|
||||
}
|
||||
|
||||
@Test
|
||||
void 取草稿_editorSource为空返found_false() throws Exception {
|
||||
Model m = Mockito.mock(Model.class);
|
||||
when(repositoryService.getModel("m2")).thenReturn(m);
|
||||
when(repositoryService.getModelEditorSource("m2")).thenReturn(null);
|
||||
|
||||
mvc.perform(get("/api/flow/models/m2"))
|
||||
.andExpect(status().isOk())
|
||||
.andExpect(jsonPath("$.found").value(false));
|
||||
}
|
||||
|
||||
@Test
|
||||
void 建草稿_合法XML落库返modelId() throws Exception {
|
||||
Model m = Mockito.mock(Model.class);
|
||||
when(m.getId()).thenReturn("newid");
|
||||
when(repositoryService.newModel()).thenReturn(m);
|
||||
|
||||
String body = "{\"name\":\"新流程\",\"key\":\"newproc\",\"bpmnXml\":"
|
||||
+ jsonString(VALID_BPMN) + ",\"metaInfo\":\"{}\"}";
|
||||
mvc.perform(post("/api/flow/models").contentType(MediaType.APPLICATION_JSON).content(body))
|
||||
.andExpect(status().isOk())
|
||||
.andExpect(jsonPath("$.modelId").value("newid"));
|
||||
|
||||
verify(m).setName("新流程");
|
||||
verify(m).setKey("newproc");
|
||||
verify(repositoryService).saveModel(m);
|
||||
ArgumentCaptor<byte[]> src = ArgumentCaptor.forClass(byte[].class);
|
||||
verify(repositoryService).addModelEditorSource(eq("newid"), src.capture());
|
||||
org.junit.jupiter.api.Assertions.assertTrue(
|
||||
new String(src.getValue(), StandardCharsets.UTF_8).contains("<process id=\"p1\""));
|
||||
}
|
||||
|
||||
@Test
|
||||
void 建草稿_坏XML返422且不落库() throws Exception {
|
||||
String body = "{\"name\":\"坏流程\",\"key\":\"bad\",\"bpmnXml\":"
|
||||
+ jsonString("<definitions><broken") + "}";
|
||||
mvc.perform(post("/api/flow/models").contentType(MediaType.APPLICATION_JSON).content(body))
|
||||
.andExpect(status().isUnprocessableEntity())
|
||||
.andExpect(jsonPath("$.status").value(1))
|
||||
.andExpect(jsonPath("$.msg").isNotEmpty());
|
||||
|
||||
// 解析守卫先于任何写:坏 XML 一律不落库
|
||||
verify(repositoryService, never()).newModel();
|
||||
verify(repositoryService, never()).saveModel(any());
|
||||
verify(repositoryService, never()).addModelEditorSource(anyString(), any());
|
||||
}
|
||||
|
||||
@Test
|
||||
void 更新_草稿不存在返404() throws Exception {
|
||||
when(repositoryService.getModel("nope")).thenReturn(null);
|
||||
|
||||
String body = "{\"name\":\"x\",\"bpmnXml\":" + jsonString(VALID_BPMN) + "}";
|
||||
mvc.perform(put("/api/flow/models/nope").contentType(MediaType.APPLICATION_JSON).content(body))
|
||||
.andExpect(status().isNotFound())
|
||||
.andExpect(jsonPath("$.status").value(1));
|
||||
|
||||
verify(repositoryService, never()).saveModel(any());
|
||||
}
|
||||
|
||||
@Test
|
||||
void 更新_合法XML更新name与editorSource返status0() throws Exception {
|
||||
Model m = Mockito.mock(Model.class);
|
||||
when(repositoryService.getModel("m1")).thenReturn(m);
|
||||
|
||||
String body = "{\"name\":\"改名\",\"bpmnXml\":" + jsonString(VALID_BPMN) + ",\"metaInfo\":\"{}\"}";
|
||||
mvc.perform(put("/api/flow/models/m1").contentType(MediaType.APPLICATION_JSON).content(body))
|
||||
.andExpect(status().isOk())
|
||||
.andExpect(jsonPath("$.status").value(0));
|
||||
|
||||
verify(m).setName("改名");
|
||||
verify(m).setMetaInfo("{}");
|
||||
verify(repositoryService).saveModel(m);
|
||||
verify(repositoryService).addModelEditorSource(eq("m1"), any());
|
||||
}
|
||||
|
||||
@Test
|
||||
void 更新_坏XML返422且不落库() throws Exception {
|
||||
Model m = Mockito.mock(Model.class);
|
||||
when(repositoryService.getModel("m1")).thenReturn(m);
|
||||
|
||||
String body = "{\"name\":\"x\",\"bpmnXml\":" + jsonString("<nope") + "}";
|
||||
mvc.perform(put("/api/flow/models/m1").contentType(MediaType.APPLICATION_JSON).content(body))
|
||||
.andExpect(status().isUnprocessableEntity())
|
||||
.andExpect(jsonPath("$.status").value(1));
|
||||
|
||||
verify(repositoryService, never()).saveModel(any());
|
||||
verify(repositoryService, never()).addModelEditorSource(anyString(), any());
|
||||
}
|
||||
|
||||
@Test
|
||||
void fromProcDef_净化XML与idMap落metaInfo() throws Exception {
|
||||
// 存量流程 process id 为中文,from-procdef 净化后建草稿:editorSource 无中文 id,metaInfo JSON 含
|
||||
// eip4IdMap + sourceProcDefId + 中文 originalKey(发布时逆向还原原 key)。
|
||||
ProcessDefinition def = Mockito.mock(ProcessDefinition.class);
|
||||
when(def.getName()).thenReturn("总包合同交底");
|
||||
when(def.getKey()).thenReturn("总包合同交底");
|
||||
when(repositoryService.createProcessDefinitionQuery()).thenReturn(processDefinitionQuery);
|
||||
when(processDefinitionQuery.processDefinitionId("def-1")).thenReturn(processDefinitionQuery);
|
||||
when(processDefinitionQuery.singleResult()).thenReturn(def);
|
||||
|
||||
String xml = "<definitions>"
|
||||
+ "<process id=\"总包合同交底\" name=\"总包合同交底\">"
|
||||
+ "<userTask id=\"sid-A\"/>"
|
||||
+ "<sequenceFlow id=\"f1\" sourceRef=\"总包合同交底\" targetRef=\"sid-A\"/>"
|
||||
+ "</process></definitions>";
|
||||
when(repositoryService.getProcessModel("def-1"))
|
||||
.thenReturn(new ByteArrayInputStream(xml.getBytes(StandardCharsets.UTF_8)));
|
||||
|
||||
Model m = Mockito.mock(Model.class);
|
||||
when(m.getId()).thenReturn("draft-1");
|
||||
when(repositoryService.newModel()).thenReturn(m);
|
||||
|
||||
String body = "{\"procDefId\":\"def-1\"}";
|
||||
mvc.perform(post("/api/flow/models/from-procdef").contentType(MediaType.APPLICATION_JSON).content(body))
|
||||
.andExpect(status().isOk())
|
||||
.andExpect(jsonPath("$.modelId").value("draft-1"));
|
||||
|
||||
verify(m).setName("总包合同交底");
|
||||
verify(m).setKey("总包合同交底");
|
||||
// metaInfo:JSON 含 eip4IdMap + sourceProcDefId + 中文 originalKey
|
||||
ArgumentCaptor<String> meta = ArgumentCaptor.forClass(String.class);
|
||||
verify(m).setMetaInfo(meta.capture());
|
||||
String metaJson = meta.getValue();
|
||||
org.junit.jupiter.api.Assertions.assertTrue(metaJson.contains("eip4IdMap"), metaJson);
|
||||
org.junit.jupiter.api.Assertions.assertTrue(metaJson.contains("\"sourceProcDefId\":\"def-1\""), metaJson);
|
||||
org.junit.jupiter.api.Assertions.assertTrue(metaJson.contains("\"originalKey\":\"总包合同交底\""), metaJson);
|
||||
org.junit.jupiter.api.Assertions.assertTrue(metaJson.contains("总包合同交底"), metaJson); // idMap 键含中文原 id
|
||||
// editorSource:净化后 XML 不再含中文 id(id/ref 属性),name 保留中文
|
||||
ArgumentCaptor<byte[]> src = ArgumentCaptor.forClass(byte[].class);
|
||||
verify(repositoryService).addModelEditorSource(eq("draft-1"), src.capture());
|
||||
String savedXml = new String(src.getValue(), StandardCharsets.UTF_8);
|
||||
org.junit.jupiter.api.Assertions.assertFalse(savedXml.contains("id=\"总包合同交底\""), savedXml);
|
||||
org.junit.jupiter.api.Assertions.assertFalse(savedXml.contains("sourceRef=\"总包合同交底\""), savedXml);
|
||||
org.junit.jupiter.api.Assertions.assertTrue(savedXml.contains("name=\"总包合同交底\""), savedXml);
|
||||
}
|
||||
|
||||
@Test
|
||||
void fromProcDef_procDef不存在返found_false不落库() throws Exception {
|
||||
when(repositoryService.createProcessDefinitionQuery()).thenReturn(processDefinitionQuery);
|
||||
when(processDefinitionQuery.processDefinitionId("nope")).thenReturn(processDefinitionQuery);
|
||||
when(processDefinitionQuery.singleResult()).thenReturn(null);
|
||||
|
||||
mvc.perform(post("/api/flow/models/from-procdef")
|
||||
.contentType(MediaType.APPLICATION_JSON).content("{\"procDefId\":\"nope\"}"))
|
||||
.andExpect(status().isOk())
|
||||
.andExpect(jsonPath("$.found").value(false));
|
||||
|
||||
verify(repositoryService, never()).newModel();
|
||||
}
|
||||
|
||||
/** 把任意字符串编成 JSON 字符串字面量(转义引号/反斜杠/换行),用于拼请求体。 */
|
||||
private static String jsonString(String s) {
|
||||
StringBuilder sb = new StringBuilder("\"");
|
||||
for (int i = 0; i < s.length(); i++) {
|
||||
char c = s.charAt(i);
|
||||
switch (c) {
|
||||
case '"' -> sb.append("\\\"");
|
||||
case '\\' -> sb.append("\\\\");
|
||||
case '\n' -> sb.append("\\n");
|
||||
case '\r' -> sb.append("\\r");
|
||||
case '\t' -> sb.append("\\t");
|
||||
default -> sb.append(c);
|
||||
}
|
||||
}
|
||||
return sb.append("\"").toString();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user