-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(specification-validation) implement Invalid Indicator validation
- Add Invalid Indicator Rule validator - Add tests Closes: MRSPECS-45
- Loading branch information
1 parent
6fed3e5
commit c99a938
Showing
15 changed files
with
377 additions
and
40 deletions.
There are no files selected for viewing
42 changes: 42 additions & 0 deletions
42
...n/java/org/folio/rspec/validation/validator/marc/impl/AbstractIndicatorRuleValidator.java
This file contains 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,42 @@ | ||
package org.folio.rspec.validation.validator.marc.impl; | ||
|
||
import org.folio.rspec.domain.dto.DefinitionType; | ||
import org.folio.rspec.domain.dto.SeverityType; | ||
import org.folio.rspec.domain.dto.SpecificationFieldDto; | ||
import org.folio.rspec.domain.dto.ValidationError; | ||
import org.folio.rspec.i18n.TranslationProvider; | ||
import org.folio.rspec.validation.validator.SpecificationRuleValidator; | ||
import org.folio.rspec.validation.validator.marc.model.MarcField; | ||
import org.folio.rspec.validation.validator.marc.model.MarcIndicator; | ||
|
||
abstract class AbstractIndicatorRuleValidator implements SpecificationRuleValidator<MarcField, SpecificationFieldDto> { | ||
|
||
private final TranslationProvider translationProvider; | ||
|
||
AbstractIndicatorRuleValidator(TranslationProvider translationProvider) { | ||
this.translationProvider = translationProvider; | ||
} | ||
|
||
@Override | ||
public DefinitionType definitionType() { | ||
return DefinitionType.INDICATOR; | ||
} | ||
|
||
@Override | ||
public SeverityType severity() { | ||
return SeverityType.ERROR; | ||
} | ||
|
||
protected ValidationError buildError(MarcIndicator marcIndicator, | ||
SpecificationFieldDto fieldDefinition) { | ||
var message = translationProvider.format(ruleCode()); | ||
return ValidationError.builder() | ||
.path(marcIndicator.reference().toString()) | ||
.definitionType(definitionType()) | ||
.definitionId(fieldDefinition.getId()) | ||
.severity(severity()) | ||
.ruleCode(ruleCode()) | ||
.message(message) | ||
.build(); | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
...in/java/org/folio/rspec/validation/validator/marc/impl/InvalidIndicatorRuleValidator.java
This file contains 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,37 @@ | ||
package org.folio.rspec.validation.validator.marc.impl; | ||
|
||
import java.util.List; | ||
import org.folio.rspec.domain.dto.SpecificationFieldDto; | ||
import org.folio.rspec.domain.dto.ValidationError; | ||
import org.folio.rspec.i18n.TranslationProvider; | ||
import org.folio.rspec.validation.validator.SpecificationRuleCode; | ||
import org.folio.rspec.validation.validator.marc.model.MarcDataField; | ||
import org.folio.rspec.validation.validator.marc.model.MarcField; | ||
import org.folio.rspec.validation.validator.marc.model.MarcRuleCode; | ||
import org.folio.rspec.validation.validator.marc.utils.TagsMatcher; | ||
|
||
class InvalidIndicatorRuleValidator extends AbstractIndicatorRuleValidator { | ||
|
||
InvalidIndicatorRuleValidator(TranslationProvider translationProvider) { | ||
super(translationProvider); | ||
} | ||
|
||
@Override | ||
public List<ValidationError> validate(MarcField marcField, SpecificationFieldDto field) { | ||
if (marcField instanceof MarcDataField) { | ||
var indicators = ((MarcDataField) marcField).indicators(); | ||
if (indicators != null) { | ||
return indicators.stream() | ||
.filter(marcIndicator -> !TagsMatcher.matchesIndicator(marcIndicator.value().toString())) | ||
.map(marcIndicator -> buildError(marcIndicator, field)) | ||
.toList(); | ||
} | ||
} | ||
return List.of(); | ||
} | ||
|
||
@Override | ||
public SpecificationRuleCode supportedRule() { | ||
return MarcRuleCode.INVALID_INDICATOR; | ||
} | ||
} |
This file contains 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 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 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 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 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
70 changes: 70 additions & 0 deletions
70
...ava/org/folio/rspec/validation/validator/marc/impl/InvalidIndicatorRuleValidatorTest.java
This file contains 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,70 @@ | ||
package org.folio.rspec.validation.validator.marc.impl; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
import static org.mockito.Mockito.when; | ||
|
||
import java.util.List; | ||
import java.util.UUID; | ||
import org.folio.rspec.domain.dto.SpecificationFieldDto; | ||
import org.folio.rspec.domain.dto.ValidationError; | ||
import org.folio.rspec.i18n.TranslationProvider; | ||
import org.folio.rspec.validation.validator.marc.model.MarcDataField; | ||
import org.folio.rspec.validation.validator.marc.model.MarcIndicator; | ||
import org.folio.rspec.validation.validator.marc.model.Reference; | ||
import org.folio.spring.testing.type.UnitTest; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.mockito.InjectMocks; | ||
import org.mockito.Mock; | ||
import org.mockito.junit.jupiter.MockitoExtension; | ||
|
||
@UnitTest | ||
@ExtendWith(MockitoExtension.class) | ||
class InvalidIndicatorRuleValidatorTest { | ||
|
||
@Mock | ||
private TranslationProvider translationProvider; | ||
|
||
@InjectMocks | ||
private InvalidIndicatorRuleValidator validator; | ||
|
||
@Test | ||
void validate_whenInvalidIndicators_shouldReturnValidationError() { | ||
var fieldDefinition = new SpecificationFieldDto().id(UUID.randomUUID()).repeatable(false); | ||
var marcField = new MarcDataField( | ||
Reference.forTag("tag", 1), | ||
getIndicators('X', 'x'), | ||
null); | ||
|
||
when(translationProvider.format(validator.supportedRule().getCode())).thenReturn("message"); | ||
|
||
var errors = validator.validate(marcField, fieldDefinition); | ||
|
||
assertEquals(1, errors.size()); | ||
assertEquals(validator.definitionType(), errors.get(0).getDefinitionType()); | ||
assertEquals(validator.severity(), errors.get(0).getSeverity()); | ||
assertEquals(validator.supportedRule().getCode(), errors.get(0).getRuleCode()); | ||
assertEquals("message", errors.get(0).getMessage()); | ||
} | ||
|
||
@Test | ||
void validate_whenValidIndicators_shouldReturnEmptyList() { | ||
var fieldDefinition = new SpecificationFieldDto().id(UUID.randomUUID()).repeatable(false); | ||
var marcField = new MarcDataField( | ||
Reference.forTag("tag", 0), | ||
getIndicators('#', '#'), | ||
null); | ||
|
||
List<ValidationError> errors = validator.validate(marcField, fieldDefinition); | ||
|
||
assertTrue(errors.isEmpty()); | ||
} | ||
|
||
private static List<MarcIndicator> getIndicators(char ind1, char ind2) { | ||
return List.of( | ||
new MarcIndicator(Reference.forIndicator(Reference.forTag("ind"), 0), ind1), | ||
new MarcIndicator(Reference.forIndicator(Reference.forTag("ind"), 1), ind2) | ||
); | ||
} | ||
} |
This file contains 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
Oops, something went wrong.