Search in sources :

Example 1 with ValidationError

use of org.folio.circulation.support.http.server.ValidationError 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 ValidationError

use of org.folio.circulation.support.http.server.ValidationError 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 ValidationError

use of org.folio.circulation.support.http.server.ValidationError in project mod-circulation by folio-org.

the class RequestByInstanceIdResourceTests method getExpectedErrorMessages.

@Test
void getExpectedErrorMessages() {
    HttpFailure validationError = ValidationErrorFailure.singleValidationError(new ValidationError("validationError", "someParam", "null"));
    String errorMessage = RequestByInstanceIdResource.getErrorMessage(validationError);
    assertTrue(errorMessage.contains("validationError"));
    HttpFailure serverErrorFailure = new ServerErrorFailure("serverError");
    errorMessage = RequestByInstanceIdResource.getErrorMessage(serverErrorFailure);
    assertEquals("serverError", errorMessage);
    HttpFailure badRequestFailure = new BadRequestFailure("badRequestFailure");
    errorMessage = RequestByInstanceIdResource.getErrorMessage(badRequestFailure);
    assertEquals("badRequestFailure", errorMessage);
    Response fakeResponse = new Response(500, "fakeResponseFailure", "text/javascript");
    HttpFailure forwardOnFailure = new ForwardOnFailure(fakeResponse);
    errorMessage = RequestByInstanceIdResource.getErrorMessage(forwardOnFailure);
    assertEquals("fakeResponseFailure", errorMessage);
}
Also used : Response(org.folio.circulation.support.http.client.Response) ServerErrorFailure(org.folio.circulation.support.ServerErrorFailure) HttpFailure(org.folio.circulation.support.HttpFailure) BadRequestFailure(org.folio.circulation.support.BadRequestFailure) ValidationError(org.folio.circulation.support.http.server.ValidationError) ForwardOnFailure(org.folio.circulation.support.ForwardOnFailure) Test(org.junit.jupiter.api.Test)

Example 4 with ValidationError

use of org.folio.circulation.support.http.server.ValidationError in project mod-circulation by folio-org.

the class RequestServiceUtility method refuseWhenMovedToDifferentInstance.

static Result<RequestAndRelatedRecords> refuseWhenMovedToDifferentInstance(RequestAndRelatedRecords requestAndRelatedRecords) {
    Request request = requestAndRelatedRecords.getRequest();
    Item item = request.getItem();
    if (!Objects.equals(item.getInstanceId(), request.getInstanceId())) {
        HashMap<String, String> parameters = new HashMap<>();
        parameters.put(ITEM_ID, request.getItemId());
        parameters.put(INSTANCE_ID, item.getInstanceId());
        return failedValidation(new ValidationError("Request can only be moved to an item with the same instance ID", parameters));
    }
    return succeeded(requestAndRelatedRecords);
}
Also used : HashMap(java.util.HashMap) ValidationError(org.folio.circulation.support.http.server.ValidationError)

Example 5 with ValidationError

use of org.folio.circulation.support.http.server.ValidationError in project mod-circulation by folio-org.

the class RequestServiceUtility method refuseWhenUserIsInactive.

static Result<RequestAndRelatedRecords> refuseWhenUserIsInactive(RequestAndRelatedRecords request) {
    User requester = request.getRequest().getRequester();
    if (requester != null && requester.isInactive()) {
        Map<String, String> parameters = new HashMap<>();
        parameters.put(REQUESTER_ID, request.getRequest().getUserId());
        parameters.put(ITEM_ID, request.getRequest().getItemId());
        String message = "Inactive users cannot make requests";
        return failedValidation(new ValidationError(message, parameters));
    } else {
        return of(() -> request);
    }
}
Also used : HashMap(java.util.HashMap) ValidationError(org.folio.circulation.support.http.server.ValidationError)

Aggregations

ValidationError (org.folio.circulation.support.http.server.ValidationError)19 ZonedDateTime (java.time.ZonedDateTime)9 ArrayList (java.util.ArrayList)9 Loan (org.folio.circulation.domain.Loan)8 JsonObject (io.vertx.core.json.JsonObject)6 ValidationErrorFailure (org.folio.circulation.support.ValidationErrorFailure)6 HashMap (java.util.HashMap)5 Request (org.folio.circulation.domain.Request)5 WebContext (org.folio.circulation.support.http.server.WebContext)5 Test (org.junit.jupiter.api.Test)5 List (java.util.List)4 RenewalValidator.itemByIdValidationError (org.folio.circulation.resources.RenewalValidator.itemByIdValidationError)4 RenewalValidator.loanPolicyValidationError (org.folio.circulation.resources.RenewalValidator.loanPolicyValidationError)4 Result (org.folio.circulation.support.results.Result)4 Router (io.vertx.ext.web.Router)3 RoutingContext (io.vertx.ext.web.RoutingContext)3 Collection (java.util.Collection)3 Map (java.util.Map)3 UUID (java.util.UUID)3 CompletableFuture (java.util.concurrent.CompletableFuture)3