Skip to content

Commit

Permalink
Fixed test + cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
basiliskus committed Dec 13, 2024
1 parent 07b2aa4 commit 031b66c
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,6 @@ public Map<String, HL7Message> parseAndMapMessageByControlId(List<HL7FileStream>
String.format("MSH-10 is empty for file: %s", fileName));
}
messageMap.put(msh10, message);
// } catch (HL7Exception e) {
// throw new HapiHL7FileMatcherException(
// String.format("Failed to parse HL7 message from file:
// %s", fileName),
// e);
} catch (IOException e) {
throw new HL7FileMatcherException(
String.format("Failed to read file: %s", fileName), e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public class HL7Parser {

public static HL7Message parse(String content) {
Map<String, List<String>> segments = new HashMap<>();
Map<String, Character> encodingCharacters = new HashMap<>();
String encodingCharactersField = null;
String[] lines = content.split(NEWLINE_REGEX);
for (String line : lines) {
if (line.trim().isEmpty()) continue;
Expand All @@ -42,13 +42,13 @@ public static HL7Message parse(String content) {
List<String> segmentFields =
new ArrayList<>(Arrays.asList(fields).subList(1, fields.length));
if (Objects.equals(segmentName, MSH_SEGMENT_NAME)) {
encodingCharacters = getEncodingCharacters(fields[1]);
encodingCharactersField = fields[1];
segmentFields.add(0, String.valueOf(DEFAULT_FIELD_DELIMITER));
}
segments.put(segmentName, segmentFields);
}

return new HL7Message(segments, encodingCharacters);
return new HL7Message(segments, getEncodingCharacters(encodingCharactersField));
}

public static String parseAndGetValue(List<String> fields, char[] delimiters, int... indices) {
Expand All @@ -59,13 +59,13 @@ public static String parseAndGetValue(List<String> fields, char[] delimiters, in
String value = fields.get(indices[0] - 1);
for (int i = 1; i < indices.length; i++) {
if (i >= delimiters.length) {
throw new IllegalArgumentException("Invalid delimiter index (out of bounds): " + i);
return null;
}
char levelDelimiter = delimiters[i];
int index = indices[i] - 1;
String[] parts = value.split(Pattern.quote(String.valueOf(levelDelimiter)));
if (index < 0 || index >= parts.length) {
throw new IllegalArgumentException("Invalid field index (out of bounds): " + index);
return null;
}
value = parts[index];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import spock.lang.Specification

class HL7ParserTest extends Specification {

def "test defined assertions on relevant messages"() {
def "getValue returns the expected value given the segment name and indices"() {
given:
def content = """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|
Expand Down

0 comments on commit 031b66c

Please sign in to comment.