Search in sources :

Example 6 with Result

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

the class VertxWebClientOkapiHttpClientTests method canGetJsonUsingQueryParameters.

@Test
public void canGetJsonUsingQueryParameters() throws InterruptedException, ExecutionException, TimeoutException {
    fakeWebServer.stubFor(matchingFolioHeaders(get(urlPathEqualTo("/record"))).withQueryParam("first-parameter", equalTo("foo")).withQueryParam("second-parameter", equalTo("bar")).willReturn(okJson(dummyJsonResponseBody())));
    OkapiHttpClient client = createClient();
    CompletableFuture<Result<Response>> getCompleted = client.get(fakeWebServer.url("/record"), namedParameter("first-parameter", "foo"), namedParameter("second-parameter", "bar"));
    final Response response = getCompleted.get(2, SECONDS).value();
    assertThat(response, hasStatus(HTTP_OK));
    assertThat(response.getJson().getString("message"), is("hello"));
    assertThat(response.getContentType(), is("application/json"));
}
Also used : WireMock.aResponse(com.github.tomakehurst.wiremock.client.WireMock.aResponse) Result(org.folio.circulation.support.results.Result) Test(org.junit.Test)

Example 7 with Result

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

the class VertxWebClientOkapiHttpClientTests method canDeleteAResource.

@Test
public void canDeleteAResource() throws InterruptedException, ExecutionException, TimeoutException {
    fakeWebServer.stubFor(matchingFolioHeaders(delete(urlPathEqualTo("/record"))).willReturn(noContent()));
    OkapiHttpClient client = createClient();
    CompletableFuture<Result<Response>> getCompleted = client.delete(fakeWebServer.url("/record"));
    final Response response = getCompleted.get(2, SECONDS).value();
    assertThat(response, hasStatus(HTTP_NO_CONTENT));
}
Also used : WireMock.aResponse(com.github.tomakehurst.wiremock.client.WireMock.aResponse) Result(org.folio.circulation.support.results.Result) Test(org.junit.Test)

Example 8 with Result

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

the class VertxWebClientOkapiHttpClientTests method failsWhenGetTimesOut.

@Test
public void failsWhenGetTimesOut() throws InterruptedException, ExecutionException, TimeoutException {
    fakeWebServer.stubFor(matchingFolioHeaders(get(urlPathEqualTo("/record"))).willReturn(aResponse().withFixedDelay(1000)));
    OkapiHttpClient client = createClient();
    CompletableFuture<Result<Response>> getCompleted = client.get(fakeWebServer.url("/record"), Duration.of(500, MILLIS));
    final Result<Response> responseResult = getCompleted.get(1, SECONDS);
    assertThat(responseResult.failed(), is(true));
    assertThat(responseResult.cause(), is(instanceOf(ServerErrorFailure.class)));
    final ServerErrorFailure cause = (ServerErrorFailure) responseResult.cause();
    assertThat(cause.getReason(), containsString("The timeout period of 500ms has been exceeded while executing " + "GET /record for server localhost"));
}
Also used : WireMock.aResponse(com.github.tomakehurst.wiremock.client.WireMock.aResponse) ServerErrorFailure(org.folio.circulation.support.ServerErrorFailure) Result(org.folio.circulation.support.results.Result) Test(org.junit.Test)

Example 9 with Result

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

the class OverduePeriodCalculatorService method minutesOverdueExcludingClosedPeriods.

private CompletableFuture<Result<Integer>> minutesOverdueExcludingClosedPeriods(Loan loan, ZonedDateTime returnDate) {
    ZonedDateTime dueDate = loan.getDueDate();
    String itemLocationPrimaryServicePoint = getItemLocationPrimaryServicePoint(loan).toString();
    return calendarRepository.fetchOpeningDaysBetweenDates(itemLocationPrimaryServicePoint, dueDate, returnDate, false).thenApply(r -> r.next(openingDays -> getOpeningDaysDurationMinutes(openingDays, dueDate, returnDate)));
}
Also used : CompletableFuture.completedFuture(java.util.concurrent.CompletableFuture.completedFuture) Result.succeeded(org.folio.circulation.support.results.Result.succeeded) DateTimeUtil.isWithinMillis(org.folio.circulation.support.utils.DateTimeUtil.isWithinMillis) ZonedDateTime(java.time.ZonedDateTime) Collection(java.util.Collection) LoanPolicyRepository(org.folio.circulation.infrastructure.storage.loans.LoanPolicyRepository) CompletableFuture(java.util.concurrent.CompletableFuture) DateTimeUtil.isBeforeMillis(org.folio.circulation.support.utils.DateTimeUtil.isBeforeMillis) UUID(java.util.UUID) Result(org.folio.circulation.support.results.Result) CalendarRepository(org.folio.circulation.infrastructure.storage.CalendarRepository) ResultBinding.flatMapResult(org.folio.circulation.support.results.ResultBinding.flatMapResult) ChronoUnit(java.time.temporal.ChronoUnit) ObjectUtils.allNotNull(org.apache.commons.lang3.ObjectUtils.allNotNull) LocalDate(java.time.LocalDate) DateTimeUtil.isAfterMillis(org.folio.circulation.support.utils.DateTimeUtil.isAfterMillis) ZonedDateTime(java.time.ZonedDateTime)

Example 10 with Result

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

the class RequestByInstanceIdResource method validateRequester.

private Result<Collection<Item>> validateRequester(Map<Item, RequestQueue> itemRequestQueueMap, InstanceRequestRelatedRecords requestPackage) {
    if (!itemRequestQueueMap.isEmpty()) {
        Collection<RequestQueue> requestQueues = itemRequestQueueMap.values();
        String requesterId = requestPackage.getInstanceLevelRequest().getRequesterId().toString();
        for (RequestQueue queue : requestQueues) {
            final Optional<Request> matchingRequest = queue.getRequests().stream().filter(request -> request.isOpen() && Objects.equals(request.getUserId(), requesterId)).findFirst();
            if (matchingRequest.isPresent()) {
                Map<String, String> parameters = new HashMap<>();
                parameters.put(REQUESTER_ID, requesterId);
                parameters.put(ITEM_ID, matchingRequest.get().getItemId());
                parameters.put("instanceId", requestPackage.getInstanceLevelRequest().getInstanceId().toString());
                String message = "This requester already has an open request for an item of this instance";
                return failedValidation(new ValidationError(message, parameters));
            }
        }
    }
    return of(itemRequestQueueMap::keySet);
}
Also used : HOLD(org.folio.circulation.domain.RequestType.HOLD) UserRepository(org.folio.circulation.infrastructure.storage.users.UserRepository) UpdateRequestQueue(org.folio.circulation.domain.UpdateRequestQueue) CreateRequestService(org.folio.circulation.domain.CreateRequestService) LoanRepository(org.folio.circulation.infrastructure.storage.loans.LoanRepository) ConfigurationRepository(org.folio.circulation.infrastructure.storage.ConfigurationRepository) InstanceRequestRelatedRecords(org.folio.circulation.domain.InstanceRequestRelatedRecords) ZonedDateTime(java.time.ZonedDateTime) RequestQueueRepository(org.folio.circulation.infrastructure.storage.requests.RequestQueueRepository) LoanPolicyRepository(org.folio.circulation.infrastructure.storage.loans.LoanPolicyRepository) ServerErrorResponse(org.folio.circulation.support.http.server.ServerErrorResponse) Router(io.vertx.ext.web.Router) ValidationErrorFailure.singleValidationError(org.folio.circulation.support.ValidationErrorFailure.singleValidationError) UpdateItem(org.folio.circulation.domain.UpdateItem) RoutingContext(io.vertx.ext.web.RoutingContext) JsonPropertyWriter.write(org.folio.circulation.support.json.JsonPropertyWriter.write) Item(org.folio.circulation.domain.Item) REQUEST_LEVEL(org.folio.circulation.domain.representations.RequestProperties.REQUEST_LEVEL) ServicePointRepository(org.folio.circulation.infrastructure.storage.ServicePointRepository) HttpFailure(org.folio.circulation.support.HttpFailure) Map(java.util.Map) CommonFailures.failedDueToServerError(org.folio.circulation.support.results.CommonFailures.failedDueToServerError) JsonObject(io.vertx.core.json.JsonObject) ItemRepository(org.folio.circulation.infrastructure.storage.inventory.ItemRepository) RequestRepresentation(org.folio.circulation.domain.RequestRepresentation) RequestBlockValidators.regularRequestBlockValidators(org.folio.circulation.resources.RequestBlockValidators.regularRequestBlockValidators) Result.of(org.folio.circulation.support.results.Result.of) Result.succeeded(org.folio.circulation.support.results.Result.succeeded) MethodHandles(java.lang.invoke.MethodHandles) Collection(java.util.Collection) Loan(org.folio.circulation.domain.Loan) RequestLoanValidator(org.folio.circulation.domain.validation.RequestLoanValidator) RequestPolicyRepository(org.folio.circulation.infrastructure.storage.requests.RequestPolicyRepository) UUID(java.util.UUID) ValidationError(org.folio.circulation.support.http.server.ValidationError) Result(org.folio.circulation.support.results.Result) Collectors(java.util.stream.Collectors) EventPublisher(org.folio.circulation.services.EventPublisher) Objects(java.util.Objects) PROXY_USER_ID(org.folio.circulation.domain.representations.RequestProperties.PROXY_USER_ID) ServicePointPickupLocationValidator(org.folio.circulation.domain.validation.ServicePointPickupLocationValidator) RouteRegistration(org.folio.circulation.support.RouteRegistration) List(java.util.List) PAGE(org.folio.circulation.domain.RequestType.PAGE) Logger(org.apache.logging.log4j.Logger) RECALL(org.folio.circulation.domain.RequestType.RECALL) HOLD_SHELF(org.folio.circulation.domain.RequestFulfilmentPreference.HOLD_SHELF) InstanceRepository(org.folio.circulation.infrastructure.storage.inventory.InstanceRepository) REQUEST_TYPE(org.folio.circulation.domain.representations.RequestProperties.REQUEST_TYPE) Optional(java.util.Optional) BadRequestFailure(org.folio.circulation.support.BadRequestFailure) FULFILMENT_PREFERENCE(org.folio.circulation.domain.representations.RequestProperties.FULFILMENT_PREFERENCE) CreateRequestRepositories(org.folio.circulation.domain.CreateRequestRepositories) RequestRepository(org.folio.circulation.infrastructure.storage.requests.RequestRepository) HttpClient(io.vertx.core.http.HttpClient) Clients(org.folio.circulation.support.Clients) WebContext(org.folio.circulation.support.http.server.WebContext) Request(org.folio.circulation.domain.Request) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) FailFastErrorHandler(org.folio.circulation.resources.handlers.error.FailFastErrorHandler) JsonHttpResponse(org.folio.circulation.support.http.server.JsonHttpResponse) UpdateUponRequest(org.folio.circulation.domain.UpdateUponRequest) ArrayList(java.util.ArrayList) TlrSettingsConfiguration(org.folio.circulation.domain.configuration.TlrSettingsConfiguration) ForwardOnFailure(org.folio.circulation.support.ForwardOnFailure) RequestAndRelatedRecords(org.folio.circulation.domain.RequestAndRelatedRecords) RequestQueue(org.folio.circulation.domain.RequestQueue) REQUESTER_ID(org.folio.circulation.domain.representations.RequestProperties.REQUESTER_ID) LinkedList(java.util.LinkedList) RequestType(org.folio.circulation.domain.RequestType) RequestFulfilmentPreference(org.folio.circulation.domain.RequestFulfilmentPreference) ItemByInstanceIdFinder(org.folio.circulation.storage.ItemByInstanceIdFinder) ValidationErrorFailure(org.folio.circulation.support.ValidationErrorFailure) ServerErrorFailure(org.folio.circulation.support.ServerErrorFailure) Result.ofAsync(org.folio.circulation.support.results.Result.ofAsync) ITEM_ID(org.folio.circulation.domain.representations.RequestProperties.ITEM_ID) ValidationErrorFailure.failedValidation(org.folio.circulation.support.ValidationErrorFailure.failedValidation) InstanceRequestItemsComparer.sortRequestQueues(org.folio.circulation.domain.InstanceRequestItemsComparer.sortRequestQueues) UpdateLoan(org.folio.circulation.domain.UpdateLoan) Collectors.toList(java.util.stream.Collectors.toList) RequestLevel(org.folio.circulation.domain.RequestLevel) ProxyRelationshipValidator(org.folio.circulation.domain.validation.ProxyRelationshipValidator) RequestByInstanceIdRequest(org.folio.circulation.domain.representations.RequestByInstanceIdRequest) LogManager(org.apache.logging.log4j.LogManager) HashMap(java.util.HashMap) UpdateRequestQueue(org.folio.circulation.domain.UpdateRequestQueue) RequestQueue(org.folio.circulation.domain.RequestQueue) Request(org.folio.circulation.domain.Request) UpdateUponRequest(org.folio.circulation.domain.UpdateUponRequest) RequestByInstanceIdRequest(org.folio.circulation.domain.representations.RequestByInstanceIdRequest) ValidationErrorFailure.singleValidationError(org.folio.circulation.support.ValidationErrorFailure.singleValidationError) ValidationError(org.folio.circulation.support.http.server.ValidationError)

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