Skip to content

Commit

Permalink
MDEXP-726 - suppression as string
Browse files Browse the repository at this point in the history
  • Loading branch information
alekGbuz committed Apr 16, 2024
1 parent b6dbdab commit ad42ab7
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
import org.folio.dataexp.exception.file.definition.UploadFileException;
import org.folio.dataexp.exception.job.profile.DefaultJobProfileException;
import org.folio.dataexp.exception.mapping.profile.DefaultMappingProfileException;
import org.folio.dataexp.exception.mapping.profile.MappingProfileSuppressionFieldPatternException;
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.http.HttpStatus;
Expand Down Expand Up @@ -50,11 +51,16 @@ public ResponseEntity<String> handleMappingProfileTransformationEmptyException(f
return new ResponseEntity<>(e.getMessage(), HttpStatus.UNPROCESSABLE_ENTITY);
}

@ExceptionHandler(MappingProfileSuppressionFieldPatternException.class)
public ResponseEntity<Errors> handleMappingProfileSuppressionFieldPatternException(final MappingProfileSuppressionFieldPatternException e) {
@ExceptionHandler(MappingProfileFieldsSuppressionPatternException.class)
public ResponseEntity<Errors> handleMappingProfileFieldsSuppressionPatternException(final MappingProfileFieldsSuppressionPatternException e) {
return new ResponseEntity<>(e.getErrors(), HttpStatus.UNPROCESSABLE_ENTITY);
}

@ExceptionHandler(MappingProfileFieldsSuppressionException.class)
public ResponseEntity<Errors> handleMappingProfileFieldsSuppressionException(final MappingProfileFieldsSuppressionException e) {
return new ResponseEntity<>(HttpStatus.UNPROCESSABLE_ENTITY);
}

@ExceptionHandler(DefaultJobProfileException.class)
public ResponseEntity<String> handleDefaultJobProfileException(final DefaultJobProfileException e) {
return new ResponseEntity<>(e.getMessage(), HttpStatus.FORBIDDEN);
Expand Down
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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
import lombok.Getter;
import org.folio.dataexp.domain.dto.Errors;

public class MappingProfileSuppressionFieldPatternException extends RuntimeException {
public class MappingProfileFieldsSuppressionPatternException extends RuntimeException {
@Getter
private final Errors errors;

public MappingProfileSuppressionFieldPatternException(String message, Errors errors) {
public MappingProfileFieldsSuppressionPatternException(String message, Errors errors) {
super(message);
this.errors = errors;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
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.MappingProfileSuppressionFieldPatternException;
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;
Expand All @@ -30,6 +31,7 @@ public class MappingProfileValidator {

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";

public void validate(MappingProfile mappingProfile) {
validateMappingProfileTransformations(mappingProfile);
Expand Down Expand Up @@ -70,11 +72,17 @@ private void validateMappingProfileTransformations(MappingProfile mappingProfile
}

private void validateMappingProfileSuppression(MappingProfile mappingProfile) {
var suppressionFields = mappingProfile.getSuppression();
if (Objects.nonNull(suppressionFields)) {
var recordTypes = mappingProfile.getRecordTypes();
boolean isExistAllItemRecordType = recordTypes.stream().allMatch(type -> type == RecordTypes.ITEM);
if (!recordTypes.isEmpty() && isExistAllItemRecordType) {
throw new MappingProfileFieldsSuppressionException(ERROR_USAGE_SUPPRESSION_FIELD_FOR_ITEM_RECORD_TYPE);
}
var fieldsSuppressionAsStr = mappingProfile.getFieldsSuppression();
if (Objects.nonNull(fieldsSuppressionAsStr)) {
var fieldsSuppression = fieldsSuppressionAsStr.split(",");
var parameters = new ArrayList<ParametersInner>();
for (int i = 0; i < suppressionFields.size(); i++) {
var suppression = suppressionFields.get(i);
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()
Expand All @@ -94,7 +102,7 @@ private void validateMappingProfileSuppression(MappingProfile mappingProfile) {
errorItem.setParameters(List.of(parameter));
}
errors.setTotalRecords(errors.getErrors().size());
throw new MappingProfileSuppressionFieldPatternException(VALIDATION_ERROR_MESSAGE, errors);
throw new MappingProfileFieldsSuppressionPatternException(VALIDATION_ERROR_MESSAGE, errors);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,14 @@
"MARC"
]
},
"suppression": {
"description": "Fields to suppress",
"type": "array",
"items": {
"type": "string"
}
"fieldsSuppression": {
"description": "Fields to suppress",
"type": "string"
},
"suppress999ff": {
"description": "Supress 999 field rule",
"type": "boolean",
"default": false
},
"metadata": {
"description": "Metadata provided by the server",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ void postMappingProfileTest() {
mappingProfile.setDefault(true);
mappingProfile.setName("mappingProfile");
mappingProfile.setTransformations(List.of(transformation));
mappingProfile.setSuppression(List.of("902"));
mappingProfile.setFieldsSuppression("902");
var user = new User();
user.setPersonal(new User.Personal());

Expand Down Expand Up @@ -286,7 +286,7 @@ void postMappingProfileIfSuppressionNotMatchTest() {
mappingProfile.setDefault(true);
mappingProfile.setName("mappingProfile");
mappingProfile.setTransformations(List.of(transformation));
mappingProfile.setSuppression(List.of("90"));
mappingProfile.setFieldsSuppression("897 , 90");
var user = new User();
user.setPersonal(new User.Personal());

Expand All @@ -310,7 +310,7 @@ void postMappingProfileIfSuppressionNotMatchTest() {

assertEquals("must match \\\"^\\d{3}$\\\"", error.getMessage());
assertEquals(1, error.getParameters().size());
assertEquals("suppressionFields[0]", error.getParameters().get(0).getKey());
assertEquals("suppressionFields[1]", error.getParameters().get(0).getKey());
assertEquals("90", error.getParameters().get(0).getValue());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
import org.folio.dataexp.domain.dto.MappingProfile;
import org.folio.dataexp.domain.dto.RecordTypes;
import org.folio.dataexp.domain.dto.Transformations;
import org.folio.dataexp.exception.mapping.profile.MappingProfileSuppressionFieldPatternException;
import org.folio.dataexp.exception.mapping.profile.MappingProfileFieldsSuppressionException;
import org.folio.dataexp.exception.mapping.profile.MappingProfileFieldsSuppressionPatternException;
import org.folio.dataexp.exception.mapping.profile.MappingProfileTransformationPatternException;
import org.junit.jupiter.api.Test;

Expand Down Expand Up @@ -40,10 +41,27 @@ void validateMappingProfileSuppressionTest() {
mappingProfile.setTransformations(List.of());
var validator = new MappingProfileValidator();

mappingProfile.setSuppression(List.of("90"));
assertThrows(MappingProfileSuppressionFieldPatternException.class, () -> validator.validate(mappingProfile));
mappingProfile.setFieldsSuppression("90");
assertThrows(MappingProfileFieldsSuppressionPatternException.class, () -> validator.validate(mappingProfile));

mappingProfile.setSuppression(List.of("9000"));
assertThrows(MappingProfileSuppressionFieldPatternException.class, () -> validator.validate(mappingProfile));
mappingProfile.setFieldsSuppression("9000");
assertThrows(MappingProfileFieldsSuppressionPatternException.class, () -> validator.validate(mappingProfile));

mappingProfile.setFieldsSuppression("aab, 900");
assertThrows(MappingProfileFieldsSuppressionPatternException.class, () -> validator.validate(mappingProfile));
}

@Test
void validateMappingProfileSuppressionForItemRecordTest() {
var mappingProfile = new MappingProfile();
mappingProfile.setId(UUID.randomUUID());
mappingProfile.setDefault(true);
mappingProfile.setName("mappingProfile");
mappingProfile.setTransformations(List.of());
var validator = new MappingProfileValidator();
mappingProfile.recordTypes(List.of(RecordTypes.ITEM));

mappingProfile.setFieldsSuppression("900");
assertThrows(MappingProfileFieldsSuppressionException.class, () -> validator.validate(mappingProfile));
}
}

0 comments on commit ad42ab7

Please sign in to comment.