use of org.folio.circulation.support.ValidationErrorFailure in project mod-circulation by folio-org.
the class FakeStorageModule method checkUniqueProperties.
private void checkUniqueProperties(RoutingContext routingContext) {
if (uniqueProperties.isEmpty()) {
routingContext.next();
return;
}
final WebContext context = new WebContext(routingContext);
JsonObject body = getJsonFromBody(routingContext);
ArrayList<ValidationError> errors = new ArrayList<>();
uniqueProperties.forEach(uniqueProperty -> {
String proposedValue = body.getString(uniqueProperty);
Map<String, JsonObject> records = getResourcesForTenant(new WebContext(routingContext));
if (records.values().stream().map(record -> record.getString(uniqueProperty)).anyMatch(usedValue -> usedValue.equals(proposedValue))) {
errors.add(new ValidationError(format("%s with this %s already exists", recordTypeName, uniqueProperty), uniqueProperty, proposedValue));
context.write(new ValidationErrorFailure(errors));
}
});
if (errors.isEmpty()) {
routingContext.next();
}
}
use of org.folio.circulation.support.ValidationErrorFailure in project mod-circulation by folio-org.
the class FakeStorageModule method checkRequiredProperties.
private void checkRequiredProperties(RoutingContext routingContext) {
final WebContext context = new WebContext(routingContext);
JsonObject body = getJsonFromBody(routingContext);
ArrayList<ValidationError> errors = new ArrayList<>();
requiredProperties.forEach(requiredProperty -> {
if (!body.getMap().containsKey(requiredProperty)) {
errors.add(new ValidationError("Required property missing", requiredProperty, ""));
}
});
if (errors.isEmpty()) {
routingContext.next();
} else {
context.write(new ValidationErrorFailure(errors));
}
}
use of org.folio.circulation.support.ValidationErrorFailure in project mod-circulation by folio-org.
the class OverridingErrorHandler method extendOverridableErrors.
private ValidationErrorFailure extendOverridableErrors(ValidationErrorFailure validationFailure) {
final CirculationErrorType errorType = getErrors().get(validationFailure);
if (!OVERRIDABLE_ERROR_TYPES.containsKey(errorType)) {
return validationFailure;
}
OverridableBlockType blockType = OVERRIDABLE_ERROR_TYPES.get(errorType);
OkapiPermissions missingOverridePermissions = blockType.getMissingOverridePermissions(okapiPermissions);
return new ValidationErrorFailure(validationFailure.getErrors().stream().map(error -> new BlockOverrideError(error, blockType, missingOverridePermissions)).collect(toList()));
}
use of org.folio.circulation.support.ValidationErrorFailure in project mod-circulation by folio-org.
the class FailureMatchers method errorResultFor.
public static <T> TypeSafeDiagnosingMatcher<Result<T>> errorResultFor(String propertyName, String reason) {
return new TypeSafeDiagnosingMatcher<Result<T>>() {
@Override
public void describeTo(Description description) {
description.appendValue(reason).appendText(" for ").appendValue(propertyName);
}
@Override
protected boolean matchesSafely(Result<T> failure, Description description) {
if (failure.succeeded()) {
description.appendText("is not a failed result");
return false;
}
if (failure.cause() instanceof ValidationErrorFailure) {
ValidationErrorFailure error = (ValidationErrorFailure) failure.cause();
if (!error.hasErrorForKey(propertyName)) {
description.appendText("not for ").appendValue(propertyName).appendText(" property");
return false;
}
description.appendValue(error.toString());
return error.hasErrorWithReason(reason);
} else {
description.appendText("is not a validation error failure");
return false;
}
}
};
}
use of org.folio.circulation.support.ValidationErrorFailure in project mod-circulation by folio-org.
the class DeferFailureErrorHandlerTest method failWithValidationErrorsReturnsValidationErrorsOnly.
@Test
void failWithValidationErrorsReturnsValidationErrorsOnly() {
ValidationError firstValidationError = new ValidationError("error1", "key1", "value1");
ValidationErrorFailure firstValidationFailure = new ValidationErrorFailure(List.of(firstValidationError));
ValidationError secondValidationError = new ValidationError("error2", "key2", "value2");
ValidationErrorFailure secondValidationFailure = new ValidationErrorFailure(List.of(secondValidationError));
handler.handleValidationError(firstValidationFailure, INVALID_ITEM_ID, SUCCEEDED_RESULT);
handler.handleAnyError(SERVER_ERROR, ITEM_DOES_NOT_EXIST, SUCCEEDED_RESULT);
handler.handleValidationError(secondValidationFailure, USER_IS_INACTIVE, SUCCEEDED_RESULT);
Result<String> output = handler.failWithValidationErrors("should not return this string");
assertTrue(output.failed());
assertTrue(output.cause() instanceof ValidationErrorFailure);
Collection<ValidationError> errors = ((ValidationErrorFailure) output.cause()).getErrors();
assertEquals(2, errors.size());
assertTrue(errors.contains(firstValidationError));
assertTrue(errors.contains(secondValidationError));
}
Aggregations