fix(generalOPD): replace RuntimeException with IEMRException and add unit tests#209
Conversation
…unit tests - Replace all bare RuntimeException/Exception throws in saveNurseData, saveDoctorData, and updateGeneralOPDDoctorData with descriptive IEMRException - Update GeneralOPDService interface to explicitly declare IEMRException - Add 14 unit tests for NCDScreeningServiceImpl (all 6 screening save methods) - Add 9 unit tests for IemrMmuLoginServiceImpl (all 4 login methods) - All 23 new tests pass (0 failures, 0 errors)
📝 WalkthroughWalkthroughThis PR standardizes exception handling in GeneralOPD service operations by replacing generic ChangesGeneralOPD Exception Handling Standardization
New Test Coverage for Login and NCD Screening Services
🎯 3 (Moderate) | ⏱️ ~25 minutes
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
Tip 💬 Introducing Slack Agent: The best way for teams to turn conversations into code.Slack Agent is built on CodeRabbit's deep understanding of your code, so your team can collaborate across the entire SDLC without losing context.
Built for teams:
One agent for your entire SDLC. Right inside Slack. Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 2
🧹 Nitpick comments (2)
src/test/java/com/iemr/hwc/service/login/IemrMmuLoginServiceImplTest.java (1)
91-97: ⚡ Quick winPrefer structural JSON assertions over
String.contains(...)checks.These checks are fragile; parsing the response and asserting key presence/value types will make tests more reliable against formatting/noise changes while still validating behavior.
Also applies to: 123-127, 152-157, 180-185
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@src/test/java/com/iemr/hwc/service/login/IemrMmuLoginServiceImplTest.java` around lines 91 - 97, The test uses fragile String.contains assertions for the response from getUserServicePointVanDetails; instead parse the result into a JSON object (e.g., Jackson ObjectMapper -> JsonNode) and replace the assertTrue(result.contains(...)) checks with structural assertions such as node.has("userVanDetails"), node.has("userSpDetails"), node.has("parkingPlaceLocationList") and, where appropriate, assert the expected node types (array/object) or values; apply the same change pattern to the other similar assertions mentioned (the blocks around lines 123-127, 152-157, 180-185) in IemrMmuLoginServiceImplTest so tests validate JSON structure rather than string formatting.src/test/java/com/iemr/hwc/service/ncdscreening/NCDScreeningServiceImplTest.java (1)
173-247: ⚡ Quick winAdd missing
null idfailure-path tests for remaining save methods.Line 173 onward covers
repo returns nullfor oral/breast/cervical/cbac, but not thesave()returns entity withnullid` scenario that you already test for diabetes/hypertension. Add that case for parity and regression protection.🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@src/test/java/com/iemr/hwc/service/ncdscreening/NCDScreeningServiceImplTest.java` around lines 173 - 247, Add tests that cover the "repo returns entity with null id" failure path for saveOralCancerDetails, saveBreastCancerDetails, saveCervicalDetails and saveCbacDetails (similar to the diabetes/hypertension null-id tests). For each method, mock the corresponding repo (oralCancerScreeningRepo, breastCancerScreeningRepo, cervicalCancerScreeningRepo, cbacDetailsRepo) to return the entity instance whose getId() returns null, then assertThrows(IEMRException.class, () -> ncdScreeningService.saveXxxDetails(...)) and verify the exception message matches the existing failure messages ("Error while saving oral screening", "Error while saving breast cancer screening", "Error while saving cervical screening", "Error while saving Cbac details"). Ensure you add one test per save method using the same naming pattern as the other tests.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@src/main/java/com/iemr/hwc/service/generalOPD/GeneralOPDServiceImpl.java`:
- Line 814: The null-input branch in saveDoctorData currently returns a null
saveSuccessFlag; instead throw new IEMRException("Invalid input") when
requestOBJ == null to honor the declared exception contract and keep behavior
deterministic; update the similar null-input branch later in the file (the
corresponding doctor save/update method around the second occurrence) to throw
the same IEMRException rather than returning null so both code paths
consistently signal invalid input.
- Line 1450: The method updateGeneralOPDDoctorData currently returns a null
updateSuccessFlag when requestOBJ is null; instead detect a missing/null
requestOBJ at the top of updateGeneralOPDDoctorData and throw an IEMRException
with a clear message (e.g., "Missing doctor update input") rather than returning
null; apply the same change to the other update method in this class that
follows the same pattern (the later update method that also returns
updateSuccessFlag) so any null requestOBJ consistently raises IEMRException.
---
Nitpick comments:
In `@src/test/java/com/iemr/hwc/service/login/IemrMmuLoginServiceImplTest.java`:
- Around line 91-97: The test uses fragile String.contains assertions for the
response from getUserServicePointVanDetails; instead parse the result into a
JSON object (e.g., Jackson ObjectMapper -> JsonNode) and replace the
assertTrue(result.contains(...)) checks with structural assertions such as
node.has("userVanDetails"), node.has("userSpDetails"),
node.has("parkingPlaceLocationList") and, where appropriate, assert the expected
node types (array/object) or values; apply the same change pattern to the other
similar assertions mentioned (the blocks around lines 123-127, 152-157, 180-185)
in IemrMmuLoginServiceImplTest so tests validate JSON structure rather than
string formatting.
In
`@src/test/java/com/iemr/hwc/service/ncdscreening/NCDScreeningServiceImplTest.java`:
- Around line 173-247: Add tests that cover the "repo returns entity with null
id" failure path for saveOralCancerDetails, saveBreastCancerDetails,
saveCervicalDetails and saveCbacDetails (similar to the diabetes/hypertension
null-id tests). For each method, mock the corresponding repo
(oralCancerScreeningRepo, breastCancerScreeningRepo,
cervicalCancerScreeningRepo, cbacDetailsRepo) to return the entity instance
whose getId() returns null, then assertThrows(IEMRException.class, () ->
ncdScreeningService.saveXxxDetails(...)) and verify the exception message
matches the existing failure messages ("Error while saving oral screening",
"Error while saving breast cancer screening", "Error while saving cervical
screening", "Error while saving Cbac details"). Ensure you add one test per save
method using the same naming pattern as the other tests.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro
Run ID: 5120f863-bbaf-462c-925f-7cb655fb54de
📒 Files selected for processing (4)
src/main/java/com/iemr/hwc/service/generalOPD/GeneralOPDService.javasrc/main/java/com/iemr/hwc/service/generalOPD/GeneralOPDServiceImpl.javasrc/test/java/com/iemr/hwc/service/login/IemrMmuLoginServiceImplTest.javasrc/test/java/com/iemr/hwc/service/ncdscreening/NCDScreeningServiceImplTest.java
| @Override | ||
| @Transactional(rollbackFor = Exception.class) | ||
| public Long saveDoctorData(JsonObject requestOBJ, String Authorization) throws Exception { | ||
| public Long saveDoctorData(JsonObject requestOBJ, String Authorization) throws IEMRException, Exception { |
There was a problem hiding this comment.
Do not silently return null when doctor save input is missing.
Line 814 introduces explicit IEMRException, but the requestOBJ == null path still returns saveSuccessFlag as null. Throw an IEMRException("Invalid input") there to keep contract behavior deterministic.
Suggested fix
- } else {
- // request OBJ is null.
- }
+ } else {
+ throw new IEMRException("Invalid input");
+ }
return saveSuccessFlag;Also applies to: 1010-1013
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
In `@src/main/java/com/iemr/hwc/service/generalOPD/GeneralOPDServiceImpl.java` at
line 814, The null-input branch in saveDoctorData currently returns a null
saveSuccessFlag; instead throw new IEMRException("Invalid input") when
requestOBJ == null to honor the declared exception contract and keep behavior
deterministic; update the similar null-input branch later in the file (the
corresponding doctor save/update method around the second occurrence) to throw
the same IEMRException rather than returning null so both code paths
consistently signal invalid input.
| // update doctor data | ||
| @Transactional(rollbackFor = Exception.class) | ||
| public Long updateGeneralOPDDoctorData(JsonObject requestOBJ, String Authorization) throws Exception { | ||
| public Long updateGeneralOPDDoctorData(JsonObject requestOBJ, String Authorization) throws IEMRException, Exception { |
There was a problem hiding this comment.
Do not silently return null when doctor update input is missing.
Line 1450 follows the same pattern: null requestOBJ falls through and returns updateSuccessFlag as null. This should raise IEMRException to match the standardized exception handling in this PR.
Suggested fix
- } else {
- // request OBJ is null.
- }
+ } else {
+ throw new IEMRException("Invalid input");
+ }
return updateSuccessFlag;Also applies to: 1660-1663
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
In `@src/main/java/com/iemr/hwc/service/generalOPD/GeneralOPDServiceImpl.java` at
line 1450, The method updateGeneralOPDDoctorData currently returns a null
updateSuccessFlag when requestOBJ is null; instead detect a missing/null
requestOBJ at the top of updateGeneralOPDDoctorData and throw an IEMRException
with a clear message (e.g., "Missing doctor update input") rather than returning
null; apply the same change to the other update method in this class that
follows the same pattern (the later update method that also returns
updateSuccessFlag) so any null requestOBJ consistently raises IEMRException.
IEMRException extends Exception so declaring both is a SonarQube code smell. Revert interface/impl throws clauses to Exception while keeping IEMRException as the thrown type in the method bodies for specificity.
|



Summary
RuntimeExceptionand genericExceptionthrows with domain-specificIEMRExceptionin General OPD serviceTest plan
NCDScreeningServiceImplTest— all 14 tests passIemrMmuLoginServiceImplTest— all 9 tests passsaveNurseData,saveDoctorData,updateGeneralOPDDoctorDatathrowIEMRExceptionon failure instead of leaking as unhandled 500 errors