Skip to content

Commit

Permalink
Added test cobverage for HL7Message and fixed parsing issue
Browse files Browse the repository at this point in the history
  • Loading branch information
basiliskus committed Dec 17, 2024
1 parent 8371cc3 commit ea1d47f
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ public List<HL7Segment> getSegments(String name) {
return segments.stream().filter(segment -> segment.name().equals(name)).toList();
}

public int getSegmentCount(String name) {
return getSegments(name).size();
}

public boolean hasSegment(String name, int index) {
return getSegmentCount(name) > index;
}
Expand All @@ -40,18 +44,6 @@ public HL7Segment getSegment(String name) throws HL7MessageException {
return getSegment(name, 0);
}

public List<String> getSegmentFields(String name, int index) throws HL7MessageException {
return getSegment(name, index).fields();
}

public List<String> getSegmentFields(String name) throws HL7MessageException {
return getSegment(name, 0).fields();
}

public int getSegmentCount(String name) {
return getSegments(name).size();
}

public String getValue(String hl7Path) throws HL7MessageException {
Matcher hl7FieldNameMatcher = HL7Parser.HL7_FIELD_NAME_PATTERN.matcher(hl7Path);
if (!hl7FieldNameMatcher.matches()) {
Expand All @@ -66,7 +58,7 @@ public String getValue(String hl7Path) throws HL7MessageException {
}

public String getValue(String segmentName, int... indices) throws HL7MessageException {
List<String> fields = getSegmentFields(segmentName);
List<String> fields = getSegment(segmentName).fields();
char[] levelDelimiters = this.getOrderedLevelDelimiters();
return HL7Parser.parseAndGetValue(fields, levelDelimiters, indices);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ public static HL7Message parse(String content) {
String[] lines = content.split(NEWLINE_REGEX);
for (String line : lines) {
if (line.trim().isEmpty()) continue;
String[] fields = line.split(Pattern.quote(String.valueOf(DEFAULT_FIELD_DELIMITER)));
String[] fields =
line.split(Pattern.quote(String.valueOf(DEFAULT_FIELD_DELIMITER)), -1);
String segmentName = fields[0];
List<String> segmentFields =
new ArrayList<>(Arrays.asList(fields).subList(1, fields.length));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,104 @@ package gov.hhs.cdc.trustedintermediary.rse2e.hl7
import spock.lang.Specification

class HL7MessageTest extends Specification {

HL7Message message
String messageContent

def setup() {
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^automated-staging-test-receiver-id^DNS|20230101010000-0000||ORM^O01^ORM_O01|001|N|2.5.1||||||||||
PID|1||1300974^^^Baptist East^MR||ONE^TESTCASE||202402210152-0500|F^Female^HL70001||2106-3^White^HL70005|1234 GPCS WAY^^MONTGOMERY^Alabama^36117^USA^home^^Montgomery|||||||2227600015||||N^Not Hispanic or Latino^HL70189|||1|||||||||||||||LRI_NG_FRN_PROFILE^^2.16.840.1.113883.9.195.3.4^ISO~LRI_NDBS_COMPONENT^^2.16.840.1.113883.9.195.3.6^ISO~LAB_PRN_Component^^2.16.840.1.113883.9.81^ISO~LAB_TO_COMPONENT^^2.16.840.1.113883.9.22^ISO|
NK1|1|ONE^MOMFIRST|MTH^Mother^HL70063||^^^^^804^5693861||||||||||||||||||||||||||||123456789^^^Medicaid&2.16.840.1.113883.4.446&ISO^MD||||000-00-0000^^^ssn&2.16.840.1.113883.4.1&ISO^SS
ORC|NW|4560411583^ORDERID||||||||||12345^^^^^^^^NPI&2.16.840.1.113883.4.6&ISO^L|||||||||
OBR|1|4560411583^ORDERID||54089-8^Newborn screening panel AHIC^LN|||202402221854-0500|||||||||12345^^^^^^^^NPI&2.16.840.1.113883.4.6&ISO^L||||||||
OBX|1|ST|57723-9^Unique bar code number of Current sample^LN||123456||||||F|||202402221854-0500
OBX|2|NM|||3122||||||F|||202402221854-0500||"""
message = HL7Parser.parse(messageContent)
}

def "getUnderlyingData should correctly return itself"() {
given:
def messageContent = """MSH|^~\\&|Epic^1.2.840.114350.1.13.145.2.7.2.695071^ISO
PID|1||11102779^^^CR^MR||SMITH^BB SARAH^^^^^L"""
def hl7Message = HL7Parser.parse(messageContent)
expect:
message.getUnderlyingData() == message
}

def "toString should return the original string content"() {
expect:
hl7Message.getUnderlyingData() == hl7Message
message.toString() == messageContent
}

def "getIdentifier should return the MSH-10 identifier of the underlying message"() {
given:
def expectedIdentifier = "001"
def messageContent = "MSH|^~\\&|SISGDSP|SISGDSP|SISHIERECEIVER^11223344^L,M,N|^^L,M,N|20230706093730||ORU^R01^ORU_R01|" + expectedIdentifier + "|D|2.5.1"
def hl7Message = HL7Parser.parse(messageContent)
expect:
message.getIdentifier() == "001"
}

def "getSegments returns the expected segments"() {
when:
def actualSegment = message.getSegments("OBX").get(1)

then:
actualSegment.name() == "OBX"
actualSegment.fields().get(0) == "2"
}

def "getSegmentCount returns the expected number of segments"() {
expect:
message.getSegmentCount("MSH") == 1
message.getSegmentCount("PID") == 1
message.getSegmentCount("OBX") == 2
}

def "hasSegment returns expected boolean"() {
expect:
message.hasSegment("MSH", 0)
message.hasSegment("PID", 0)
message.hasSegment("OBX", 0)
message.hasSegment("OBX", 1)
!message.hasSegment("MSH", 1)
!message.hasSegment("ZZZ", 0)
}

def "getSegment returns the expected segment"() {
when:
def name = hl7Message.getIdentifier()
def obxSegment1 = message.getSegment("OBX")
def obxSegment2 = message.getSegment("OBX", 1)

then:
name == expectedIdentifier
obxSegment1.name() == "OBX"
obxSegment1.fields().get(0) == "1"
obxSegment2.name() == "OBX"
obxSegment2.fields().get(0) == "2"
}

def "getValue returns the expected value given the segment name and indices"() {
expect:
message.getValue("PID", 3) == message.getValue("PID-3")
message.getValue("PID", 3, 0) == message.getValue("PID-3.0")
message.getValue("PID", 40, 4, 1) == message.getValue("PID-40.4.1")
message.getValue("NK1", 33, 4, 1, 1) == message.getValue("NK1-33.4.1.1")
message.getValue("PID-3") == "1300974^^^Baptist East^MR"
message.getValue("PID-3.0") == null
message.getValue("PID-3.1") == "1300974"
message.getValue("PID-3.2") == ""
message.getValue("PID-3.4") == "Baptist East"
message.getValue("PID-3.5") == "MR"
message.getValue("PID-3.6") == null
message.getValue("PID-40.4.1") == "ISO"
message.getValue("PID-40.4.2") == "LRI_NDBS_COMPONENT"
message.getValue("PID-40.4.3") == null
message.getValue("NK1-33.4.1.1") == "Medicaid"
message.getValue("NK1-33.4.1.1.1") == null
}

def "getValue should throws an HL7MessageException when the segment is not found"() {
when:
message.getValue("ZZZ", 1)

then:
thrown(HL7MessageException)
}

def "getEscapeCharacter should return the escape character"() {
expect:
message.getEscapeCharacter() == '\\'
}
}

0 comments on commit ea1d47f

Please sign in to comment.