-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
MDEXP- 726 Extend mapping profile schema (#472)
* MDEXP-726 - mapping profile suppression * MDEXP-726 - mapping profile suppression validation * MDEXP-726 - mapping profile suppression validation * MDEXP-726 - fix tests * MDEXP-726 - fix sonar issues * MDEXP-726 - update validation tests * MDEXP-726 - update validation tests * MDEXP-726 - suppression as string * MDEXP-726 - suppression as string * MDEXP-726 - fix sonar * MDEXP-726 - error message
- Loading branch information
Showing
9 changed files
with
287 additions
and
47 deletions.
There are no files selected for viewing
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
7 changes: 7 additions & 0 deletions
7
...org/folio/dataexp/exception/mapping/profile/MappingProfileFieldsSuppressionException.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,7 @@ | ||
package org.folio.dataexp.exception.mapping.profile; | ||
|
||
public class MappingProfileFieldsSuppressionException extends RuntimeException { | ||
public MappingProfileFieldsSuppressionException(String message) { | ||
super(message); | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
...io/dataexp/exception/mapping/profile/MappingProfileFieldsSuppressionPatternException.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,14 @@ | ||
package org.folio.dataexp.exception.mapping.profile; | ||
|
||
import lombok.Getter; | ||
import org.folio.dataexp.domain.dto.Errors; | ||
|
||
public class MappingProfileFieldsSuppressionPatternException extends RuntimeException { | ||
@Getter | ||
private final Errors errors; | ||
|
||
public MappingProfileFieldsSuppressionPatternException(String message, Errors errors) { | ||
super(message); | ||
this.errors = errors; | ||
} | ||
} |
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
109 changes: 109 additions & 0 deletions
109
src/main/java/org/folio/dataexp/service/validators/MappingProfileValidator.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,109 @@ | ||
package org.folio.dataexp.service.validators; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import org.apache.commons.lang3.StringUtils; | ||
import org.folio.dataexp.domain.dto.Errors; | ||
import org.folio.dataexp.domain.dto.MappingProfile; | ||
import org.folio.dataexp.domain.dto.ParametersInner; | ||
import org.folio.dataexp.domain.dto.RecordTypes; | ||
import org.folio.dataexp.exception.mapping.profile.MappingProfileFieldsSuppressionException; | ||
import org.folio.dataexp.exception.mapping.profile.MappingProfileFieldsSuppressionPatternException; | ||
import org.folio.dataexp.exception.mapping.profile.MappingProfileTransformationEmptyException; | ||
import org.folio.dataexp.exception.mapping.profile.MappingProfileTransformationPatternException; | ||
import org.springframework.stereotype.Component; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.regex.Pattern; | ||
|
||
@Component | ||
@RequiredArgsConstructor | ||
public class MappingProfileValidator { | ||
|
||
private static final String VALIDATION_ERROR_MESSAGE = "Mapping profile validation exception"; | ||
private static final String ERROR_CODE = "javax.validation.constraints.Pattern.message"; | ||
private static final String ERROR_VALIDATION_MESSAGE_PATTERN = "must match \\\"%s\\\""; | ||
|
||
private static final Pattern TRANSFORMATION_PATTERN = Pattern.compile("((\\d{3}([\\s]|[\\d]|[a-zA-Z]){2}(\\$([a-zA-Z]|[\\d]{1,2}))?)|(^$))"); | ||
private static final String ERROR_VALIDATION_TRANSFORMATIONS_PARAMETER_KEY_PATTERN = "transformations[%s].transformation"; | ||
private static final String TRANSFORMATION_ITEM_EMPTY_VALUE_MESSAGE = "Transformations for fields with item record type cannot be empty. Please provide a value."; | ||
|
||
private static final Pattern SUPPRESSION_FIELD_PATTERN = Pattern.compile("^\\d{3}$"); | ||
private static final String ERROR_VALIDATION_SUPPRESSION_FIELD_PARAMETER_KEY_PATTERN = "suppressionFields[%s]"; | ||
private static final String ERROR_USAGE_SUPPRESSION_FIELD_FOR_ITEM_RECORD_TYPE = "Suppression field can not be used only for Item record type"; | ||
private static final String SUPPRESSION_VALIDATION_ERROR_MESSAGE = "Suppressed fields can be represented by three digits only and need to be separated by a comma."; | ||
|
||
public void validate(MappingProfile mappingProfile) { | ||
validateMappingProfileTransformations(mappingProfile); | ||
validateMappingProfileSuppression(mappingProfile); | ||
} | ||
|
||
private void validateMappingProfileTransformations(MappingProfile mappingProfile) { | ||
var transformations = mappingProfile.getTransformations(); | ||
var parameters = new ArrayList<ParametersInner>(); | ||
for (int i = 0; i < transformations.size(); i++) { | ||
var transformation = transformations.get(i); | ||
var matcher = TRANSFORMATION_PATTERN.matcher(transformation.getTransformation()); | ||
if (!matcher.matches()) { | ||
var parameter = ParametersInner.builder() | ||
.key(String.format(ERROR_VALIDATION_TRANSFORMATIONS_PARAMETER_KEY_PATTERN, i)) | ||
.value(transformation.getTransformation()).build(); | ||
parameters.add(parameter); | ||
} | ||
} | ||
if (!parameters.isEmpty()) { | ||
var errors = new Errors(); | ||
for (var parameter : parameters) { | ||
var errorItem = new org.folio.dataexp.domain.dto.Error(); | ||
errorItem.setCode(ERROR_CODE); | ||
errorItem.type("1"); | ||
errorItem.message(String.format(ERROR_VALIDATION_MESSAGE_PATTERN, TRANSFORMATION_PATTERN)); | ||
errors.addErrorsItem(errorItem); | ||
errorItem.setParameters(List.of(parameter)); | ||
} | ||
errors.setTotalRecords(errors.getErrors().size()); | ||
throw new MappingProfileTransformationPatternException(VALIDATION_ERROR_MESSAGE, errors); | ||
} | ||
for (var transformation : transformations) { | ||
if (StringUtils.isEmpty(transformation.getTransformation()) && transformation.getRecordType() == RecordTypes.ITEM) { | ||
throw new MappingProfileTransformationEmptyException(TRANSFORMATION_ITEM_EMPTY_VALUE_MESSAGE); | ||
} | ||
} | ||
} | ||
|
||
private void validateMappingProfileSuppression(MappingProfile mappingProfile) { | ||
var recordTypes = mappingProfile.getRecordTypes(); | ||
boolean isExistAllItemRecordType = recordTypes.stream().allMatch(type -> type == RecordTypes.ITEM); | ||
var fieldsSuppressionAsStr = mappingProfile.getFieldsSuppression(); | ||
if (StringUtils.isNotEmpty(fieldsSuppressionAsStr)) { | ||
if (!recordTypes.isEmpty() && isExistAllItemRecordType) { | ||
throw new MappingProfileFieldsSuppressionException(ERROR_USAGE_SUPPRESSION_FIELD_FOR_ITEM_RECORD_TYPE); | ||
} | ||
var fieldsSuppression = fieldsSuppressionAsStr.split(","); | ||
var parameters = new ArrayList<ParametersInner>(); | ||
for (int i = 0; i < fieldsSuppression.length; i++) { | ||
var suppression = StringUtils.trim(fieldsSuppression[i]); | ||
var matcher = SUPPRESSION_FIELD_PATTERN.matcher(suppression); | ||
if (!matcher.matches()) { | ||
var parameter = ParametersInner.builder() | ||
.key(String.format(ERROR_VALIDATION_SUPPRESSION_FIELD_PARAMETER_KEY_PATTERN, i)) | ||
.value(suppression).build(); | ||
parameters.add(parameter); | ||
} | ||
} | ||
if (!parameters.isEmpty()) { | ||
var errors = new Errors(); | ||
for (var parameter : parameters) { | ||
var errorItem = new org.folio.dataexp.domain.dto.Error(); | ||
errorItem.setCode(ERROR_CODE); | ||
errorItem.type("1"); | ||
errorItem.message(String.format(ERROR_VALIDATION_MESSAGE_PATTERN, SUPPRESSION_FIELD_PATTERN)); | ||
errors.addErrorsItem(errorItem); | ||
errorItem.setParameters(List.of(parameter)); | ||
} | ||
errors.setTotalRecords(errors.getErrors().size()); | ||
throw new MappingProfileFieldsSuppressionPatternException(SUPPRESSION_VALIDATION_ERROR_MESSAGE, errors); | ||
} | ||
} | ||
} | ||
} |
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
Oops, something went wrong.