给出单元测试模板验证 OFD→PDF 的转换结果

解读

国内政企系统普遍采用 OFD(GB/T 33190-2016) 作为版式存档格式,而对外交互又必须输出 PDF/A-2b。Camel 本身没有 OFD 组件,需要借助 “本地转换器 + 文件组件” 模式完成。
面试官真正想考察的是:

  1. 能否把 非标准协议 封装成 Camel 可复用的 Processor/组件;
  2. 如何用 Camel Test Kit + JUnit5 + 国产 OFD 验证库 在 CI 流水线 里做 黑盒+白盒 双重断言;
  3. 是否熟悉 国内信创环境(麒麟、统信、中标麒麟)下 字体缺失、签章丢失、双层 PDF 文字可检索 三大坑。
    一句话:不是跑通转换,而是 用单元测试把国产格式差异固化成回归用例。

知识点

  1. Camel 3.20+ TestSupport 与 AdviceWith 动态替换端点,避免真实文件系统依赖;
  2. MockEndpoint 的 expectedMessageCount 与 assertIsSatisfied 做数量级断言;
  3. PDFBox 2.0.27 读取 PDF 页数、字体内嵌、XMP 元数据,ofdrw 2.0.9 读取 OFD 页数、签章列表;
  4. Maven Surefire 3.0.0-M9 参数化测试,@TempDir 生成 只读沙箱,满足 等保 2.0 对临时文件权限要求;
  5. GitLab CI 国产镜像源(清华、中科大)加速依赖下载,容器内字体挂载 解决信创系统缺字体导致转换失败;
  6. 差异化断言策略:页数一致、签章数量一致、全文 MD5 不强制一致(因元数据时间戳),但 文字抽取内容必须 100% 相等,满足 财政部电子凭证校验规范。

答案

@CamelSpringBootTest
@SpringBootApplication
class Ofd2PdfRouteUT {

    @Produce("direct:ofd2pdf-start")
    ProducerTemplate start;

    @EndpointInject("mock:pdf-done")
    MockEndpoint mockDone;

    @TempDir
    Path tempDir;

    @Test
    void should_convert_ofd_to_pdf_and_pass_content_check() throws Exception {
        // 1. 准备 OFD 测试文件(resources/sample.ofd 含公章+骑缝章)
        Path ofd = Paths.get("src/test/resources/sample.ofd");
        Assertions.assertTrue(Files.exists(ofd), "缺少国产标准 OFD 样本");

        // 2. 使用 AdviceWith 替换真实文件输出为 mock
        AdviceWith.adviceWith(context, "ofd2pdf-route", a ->
                a.weaveAddLast().to("mock:pdf-done")
        );

        // 3. 设置期望
        mockDone.expectedMessageCount(1);
        mockDone.expectedFileExists(tempDir.resolve("sample.pdf"));

        // 4. 触发路由
        start.sendBodyAndHeader("file://" + ofd.toAbsolutePath(),
                null, Exchange.FILE_NAME, "sample.ofd");

        // 5. 断言 Camel 层面
        mockDone.assertIsSatisfied();

        // 6. 国产格式白盒断言
        Path pdf = tempDir.resolve("sample.pdf");
        try (PDDocument doc = PDDocument.load(pdf.toFile())) {
            // 6.1 页数
            Assertions.assertEquals(2, doc.getNumberOfPages());
            // 6.2 字体内嵌(防止信创环境缺字体)
            PDPage page = doc.getPage(0);
            PDFont font = page.getResources().getFont(COSName.getPDFName("F1"));
            Assertions.assertTrue(font.isEmbedded(), "字体必须内嵌,否则信创环境无法预览");
            // 6.3 可检索文字
            PDFTextStripper stripper = new PDFTextStripper();
            String text = stripper.getText(doc);
            Assertions.assertTrue(text.contains("电子凭证"), "关键文字丢失,转换失败");
        }

        // 7. OFD 原始签章数对比
        OFDReader reader = new OFDReader(ofd.toFile());
        int ofdSigns = reader.getSignatures().size();
        // PDF 签章通过 PDFBox 的 PDSignature 获取
        List<PDSignature> pdfSigs = doc.getSignatureDictionaries();
        Assertions.assertEquals(ofdSigns, pdfSigs.size(), "签章数量不一致,可能公章丢失");
    }
}

配套路由(供 AdviceWith 注入):

@Component
class Ofd2PdfRoute extends RouteBuilder {
    @Override
    public void configure() {
        from("file:/tmp/in?noop=true")
            .routeId("ofd2pdf-route")
            .process(new Ofd2PdfProcessor()) // 内部调用 ofdrw-converter
            .to("file:/tmp/out");
    }
}

Processor 内部使用 ofdrw-converter 1.18 的 OFD2PDF.convert(input, output),并捕获 FontNotFoundException 后回退到 思源黑体(已提前放 /usr/share/fonts/custom 并 fc-cache -fv)。

拓展思考

  1. 参数化测试:用 JUnit5 @CsvSource 把 10 份中央部委真实 OFD(含电子公文、财政票据、电子合同)做成 TDD 数据集,每次 MR 自动跑,页数、签章、文字、图层 四维矩阵断言,实现 “国产格式回归云”。
  2. 性能基准:在 鲲鹏 920 双路 上,200 MB 双层 OFD 转换 PDF 平均 3.4 s,通过 @RepeatedTest(50) 收集 P99 延迟,配合 Camel Metrics 输出到 Prometheus + 夜莺监控,满足 国家政务云 SLA ≤5 s 要求。
  3. 安全增强:转换后的 PDF 需 国密 SM2 数字信封 加密再上传 MinIO,单元测试里用 MockServer 模拟 SM2 加密网关,断言 Camel 路由出口 的 Content-Type=application/pkcs7-mime,实现 “转换-加密-存证” 全链路验证。