package cn.lihu.jh.module.ecg.service.appointment;
import org.junit.Test;
import java.lang.reflect.Method;
import java.util.*;
import static org.junit.Assert.*;
/**
* 回归测试:HIS 报文解析的**类型安全导航**。
*
*
背景
* 2026-09-28 生产日志出现 **287 次** {@code ClassCastException:
* class java.lang.String cannot be cast to class java.util.Map},
* 集中在 {@code extractCheckCodeFromComponent2} 等方法的链式强制转换上。
*
* 原因是 HIS 报文经 Jackson 反序列化后**同一字段类型并不稳定**:
* {@code observationRequest.code} 在部分报文中是 {@code Map}
* ({@code code={code=7,...}}),在另一些报文中是**空字符串**({@code code=})。
* 链式 {@code .map(m -> (Map) m.get("code"))} 遇到字符串即抛异常,
* **导致整条消息处理中断**(预约时间、房间等字段写不进库)。
*
* 本测试锁定修复后的行为:遇到任意类型混合都返回 null 而**不抛异常**。
*/
public class HisParseSafetyTest {
/** 构造典型报文片段:{@code code=} 为空字符串 */
private static Map payloadWithEmptyStringCode() {
Map codeLeaf = new LinkedHashMap<>();
codeLeaf.put("code", ""); // ← 关键:空字符串而非 Map
codeLeaf.put("displayName", mapOf("value", "检查结束"));
Map obs = new LinkedHashMap<>();
obs.put("id", mapOf("item", mapOf("extension", "2126092800574")));
obs.put("code", ""); // ← 关键:observationRequest.code 是空字符串
obs.put("component1", mapOf("processStep", mapOf("code", codeLeaf)));
obs.put("component2", mapOf("observationRequest", mapOf("code", "")));
return mapOf("controlActProcess",
mapOf("subject", mapOf("placerGroup", mapOf("component2", obs))));
}
/** 正常形态:{@code code} 是 Map(按 S0201ECG 真实结构构造) */
private static Map payloadWithMapCode() {
// 检查状态:component2.observationRequest.component1.processStep.code.code
Map statusCode = mapOf(
"code", "7",
"codeSystem", "2.16.156.10011.2.5.1.12",
"displayName", mapOf("value", "检查结束"));
Map processStep = mapOf("code", statusCode);
Map component1 = mapOf("processStep", processStep);
// 检查项目:component2.observationRequest.code.code
Map checkCode = mapOf(
"code", "Y1202065",
"displayName", mapOf("value", "床边心电图(常规+频谱)"));
Map innerObs = mapOf(
"id", mapOf("item", mapOf("root", "2.16.156.10011.1.28", "extension", "113034157")),
"code", checkCode,
"component1", component1);
Map component2 = mapOf("observationRequest", innerObs);
Map outerObs = mapOf("component2", component2);
return mapOf("controlActProcess",
mapOf("subject",
mapOf("observationRequest", outerObs)));
}
private static Map mapOf(Object... kv) {
Map m = new LinkedHashMap<>();
for (int i = 0; i + 1 < kv.length; i += 2) {
m.put((String) kv[i], kv[i + 1]);
}
return m;
}
/** 反射调用私有的 dig 方法 */
private static Map invokeDig(Map root, String... path) throws Exception {
Method m = AppointmentServiceImpl.class.getDeclaredMethod("dig", Map.class, String[].class);
m.setAccessible(true);
@SuppressWarnings("unchecked")
Map r = (Map) m.invoke(null, root, path);
return r;
}
/** 反射调用私有的 digText 方法 */
private static String invokeDigText(Map root, String... path) throws Exception {
Method m = AppointmentServiceImpl.class.getDeclaredMethod("digText", Map.class, String[].class);
m.setAccessible(true);
return (String) m.invoke(null, root, path);
}
// ==================== 核心:不得抛 ClassCastException ====================
@Test
public void testDigText_emptyStringInPath_shouldReturnNullNotThrow() throws Exception {
Map payload = payloadWithEmptyStringCode();
// code 是空字符串 → 应安全返回 null,而不是抛 ClassCastException
String v = invokeDigText(payload, "controlActProcess", "subject", "placerGroup",
"component2", "observationRequest", "code", "code");
assertNull(v);
}
@Test
public void testDigText_stringWhereMapExpected_shouldNotThrow() throws Exception {
// 整条路径上放字符串,模拟最恶劣的形态
Map payload = mapOf("controlActProcess", "I-AM-A-STRING");
assertNull(invokeDigText(payload, "controlActProcess", "subject", "placerGroup"));
}
@Test
public void testDig_emptyStringInPath_shouldReturnNullNotThrow() throws Exception {
Map payload = payloadWithEmptyStringCode();
assertNull(invokeDig(payload, "controlActProcess", "subject", "placerGroup",
"component2", "observationRequest", "component1", "processStep", "code", "ZDY"));
}
@Test
public void testDig_nullInPath_shouldReturnNull() throws Exception {
assertNull(invokeDig(mapOf("a", null), "a", "b"));
assertNull(invokeDig(new LinkedHashMap<>(), "notExist"));
}
@Test
public void testDigText_listInPath_shouldTakeFirstElement() throws Exception {
// item=[{code=X}] 形态:digText 应自动取首元素继续下钻
Map payload = mapOf("methodCode",
mapOf("item", Collections.singletonList(mapOf("code", "M001"))));
assertEquals("M001", invokeDigText(payload, "methodCode", "item", "code"));
}
@Test
public void testDigText_emptyListInPath_shouldReturnNull() throws Exception {
Map payload = mapOf("methodCode", mapOf("item", Collections.emptyList()));
assertNull(invokeDigText(payload, "methodCode", "item", "code"));
}
@Test
public void testDigText_shouldStillReadNormalValues() throws Exception {
Map payload = payloadWithMapCode();
// 正常形态必须照常读出,不能因加了防御而失效
// 与生产代码 extractCheckCodeFromComponent2 使用的是**同样的路径写法**
assertEquals("Y1202065", invokeDigText(payload, "controlActProcess", "subject",
"observationRequest", "component2", "observationRequest", "code", "code"));
}
@Test
public void testDigText_shouldReadProcessStepStatus() throws Exception {
// 检查状态码:controlActProcess.subject.observationRequest.component2
// .observationRequest.component1.processStep.code.code
Map payload = payloadWithMapCode();
assertEquals("7", invokeDigText(payload, "controlActProcess", "subject",
"observationRequest", "component2", "observationRequest",
"component1", "processStep", "code", "code"));
}
@Test
public void testDigText_basicTraversal() throws Exception {
// 最基础:三层 Map 取值
Map payload = mapOf("a", mapOf("b", mapOf("c", "VALUE")));
assertEquals("VALUE", invokeDigText(payload, "a", "b", "c"));
// 两层
assertEquals("X", invokeDigText(mapOf("a", mapOf("b", "X")), "a", "b"));
// 一层
assertEquals("Y", invokeDigText(mapOf("a", "Y"), "a"));
// Map 末端取 value
assertEquals("Z", invokeDigText(mapOf("a", mapOf("b", mapOf("value", "Z"))), "a", "b"));
}
@Test
public void testDigText_blankString_shouldReturnNull() throws Exception {
// 空/纯空白字符串应视为"无值",便于上层用 isEmpty 判断
Map payload = mapOf("k", " ");
assertNull(invokeDigText(payload, "k"));
assertNull(invokeDigText(mapOf("k", ""), "k"));
}
// ==================== 医嘱号提取(findExtensionByRoot) ====================
//
// 依据 HIS《交互服务通用接口文档》:
// 6.1 申请单新增服务 → observationRequest.id.item[root=2.16.156.10011.1.28] 注释为「医嘱ID」
// 6.4 检查预约状态服务 → 同一 root 注释为「医嘱号」
// 实测 S0201ECG 220 条、S0202ECG 29 条均携带该值(形如 112663505)。
//
// 关键风险:id.item 在部分服务中是**数组**,同时含
// 1.24 申请单号 与 1.28 医嘱号
// 若用 digText(遇 List 只取首元素)会把**申请单号当成医嘱号**。
// 下列用例锁定「按 root 匹配」的正确行为。
private static final String ROOT_ORDER_NO = "2.16.156.10011.1.28";
private static final String ROOT_APPLY_NO = "2.16.156.10011.1.24";
/** 反射调用私有的 findExtensionByRoot 方法 */
private static String invokeFindExtensionByRoot(Object idNode, String root) throws Exception {
Method m = AppointmentServiceImpl.class.getDeclaredMethod("findExtensionByRoot", Object.class, String.class);
m.setAccessible(true);
return (String) m.invoke(null, idNode, root);
}
@Test
public void testFindExtensionByRoot_singleMap_shouldReturnExtension() throws Exception {
// 实测 S0201ECG 的真实形态:id={item={root=1.28, extension=112663505}}
Object idNode = mapOf("item", mapOf("root", ROOT_ORDER_NO, "extension", "112663505"));
assertEquals("112663505", invokeFindExtensionByRoot(idNode, ROOT_ORDER_NO));
}
@Test
public void testFindExtensionByRoot_listWithBothRoots_shouldPickOrderNo() throws Exception {
// 关键用例:数组同时含 1.24(申请单号) 与 1.28(医嘱号)
// digText 会取首元素(申请单号)→ 错;findExtensionByRoot 必须按 root 命中 1.28
List