Search in sources :

Example 1 with FundCollection

use of org.folio.rest.acq.model.finance.FundCollection in project mod-orders by folio-org.

the class FundService method getAllFundsByIds.

private CompletableFuture<List<Fund>> getAllFundsByIds(Collection<String> ids, RequestContext requestContext) {
    String query = convertIdsToCqlQuery(ids);
    RequestEntry requestEntry = new RequestEntry(ENDPOINT).withQuery(query).withLimit(MAX_IDS_FOR_GET_RQ).withOffset(0);
    return restClient.get(requestEntry, requestContext, FundCollection.class).thenApply(FundCollection::getFunds).thenApply(funds -> {
        if (funds.size() == ids.size()) {
            return funds;
        }
        List<Parameter> parameters = ids.stream().filter(id -> funds.stream().noneMatch(fund -> fund.getId().equals(id))).map(id -> new Parameter().withValue(id).withKey("funds")).collect(Collectors.toList());
        throw new HttpException(404, FUNDS_NOT_FOUND.toError().withParameters(parameters));
    });
}
Also used : RestClient(org.folio.rest.core.RestClient) FUNDS_NOT_FOUND(org.folio.rest.core.exceptions.ErrorCodes.FUNDS_NOT_FOUND) Collection(java.util.Collection) HelperUtils(org.folio.orders.utils.HelperUtils) HttpException(org.folio.rest.core.exceptions.HttpException) RequestEntry(org.folio.rest.core.models.RequestEntry) HelperUtils.collectResultsOnSuccess(org.folio.orders.utils.HelperUtils.collectResultsOnSuccess) CompletableFuture(java.util.concurrent.CompletableFuture) CompletionException(java.util.concurrent.CompletionException) CompositeFund(org.folio.rest.acq.model.finance.CompositeFund) Fund(org.folio.rest.acq.model.finance.Fund) Collectors(java.util.stream.Collectors) StreamEx.ofSubLists(one.util.streamex.StreamEx.ofSubLists) ArrayList(java.util.ArrayList) MAX_IDS_FOR_GET_RQ(org.folio.rest.RestConstants.MAX_IDS_FOR_GET_RQ) List(java.util.List) HelperUtils.convertIdsToCqlQuery(org.folio.orders.utils.HelperUtils.convertIdsToCqlQuery) RequestContext(org.folio.rest.core.models.RequestContext) FundCollection(org.folio.rest.acq.model.finance.FundCollection) Parameter(org.folio.rest.jaxrs.model.Parameter) Collections(java.util.Collections) Parameter(org.folio.rest.jaxrs.model.Parameter) HttpException(org.folio.rest.core.exceptions.HttpException) RequestEntry(org.folio.rest.core.models.RequestEntry)

Example 2 with FundCollection

use of org.folio.rest.acq.model.finance.FundCollection in project mod-orders by folio-org.

the class MockServer method handleGetFundById.

private void handleGetFundById(RoutingContext ctx) {
    logger.info("got: " + ctx.request().path());
    String id = ctx.request().getParam(ID);
    logger.info("id: " + id);
    FundCollection funds = getFundsByIds(Collections.singletonList(id)).mapTo(FundCollection.class);
    if (funds.getTotalRecords() == 0) {
        serverResponse(ctx, 404, APPLICATION_JSON, id);
    } else {
        JsonObject fund = new JsonObject().put("fund", JsonObject.mapFrom(funds.getFunds().get(0))).put("groupIds", new JsonArray());
        addServerRqRsData(HttpMethod.GET, FUNDS, fund);
        serverResponse(ctx, 200, APPLICATION_JSON, fund.encodePrettily());
    }
}
Also used : JsonArray(io.vertx.core.json.JsonArray) JsonObject(io.vertx.core.json.JsonObject) FundCollection(org.folio.rest.acq.model.finance.FundCollection)

Example 3 with FundCollection

use of org.folio.rest.acq.model.finance.FundCollection in project mod-orders by folio-org.

the class FundServiceTest method testShouldRetrieveFundsByFundIds.

@Test
void testShouldRetrieveFundsByFundIds() throws UnsupportedEncodingException {
    // Given
    String fundId1 = UUID.randomUUID().toString();
    String fundId2 = UUID.randomUUID().toString();
    List<String> fundIds = List.of(fundId1, fundId2);
    List<Fund> funds = List.of(new Fund().withId(fundId1), new Fund().withId(fundId2));
    FundCollection fundCollection = new FundCollection().withFunds(funds).withTotalRecords(1);
    String query = URLEncoder.encode("id==(" + fundId1 + " or " + fundId2 + ")", StandardCharsets.UTF_8.toString());
    doReturn(completedFuture(fundCollection)).when(restClient).get(any(), any(), any());
    // When
    List<Fund> actFund = fundService.getAllFunds(fundIds, requestContext).join();
    // Then
    ArgumentCaptor<RequestEntry> argumentCaptor = ArgumentCaptor.forClass(RequestEntry.class);
    verify(restClient).get(argumentCaptor.capture(), any(), eq(FundCollection.class));
    RequestEntry requestEntry = argumentCaptor.getValue();
    assertEquals(query, requestEntry.getQueryParams().get("query"));
    assertEquals(0, requestEntry.getQueryParams().get("offset"));
    assertEquals(MAX_IDS_FOR_GET_RQ, requestEntry.getQueryParams().get("limit"));
    assertThat(actFund, equalTo(fundCollection.getFunds()));
}
Also used : CompositeFund(org.folio.rest.acq.model.finance.CompositeFund) Fund(org.folio.rest.acq.model.finance.Fund) FundCollection(org.folio.rest.acq.model.finance.FundCollection) RequestEntry(org.folio.rest.core.models.RequestEntry) Test(org.junit.jupiter.api.Test)

Example 4 with FundCollection

use of org.folio.rest.acq.model.finance.FundCollection in project mod-orders by folio-org.

the class FundService method getFundsByLedgerId.

public CompletableFuture<List<Fund>> getFundsByLedgerId(String ledgerId, RequestContext requestContext) {
    String query = String.format(FUNDS_BY_LEDGER_ID_QUERY, ledgerId);
    RequestEntry requestEntry = new RequestEntry(ENDPOINT).withQuery(query).withLimit(Integer.MAX_VALUE).withOffset(0);
    return restClient.get(requestEntry, requestContext, FundCollection.class).thenApply(FundCollection::getFunds);
}
Also used : FundCollection(org.folio.rest.acq.model.finance.FundCollection) RequestEntry(org.folio.rest.core.models.RequestEntry)

Example 5 with FundCollection

use of org.folio.rest.acq.model.finance.FundCollection in project mod-orders by folio-org.

the class FundService method getFundsByIds.

private CompletableFuture<List<Fund>> getFundsByIds(Collection<String> ids, RequestContext requestContext) {
    String query = convertIdsToCqlQuery(ids);
    RequestEntry requestEntry = new RequestEntry(ENDPOINT).withQuery(query).withLimit(MAX_IDS_FOR_GET_RQ).withOffset(0);
    return restClient.get(requestEntry, requestContext, FundCollection.class).thenApply(FundCollection::getFunds);
}
Also used : FundCollection(org.folio.rest.acq.model.finance.FundCollection) RequestEntry(org.folio.rest.core.models.RequestEntry)

Aggregations

FundCollection (org.folio.rest.acq.model.finance.FundCollection)9 RequestEntry (org.folio.rest.core.models.RequestEntry)6 Fund (org.folio.rest.acq.model.finance.Fund)5 CompositeFund (org.folio.rest.acq.model.finance.CompositeFund)4 JsonObject (io.vertx.core.json.JsonObject)3 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 Test (org.junit.jupiter.api.Test)2 JsonArray (io.vertx.core.json.JsonArray)1 Collection (java.util.Collection)1 Collections (java.util.Collections)1 CompletableFuture (java.util.concurrent.CompletableFuture)1 CompletionException (java.util.concurrent.CompletionException)1 ExecutionException (java.util.concurrent.ExecutionException)1 TimeoutException (java.util.concurrent.TimeoutException)1 Collectors (java.util.stream.Collectors)1 Collectors.toList (java.util.stream.Collectors.toList)1 StreamEx.ofSubLists (one.util.streamex.StreamEx.ofSubLists)1 HelperUtils (org.folio.orders.utils.HelperUtils)1