-
Notifications
You must be signed in to change notification settings - Fork 1
Add validator for Belgif openapi-employment-identifier EmployerId #240
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
Open
jpraet
wants to merge
2
commits into
main
Choose a base branch
from
feature/validate-employerId
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
...validator/src/main/java/io/github/belgif/rest/problem/validation/EmployerIdValidator.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| package io.github.belgif.rest.problem.validation; | ||
|
|
||
| import java.util.Optional; | ||
|
|
||
| import io.github.belgif.rest.problem.api.Input; | ||
| import io.github.belgif.rest.problem.api.InputValidationIssue; | ||
| import io.github.belgif.rest.problem.api.InputValidationIssues; | ||
|
|
||
| /** | ||
| * Validates the structure of a Belgif openapi-employment-identifier EmployerId. | ||
| * | ||
| * @see <a href="https://github.com/belgif/openapi-employment-identifier">openapi-employment-identifier</a> | ||
| */ | ||
| class EmployerIdValidator extends AbstractSingleInputValidator<Long> { | ||
|
|
||
| EmployerIdValidator(Input<Long> input) { | ||
| super(input); | ||
| } | ||
|
|
||
| @Override | ||
| public Optional<InputValidationIssue> validate() { | ||
| // Belgif openapi-employment-identifier EmployerId already enforces 197 -> 5999999999 range: | ||
| // we only need to validate the checksum here | ||
| int checksum = (int) (getValue() % 100); | ||
| if (!(checksum == calculateNssoChecksum() || (isInPlaRange() && checksum == calculatePlaChecksum()))) { | ||
| return Optional.of(InputValidationIssues.invalidEmployerId(getIn(), getName(), getValue())); | ||
| } | ||
| return Optional.empty(); | ||
| } | ||
|
|
||
| private int calculateNssoChecksum() { | ||
| long radix = getValue() / 100; | ||
| int checksum = (int) (96 - (radix * 100) % 97L); | ||
jflabatBCSS marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| return checksum == 0 ? 97 : checksum; | ||
| } | ||
|
|
||
| private int calculatePlaChecksum() { | ||
| long radix = getValue() / 100; | ||
| int checksum = (int) (radix % 97L); | ||
| return checksum == 0 ? 97 : checksum; | ||
| } | ||
|
|
||
| private boolean isInPlaRange() { | ||
| return getValue() >= 197L && getValue() <= 99999926L; | ||
| } | ||
|
|
||
| } | ||
39 changes: 39 additions & 0 deletions
39
...dator/src/test/java/io/github/belgif/rest/problem/validation/EmployerIdValidatorTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| package io.github.belgif.rest.problem.validation; | ||
|
|
||
| import static io.github.belgif.rest.problem.api.InEnum.*; | ||
| import static org.assertj.core.api.Assertions.*; | ||
|
|
||
| import org.junit.jupiter.params.ParameterizedTest; | ||
| import org.junit.jupiter.params.provider.ValueSource; | ||
|
|
||
| import io.github.belgif.rest.problem.api.Input; | ||
| import io.github.belgif.rest.problem.api.InputValidationIssues; | ||
|
|
||
| class EmployerIdValidatorTest { | ||
|
|
||
| @ParameterizedTest | ||
| @ValueSource(longs = { 100006L, 212345609L, 312345625L, 200000031L, 499999982L, 187995796L, 168676597L }) | ||
| void okNssoNumber(Long employerId) { | ||
| assertThat(new EmployerIdValidator(Input.body("test", employerId)).validate()).isEmpty(); | ||
| } | ||
|
|
||
| @ParameterizedTest | ||
| @ValueSource(longs = { 5134794036L, 5000000120L, 5999999989L, 5678901277L }) | ||
| void okProvisionalNssoNumber(Long employerId) { | ||
| assertThat(new EmployerIdValidator(Input.body("test", employerId)).validate()).isEmpty(); | ||
| } | ||
|
|
||
| @ParameterizedTest | ||
| @ValueSource(longs = { 43220065L, 2130057L, 22300094L, 5170096L, 55290097L }) | ||
| void okPplNumber(Long employerId) { | ||
| assertThat(new EmployerIdValidator(Input.body("test", employerId)).validate()).isEmpty(); | ||
| } | ||
|
|
||
| @ParameterizedTest | ||
| @ValueSource(longs = { 4000000100L, 6999999999L, 5678901279L, 1000000047L, 5000000121L }) | ||
| void nok(Long employerId) { | ||
| assertThat(new EmployerIdValidator(Input.body("test", employerId)).validate()) | ||
| .contains(InputValidationIssues.invalidEmployerId(BODY, "test", employerId)); | ||
| } | ||
|
|
||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
According to smalsutils-validation there is overlap between NssoNumber range and PlaNumber range,
so I chose to also allow NSSO checksum when isInPlaRange() is true.