Search in sources :

Example 11 with HttpException

use of org.folio.rest.core.exceptions.HttpException in project mod-orders by folio-org.

the class ManualExchangeRateProvider method getExchangeRate.

@Override
public ExchangeRate getExchangeRate(ConversionQuery conversionQuery) {
    ExchangeRateBuilder builder = new ExchangeRateBuilder(ConversionContext.of());
    builder.setBase(conversionQuery.getBaseCurrency());
    builder.setTerm(conversionQuery.getCurrency());
    if (conversionQuery.get(RATE_KEY, Double.class) == null) {
        throw new HttpException(500, "Rate must be provided in provider : " + this.getClass().getSimpleName());
    }
    builder.setFactor(DefaultNumberValue.of(conversionQuery.get(RATE_KEY, Double.class)));
    return builder.build();
}
Also used : ExchangeRateBuilder(org.javamoney.moneta.convert.ExchangeRateBuilder) HttpException(org.folio.rest.core.exceptions.HttpException)

Example 12 with HttpException

use of org.folio.rest.core.exceptions.HttpException 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 13 with HttpException

use of org.folio.rest.core.exceptions.HttpException in project mod-orders by folio-org.

the class PurchaseOrderHelperTest method testDeleteOrderLinkedToInvoiceWithError.

@Test
void testDeleteOrderLinkedToInvoiceWithError() {
    // given
    InvoiceLineService invoiceLineService = new InvoiceLineService(restClient);
    RestClient restClient = mock(RestClient.class, CALLS_REAL_METHODS);
    OrderInvoiceRelationService orderInvoiceRelationService = spy(new OrderInvoiceRelationService(restClient, invoiceLineService));
    // for returning non empty collection
    OrderInvoiceRelationshipCollection oirCollection = new OrderInvoiceRelationshipCollection().withOrderInvoiceRelationships(Collections.singletonList(new OrderInvoiceRelationship())).withTotalRecords(1);
    doReturn(completedFuture(oirCollection)).when(restClient).get(any(), any(), any());
    CompletableFuture<Void> future = orderInvoiceRelationService.checkOrderInvoiceRelationship(ORDER_ID, new RequestContext(ctxMock, okapiHeadersMock));
    CompletionException exception = assertThrows(CompletionException.class, future::join);
    HttpException httpException = (HttpException) exception.getCause();
    assertEquals(ErrorCodes.ORDER_RELATES_TO_INVOICE.getDescription(), httpException.getMessage());
}
Also used : OrderInvoiceRelationService(org.folio.service.orders.OrderInvoiceRelationService) OrderInvoiceRelationshipCollection(org.folio.rest.acq.model.OrderInvoiceRelationshipCollection) CompletionException(java.util.concurrent.CompletionException) RestClient(org.folio.rest.core.RestClient) OrderInvoiceRelationship(org.folio.rest.acq.model.OrderInvoiceRelationship) InvoiceLineService(org.folio.service.invoice.InvoiceLineService) HttpException(org.folio.rest.core.exceptions.HttpException) RequestContext(org.folio.rest.core.models.RequestContext) Test(org.junit.jupiter.api.Test)

Example 14 with HttpException

use of org.folio.rest.core.exceptions.HttpException in project mod-orders by folio-org.

the class InventoryManagerTest method shouldThrowExceptionIfHoldingIsNotAlreadyExist.

@Test
void shouldThrowExceptionIfHoldingIsNotAlreadyExist() {
    String instanceId = UUID.randomUUID().toString();
    String holdingId = UUID.randomUUID().toString();
    Location location = new Location().withHoldingId(holdingId).withQuantity(1).withQuantityPhysical(1);
    String msg = String.format(HOLDINGS_BY_ID_NOT_FOUND.getDescription(), holdingId);
    Error error = new Error().withCode(HOLDINGS_BY_ID_NOT_FOUND.getCode()).withMessage(msg);
    when(restClient.getAsJsonObject(any(RequestEntry.class), eq(requestContext))).thenThrow(new CompletionException(new HttpException(NOT_FOUND, error)));
    CompletionException exception = assertThrows(CompletionException.class, () -> inventoryManager.getOrCreateHoldingsRecord(instanceId, location, requestContext).join());
    assertThat(exception.getCause(), IsInstanceOf.instanceOf(HttpException.class));
    HttpException cause = (HttpException) exception.getCause();
    assertEquals(NOT_FOUND, cause.getCode());
    assertEquals(error, cause.getError());
}
Also used : CompletionException(java.util.concurrent.CompletionException) Error(org.folio.rest.jaxrs.model.Error) HttpException(org.folio.rest.core.exceptions.HttpException) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) RequestEntry(org.folio.rest.core.models.RequestEntry) Location(org.folio.rest.jaxrs.model.Location) Test(org.junit.jupiter.api.Test)

Example 15 with HttpException

use of org.folio.rest.core.exceptions.HttpException in project mod-orders by folio-org.

the class ReasonForClosureServiceTest method testUpdateReasonForClosureWithIdMismatchFails.

@Test
void testUpdateReasonForClosureWithIdMismatchFails() {
    ReasonForClosure suffix = new ReasonForClosure().withId(UUID.randomUUID().toString());
    CompletableFuture<Void> result = reasonForClosureService.updateReasonForClosure(UUID.randomUUID().toString(), suffix, requestContext);
    CompletionException expectedException = assertThrows(CompletionException.class, result::join);
    HttpException httpException = (HttpException) expectedException.getCause();
    assertEquals(422, httpException.getCode());
    assertEquals(MISMATCH_BETWEEN_ID_IN_PATH_AND_BODY.toError(), httpException.getError());
}
Also used : ReasonForClosure(org.folio.rest.jaxrs.model.ReasonForClosure) CompletionException(java.util.concurrent.CompletionException) HttpException(org.folio.rest.core.exceptions.HttpException) Test(org.junit.jupiter.api.Test)

Aggregations

HttpException (org.folio.rest.core.exceptions.HttpException)54 CompletionException (java.util.concurrent.CompletionException)26 Test (org.junit.jupiter.api.Test)26 Error (org.folio.rest.jaxrs.model.Error)18 CompositePoLine (org.folio.rest.jaxrs.model.CompositePoLine)16 Parameter (org.folio.rest.jaxrs.model.Parameter)15 ArrayList (java.util.ArrayList)14 CompletableFuture (java.util.concurrent.CompletableFuture)14 RequestContext (org.folio.rest.core.models.RequestContext)14 List (java.util.List)13 RequestEntry (org.folio.rest.core.models.RequestEntry)13 CompositePurchaseOrder (org.folio.rest.jaxrs.model.CompositePurchaseOrder)13 JsonObject (io.vertx.core.json.JsonObject)12 FundDistribution (org.folio.rest.jaxrs.model.FundDistribution)12 Map (java.util.Map)11 CollectionUtils (org.apache.commons.collections4.CollectionUtils)10 LogManager (org.apache.logging.log4j.LogManager)10 Logger (org.apache.logging.log4j.Logger)10 RestClient (org.folio.rest.core.RestClient)10 Location (org.folio.rest.jaxrs.model.Location)10