Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,20 @@ public SELF establishmentUnitNumber(Input<String> establishmentUnitNumber) {
return getThis();
}

/**
* Validate the structure of a Belgif openapi-employment-identifier EmployerId.
*
* @param employerId the employer id
* @return this request validator
* @see <a href="https://github.com/belgif/openapi-employment-identifier">openapi-employment-identifier</a>
*/
public SELF employerId(Input<Long> employerId) {
if (employerId != null && employerId.getValue() != null) {
addValidator(new EmployerIdValidator(employerId));
}
return getThis();
}

/**
* Validate the structure of a Belgif openapi-time Period or PeriodOptionalEnd.
*
Expand Down
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()))) {
Copy link
Contributor Author

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.

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);
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;
}

}
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));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,24 @@ void establishmentUnitNumberInvalid() {
InputValidationIssues.invalidEstablishmentUnitNumber(BODY, "establishmentUnitNumber", "2111111111"));
}

@Test
void employerIdValid() {
assertValid(new RequestValidator().employerId(Input.body("employerId", 312345625L)));
}

@Test
void employerIdNull() {
assertValid(new RequestValidator().employerId(null));
assertValid(new RequestValidator().employerId(Input.body("employerId", null)));
}

@Test
void employerIdInvalid() {
assertInvalid(
new RequestValidator().employerId(Input.body("employerId", 5678901279L)),
InputValidationIssues.invalidEmployerId(BODY, "employerId", 5678901279L));
}

@Test
void periodValid() {
InputPeriod period = new InputPeriod(LocalDate.of(2023, 1, 1), LocalDate.of(2023, 12, 31));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,11 @@ public static InputValidationIssue invalidEstablishmentUnitNumber(InEnum in, Str
I18N.getLocalizedString("invalidStructure.establishmentUnitNumber.detail", establishmentUnitNumber));
}

public static InputValidationIssue invalidEmployerId(InEnum in, String name, Long employerId) {
return invalidStructure(in, name, employerId,
I18N.getLocalizedString("invalidStructure.employerId.detail", employerId));
}

public static InputValidationIssue exactlyOneOfExpected(List<Input<?>> inputs) {
InputValidationIssue issue =
invalidInputExt(ISSUE_TYPE_EXACTLY_ONE_OF_EXPECTED, "Exactly one of these inputs must be present")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ invalidStructure.incompleteDate.detail=Incomplete date %s is invalid
invalidStructure.yearMonth.detail=Year month %s is invalid
invalidStructure.enterpriseNumber.detail=Enterprise number %s is invalid
invalidStructure.establishmentUnitNumber.detail=Establishment unit number %s is invalid
invalidStructure.employerId.detail=Employer ID %s is invalid
invalidPeriod.detail=%s should not precede %s
exactlyOneOfExpected.detail=Exactly one of these inputs must be present: %s
anyOfExpected.detail=Any of these inputs must be present: %s
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ invalidStructure.incompleteDate.detail=Das unvollst
invalidStructure.yearMonth.detail=Jahr-Monat %s ist nicht g�ltig
invalidStructure.enterpriseNumber.detail=Die Unternehmensnummer %s ist nicht g�ltig
invalidStructure.establishmentUnitNumber.detail=Die Standortnummer %s ist nicht g�ltig
invalidStructure.employerId.detail=Die Arbeitgeber ID %s ist nicht g�ltig
invalidPeriod.detail=%s darf nicht vor %s liegen
exactlyOneOfExpected.detail=Genau einer dieser Inputs muss vorhanden sein: %s
anyOfExpected.detail=Mindestens einer dieser Inputs muss vorhanden sein: %s
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ invalidStructure.incompleteDate.detail=La date incompl
invalidStructure.yearMonth.detail=L'ann�e et le mois %s sont invalides
invalidStructure.enterpriseNumber.detail=Le num�ro d'entreprise %s est invalide
invalidStructure.establishmentUnitNumber.detail=Le num�ro de l'unit� d'�tablissement %s est invalide
invalidStructure.employerId.detail=L'identifiant d'employeur %s est invalide
invalidPeriod.detail=%s ne peut pas �tre ant�rieur � %s
exactlyOneOfExpected.detail=Exactement un de ces inputs doit �tre pr�sent: %s
anyOfExpected.detail=Au moins un de ces inputs doit �tre pr�sent: %s
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ invalidStructure.incompleteDate.detail=Onvolledige datum %s is ongeldig
invalidStructure.yearMonth.detail=Jaar maand %s is ongeldig
invalidStructure.enterpriseNumber.detail=Ondernemingsnummer %s is ongeldig
invalidStructure.establishmentUnitNumber.detail=Vestigingseenheidsnummer %s is ongeldig
invalidStructure.employerId.detail=Werkgever ID %s is ongeldig
invalidPeriod.detail=%s mag niet voorafgaan aan %s
exactlyOneOfExpected.detail=Exact ��n van deze inputs moet aanwezig zijn: %s
anyOfExpected.detail=Ten minste ��n van deze inputs moet aanwezig zijn: %s
Expand Down
4 changes: 4 additions & 0 deletions src/main/asciidoc/release-notes.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@
* Add href property with documentation URL for belgif (e.g. https://www.belgif.be/specification/rest/api-guide/issues/schemaViolation.html) and belgif-ext (e.g. https://www.belgif.be/specification/rest/api-guide/issues/ext/invalidStructure.html) issue types
* Add Input parameter alternatives in InputValidationIssues for invalidSsin, unknownSsin, canceledSsin and replacedSsin

*belgif-rest-problem-validator:*

* Add validator for Belgif openapi-employment-identifier https://github.com/belgif/openapi-employment-identifier/blob/main/src/main/openapi/employment/identifier/v1/employment-identifier-v1.yaml[EmployerId]

== Version 0.17

[WARNING]
Expand Down