Search in sources :

Example 1 with ValidationErrorFailure

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();
    }
}
Also used : Arrays(java.util.Arrays) ZonedDateTime(java.time.ZonedDateTime) BiFunction(java.util.function.BiFunction) RequiredArgsConstructor(lombok.RequiredArgsConstructor) Router(io.vertx.ext.web.Router) DateFormatUtil.formatDateTime(org.folio.circulation.support.utils.DateFormatUtil.formatDateTime) RoutingContext(io.vertx.ext.web.RoutingContext) BodyHandler(io.vertx.ext.web.handler.BodyHandler) StringUtils(org.apache.commons.lang3.StringUtils) Map(java.util.Map) CommonFailures.failedDueToServerError(org.folio.circulation.support.results.CommonFailures.failedDueToServerError) JsonObject(io.vertx.core.json.JsonObject) MATCH_ALL_RECORDS(api.support.fakes.CqlPredicate.MATCH_ALL_RECORDS) MethodHandles(java.lang.invoke.MethodHandles) Collection(java.util.Collection) Set(java.util.Set) UUID(java.util.UUID) ValidationError(org.folio.circulation.support.http.server.ValidationError) Result(org.folio.circulation.support.results.Result) EqualsAndHashCode(lombok.EqualsAndHashCode) Collectors(java.util.stream.Collectors) String.format(java.lang.String.format) Objects(java.util.Objects) NoContentResponse.noContent(org.folio.circulation.support.http.server.NoContentResponse.noContent) List(java.util.List) Stream(java.util.stream.Stream) Logger(org.apache.logging.log4j.Logger) Buffer(io.vertx.core.buffer.Buffer) ClockUtil(org.folio.circulation.support.utils.ClockUtil) HttpServerResponse(io.vertx.core.http.HttpServerResponse) AbstractVerticle(io.vertx.core.AbstractVerticle) ClockUtil.getZonedDateTime(org.folio.circulation.support.utils.ClockUtil.getZonedDateTime) Json(io.vertx.core.json.Json) Getter(lombok.Getter) WebContext(org.folio.circulation.support.http.server.WebContext) HashMap(java.util.HashMap) Function(java.util.function.Function) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) APITestContext(api.support.APITestContext) ClientErrorResponse(org.folio.circulation.support.http.server.ClientErrorResponse) ValidationErrorFailure(org.folio.circulation.support.ValidationErrorFailure) JsonSchemaValidator(org.folio.circulation.infrastructure.serialization.JsonSchemaValidator) JsonArray(io.vertx.core.json.JsonArray) StringUtils.isBlank(org.apache.commons.lang3.StringUtils.isBlank) JsonHttpResponse.created(org.folio.circulation.support.http.server.JsonHttpResponse.created) HttpMethod(io.vertx.core.http.HttpMethod) HttpStatus(org.folio.HttpStatus) Collections(java.util.Collections) LogManager(org.apache.logging.log4j.LogManager) WebContext(org.folio.circulation.support.http.server.WebContext) ArrayList(java.util.ArrayList) JsonObject(io.vertx.core.json.JsonObject) ValidationError(org.folio.circulation.support.http.server.ValidationError) ValidationErrorFailure(org.folio.circulation.support.ValidationErrorFailure)

Example 2 with ValidationErrorFailure

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));
    }
}
Also used : WebContext(org.folio.circulation.support.http.server.WebContext) ArrayList(java.util.ArrayList) JsonObject(io.vertx.core.json.JsonObject) ValidationError(org.folio.circulation.support.http.server.ValidationError) ValidationErrorFailure(org.folio.circulation.support.ValidationErrorFailure)

Example 3 with ValidationErrorFailure

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()));
}
Also used : OverridableBlockType(org.folio.circulation.domain.override.OverridableBlockType) OkapiPermissions(org.folio.circulation.support.http.OkapiPermissions) ValidationErrorFailure(org.folio.circulation.support.ValidationErrorFailure) BlockOverrideError(org.folio.circulation.support.http.server.BlockOverrideError)

Example 4 with ValidationErrorFailure

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;
            }
        }
    };
}
Also used : Description(org.hamcrest.Description) ValidationErrorFailure(org.folio.circulation.support.ValidationErrorFailure) TypeSafeDiagnosingMatcher(org.hamcrest.TypeSafeDiagnosingMatcher) Result(org.folio.circulation.support.results.Result)

Example 5 with ValidationErrorFailure

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));
}
Also used : ValidationError(org.folio.circulation.support.http.server.ValidationError) ValidationErrorFailure(org.folio.circulation.support.ValidationErrorFailure) Test(org.junit.jupiter.api.Test)

Aggregations

ValidationErrorFailure (org.folio.circulation.support.ValidationErrorFailure)6 ValidationError (org.folio.circulation.support.http.server.ValidationError)4 JsonObject (io.vertx.core.json.JsonObject)3 ArrayList (java.util.ArrayList)3 WebContext (org.folio.circulation.support.http.server.WebContext)3 Result (org.folio.circulation.support.results.Result)2 APITestContext (api.support.APITestContext)1 MATCH_ALL_RECORDS (api.support.fakes.CqlPredicate.MATCH_ALL_RECORDS)1 AbstractVerticle (io.vertx.core.AbstractVerticle)1 Buffer (io.vertx.core.buffer.Buffer)1 HttpMethod (io.vertx.core.http.HttpMethod)1 HttpServerResponse (io.vertx.core.http.HttpServerResponse)1 Json (io.vertx.core.json.Json)1 JsonArray (io.vertx.core.json.JsonArray)1 Router (io.vertx.ext.web.Router)1 RoutingContext (io.vertx.ext.web.RoutingContext)1 BodyHandler (io.vertx.ext.web.handler.BodyHandler)1 String.format (java.lang.String.format)1 MethodHandles (java.lang.invoke.MethodHandles)1 ZonedDateTime (java.time.ZonedDateTime)1