|
|
@@ -0,0 +1,177 @@
|
|
|
+package com.train.service.impl;
|
|
|
+
|
|
|
+import static org.junit.jupiter.api.Assertions.assertEquals;
|
|
|
+import static org.junit.jupiter.api.Assertions.assertNotNull;
|
|
|
+import static org.junit.jupiter.api.Assertions.assertNull;
|
|
|
+
|
|
|
+import java.util.Arrays;
|
|
|
+import java.util.Collections;
|
|
|
+import java.util.HashSet;
|
|
|
+import java.util.LinkedHashMap;
|
|
|
+import java.util.Map;
|
|
|
+import java.util.Set;
|
|
|
+
|
|
|
+import org.junit.jupiter.api.Test;
|
|
|
+
|
|
|
+import com.train.common.StateGuard;
|
|
|
+
|
|
|
+/**
|
|
|
+ * StateGuard 状态机全枚举单测(纯静态、不碰 Mapper、不启 Spring)。
|
|
|
+ *
|
|
|
+ * 覆盖:工单 7 态 × 目标态全枚举(合法转移通过 / 非法返回错误);晋升 3 态;批次 3 态;
|
|
|
+ * 以及 null / 空串 / 未知态边界。
|
|
|
+ *
|
|
|
+ * 合法可达关系红线(不得改动):pending→accepted→arrived→done→confirmed,
|
|
|
+ * cancel 仅从非终态可达;arrived 重复打卡为幂等自环。
|
|
|
+ */
|
|
|
+public class StateGuardTest {
|
|
|
+
|
|
|
+ /** 工单合法转移表(与 StateGuard.ORDER_TRANS 对齐) */
|
|
|
+ private static final Map<String, Set<String>> ORDER_LEGAL = new LinkedHashMap<>();
|
|
|
+
|
|
|
+ static {
|
|
|
+ ORDER_LEGAL.put(StateGuard.O_PENDING, setOf(StateGuard.O_ACCEPTED, StateGuard.O_CANCELLED));
|
|
|
+ ORDER_LEGAL.put(StateGuard.O_ACCEPTED, setOf(StateGuard.O_ARRIVED, StateGuard.O_CANCELLED));
|
|
|
+ ORDER_LEGAL.put(StateGuard.O_ARRIVED, setOf(StateGuard.O_DOING, StateGuard.O_DONE, StateGuard.O_ARRIVED));
|
|
|
+ ORDER_LEGAL.put(StateGuard.O_DOING, setOf(StateGuard.O_DONE));
|
|
|
+ ORDER_LEGAL.put(StateGuard.O_DONE, setOf(StateGuard.O_CONFIRMED));
|
|
|
+ ORDER_LEGAL.put(StateGuard.O_CONFIRMED, Collections.<String>emptySet());
|
|
|
+ ORDER_LEGAL.put(StateGuard.O_CANCELLED, Collections.<String>emptySet());
|
|
|
+ }
|
|
|
+
|
|
|
+ private static final String[] ORDER_STATES = {
|
|
|
+ StateGuard.O_PENDING, StateGuard.O_ACCEPTED, StateGuard.O_ARRIVED, StateGuard.O_DOING,
|
|
|
+ StateGuard.O_DONE, StateGuard.O_CONFIRMED, StateGuard.O_CANCELLED
|
|
|
+ };
|
|
|
+
|
|
|
+ /** 全枚举 7×7:checkOrder 通过 ⟺ 目标态在合法转移集内 */
|
|
|
+ @Test
|
|
|
+ void orderTransitionMatrix() {
|
|
|
+ int legalCount = 0;
|
|
|
+ for (String from : ORDER_STATES) {
|
|
|
+ for (String to : ORDER_STATES) {
|
|
|
+ String err = StateGuard.checkOrder(from, to);
|
|
|
+ boolean expectedLegal = ORDER_LEGAL.get(from).contains(to);
|
|
|
+ if (expectedLegal) {
|
|
|
+ assertNull(err, "应为合法转移: " + from + " -> " + to);
|
|
|
+ legalCount++;
|
|
|
+ } else {
|
|
|
+ assertNotNull(err, "应为非法转移: " + from + " -> " + to);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ // 主链合法转移:pending→accepted / pending→cancelled / accepted→arrived / accepted→cancelled /
|
|
|
+ // arrived→doing / arrived→done / arrived→arrived(自环) / doing→done / done→confirmed = 9
|
|
|
+ assertEquals(9, legalCount);
|
|
|
+ }
|
|
|
+
|
|
|
+ /** 主链逐跳:pending→accepted→arrived→done→confirmed 全程放行 */
|
|
|
+ @Test
|
|
|
+ void happyPathAllowed() {
|
|
|
+ assertNull(StateGuard.checkOrder(StateGuard.O_PENDING, StateGuard.O_ACCEPTED));
|
|
|
+ assertNull(StateGuard.checkOrder(StateGuard.O_ACCEPTED, StateGuard.O_ARRIVED));
|
|
|
+ assertNull(StateGuard.checkOrder(StateGuard.O_ARRIVED, StateGuard.O_DONE));
|
|
|
+ assertNull(StateGuard.checkOrder(StateGuard.O_DONE, StateGuard.O_CONFIRMED));
|
|
|
+ }
|
|
|
+
|
|
|
+ /** cancel 仅从 pending/accepted 可达(既有 V1 口径);到岗后/完成后均不可 cancel */
|
|
|
+ @Test
|
|
|
+ void cancelOnlyFromPendingOrAccepted() {
|
|
|
+ assertNull(StateGuard.checkOrder(StateGuard.O_PENDING, StateGuard.O_CANCELLED));
|
|
|
+ assertNull(StateGuard.checkOrder(StateGuard.O_ACCEPTED, StateGuard.O_CANCELLED));
|
|
|
+ // 既有实现:到岗/进行中/完成后不可 cancel(保留原合法可达关系,未擅自放宽)
|
|
|
+ assertNotNull(StateGuard.checkOrder(StateGuard.O_ARRIVED, StateGuard.O_CANCELLED));
|
|
|
+ assertNotNull(StateGuard.checkOrder(StateGuard.O_DOING, StateGuard.O_CANCELLED));
|
|
|
+ assertNotNull(StateGuard.checkOrder(StateGuard.O_DONE, StateGuard.O_CANCELLED));
|
|
|
+ assertNotNull(StateGuard.checkOrder(StateGuard.O_CONFIRMED, StateGuard.O_CANCELLED));
|
|
|
+ assertNotNull(StateGuard.checkOrder(StateGuard.O_CANCELLED, StateGuard.O_CANCELLED));
|
|
|
+ }
|
|
|
+
|
|
|
+ /** arrived 重复打卡=幂等自环:合法且不新增可达态 */
|
|
|
+ @Test
|
|
|
+ void arrivedSelfLoopIdempotent() {
|
|
|
+ assertNull(StateGuard.checkOrder(StateGuard.O_ARRIVED, StateGuard.O_ARRIVED));
|
|
|
+ // 但其它态不可"打卡"到 arrived
|
|
|
+ assertNotNull(StateGuard.checkOrder(StateGuard.O_PENDING, StateGuard.O_ARRIVED));
|
|
|
+ assertNotNull(StateGuard.checkOrder(StateGuard.O_DOING, StateGuard.O_ARRIVED));
|
|
|
+ assertNotNull(StateGuard.checkOrder(StateGuard.O_DONE, StateGuard.O_ARRIVED));
|
|
|
+ }
|
|
|
+
|
|
|
+ /** 晋升 3 态:evaluated→promoted/rejected;两者均终态 */
|
|
|
+ @Test
|
|
|
+ void promotionTransitions() {
|
|
|
+ assertNull(StateGuard.checkPromotion(StateGuard.PR_EVALUATED, StateGuard.PR_PROMOTED));
|
|
|
+ assertNull(StateGuard.checkPromotion(StateGuard.PR_EVALUATED, StateGuard.PR_REJECTED));
|
|
|
+ assertNotNull(StateGuard.checkPromotion(StateGuard.PR_PROMOTED, StateGuard.PR_PROMOTED));
|
|
|
+ assertNotNull(StateGuard.checkPromotion(StateGuard.PR_PROMOTED, StateGuard.PR_EVALUATED));
|
|
|
+ assertNotNull(StateGuard.checkPromotion(StateGuard.PR_REJECTED, StateGuard.PR_EVALUATED));
|
|
|
+ assertNotNull(StateGuard.checkPromotion(StateGuard.PR_REJECTED, StateGuard.PR_PROMOTED));
|
|
|
+ }
|
|
|
+
|
|
|
+ /** 批次 3 态单向:draft→published→closed,不可回退/跳级 */
|
|
|
+ @Test
|
|
|
+ void cohortTransitions() {
|
|
|
+ assertNull(StateGuard.checkCohort(StateGuard.C_DRAFT, StateGuard.C_PUBLISHED));
|
|
|
+ assertNull(StateGuard.checkCohort(StateGuard.C_PUBLISHED, StateGuard.C_CLOSED));
|
|
|
+ assertNotNull(StateGuard.checkCohort(StateGuard.C_DRAFT, StateGuard.C_CLOSED));
|
|
|
+ assertNotNull(StateGuard.checkCohort(StateGuard.C_PUBLISHED, StateGuard.C_DRAFT));
|
|
|
+ assertNotNull(StateGuard.checkCohort(StateGuard.C_CLOSED, StateGuard.C_PUBLISHED));
|
|
|
+ }
|
|
|
+
|
|
|
+ /** 边界:null / 空串 / 未知态 */
|
|
|
+ @Test
|
|
|
+ void boundaryStates() {
|
|
|
+ // from 为 null/空串
|
|
|
+ assertNotNull(StateGuard.checkOrder(null, StateGuard.O_ACCEPTED));
|
|
|
+ assertNotNull(StateGuard.checkOrder("", StateGuard.O_ACCEPTED));
|
|
|
+ assertNotNull(StateGuard.checkOrder(" ", StateGuard.O_ACCEPTED));
|
|
|
+ // to 为 null/空串
|
|
|
+ assertNotNull(StateGuard.checkOrder(StateGuard.O_PENDING, null));
|
|
|
+ assertNotNull(StateGuard.checkOrder(StateGuard.O_PENDING, ""));
|
|
|
+ // 未知态
|
|
|
+ assertNotNull(StateGuard.checkOrder("nonsense", StateGuard.O_ACCEPTED));
|
|
|
+ assertNotNull(StateGuard.checkOrder(StateGuard.O_PENDING, "nonsense"));
|
|
|
+ // promotion / cohort 同样拒绝空态
|
|
|
+ assertNotNull(StateGuard.checkPromotion(null, StateGuard.PR_PROMOTED));
|
|
|
+ assertNotNull(StateGuard.checkCohort(StateGuard.C_DRAFT, ""));
|
|
|
+ }
|
|
|
+
|
|
|
+ /** 收敛校验:ServiceOrderService 四跳的状态门禁必须与原内联条件逐跳等价 */
|
|
|
+ @Test
|
|
|
+ void serviceOrderGateMatchesLegacyInlineChecks() {
|
|
|
+ // 爬过单:仍仅 pending 放行;其余 6 态一律拒绝
|
|
|
+ assertNull(StateGuard.checkOrder(StateGuard.O_PENDING, StateGuard.O_ACCEPTED));
|
|
|
+ for (String from : ORDER_STATES) {
|
|
|
+ if (!StateGuard.O_PENDING.equals(from)) {
|
|
|
+ assertNotNull(StateGuard.checkOrder(from, StateGuard.O_ACCEPTED), "爬单仅 pending: " + from);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ // 到岗打卡:accepted 与 arrived 放行(arrived 自环=重复打卡刷新时间戳);其余 5 态拒绝
|
|
|
+ assertNull(StateGuard.checkOrder(StateGuard.O_ACCEPTED, StateGuard.O_ARRIVED));
|
|
|
+ assertNull(StateGuard.checkOrder(StateGuard.O_ARRIVED, StateGuard.O_ARRIVED));
|
|
|
+ for (String from : ORDER_STATES) {
|
|
|
+ if (!StateGuard.O_ACCEPTED.equals(from) && !StateGuard.O_ARRIVED.equals(from)) {
|
|
|
+ assertNotNull(StateGuard.checkOrder(from, StateGuard.O_ARRIVED), "到岗仅 accepted/arrived: " + from);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ // 提交完成:arrived 与 doing 放行;其余 5 态拒绝
|
|
|
+ assertNull(StateGuard.checkOrder(StateGuard.O_ARRIVED, StateGuard.O_DONE));
|
|
|
+ assertNull(StateGuard.checkOrder(StateGuard.O_DOING, StateGuard.O_DONE));
|
|
|
+ for (String from : ORDER_STATES) {
|
|
|
+ if (!StateGuard.O_ARRIVED.equals(from) && !StateGuard.O_DOING.equals(from)) {
|
|
|
+ assertNotNull(StateGuard.checkOrder(from, StateGuard.O_DONE), "爬过完成仅 arrived/doing: " + from);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ // 客户确认:仅 done 放行;其余 6 态拒绝
|
|
|
+ assertNull(StateGuard.checkOrder(StateGuard.O_DONE, StateGuard.O_CONFIRMED));
|
|
|
+ for (String from : ORDER_STATES) {
|
|
|
+ if (!StateGuard.O_DONE.equals(from)) {
|
|
|
+ assertNotNull(StateGuard.checkOrder(from, StateGuard.O_CONFIRMED), "确认仅 done: " + from);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private static Set<String> setOf(String... values) {
|
|
|
+ return new HashSet<>(Arrays.asList(values));
|
|
|
+ }
|
|
|
+}
|