Skip to content

Add unit tests for BaseHyphenatedIdentifierValidator#1

Closed
JasonWang-0401 wants to merge 1 commit into
masterfrom
add-base-hyphenated-identifier-validator-tests
Closed

Add unit tests for BaseHyphenatedIdentifierValidator#1
JasonWang-0401 wants to merge 1 commit into
masterfrom
add-base-hyphenated-identifier-validator-tests

Conversation

@JasonWang-0401
Copy link
Copy Markdown
Owner

Summary

BaseHyphenatedIdentifierValidator is the abstract base class that implements the shared
framework for OpenMRS's "hyphenated check-digit" patient identifier validators
(LuhnIdentifierValidator, VerhoeffIdentifierValidator). It had no direct unit test
its behaviour was only exercised indirectly through the two subclass tests, where it is
entangled with each subclass's own check-digit algorithm. (LuhnIdentifierValidatorTest
even carries a // TODO split this into multiple tests note on its single validation method.)

What this adds

A new test, BaseHyphenatedIdentifierValidatorTest, that uses a tiny stub subclass with a
fixed, caller-controlled check digit so the base-class contract can be verified in isolation,
independent of any real check-digit math:

  • getValidIdentifier appends - + the correct check letter (the full 0→A … 9→J
    mapping), and uses X as the check character for an out-of-range digit.
  • isValid returns true/false correctly, accepts the check letter case-insensitively,
    and works whether the check digit is supplied as a letter (id-D) or an integer (id-3).
  • Malformed input throws UnallowedIdentifierException: no hyphen, a trailing hyphen, a
    multi-character check digit (id-10), and a check character that is neither A–J nor a
    digit (id-K).
  • The allowed-character rules are enforced: null, empty, whitespace, and disallowed
    characters are all rejected.

The test doubles as living documentation of what a valid hyphenated identifier looks like,
in one place and independent of the Luhn/Verhoeff algorithms.

Testing

./mvnw -pl api test -Dtest=BaseHyphenatedIdentifierValidatorTest

Tests run: 10, Failures: 0, Errors: 0, Skipped: 0 — BUILD SUCCESS.

Pure JUnit 5 unit test; no Spring context and no database.

🤖 Generated with Claude Code

The abstract base class for hyphenated check-digit identifier validators had no direct test; its logic was only exercised indirectly through the Luhn and Verhoeff subclass tests, where it is tangled with each subclass's check-digit algorithm.

This adds a focused unit test that uses a stub subclass with a fixed, caller-controlled check digit to verify the base-class contract in isolation: hyphen/check-digit parsing, the 0-9 <-> A-J mapping (including the out-of-range 'X' default), case-insensitive check letters, and the allowed-character rules (null/empty/whitespace/disallowed).

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@JasonWang-0401
Copy link
Copy Markdown
Owner Author

Superseded by the upstream PR openmrs#6124 (same commit, opened against the official repository).

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