From e350ce17145f7a919c548d3d2c949f0e8ab2ed6b Mon Sep 17 00:00:00 2001 From: Basilio Bogado <541149+basiliskus@users.noreply.github.com> Date: Mon, 23 Dec 2024 10:49:58 -0800 Subject: [PATCH] Moved logic in HL7Parser.parseMessageFieldValue to HL7Message.getValue to avoid circular dependency --- .../rse2e/hl7/HL7Message.java | 24 +++++- .../rse2e/hl7/HL7Parser.java | 30 -------- .../rse2e/hl7/HL7MessageTest.groovy | 7 +- .../rse2e/hl7/HL7ParserTest.groovy | 75 ------------------- 4 files changed, 24 insertions(+), 112 deletions(-) diff --git a/rs-e2e/src/main/java/gov/hhs/cdc/trustedintermediary/rse2e/hl7/HL7Message.java b/rs-e2e/src/main/java/gov/hhs/cdc/trustedintermediary/rse2e/hl7/HL7Message.java index 8d4031336..05d0cae26 100644 --- a/rs-e2e/src/main/java/gov/hhs/cdc/trustedintermediary/rse2e/hl7/HL7Message.java +++ b/rs-e2e/src/main/java/gov/hhs/cdc/trustedintermediary/rse2e/hl7/HL7Message.java @@ -2,6 +2,7 @@ import gov.hhs.cdc.trustedintermediary.wrappers.HealthData; import java.util.List; +import java.util.regex.Pattern; /** * Represents a HL7 message that implements the HealthData interface and adds methods to access the @@ -74,6 +75,27 @@ public HL7Segment getSegment(String name) throws HL7MessageException { public String getValue(String path) throws HL7MessageException { HL7Path hl7Path = HL7Parser.parsePath(path); - return HL7Parser.parseMessageFieldValue(this, hl7Path); + int[] indices = hl7Path.indices(); + List fields = this.getSegment(hl7Path.segmentName()).fields(); + char[] delimiters = this.getEncoding().getOrderedDelimiters(); + + if (indices[0] > fields.size()) { + return ""; + } + + String value = fields.get(indices[0] - 1); + for (int i = 1; i < indices.length; i++) { + if (i >= delimiters.length) { + throw new HL7ParserException("Invalid HL7 path: too many sub-levels provided"); + } + char segmentDelimiter = delimiters[i]; + int index = indices[i] - 1; + String[] parts = value.split(Pattern.quote(String.valueOf(segmentDelimiter))); + if (index < 0 || index >= parts.length) { + return ""; + } + value = parts[index]; + } + return value; } } diff --git a/rs-e2e/src/main/java/gov/hhs/cdc/trustedintermediary/rse2e/hl7/HL7Parser.java b/rs-e2e/src/main/java/gov/hhs/cdc/trustedintermediary/rse2e/hl7/HL7Parser.java index 267831638..5f78da3f0 100644 --- a/rs-e2e/src/main/java/gov/hhs/cdc/trustedintermediary/rse2e/hl7/HL7Parser.java +++ b/rs-e2e/src/main/java/gov/hhs/cdc/trustedintermediary/rse2e/hl7/HL7Parser.java @@ -37,36 +37,6 @@ public static HL7Message parseMessage(String content) { return new HL7Message(segments, HL7Encoding.fromEncodingField(encodingCharactersField)); } - public static String parseMessageFieldValue(HL7Message message, HL7Path hl7Path) - throws HL7ParserException, HL7MessageException { - if (hl7Path == null || hl7Path.indices().length == 0 || message == null) { - throw new HL7ParserException("Invalid HL7 path: message or path is null or empty"); - } - - int[] indices = hl7Path.indices(); - List fields = message.getSegment(hl7Path.segmentName()).fields(); - char[] delimiters = message.getEncoding().getOrderedDelimiters(); - - if (indices[0] > fields.size()) { - return ""; - } - - String value = fields.get(indices[0] - 1); - for (int i = 1; i < indices.length; i++) { - if (i >= delimiters.length) { - throw new HL7ParserException("Invalid HL7 path: too many sub-levels provided"); - } - char segmentDelimiter = delimiters[i]; - int index = indices[i] - 1; - String[] parts = value.split(Pattern.quote(String.valueOf(segmentDelimiter))); - if (index < 0 || index >= parts.length) { - return ""; - } - value = parts[index]; - } - return value; - } - public static HL7Path parsePath(String path) { Matcher matcher = HL7_FIELD_NAME_PATTERN.matcher(path); if (!matcher.matches()) { diff --git a/rs-e2e/src/test/groovy/gov/hhs/cdc/trustedintermediary/rse2e/hl7/HL7MessageTest.groovy b/rs-e2e/src/test/groovy/gov/hhs/cdc/trustedintermediary/rse2e/hl7/HL7MessageTest.groovy index d939316c1..5579c7f83 100644 --- a/rs-e2e/src/test/groovy/gov/hhs/cdc/trustedintermediary/rse2e/hl7/HL7MessageTest.groovy +++ b/rs-e2e/src/test/groovy/gov/hhs/cdc/trustedintermediary/rse2e/hl7/HL7MessageTest.groovy @@ -89,6 +89,7 @@ OBX|2|NM|||3122||||||F|||202402221854-0500||""" message.getValue("PID-40.4.2") == "LRI_NDBS_COMPONENT" message.getValue("PID-40.4.3") == "" message.getValue("NK1-33.4.1.1") == "Medicaid" + message.getValue("PID-99") == "" } def "getValue should throws an exception when the hl7 field index has too many sublevels"() { @@ -106,10 +107,4 @@ OBX|2|NM|||3122||||||F|||202402221854-0500||""" then: thrown(HL7MessageException) } - - // TODO: move to HL7EncodingTest - def "getEscapeCharacter should return the escape character"() { - expect: - message.getEncoding().getEscapeCharacter() == '\\' as char - } } diff --git a/rs-e2e/src/test/groovy/gov/hhs/cdc/trustedintermediary/rse2e/hl7/HL7ParserTest.groovy b/rs-e2e/src/test/groovy/gov/hhs/cdc/trustedintermediary/rse2e/hl7/HL7ParserTest.groovy index 503663efa..5432bfcdc 100644 --- a/rs-e2e/src/test/groovy/gov/hhs/cdc/trustedintermediary/rse2e/hl7/HL7ParserTest.groovy +++ b/rs-e2e/src/test/groovy/gov/hhs/cdc/trustedintermediary/rse2e/hl7/HL7ParserTest.groovy @@ -49,79 +49,4 @@ PID|||12345""" then: result.getSegments().get(0).fields().get(3) == "" } - - def "parseMessageFieldValue should handle different field levels"() { - given: - def message = HL7Parser.parseMessage("TST|value1|component1^component2|rep1~rep2^|comp1^~sub1&sub2") - - when: - def hl7Path = HL7Parser.parsePath(path) - def result = HL7Parser.parseMessageFieldValue(message, hl7Path) - - then: - result == expectedValue - - where: - scenario | path | expectedValue - "simple field" | "TST-1" | "value1" - "component" | "TST-2.2" | "component2" - "repetition" | "TST-3.1.2" | "rep2" - "subcomponent" | "TST-4.2.2.2" | "sub2" - "invalid index" | "TST-5" | "" - } - - def "parseMessageFieldValue returns an exception when hl7 path argument is empty or null"() { - when: - def hl7Path = HL7Parser.parsePath("") - HL7Parser.parseMessageFieldValue(null, hl7Path) - - then: - thrown(HL7ParserException) - - when: - def message = HL7Parser.parseMessage("MSH|fakeValues") - HL7Parser.parseMessageFieldValue(message, null) - - then: - thrown(HL7ParserException) - } - - def "parseMessageFieldValue returns an exception when message argument is null"() { - when: - HL7Parser.parseMessageFieldValue(null, null) - - then: - thrown(HL7ParserException) - - when: - def hl7Path = HL7Parser.parsePath("MSH-3") - HL7Parser.parseMessageFieldValue(null, hl7Path) - - then: - thrown(HL7ParserException) - } - - def "parseMessageFieldValue returns an exception when message argument is empty"() { - given: - def hl7Path = HL7Parser.parsePath("MSH-3") - def message = HL7Parser.parseMessage("") - - when: - def out = HL7Parser.parseMessageFieldValue(message, hl7Path) - - then: - thrown(HL7MessageException) - } - - def "parseMessageFieldValue returns an empty string if the indices in hl7 path are pointing outside the expected range"() { - given: - def message = HL7Parser.parseMessage("MSH|fakeValues\nOBR|fakeValues") - def hl7Path = HL7Parser.parsePath("MSH-3") - - when: - def out = HL7Parser.parseMessageFieldValue(message, hl7Path) - - then: - out == "" - } }