fix(gateway): T2.2 评审小项——幂等注释/降级标记/PUT胶水测试
M-1(javadoc):FlowableClient put()/post() 点破幂等性差别——PUT 覆盖写幂等,execute 连接级重试安全;
POST 非幂等,重试存在重复创建窗口,接受理由=草稿廉价可覆盖(非不可逆副作用)+本地同机链路瞬时故障概率极低,
语义级 4xx/5xx 绝不重试故无此窗口。
M-2(一行):wftemplates 读库异常降级分支返回 {templates:[],degraded:true},正常路径不带 degraded 键;
测试两分支各断言(正常 doesNotContainKey degraded / 降级 containsEntry degraded=true)。
M-3(胶水):照 glueMvc 补 PUT /api/workflow/models/{id} MockMvc 路由层测试(PUT 谓词匹配+@PathVariable
绑定 id=m1+200 透传)。
测试:ProcessModelerControllerTest 20→21(+PUT 胶水),FlowableClientTest 18(M-1 纯注释不加测试)。
platform-flow-gateway 全模块绿,BUILD SUCCESS,既有测试零改动。
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
@@ -368,6 +368,12 @@ public class FlowableClient {
|
||||
.body(TASK_LIST));
|
||||
}
|
||||
|
||||
/**
|
||||
* POST 转发。<b>幂等性</b>:POST 非幂等——{@link #execute} 的<b>连接级</b>重试(连不上/读超时后重放)
|
||||
* 存在重复创建窗口。接受该风险的理由:①草稿创建廉价且可覆盖(重复草稿人工删即可,非扣款类不可逆副作用);
|
||||
* ②本地网关↔flowable-service 同机链路,连接级瞬时故障概率极低。语义级 4xx/5xx 由 {@link #execute} 保证
|
||||
* <b>绝不</b>重试,故业务错无重复窗口。对比 {@link #put}(覆盖写幂等,重试安全)。
|
||||
*/
|
||||
private <T> T post(String op, String path, Object body, Class<T> respType) {
|
||||
return execute(op, () -> restClient.post()
|
||||
.uri(path)
|
||||
@@ -387,6 +393,9 @@ public class FlowableClient {
|
||||
/**
|
||||
* PUT 转发(RestClient 无既有封装 → 照 {@link #post} 补)。{@code uriTemplate}+{@code uriVars} 走 RestClient
|
||||
* 严格编码({@code id} 作 uri 变量,禁字符串拼接);连接级重试 + 4xx/5xx 透传语义与 post 一致。
|
||||
*
|
||||
* <p><b>幂等性</b>:PUT 是覆盖写、天然幂等 → {@link #execute} 的连接级重试<b>安全</b>(重放同一 PUT 落到同一
|
||||
* 草稿 id,不产生额外副作用),无 {@link #post} 那种重复创建窗口。</p>
|
||||
*/
|
||||
private <T> T put(String op, String uriTemplate, Object body, Class<T> respType, Object... uriVars) {
|
||||
return execute(op, () -> restClient.put()
|
||||
|
||||
@@ -190,14 +190,20 @@ public class ProcessModelerController {
|
||||
return denied;
|
||||
}
|
||||
List<Map<String, Object>> templates;
|
||||
boolean degraded = false;
|
||||
try {
|
||||
templates = jdbcTemplate.query(WFTEMPLATES_SQL, WFTEMPLATE_ROW);
|
||||
} catch (RuntimeException e) {
|
||||
log.warn("view_wftemplate 选择器读取降级为空列表: {}", e.toString());
|
||||
templates = List.of();
|
||||
degraded = true;
|
||||
}
|
||||
Map<String, Object> resp = new LinkedHashMap<>();
|
||||
resp.put("templates", templates);
|
||||
if (degraded) {
|
||||
// 降级标记:正常路径不带此键,前端可据此区分「真无模板」与「读库失败」并提示重试。
|
||||
resp.put("degraded", true);
|
||||
}
|
||||
return ResponseEntity.ok(resp);
|
||||
}
|
||||
|
||||
|
||||
@@ -4,12 +4,14 @@ import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.assertThatThrownBy;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.anyString;
|
||||
import static org.mockito.ArgumentMatchers.eq;
|
||||
import static org.mockito.Mockito.mock;
|
||||
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;
|
||||
|
||||
@@ -325,6 +327,8 @@ class ProcessModelerControllerTest {
|
||||
assertThat(templates).hasSize(2);
|
||||
assertThat(templates.get(0)).containsEntry("id", "wft-0001").containsEntry("name", "通知管理");
|
||||
assertThat(templates.get(1)).containsEntry("id", "wft-0002").containsEntry("name", "wft-0002");
|
||||
// 正常路径不带 degraded 键(与降级分支区分)
|
||||
assertThat(resp.getBody()).doesNotContainKey("degraded");
|
||||
}
|
||||
|
||||
@SuppressWarnings("rawtypes")
|
||||
@@ -339,7 +343,7 @@ class ProcessModelerControllerTest {
|
||||
|
||||
@SuppressWarnings({"unchecked", "rawtypes"})
|
||||
@Test
|
||||
void wftemplates_读库异常_降级空列表不500() {
|
||||
void wftemplates_读库异常_降级空列表带degraded标记不500() {
|
||||
when(jdbcTemplate.query(anyString(), any(RowMapper.class)))
|
||||
.thenThrow(new org.springframework.dao.DataAccessResourceFailureException("db down"));
|
||||
|
||||
@@ -348,5 +352,23 @@ class ProcessModelerControllerTest {
|
||||
assertThat(resp.getStatusCode()).isEqualTo(HttpStatus.OK);
|
||||
List<Map<String, Object>> templates = (List<Map<String, Object>>) resp.getBody().get("templates");
|
||||
assertThat(templates).isEmpty();
|
||||
assertThat(resp.getBody()).containsEntry("degraded", true); // 降级分支带标记(正常路径无此键)
|
||||
}
|
||||
|
||||
// ---- PUT 路由胶水(M-3):MockMvc 真走 PUT /api/workflow/models/{id},验路径匹配 + @PathVariable 绑定 + 200 透传 ----
|
||||
|
||||
@Test
|
||||
void 胶水_更新草稿_PUT路由绑定pathVar并200透传() throws Exception {
|
||||
Map<String, Object> downstream = Map.of("status", 0);
|
||||
when(flowableClient.updateModel(eq("m1"), any())).thenReturn(downstream);
|
||||
|
||||
glueMvc().perform(put("/api/workflow/models/{id}", "m1")
|
||||
.contentType(MediaType.APPLICATION_JSON)
|
||||
.content("{\"bpmnXml\":\"<definitions/>\"}"))
|
||||
.andExpect(status().isOk())
|
||||
.andExpect(jsonPath("$.status").value(0));
|
||||
|
||||
// 路由匹配 PUT 谓词 + {id} 绑定到 "m1" 交给控制器
|
||||
verify(flowableClient).updateModel(eq("m1"), any());
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user