From 466c3f41b4b4113c5db651f3d23f3dfd9334435c Mon Sep 17 00:00:00 2001 From: Basilio Bogado <541149+basiliskus@users.noreply.github.com> Date: Mon, 23 Dec 2024 12:07:54 -0800 Subject: [PATCH] Moved logic in HL7Parser.parsePath to HL7Path.parse to avoid circular dependency --- rs-e2e/README.md | 2 +- .../rse2e/hl7/HL7Message.java | 2 +- .../trustedintermediary/rse2e/hl7/HL7Parser.java | 15 --------------- .../trustedintermediary/rse2e/hl7/HL7Path.java | 16 ++++++++++++++++ 4 files changed, 18 insertions(+), 17 deletions(-) diff --git a/rs-e2e/README.md b/rs-e2e/README.md index c58000cad..77becbc2e 100644 --- a/rs-e2e/README.md +++ b/rs-e2e/README.md @@ -10,7 +10,7 @@ Information on how to set up the sample files evaluated by the tests can be foun - The output files generated by the framework are stored in an Azure blob storage container. Every time the tests are run, a cleanup task moves the files to a folder with the year/month/day format for better organization. The files are retained in the container for 90 days before being deleted - The code that organizes the files is using EST time zone. This means that if you are in PST, you may run into an issue if you submit a run before 9 PM PST and then run the tests after 9pm. You'd need to make sure to run both tasks before or after 9pm so the files are where they are expected to be -- The HL7 parser and expression evaluator returns an empty string when the value is not found. It will only throw an exception if the path is not valid +- The HL7 parser and expression evaluator returns an empty string when the value is not found. It will only throw an exception if the path is not a valid HL7 notation ## Running the tests 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 05d0cae26..b839b5498 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 @@ -74,7 +74,7 @@ public HL7Segment getSegment(String name) throws HL7MessageException { } public String getValue(String path) throws HL7MessageException { - HL7Path hl7Path = HL7Parser.parsePath(path); + HL7Path hl7Path = HL7Path.parse(path); int[] indices = hl7Path.indices(); List fields = this.getSegment(hl7Path.segmentName()).fields(); char[] delimiters = this.getEncoding().getOrderedDelimiters(); 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 5f78da3f0..a4a6c5808 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 @@ -4,14 +4,12 @@ import java.util.Arrays; import java.util.List; import java.util.Objects; -import java.util.regex.Matcher; import java.util.regex.Pattern; /** The HL7Parser class is responsible for parsing HL7 messages and extracting values from them. */ public class HL7Parser { static final String MSH_SEGMENT_NAME = "MSH"; static final String NEWLINE_REGEX = "\\r?\\n|\\r"; - static final Pattern HL7_FIELD_NAME_PATTERN = Pattern.compile("(\\w+)-(\\d+(?:\\.\\d+)*)"); private HL7Parser() {} @@ -37,19 +35,6 @@ public static HL7Message parseMessage(String content) { return new HL7Message(segments, HL7Encoding.fromEncodingField(encodingCharactersField)); } - public static HL7Path parsePath(String path) { - Matcher matcher = HL7_FIELD_NAME_PATTERN.matcher(path); - if (!matcher.matches()) { - throw new HL7ParserException("Invalid HL7 path format: " + path); - } - - String segmentName = matcher.group(1); - int[] indices = - Arrays.stream(matcher.group(2).split("\\.")).mapToInt(Integer::parseInt).toArray(); - - return new HL7Path(segmentName, indices); - } - public static String segmentToString(HL7Segment segment, HL7Encoding encoding) { String fieldSeparator = String.valueOf(encoding.getFieldDelimiter()); diff --git a/rs-e2e/src/main/java/gov/hhs/cdc/trustedintermediary/rse2e/hl7/HL7Path.java b/rs-e2e/src/main/java/gov/hhs/cdc/trustedintermediary/rse2e/hl7/HL7Path.java index 126bee6de..2fdd06bc2 100644 --- a/rs-e2e/src/main/java/gov/hhs/cdc/trustedintermediary/rse2e/hl7/HL7Path.java +++ b/rs-e2e/src/main/java/gov/hhs/cdc/trustedintermediary/rse2e/hl7/HL7Path.java @@ -2,9 +2,25 @@ import java.util.Arrays; import java.util.Objects; +import java.util.regex.Matcher; +import java.util.regex.Pattern; /** The HL7Path class represents a path to a specific field in an HL7 message. */ public record HL7Path(String segmentName, int[] indices) { + static final Pattern HL7_FIELD_NAME_PATTERN = Pattern.compile("(\\w+)-(\\d+(?:\\.\\d+)*)"); + + public static HL7Path parse(String path) { + Matcher matcher = HL7_FIELD_NAME_PATTERN.matcher(path); + if (!matcher.matches()) { + throw new HL7ParserException("Invalid HL7 path format: " + path); + } + + String segmentName = matcher.group(1); + int[] indices = + Arrays.stream(matcher.group(2).split("\\.")).mapToInt(Integer::parseInt).toArray(); + + return new HL7Path(segmentName, indices); + } // Need to override equals, hashCode, and toString to handle array comparison @Override