← Back to Pull requests

Improve HL7v2 loose parser test coverage #399

Open
NicoPiel opened this pull request with 236 lines diff across 5 files
View on GitHub
NicoPiel NicoPiel commented on Jul 30, 2026 18:08 UTC
Author

Adds comprehensive unit test coverage and supporting tweaks for the HL7v2 loose serializer/parser:

  • Introduces a TestUtil.normalizeLineEndings helper to make tests resilient across platforms.

  • Expands ER7Serializer tests to cover:

  • - transformWithoutSerializing behavior for various delimiter and convertLineBreaks configurations (including the delimiter-replace branch).
    - handling of external DTDs and SAX errors.
    - encoding character handling, trailing separators, empty segments, and header-only messages.
  • Renames and refreshes the HL7 serializer test suite, normalizing line endings in assertions and re-enabling an orphaned loose-serializer test run.

  • Adds a round-trip invariant test asserting ER7 -> XML -> ER7 preserves message content (with normalized line endings).

  • Updates expected HL7 XML fixtures to reflect default subcomponent-handling behavior.
  • Diff: 236 lines (+189 / -47) across 5 files Reviewer: mgaffigan Review duration: 163179m
    All Vouch review confidence checks passed
    Residual risk 0.09 = Change Risk (0.49) × (1 − Review Confidence (0.82)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.49 · 236 lines diff across 5 files
    0.49
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.78 · 5 substantive comment(s)
    0.78
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 50%
    82%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Diff Size diff_size
    0.327
    Real PR Conversation Timeline
    9 items
    github-actions[bot] github-actions[bot] commented on Jul 30, 2026 18:09 UTC
    Bot

    Test Results


    684 tests  +29   684 ✅ +29   2m 2s ⏱️ +48s
    110 suites + 1     0 💤 ± 0 
    110 files   + 1     0 ❌ ± 0 

    Results for commit e353553a. ± Comparison against base commit 776690e0.

    [test-results]:data:application/gzip;base64,H4sIAKOUa2oC/13MSw6DIBSF4a0Yxh3wKHjtZhp6gYRUpUEYme69YB9ih/93krMS50e7kEvHGD11ZMk+NWly1MmHuQLnBcqY6qzg/K3rkhH/6e4fhegPnPbjAWyMIX4k5nn/rHG8fMv+uHVzuHX7h2GafCpBrJBCSqERKDWg0ShEAHMztlcDBd47wQXjA3m+ADtBFqAJAQAA

    :recycle: This comment has been updated with latest results.

    mgaffigan mgaffigan approved these changes on Jul 30, 2026 18:55 UTC
    Approved Reviewer

    Approved these changes.

    tonygermano tonygermano commented on code on Jul 31, 2026 07:37 UTC
    Code Review
    server/src/test/java/com/mirth/connect/plugins/datatypes/hl7v2/ER7SerializerTest.java:1
    @@ -53,4 +121,86 @@ public void testValidFromXMLWithExternalDTD() throws Exception {
     		
     		assertFalse(exceptionThrown);
     	}
    +
    +	@Test
    +	public void testToXMLWithCustomEncodingCharacters() throws Exception {
    +		String er7 = "MSH#@%\\$#App1#Fac1\rPID#1#Smith@John";
    +
    +		assertEquals(XML_DECL + "<HL7Message><MSH><MSH.1>#</MSH.1><MSH.2>@%\\$</MSH.2>"
    +				+ "<MSH.3><MSH.3.1>App1</MSH.3.1></MSH.3>"
    +				+ "<MSH.4><MSH.4.1>Fac1</MSH.4.1></MSH.4></MSH>"
    +				+ "<PID><PID.1><PID.1.1>1</PID.1.1></PID.1>"
    +				+ "<PID.2><PID.2.1>Smith</PID.2.1><PID.2.2>John</PID.2.2></PID.2></PID></HL7Message>",
    +				serializer.toXML(er7));
    +	}
    +
    +	@Test
    +	public void testToXMLWithHeaderOnlyAndNoTrailingFieldSeparator() throws Exception {
    +		// Covers ER7Reader's nextDelimiter == -1 branch: MSH-2 runs to end of message.
    +		assertEquals(XML_DECL + "<HL7Message><MSH><MSH.1>|</MSH.1><MSH.2>^~\\&amp;</MSH.2></MSH></HL7Message>",
    +				serializer.toXML("MSH|^~\\&"));
    +	}
    +
    +	@Test
    +	public void testToXMLAppliesMirth1544FixupToNonHeaderFirstSegment() throws Exception {
    +		/*
    +		 * Characterization of ER7Reader's MIRTH-1544 fixup. The "^~&|" check is a positional
    +		 * substring test that does not require a header segment, so it also fires on a Z-segment,
    +		 * installing '&' as the subcomponent separator. Recorded as current behavior, not endorsed.
    +		 */
    +		assertEquals(XML_DECL + "<HL7Message><ZZZ>"
    +				+ "<ZZZ.1><ZZZ.1.1></ZZZ.1.1><ZZZ.1.2></ZZZ.1.2></ZZZ.1>"
    +				+ "<ZZZ.1><ZZZ.1.1><ZZZ.1.1.1></ZZZ.1.1.1><ZZZ.1.1.2></ZZZ.1.1.2></ZZZ.1.1></ZZZ.1>"
    +				+ "<ZZZ.2><ZZZ.2.1><ZZZ.2.1.1>a</ZZZ.2.1.1><ZZZ.2.1.2>b</ZZZ.2.1.2></ZZZ.2.1></ZZZ.2>"
    +				+ "</ZZZ></HL7Message>",
    +				serializer.toXML("ZZZ|^~&|a&b"));

    suggestion (non-blocking): update comment to describe expected behavior

    I thought there was a bug in your test until I found the "MIRTH-1544" code that tests for the string your comment already calls out (^~&|) and then treats it as if the escape character is \ and missing.

    jonbartels jonbartels commented on code on Jul 31, 2026 12:38 UTC
    Code Review
    server/src/test/java/com/mirth/connect/model/converters/TestUtil.java:1
    @@ -52,4 +52,12 @@ public static String prettyPrintXml(String input) throws Exception {
         public static String convertCRToCRLF(String input) {
             return input.replaceAll("\r", "\r\n");
         }
    +
    +    /**
    +     * Collapses CRLF/CR/LF to LF and trims surrounding whitespace, so that expected values stored
    +     * in CRLF fixture files compare equal to output whose line separator is platform-dependent.
    +     */
    +    public static String normalizeLineEndings(String input) {
    +        return input.replaceAll("\r\n|\r|\n", "\n").trim();
    

    Non-blocking comment - Is there an Apache Commons method for this?

    kryskool kryskool approved these changes on Jul 31, 2026 12:39 UTC
    Approved Reviewer

    Approved these changes.

    NicoPiel NicoPiel commented on code on Jul 31, 2026 13:04 UTC
    Code Review
    server/src/test/java/com/mirth/connect/model/converters/TestUtil.java:1
    @@ -52,4 +52,12 @@ public static String prettyPrintXml(String input) throws Exception {
         public static String convertCRToCRLF(String input) {
             return input.replaceAll("\r", "\r\n");
         }
    +
    +    /**
    +     * Collapses CRLF/CR/LF to LF and trims surrounding whitespace, so that expected values stored
    +     * in CRLF fixture files compare equal to output whose line separator is platform-dependent.
    +     */
    +    public static String normalizeLineEndings(String input) {
    +        return input.replaceAll("\r\n|\r|\n", "\n").trim();
    

    No. Checked lang3, commons-io and commons-text.

    tonygermano tonygermano commented on code on Jul 31, 2026 15:13 UTC
    Code Review
    server/tests/test-hl7-batch-output.xml:1
    @@ -236,7 +236,10 @@
             <OBR.4>
                 <OBR.4.1>MRS</OBR.4.1>
                 <OBR.4.2>Shephard</OBR.4.2>
    -            <OBR.4.3>Jane MRI W/&amp;W/O CONTRAST</OBR.4.3>
    +            <OBR.4.3>
    +                <OBR.4.3.1>Jane MRI W/</OBR.4.3.1>
    +                <OBR.4.3.2>W/O CONTRAST</OBR.4.3.2>
    

    comment (non-blocking): ouch

    Apparently the default a long time ago used to be that "parse sub-components" was off. Since the opposite is true now, I understand why this was necessary to fix the test.

    But it pains me to look at because, aside from this data strangely including a name mixed with procedure data in OBR-4, I deal with the pain on a regular basis of radiology procedures containing & in the description and not being escaped in the hl7 messages.

    tonygermano tonygermano requested changes on Jul 31, 2026 15:31 UTC
    Changes Requested Reviewer

    Thanks for re-enabling and improving upon some of these old tests.

    Would it be possible to rebase and clean up the commit history a bit? My (and AIs) suggestion would be

    The goal of the rebase is just to create a clean, atomic history of logical steps, rather than a literal recording of your local trial-and-error.

    > An ideal commit history for this PR would look something like this (4 logical commits):

    > Fix orphaned HL7v2 test suite and stale fixtures (Squash of your original commits 02 + 03)

    >
    test: cover HL7v2 loose parser encoding and degenerate separators (Squash of your original commits 04 + 05)
    > test: cover ER7Serializer.transformWithoutSerializing (Squash of your original commits 06 + 07 + 09)
    >
    test: assert HL7v2 loose parser ER7 round-trip invariant (Your original commit 08)
    > This keeps the repository history incredibly readable, preserves your excellent commit messages, and perfectly isolates each area of testing you improved.

    Commits 1 and 10 would be dropped.

    Reviewers
    Assignees
    NicoPiel NicoPiel
    Labels
    risk: low confidence: 82.2% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 48.6% (MEDIUM) · Higher = larger blast radius
    48.6%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 82.2% · Lower = rushed or shallow review
    82.2%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 8.6% · Safe to merge
    8.6%