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

Conversation

basiliskus
Copy link
Contributor

@basiliskus basiliskus commented Nov 1, 2024

Avoid ignoring exceptions in automated tests

  • Bubble up exceptions, add context, and let automated test fail
  • Throw exception if failed to find matching input or output
  • Refactored to simplify logic in matchFiles
  • Added custom exception HapiHL7FileMatcherException

Issue

#1536

Checklist

  • I have added tests to cover my changes
  • I have added logging where useful (with appropriate log level)
  • I have added JavaDocs where required
  • I have updated the documentation accordingly

Copy link

github-actions bot commented Nov 1, 2024

PR Reviewer Guide 🔍

Here are some key observations to aid the review process:

🎫 Ticket compliance analysis ✅

1536 - Fully compliant

Fully compliant requirements:

  • Make the automated test fail if any of the input or output files are not parsed correctly
⏱️ Estimated effort to review: 2 🔵🔵⚪⚪⚪
🧪 No relevant tests
🔒 No security concerns identified
⚡ Recommended focus areas for review

Exception Handling
Ensure that the new exception handling does not interfere with other parts of the test suite or application logic

Copy link

github-actions bot commented Nov 1, 2024

PR Code Suggestions ✨

Explore these optional code suggestions:

CategorySuggestion                                                                                                                                    Score
Possible bug
Add null checks to prevent NullPointerException

Add null checks for mshSegment and message objects before accessing their properties
to avoid potential NullPointerException.

rs-e2e/src/main/java/gov/hhs/cdc/trustedintermediary/rse2e/external/hapi/HapiHL7FileMatcher.java [83-85]

 Message message = parser.parse(content);
+if (message == null) throw new HL7Exception("Parsed message is null");
 MSH mshSegment = (MSH) message.get("MSH");
+if (mshSegment == null) throw new HL7Exception("MSH segment is missing");
 String msh10 = mshSegment.getMessageControlID().getValue();
Suggestion importance[1-10]: 8

Why: Adding null checks before accessing properties of potentially null objects is a critical safety measure to prevent runtime errors like NullPointerException. This suggestion significantly enhances the reliability and stability of the code.

8
Best practice
Add resource cleanup to prevent leaks in the test case

Ensure that the test case "should throw IllegalArgumentException when MSH-10 is
empty" includes a cleanup step to close the InputStream and other resources to
prevent resource leaks.

rs-e2e/src/test/groovy/gov/hhs/cdc/trustedintermediary/rse2e/external/hapi/HapiHL7FileMatcherTest.groovy [80-81]

 def inputStream = new ByteArrayInputStream(mshSegment.bytes)
 def hl7FileStream = new HL7FileStream("file1", inputStream)
+// Cleanup resources after test execution
+inputStream.close()
Suggestion importance[1-10]: 7

Why: Proper resource management is crucial in tests to prevent resource leaks. The suggestion to close the InputStream after the test is a good practice, enhancing the robustness of the test case.

7
Enhancement
Separate exception handling for HL7Exception and IOException for clearer error management

Refactor the mapMessageByControlId method to handle IOException separately from
HL7Exception to provide more specific error handling and logging for different types
of exceptions.

rs-e2e/src/main/java/gov/hhs/cdc/trustedintermediary/rse2e/external/hapi/HapiHL7FileMatcher.java [91-94]

 } catch (HL7Exception e) {
     throw new HL7Exception(
         String.format("Failed to parse HL7 message from file: %s", fileName),
         e);
+} catch (IOException e) {
+    throw new IOException(
+        String.format("Failed to read file: %s", fileName),
+        e);
 }
Suggestion importance[1-10]: 6

Why: Separating the exception handling for HL7Exception and IOException can provide clearer and more specific error management. This suggestion improves the code's maintainability and readability by handling different types of exceptions distinctly.

6
Performance
Optimize reading from input stream for better performance and memory management

Consider using a more efficient method to read the input stream into a string, such
as using a buffered reader, to improve performance and reduce memory usage.

rs-e2e/src/main/java/gov/hhs/cdc/trustedintermediary/rse2e/external/hapi/HapiHL7FileMatcher.java [82]

-String content = new String(inputStream.readAllBytes(), StandardCharsets.UTF_8);
+String content;
+try (BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, StandardCharsets.UTF_8))) {
+    content = reader.lines().collect(Collectors.joining("\n"));
+}
Suggestion importance[1-10]: 5

Why: Using a BufferedReader to read the input stream can improve performance and reduce memory usage. This suggestion is beneficial for optimizing resource utilization, especially in scenarios involving large data streams.

5

@basiliskus
Copy link
Contributor Author

/review

Copy link

github-actions bot commented Nov 4, 2024

PR Reviewer Guide 🔍

Here are some key observations to aid the review process:

🎫 Ticket compliance analysis ✅

1536 - Fully compliant

Fully compliant requirements:

  • Ensure that exceptions are not ignored in automated tests.
  • Bubble up exceptions instead of just logging an error and continuing.
  • Make the automated test fail if any of the input or output files are not parsed correctly.
⏱️ Estimated effort to review: 3 🔵🔵🔵⚪⚪
🧪 PR contains tests
🔒 No security concerns identified
⚡ Recommended focus areas for review

Exception Handling
Ensure that the new exception handling logic does not inadvertently suppress or mishandle other important exceptions that should be propagated or logged.

@basiliskus basiliskus marked this pull request as ready for review November 4, 2024 18:17
Copy link

sonarqubecloud bot commented Nov 4, 2024

Copy link
Contributor

@pluckyswan pluckyswan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe add context on why we want this instead of logging in the description but besides that me likey

@basiliskus basiliskus merged commit b791a9c into main Nov 5, 2024
17 checks passed
@basiliskus basiliskus deleted the story/1536/fail-test-with-exceptions branch November 5, 2024 16:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants