Search in sources :

Example 26 with Result

use of org.folio.circulation.support.results.Result in project mod-circulation by folio-org.

the class UpdateRequestQueueTest method removalShouldFailWhenBatchUpdateFails.

@Test
void removalShouldFailWhenBatchUpdateFails() throws Exception {
    UUID itemId = UUID.randomUUID();
    RequestQueue requestQueue = createRequestQueue(itemId, 4);
    Request requestToRemove = requestQueue.getRequests().iterator().next();
    when(requestQueueRepository.getByItemId(itemId.toString())).thenReturn(completedFuture(succeeded(requestQueue)));
    CompletableFuture<Result<Request>> completableFutureResult = updateRequestQueue.onDeletion(requestToRemove);
    assertFailedOnFailureResponse(completableFutureResult);
}
Also used : ReorderRequest(org.folio.circulation.domain.reorder.ReorderRequest) ReorderQueueRequest(org.folio.circulation.domain.reorder.ReorderQueueRequest) UUID(java.util.UUID) Result(org.folio.circulation.support.results.Result) Test(org.junit.jupiter.api.Test)

Example 27 with Result

use of org.folio.circulation.support.results.Result 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 28 with Result

use of org.folio.circulation.support.results.Result in project mod-circulation by folio-org.

the class CirculationRulesCache method getDrools.

public CompletableFuture<Result<Drools>> getDrools(String tenantId, CollectionResourceClient circulationRulesClient) {
    final CompletableFuture<Result<Drools>> cfDrools = new CompletableFuture<>();
    Rules rules = rulesMap.get(tenantId);
    if (isCurrent(rules)) {
        cfDrools.complete(succeeded(rules.drools));
        if (reloadNeeded(rules)) {
            rules.reloadInitiated = true;
            reloadRules(rules, circulationRulesClient).thenCompose(r -> r.after(updatedRules -> ofAsync(() -> updatedRules.drools)));
        }
        return cfDrools;
    }
    if (rules == null) {
        rules = new Rules();
        rulesMap.put(tenantId, rules);
    }
    return reloadRules(rules, circulationRulesClient).thenCompose(r -> r.after(updatedRules -> ofAsync(() -> updatedRules.drools)));
}
Also used : CompletableFuture.completedFuture(java.util.concurrent.CompletableFuture.completedFuture) Result.succeeded(org.folio.circulation.support.results.Result.succeeded) MethodHandles(java.lang.invoke.MethodHandles) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) CompletableFuture(java.util.concurrent.CompletableFuture) CollectionResourceClient(org.folio.circulation.support.CollectionResourceClient) ExecutableRules(org.folio.circulation.rules.ExecutableRules) Result(org.folio.circulation.support.results.Result) ServerErrorFailure(org.folio.circulation.support.ServerErrorFailure) Result.ofAsync(org.folio.circulation.support.results.Result.ofAsync) Logger(org.apache.logging.log4j.Logger) Drools(org.folio.circulation.rules.Drools) StringUtils.isBlank(org.apache.commons.lang3.StringUtils.isBlank) Text2Drools(org.folio.circulation.rules.Text2Drools) Map(java.util.Map) JsonObject(io.vertx.core.json.JsonObject) Result.failed(org.folio.circulation.support.results.Result.failed) LogManager(org.apache.logging.log4j.LogManager) CompletableFuture(java.util.concurrent.CompletableFuture) ExecutableRules(org.folio.circulation.rules.ExecutableRules) Result(org.folio.circulation.support.results.Result)

Example 29 with Result

use of org.folio.circulation.support.results.Result in project mod-circulation by folio-org.

the class EventPublisher method publishItemCheckedOutEvent.

public CompletableFuture<Result<LoanAndRelatedRecords>> publishItemCheckedOutEvent(LoanAndRelatedRecords loanAndRelatedRecords, UserRepository userRepository) {
    if (loanAndRelatedRecords.getLoan() != null) {
        Loan loan = loanAndRelatedRecords.getLoan();
        JsonObject payloadJsonObject = new JsonObject();
        write(payloadJsonObject, USER_ID_FIELD, loan.getUserId());
        write(payloadJsonObject, LOAN_ID_FIELD, loan.getId());
        write(payloadJsonObject, DUE_DATE_FIELD, loan.getDueDate());
        ofNullable(loan.getLoanPolicy()).map(LoanPolicy::getGracePeriod).filter(Period::isValid).map(Period::asJson).ifPresent(json -> write(payloadJsonObject, GRACE_PERIOD_FIELD, json));
        runAsync(() -> userRepository.getUser(loanAndRelatedRecords.getLoggedInUserId()).thenApplyAsync(r -> r.after(loggedInUser -> CompletableFuture.completedFuture(Result.succeeded(pubSubPublishingService.publishEvent(LOG_RECORD.name(), mapToCheckOutLogEventContent(loanAndRelatedRecords, loggedInUser)))))));
        return pubSubPublishingService.publishEvent(ITEM_CHECKED_OUT.name(), payloadJsonObject.encode()).handle((result, error) -> handlePublishEventError(error, loanAndRelatedRecords));
    } else {
        logger.error(FAILED_TO_PUBLISH_LOG_TEMPLATE, ITEM_CHECKED_OUT.name());
    }
    return completedFuture(succeeded(loanAndRelatedRecords));
}
Also used : UserRepository(org.folio.circulation.infrastructure.storage.users.UserRepository) RenewalContext(org.folio.circulation.resources.context.RenewalContext) LoanRepository(org.folio.circulation.infrastructure.storage.loans.LoanRepository) LogContextActionResolver(org.folio.circulation.domain.representations.logs.LogContextActionResolver) CompletableFuture.completedFuture(java.util.concurrent.CompletableFuture.completedFuture) RoutingContext(io.vertx.ext.web.RoutingContext) JsonPropertyWriter.write(org.folio.circulation.support.json.JsonPropertyWriter.write) CHECKED_IN(org.folio.circulation.domain.LoanAction.CHECKED_IN) DateFormatUtil.formatDateTimeOptional(org.folio.circulation.support.utils.DateFormatUtil.formatDateTimeOptional) LoanLogContext(org.folio.circulation.domain.representations.logs.LoanLogContext) LOAN_CLOSED(org.folio.circulation.domain.EventType.LOAN_CLOSED) LOG_EVENT_TYPE(org.folio.circulation.domain.representations.logs.LogEventPayloadField.LOG_EVENT_TYPE) LoanAndRelatedRecords(org.folio.circulation.domain.LoanAndRelatedRecords) Period(org.folio.circulation.domain.policy.Period) HttpFailure(org.folio.circulation.support.HttpFailure) CommonFailures.failedDueToServerError(org.folio.circulation.support.results.CommonFailures.failedDueToServerError) LoanAnonymizationRecords(org.folio.circulation.domain.anonymization.LoanAnonymizationRecords) JsonObject(io.vertx.core.json.JsonObject) DUE_DATE_CHANGED(org.folio.circulation.domain.LoanAction.DUE_DATE_CHANGED) LOAN(org.folio.circulation.domain.representations.logs.LogEventType.LOAN) CirculationCheckInCheckOutLogEventMapper.mapToCheckOutLogEventContent(org.folio.circulation.domain.representations.logs.CirculationCheckInCheckOutLogEventMapper.mapToCheckOutLogEventContent) Result.succeeded(org.folio.circulation.support.results.Result.succeeded) Loan(org.folio.circulation.domain.Loan) PAYLOAD(org.folio.circulation.domain.representations.logs.LogEventPayloadField.PAYLOAD) Result(org.folio.circulation.support.results.Result) LoanPolicy(org.folio.circulation.domain.policy.LoanPolicy) Logger(org.apache.logging.log4j.Logger) ITEM_CLAIMED_RETURNED(org.folio.circulation.domain.EventType.ITEM_CLAIMED_RETURNED) NoticeLogContext(org.folio.circulation.domain.representations.logs.NoticeLogContext) NOTICE(org.folio.circulation.domain.representations.logs.LogEventType.NOTICE) ClockUtil.getZonedDateTime(org.folio.circulation.support.utils.ClockUtil.getZonedDateTime) EventType(org.folio.circulation.domain.EventType) Request(org.folio.circulation.domain.Request) CompletableFuture(java.util.concurrent.CompletableFuture) ITEM_CHECKED_OUT(org.folio.circulation.domain.EventType.ITEM_CHECKED_OUT) CheckInContext(org.folio.circulation.domain.CheckInContext) AsyncCoordinationUtil.allOf(org.folio.circulation.support.AsyncCoordinationUtil.allOf) RequestUpdateLogEventMapper.mapToRequestLogEventJson(org.folio.circulation.domain.representations.logs.RequestUpdateLogEventMapper.mapToRequestLogEventJson) RequestAndRelatedRecords(org.folio.circulation.domain.RequestAndRelatedRecords) LOAN_DUE_DATE_CHANGED(org.folio.circulation.domain.EventType.LOAN_DUE_DATE_CHANGED) LogEventType(org.folio.circulation.domain.representations.logs.LogEventType) CompletableFuture.runAsync(java.util.concurrent.CompletableFuture.runAsync) Optional.ofNullable(java.util.Optional.ofNullable) NOTICE_ERROR(org.folio.circulation.domain.representations.logs.LogEventType.NOTICE_ERROR) CirculationCheckInCheckOutLogEventMapper.mapToCheckInLogEventContent(org.folio.circulation.domain.representations.logs.CirculationCheckInCheckOutLogEventMapper.mapToCheckInLogEventContent) Result.ofAsync(org.folio.circulation.support.results.Result.ofAsync) ITEM_AGED_TO_LOST(org.folio.circulation.domain.EventType.ITEM_AGED_TO_LOST) User(org.folio.circulation.domain.User) ITEM_CHECKED_IN(org.folio.circulation.domain.EventType.ITEM_CHECKED_IN) ITEM_DECLARED_LOST(org.folio.circulation.domain.EventType.ITEM_DECLARED_LOST) LOG_RECORD(org.folio.circulation.domain.EventType.LOG_RECORD) RECALLREQUESTED(org.folio.circulation.domain.LoanAction.RECALLREQUESTED) LogManager(org.apache.logging.log4j.LogManager) Loan(org.folio.circulation.domain.Loan) JsonObject(io.vertx.core.json.JsonObject) Period(org.folio.circulation.domain.policy.Period)

Example 30 with Result

use of org.folio.circulation.support.results.Result in project mod-circulation by folio-org.

the class EventPublisher method publishItemCheckedInEvents.

public CompletableFuture<Result<CheckInContext>> publishItemCheckedInEvents(CheckInContext checkInContext, UserRepository userRepository) {
    runAsync(() -> userRepository.getUser(checkInContext.getLoggedInUserId()).thenApplyAsync(r -> r.after(loggedInUser -> CompletableFuture.completedFuture(Result.succeeded(pubSubPublishingService.publishEvent(LOG_RECORD.name(), mapToCheckInLogEventContent(checkInContext, loggedInUser)))))));
    if (checkInContext.getLoan() != null) {
        Loan loan = checkInContext.getLoan();
        JsonObject payloadJsonObject = new JsonObject();
        write(payloadJsonObject, USER_ID_FIELD, loan.getUserId());
        write(payloadJsonObject, LOAN_ID_FIELD, loan.getId());
        write(payloadJsonObject, RETURN_DATE_FIELD, loan.getReturnDate());
        return pubSubPublishingService.publishEvent(ITEM_CHECKED_IN.name(), payloadJsonObject.encode()).handle((result, error) -> handlePublishEventError(error, checkInContext));
    } else {
        logger.error(FAILED_TO_PUBLISH_LOG_TEMPLATE, ITEM_CHECKED_IN.name());
    }
    return completedFuture(succeeded(checkInContext));
}
Also used : UserRepository(org.folio.circulation.infrastructure.storage.users.UserRepository) RenewalContext(org.folio.circulation.resources.context.RenewalContext) LoanRepository(org.folio.circulation.infrastructure.storage.loans.LoanRepository) LogContextActionResolver(org.folio.circulation.domain.representations.logs.LogContextActionResolver) CompletableFuture.completedFuture(java.util.concurrent.CompletableFuture.completedFuture) RoutingContext(io.vertx.ext.web.RoutingContext) JsonPropertyWriter.write(org.folio.circulation.support.json.JsonPropertyWriter.write) CHECKED_IN(org.folio.circulation.domain.LoanAction.CHECKED_IN) DateFormatUtil.formatDateTimeOptional(org.folio.circulation.support.utils.DateFormatUtil.formatDateTimeOptional) LoanLogContext(org.folio.circulation.domain.representations.logs.LoanLogContext) LOAN_CLOSED(org.folio.circulation.domain.EventType.LOAN_CLOSED) LOG_EVENT_TYPE(org.folio.circulation.domain.representations.logs.LogEventPayloadField.LOG_EVENT_TYPE) LoanAndRelatedRecords(org.folio.circulation.domain.LoanAndRelatedRecords) Period(org.folio.circulation.domain.policy.Period) HttpFailure(org.folio.circulation.support.HttpFailure) CommonFailures.failedDueToServerError(org.folio.circulation.support.results.CommonFailures.failedDueToServerError) LoanAnonymizationRecords(org.folio.circulation.domain.anonymization.LoanAnonymizationRecords) JsonObject(io.vertx.core.json.JsonObject) DUE_DATE_CHANGED(org.folio.circulation.domain.LoanAction.DUE_DATE_CHANGED) LOAN(org.folio.circulation.domain.representations.logs.LogEventType.LOAN) CirculationCheckInCheckOutLogEventMapper.mapToCheckOutLogEventContent(org.folio.circulation.domain.representations.logs.CirculationCheckInCheckOutLogEventMapper.mapToCheckOutLogEventContent) Result.succeeded(org.folio.circulation.support.results.Result.succeeded) Loan(org.folio.circulation.domain.Loan) PAYLOAD(org.folio.circulation.domain.representations.logs.LogEventPayloadField.PAYLOAD) Result(org.folio.circulation.support.results.Result) LoanPolicy(org.folio.circulation.domain.policy.LoanPolicy) Logger(org.apache.logging.log4j.Logger) ITEM_CLAIMED_RETURNED(org.folio.circulation.domain.EventType.ITEM_CLAIMED_RETURNED) NoticeLogContext(org.folio.circulation.domain.representations.logs.NoticeLogContext) NOTICE(org.folio.circulation.domain.representations.logs.LogEventType.NOTICE) ClockUtil.getZonedDateTime(org.folio.circulation.support.utils.ClockUtil.getZonedDateTime) EventType(org.folio.circulation.domain.EventType) Request(org.folio.circulation.domain.Request) CompletableFuture(java.util.concurrent.CompletableFuture) ITEM_CHECKED_OUT(org.folio.circulation.domain.EventType.ITEM_CHECKED_OUT) CheckInContext(org.folio.circulation.domain.CheckInContext) AsyncCoordinationUtil.allOf(org.folio.circulation.support.AsyncCoordinationUtil.allOf) RequestUpdateLogEventMapper.mapToRequestLogEventJson(org.folio.circulation.domain.representations.logs.RequestUpdateLogEventMapper.mapToRequestLogEventJson) RequestAndRelatedRecords(org.folio.circulation.domain.RequestAndRelatedRecords) LOAN_DUE_DATE_CHANGED(org.folio.circulation.domain.EventType.LOAN_DUE_DATE_CHANGED) LogEventType(org.folio.circulation.domain.representations.logs.LogEventType) CompletableFuture.runAsync(java.util.concurrent.CompletableFuture.runAsync) Optional.ofNullable(java.util.Optional.ofNullable) NOTICE_ERROR(org.folio.circulation.domain.representations.logs.LogEventType.NOTICE_ERROR) CirculationCheckInCheckOutLogEventMapper.mapToCheckInLogEventContent(org.folio.circulation.domain.representations.logs.CirculationCheckInCheckOutLogEventMapper.mapToCheckInLogEventContent) Result.ofAsync(org.folio.circulation.support.results.Result.ofAsync) ITEM_AGED_TO_LOST(org.folio.circulation.domain.EventType.ITEM_AGED_TO_LOST) User(org.folio.circulation.domain.User) ITEM_CHECKED_IN(org.folio.circulation.domain.EventType.ITEM_CHECKED_IN) ITEM_DECLARED_LOST(org.folio.circulation.domain.EventType.ITEM_DECLARED_LOST) LOG_RECORD(org.folio.circulation.domain.EventType.LOG_RECORD) RECALLREQUESTED(org.folio.circulation.domain.LoanAction.RECALLREQUESTED) LogManager(org.apache.logging.log4j.LogManager) Loan(org.folio.circulation.domain.Loan) JsonObject(io.vertx.core.json.JsonObject)

Aggregations

Result (org.folio.circulation.support.results.Result)65 CompletableFuture (java.util.concurrent.CompletableFuture)42 Result.succeeded (org.folio.circulation.support.results.Result.succeeded)35 Clients (org.folio.circulation.support.Clients)31 Loan (org.folio.circulation.domain.Loan)30 JsonObject (io.vertx.core.json.JsonObject)29 LoanRepository (org.folio.circulation.infrastructure.storage.loans.LoanRepository)29 CompletableFuture.completedFuture (java.util.concurrent.CompletableFuture.completedFuture)28 RoutingContext (io.vertx.ext.web.RoutingContext)26 UserRepository (org.folio.circulation.infrastructure.storage.users.UserRepository)24 WebContext (org.folio.circulation.support.http.server.WebContext)24 LogManager (org.apache.logging.log4j.LogManager)23 Logger (org.apache.logging.log4j.Logger)23 HttpClient (io.vertx.core.http.HttpClient)22 Router (io.vertx.ext.web.Router)22 List (java.util.List)21 ItemRepository (org.folio.circulation.infrastructure.storage.inventory.ItemRepository)21 EventPublisher (org.folio.circulation.services.EventPublisher)21 MethodHandles (java.lang.invoke.MethodHandles)20 Result.ofAsync (org.folio.circulation.support.results.Result.ofAsync)19