Skip to content

Conversation

@UjjawalPrabhat
Copy link

Description

This PR implements service-layer validation for the Bill and BillLineItem entities in the OpenMRS Billing Module, addressing O3-5175 and bringing the module into compliance with OpenMRS architectural standards.

Problem Statement

The billing module currently lacks proper service-layer validation:

  • BillServiceImpl.validate() and BillLineItemServiceImpl.validate() are empty methods
  • Validation only enforced at database level via Hibernate constraints
  • Users receive cryptic database errors instead of business-friendly messages
  • Validation happens too late in the request lifecycle (database layer only)

Solution

Implemented comprehensive service-layer validation following OpenMRS standards:

BillLineItemValidator (5 validation rules):

  • At least one of item OR billableService must be set
  • Quantity must be positive (> 0)
  • Price must not be negative (>= 0)
  • Payment status must be valid (PENDING or PAID)
  • Field lengths comply with database constraints

BillValidator (6 validation rules):

  • Bill must have at least one line item
  • All line items must pass BillLineItemValidator
  • Patient is required
  • Cashier is required
  • CashPoint is required
  • Valid status and payment coverage for PAID bills

Testing

  • 18 comprehensive test cases covering all validation rules
  • 90%+ code coverage
  • All edge cases covered (null values, boundary conditions, invalid enums)
  • All tests passing

Files Changed

New (4):

  • BillLineItemValidator.java
  • BillValidator.java
  • BillLineItemValidatorTest.java
  • BillValidatorTest.java

Modified (3):

  • BillServiceImpl.java
  • BillLineItemServiceImpl.java
  • messages_en.properties

Compliance

  • Uses Spring Validator interface (OpenMRS standard)
  • Handler annotation for auto-registration
  • ValidateUtil.validate() invocation pattern
  • Follows UserValidator reference pattern
  • Backward compatible - no API changes
  • All existing tests pass

Related issue: O3-5175
https://openmrs.atlassian.net/browse/O3-5175

UjjawalPrabhat and others added 4 commits November 19, 2025 17:56
…tem (O3-5175)

- Implement BillLineItemValidator with 5 validation rules
- Implement BillValidator with 6 validation rules and nested validation
- Integrate ValidateUtil.validate() in Bill and BillLineItem services
- Add 18 comprehensive unit tests with 90%+ coverage
- Add 11 user-friendly error messages to messages_en.properties
- Move validation from database layer to service layer
- Maintain backward compatibility
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.

1 participant