Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid ignoring exceptions in automated tests #1543

Merged
merged 5 commits into from
Nov 5, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ public static HapiHL7FileMatcher getInstance() {
}

public Map<Message, Message> matchFiles(
List<HL7FileStream> outputFiles, List<HL7FileStream> inputFiles) {
List<HL7FileStream> outputFiles, List<HL7FileStream> inputFiles)
throws HL7Exception, IOException {
// We pair up output and input files based on the control ID, which is in MSH-10
// Any files (either input or output) that don't have a match are logged
Map<String, Message> inputMap = mapMessageByControlId(inputFiles);
Expand Down Expand Up @@ -67,33 +68,32 @@ public Map<Message, Message> matchFiles(
return messageMap;
}

public Map<String, Message> mapMessageByControlId(List<HL7FileStream> files) {
public Map<String, Message> mapMessageByControlId(List<HL7FileStream> files)
throws HL7Exception, IOException {

Map<String, Message> messageMap = new HashMap<>();

try (HapiContext context = new DefaultHapiContext()) {
Parser parser = context.getPipeParser();

for (HL7FileStream hl7FileStream : files) {
String fileName = hl7FileStream.fileName();
try (InputStream inputStream = hl7FileStream.inputStream()) {
String content = new String(inputStream.readAllBytes(), StandardCharsets.UTF_8);
Message message = parser.parse(content);
MSH mshSegment = (MSH) message.get("MSH");
String msh10 = mshSegment.getMessageControlID().getValue();
if (msh10 == null || msh10.isEmpty()) {
logger.logError("MSH-10 is empty for : " + hl7FileStream.fileName());
continue;
throw new IllegalArgumentException(
basiliskus marked this conversation as resolved.
Show resolved Hide resolved
basiliskus marked this conversation as resolved.
Show resolved Hide resolved
String.format("MSH-10 is empty for file: %s", fileName));
}
messageMap.put(msh10, message);
} catch (IOException | HL7Exception e) {
logger.logError(
"An error occurred while parsing the message: "
+ hl7FileStream.fileName(),
} catch (HL7Exception e) {
basiliskus marked this conversation as resolved.
Show resolved Hide resolved
basiliskus marked this conversation as resolved.
Show resolved Hide resolved
throw new HL7Exception(
String.format("Failed to parse HL7 message from file: %s", fileName),
e);
}
}
} catch (IOException e) {
logger.logError("An error occurred while constructing the DefaultHapiContext", e);
}

return messageMap;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package gov.hhs.cdc.trustedintermediary.rse2e.external.hapi

import ca.uhn.hl7v2.model.Message
import ca.uhn.hl7v2.HL7Exception
import gov.hhs.cdc.trustedintermediary.context.TestApplicationContext
import gov.hhs.cdc.trustedintermediary.rse2e.HL7FileStream
import gov.hhs.cdc.trustedintermediary.wrappers.Logger
Expand Down Expand Up @@ -70,7 +71,7 @@ class HapiHL7FileMatcherTest extends Specification {
file2MshSegment == result[file2Msh10].encode().trim()
}

def "should log an error and continue when MSH-10 is empty"() {
def "should throw IllegalArgumentException when MSH-10 is empty"() {
given:
def msh1to9 = "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|"
def msh11to12 = "|T|2.5.1"
Expand All @@ -80,23 +81,21 @@ class HapiHL7FileMatcherTest extends Specification {
def hl7FileStream = new HL7FileStream("file1", inputStream)

when:
def result = fileMatcher.mapMessageByControlId([hl7FileStream])
fileMatcher.mapMessageByControlId([hl7FileStream])

then:
result.size() == 0
1 * mockLogger.logError({ it.contains("MSH-10 is empty") })
thrown(IllegalArgumentException)
}

def "should log an error when not able to parse the file as HL7 message"() {
def "should throw HL7Exception when not able to parse the file as HL7 message"() {
given:
def inputStream = new ByteArrayInputStream("".bytes)
def hl7FileStream = new HL7FileStream("badFile", inputStream)

when:
def result = fileMatcher.mapMessageByControlId([hl7FileStream])
fileMatcher.mapMessageByControlId([hl7FileStream])

then:
result.size() == 0
1 * mockLogger.logError({ it.contains("An error occurred while parsing the message") }, _ as Exception)
thrown(HL7Exception)
}
}