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 2994bd714..8b9b9df1b 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 @@ -12,25 +12,36 @@ */ public class HL7Message implements HealthData { - private final Map> segments; + private final List segments; private final Map encodingCharacters; - public HL7Message( - Map> segments, Map encodingCharacters) { + HL7Message(List segments, Map encodingCharacters) { this.segments = segments; this.encodingCharacters = encodingCharacters; } + public List getSegments(String name) { + return segments.stream().filter(segment -> segment.name().equals(name)).toList(); + } + + public HL7Segment getSegment(String name, int index) { + return getSegments(name).get(index); + } + + public HL7Segment getSegment(String name) { + return getSegments(name).get(0); + } + + public List getSegmentFields(String name, int index) { + return getSegment(name, index).fields(); + } + public List getSegmentFields(String name) { - return segments.get(name); + return getSegment(name, 0).fields(); } public int getSegmentCount(String name) { - var matches = getSegmentFields(name); - if (matches != null) { - return matches.size(); - } - return 0; + return getSegments(name).size(); } public String getValue(String hl7Path) { @@ -83,21 +94,19 @@ public String getIdentifier() { public String toString() { return String.join( HL7Parser.DEFAULT_SEGMENT_DELIMITER, - segments.entrySet().stream().map(this::formatSegment).toList()); + segments.stream().map(this::segmentToString).toList()); } - private String formatSegment(Map.Entry> entry) { - String name = entry.getKey(); - List fields = entry.getValue(); + private String segmentToString(HL7Segment segment) { String fieldSeparator = String.valueOf(getEncodingCharacter(HL7Parser.FIELD_DELIMITER_NAME)); - return name - + (name.equals(HL7Parser.MSH_SEGMENT_NAME) ? fields.get(0) : fieldSeparator) - + String.join( - fieldSeparator, - name.equals(HL7Parser.MSH_SEGMENT_NAME) - ? fields.subList(1, fields.size()) - : fields); + if (segment.name().equals(HL7Parser.MSH_SEGMENT_NAME)) { + return segment.name() + + segment.fields().get(0) + + String.join( + fieldSeparator, segment.fields().subList(1, segment.fields().size())); + } + return segment.name() + fieldSeparator + String.join(fieldSeparator, segment.fields()); } } 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 ef679b94f..e81e1c6c6 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 @@ -2,7 +2,6 @@ import java.util.ArrayList; import java.util.Arrays; -import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Objects; @@ -32,7 +31,7 @@ public class HL7Parser { protected static final Pattern HL7_FIELD_NAME_PATTERN = Pattern.compile("(\\w+)(?:-(\\S+))?"); public static HL7Message parse(String content) { - Map> segments = new HashMap<>(); + List segments = new ArrayList<>(); String encodingCharactersField = null; String[] lines = content.split(NEWLINE_REGEX); for (String line : lines) { @@ -45,7 +44,7 @@ public static HL7Message parse(String content) { encodingCharactersField = fields[1]; segmentFields.add(0, String.valueOf(DEFAULT_FIELD_DELIMITER)); } - segments.put(segmentName, segmentFields); + segments.add(new HL7Segment(segmentName, segmentFields)); } return new HL7Message(segments, getEncodingCharacters(encodingCharactersField)); diff --git a/rs-e2e/src/main/java/gov/hhs/cdc/trustedintermediary/rse2e/hl7/HL7Segment.java b/rs-e2e/src/main/java/gov/hhs/cdc/trustedintermediary/rse2e/hl7/HL7Segment.java new file mode 100644 index 000000000..b3e2dfb7a --- /dev/null +++ b/rs-e2e/src/main/java/gov/hhs/cdc/trustedintermediary/rse2e/hl7/HL7Segment.java @@ -0,0 +1,10 @@ +package gov.hhs.cdc.trustedintermediary.rse2e.hl7; + +import java.util.List; + +public record HL7Segment(String name, List fields) { + + public int getIndex() { + return Integer.parseInt(fields.get(0)); + } +} diff --git a/rs-e2e/src/test/groovy/gov/hhs/cdc/trustedintermediary/rse2e/hl7/HL7ExpressionEvaluatorTest.groovy b/rs-e2e/src/test/groovy/gov/hhs/cdc/trustedintermediary/rse2e/hl7/HL7ExpressionEvaluatorTest.groovy index e93f97b8d..a611b2940 100644 --- a/rs-e2e/src/test/groovy/gov/hhs/cdc/trustedintermediary/rse2e/hl7/HL7ExpressionEvaluatorTest.groovy +++ b/rs-e2e/src/test/groovy/gov/hhs/cdc/trustedintermediary/rse2e/hl7/HL7ExpressionEvaluatorTest.groovy @@ -7,28 +7,19 @@ import spock.lang.Specification class HL7ExpressionEvaluatorTest extends Specification { def evaluator = HL7ExpressionEvaluator.getInstance() - def encodingChars = HL7Parser.getEncodingCharacters("|^~\\&") - HL7Message mshMessage - String mshSegmentText + HL7Message hl7Message + String messageContent def setup() { TestApplicationContext.reset() TestApplicationContext.init() TestApplicationContext.register(HL7ExpressionEvaluator, evaluator) - mshSegmentText = "MSH|^~\\&|Sender Application^sender.test.com^DNS|Sender Facility^0.0.0.0.0.0.0.0^ISO|Receiver Application^0.0.0.0.0.0.0.0^ISO|Receiver Facility^simulated-lab-id^DNS|20230101010000-0000||ORM^O01^ORM_O01|111111|T|2.5.1" - - def segments = [ - MSH: [ - "MSH|^~\\&|Sender Application^sender.test.com^DNS|Sender Facility^0.0.0.0.0.0.0.0^ISO|Receiver Application^0.0.0.0.0.0.0.0^ISO|Receiver Facility^simulated-lab-id^DNS|20230101010000-0000||ORM^O01^ORM_O01|111111|T|2.5.1\n" - ], - PID: [ - "PID|1||11102779^^^CR^MR||SMITH^BB SARAH^^^^^L\n" - ] - ] - - mshMessage = new HL7Message(segments as Map>, encodingChars) + messageContent = """MSH|^~\\&|Sender Application^sender.test.com^DNS|Sender Facility^0.0.0.0.0.0.0.0^ISO|Receiver Application^0.0.0.0.0.0.0.0^ISO|Receiver Facility^simulated-lab-id^DNS|20230101010000-0000||ORM^O01^ORM_O01|111111|T|2.5.1 +PID|1||11102779^^^CR^MR||SMITH^BB SARAH^^^^^L +""" + hl7Message = HL7Parser.parse(messageContent) TestApplicationContext.injectRegisteredImplementations() } @@ -191,7 +182,7 @@ class HL7ExpressionEvaluatorTest extends Specification { def operator = "=" when: - def result = evaluator.evaluateCollectionCount(mshMessage, segmentName, rightOperand, operator) + def result = evaluator.evaluateCollectionCount(hl7Message, segmentName, rightOperand, operator) then: result @@ -204,7 +195,7 @@ class HL7ExpressionEvaluatorTest extends Specification { def operator = "=" when: - def result = evaluator.evaluateCollectionCount(mshMessage, segmentName, rightOperand, operator) + def result = evaluator.evaluateCollectionCount(hl7Message, segmentName, rightOperand, operator) then: !result @@ -217,7 +208,7 @@ class HL7ExpressionEvaluatorTest extends Specification { def operator = "=" when: - evaluator.evaluateCollectionCount(mshMessage, segmentName, rightOperand, operator) + evaluator.evaluateCollectionCount(hl7Message, segmentName, rightOperand, operator) then: def e = thrown(IllegalArgumentException) @@ -231,7 +222,7 @@ class HL7ExpressionEvaluatorTest extends Specification { def operator = "=" when: - def result = evaluator.evaluateCollectionCount(mshMessage, segmentName, rightOperand, operator) + def result = evaluator.evaluateCollectionCount(hl7Message, segmentName, rightOperand, operator) then: !result @@ -257,7 +248,7 @@ class HL7ExpressionEvaluatorTest extends Specification { def msh3 = "Sender Application^sender.test.com^DNS" when: - def result = evaluator.getLiteralOrFieldValue(mshMessage, inputMessage, operand) + def result = evaluator.getLiteralOrFieldValue(hl7Message, inputMessage, operand) then: result == msh3 @@ -270,7 +261,7 @@ class HL7ExpressionEvaluatorTest extends Specification { def inputMessage = Mock(HL7Message) when: - def result = evaluator.getFieldValue(mshMessage, inputMessage, fieldName) + def result = evaluator.getFieldValue(hl7Message, inputMessage, fieldName) then: result == msh3 @@ -282,7 +273,7 @@ class HL7ExpressionEvaluatorTest extends Specification { def inputMessage = Mock(HL7Message) when: - evaluator.getFieldValue(mshMessage, inputMessage, fieldName) + evaluator.getFieldValue(hl7Message, inputMessage, fieldName) then: def e = thrown(IllegalArgumentException) @@ -295,7 +286,7 @@ class HL7ExpressionEvaluatorTest extends Specification { def inputMessage = Mock(HL7Message) when: - evaluator.getFieldValue(mshMessage, inputMessage, fieldName) + evaluator.getFieldValue(hl7Message, inputMessage, fieldName) then: def e = thrown(IllegalArgumentException)