Search in sources :

Example 1 with HttpException

use of org.folio.invoices.rest.exceptions.HttpException in project mod-invoice by folio-org.

the class InvoiceHelper method verifyTransitionOnPaidStatus.

private void verifyTransitionOnPaidStatus(Invoice invoiceFromStorage, Invoice invoice) {
    // Once an invoice is Paid, it should no longer transition to other statuses, except Cancelled.
    if (invoiceFromStorage.getStatus() == Invoice.Status.PAID && invoice.getStatus() != Invoice.Status.CANCELLED && invoice.getStatus() != invoiceFromStorage.getStatus()) {
        List<Parameter> parameters = Collections.singletonList(new Parameter().withKey("invoiceId").withValue(invoice.getId()));
        Error error = INVALID_INVOICE_TRANSITION_ON_PAID_STATUS.toError().withParameters(parameters);
        throw new HttpException(422, error);
    }
}
Also used : Parameter(org.folio.rest.jaxrs.model.Parameter) Error(org.folio.rest.jaxrs.model.Error) HttpException(org.folio.invoices.rest.exceptions.HttpException)

Example 2 with HttpException

use of org.folio.invoices.rest.exceptions.HttpException in project mod-invoice by folio-org.

the class InvoicesImpl method processDocumentCreation.

private void processDocumentCreation(String id, String lang, Map<String, String> okapiHeaders, Handler<AsyncResult<Response>> asyncResultHandler, Context vertxContext) {
    DocumentHelper documentHelper = new DocumentHelper(okapiHeaders, vertxContext, lang);
    InvoiceDocument entity = new JsonObject(new String(requestBytesArray, StandardCharsets.UTF_8)).mapTo(InvoiceDocument.class);
    if (!entity.getDocumentMetadata().getInvoiceId().equals(id)) {
        asyncResultHandler.handle(succeededFuture(documentHelper.buildErrorResponse(new HttpException(422, MISMATCH_BETWEEN_ID_IN_PATH_AND_BODY))));
    } else {
        documentHelper.createDocument(id, entity).thenAccept(document -> {
            logInfo("Successfully created document with id={}", document);
            asyncResultHandler.handle(succeededFuture(documentHelper.buildResponseWithLocation(String.format(DOCUMENTS_LOCATION_PREFIX, id, document.getDocumentMetadata().getId()), document)));
        }).exceptionally(t -> handleErrorResponse(asyncResultHandler, documentHelper, t));
    }
}
Also used : Future.succeededFuture(io.vertx.core.Future.succeededFuture) BufferedInputStream(java.io.BufferedInputStream) ArrayUtils(org.apache.commons.lang3.ArrayUtils) Context(io.vertx.core.Context) StringUtils(org.apache.commons.lang3.StringUtils) HttpException(org.folio.invoices.rest.exceptions.HttpException) STREAM_ABORT(org.folio.rest.RestVerticle.STREAM_ABORT) RequestContext(org.folio.rest.core.models.RequestContext) Map(java.util.Map) JsonObject(io.vertx.core.json.JsonObject) AsyncResult(io.vertx.core.AsyncResult) STREAM_COMPLETE(org.folio.rest.RestVerticle.STREAM_COMPLETE) Errors(org.folio.rest.jaxrs.model.Errors) DOCUMENT_IS_TOO_LARGE(org.folio.invoices.utils.ErrorCodes.DOCUMENT_IS_TOO_LARGE) InvoiceLine(org.folio.rest.jaxrs.model.InvoiceLine) InvoiceDocument(org.folio.rest.jaxrs.model.InvoiceDocument) IOException(java.io.IOException) Validate(org.folio.rest.annotations.Validate) StandardCharsets(java.nio.charset.StandardCharsets) ONE_MB(org.apache.commons.io.FileUtils.ONE_MB) Objects(java.util.Objects) IOUtils(org.apache.commons.io.IOUtils) Stream(org.folio.rest.annotations.Stream) MISMATCH_BETWEEN_ID_IN_PATH_AND_BODY(org.folio.invoices.utils.ErrorCodes.MISMATCH_BETWEEN_ID_IN_PATH_AND_BODY) Logger(org.apache.logging.log4j.Logger) Response(javax.ws.rs.core.Response) Handler(io.vertx.core.Handler) LogManager(org.apache.logging.log4j.LogManager) Collections(java.util.Collections) Invoice(org.folio.rest.jaxrs.model.Invoice) InputStream(java.io.InputStream) JsonObject(io.vertx.core.json.JsonObject) HttpException(org.folio.invoices.rest.exceptions.HttpException) InvoiceDocument(org.folio.rest.jaxrs.model.InvoiceDocument)

Example 3 with HttpException

use of org.folio.invoices.rest.exceptions.HttpException in project mod-invoice by folio-org.

the class ExpenseClassRetrieveServiceTest method getExpenseClassByIdShouldReturnExpenseClassNotFoundWhenGetRestClientReturn404.

@Test
void getExpenseClassByIdShouldReturnExpenseClassNotFoundWhenGetRestClientReturn404() {
    CompletableFuture<ExpenseClass> future = new CompletableFuture<>();
    future.completeExceptionally(new HttpException(404, "Not found"));
    when(restClient.get(any(), any(), eq(ExpenseClass.class))).thenReturn(future);
    String expenseClassId = UUID.randomUUID().toString();
    CompletableFuture<ExpenseClass> resultFuture = expenseClassRetrieveService.getExpenseClassById(expenseClassId, requestContext);
    ExecutionException executionException = assertThrows(ExecutionException.class, resultFuture::get);
    assertThat(executionException.getCause(), instanceOf(HttpException.class));
    HttpException exception = (HttpException) executionException.getCause();
    assertEquals(404, exception.getCode());
    Errors errors = exception.getErrors();
    Error error = errors.getErrors().get(0);
    assertEquals(EXPENSE_CLASS_NOT_FOUND.getCode(), error.getCode());
    assertEquals(expenseClassId, error.getParameters().get(0).getValue());
}
Also used : ExpenseClass(org.folio.rest.acq.model.finance.ExpenseClass) Errors(org.folio.rest.jaxrs.model.Errors) CompletableFuture(java.util.concurrent.CompletableFuture) Error(org.folio.rest.jaxrs.model.Error) HttpException(org.folio.invoices.rest.exceptions.HttpException) ExecutionException(java.util.concurrent.ExecutionException) Test(org.junit.jupiter.api.Test)

Example 4 with HttpException

use of org.folio.invoices.rest.exceptions.HttpException in project mod-invoice by folio-org.

the class FundAvailabilityHolderValidatorTest method checkEnoughMoneyInBudgetShouldThrowFundCannotBePaidIfTransactionsAmountDifferenceGreaterThanBudgetRemainingAmount.

@Test
void checkEnoughMoneyInBudgetShouldThrowFundCannotBePaidIfTransactionsAmountDifferenceGreaterThanBudgetRemainingAmount() {
    String fiscalYearId = UUID.randomUUID().toString();
    String fundId = UUID.randomUUID().toString();
    String budgetId = UUID.randomUUID().toString();
    String ledgerId = UUID.randomUUID().toString();
    Transaction existingTransaction = new Transaction().withTransactionType(Transaction.TransactionType.PENDING_PAYMENT).withAmount(50d).withFiscalYearId(fiscalYearId).withFromFundId(fundId).withCurrency("USD");
    Transaction newTransaction = new Transaction().withTransactionType(Transaction.TransactionType.PENDING_PAYMENT).withAmount(60d).withFiscalYearId(fiscalYearId).withFromFundId(fundId).withCurrency("USD");
    Fund fund = new Fund().withId(fundId).withName("TestFund").withLedgerId(ledgerId).withCode("FC").withFundStatus(Fund.FundStatus.ACTIVE);
    Budget budget = new Budget().withId(budgetId).withFiscalYearId(fiscalYearId).withFundId(fundId).withAllocated(59d).withTotalFunding(59d).withAvailable(9d).withUnavailable(50d).withAwaitingPayment(50D).withAllowableExpenditure(100d);
    List<InvoiceWorkflowDataHolder> holders = new ArrayList<>();
    InvoiceWorkflowDataHolder holder = new InvoiceWorkflowDataHolder().withFund(fund).withExistingTransaction(existingTransaction).withNewTransaction(newTransaction).withBudget(budget).withRestrictExpenditures(true).withFiscalYear(new FiscalYear().withId(fiscalYearId).withCurrency("USD"));
    holders.add(holder);
    HttpException httpException = assertThrows(HttpException.class, () -> fundAvailabilityValidator.validate(holders));
    assertEquals(422, httpException.getCode());
    Error error = httpException.getErrors().getErrors().get(0);
    assertEquals(FUND_CANNOT_BE_PAID.getCode(), error.getCode());
    assertEquals(Collections.singletonList("FC").toString(), error.getParameters().get(0).getValue());
}
Also used : FiscalYear(org.folio.rest.acq.model.finance.FiscalYear) Transaction(org.folio.rest.acq.model.finance.Transaction) Fund(org.folio.rest.acq.model.finance.Fund) InvoiceWorkflowDataHolder(org.folio.models.InvoiceWorkflowDataHolder) ArrayList(java.util.ArrayList) Error(org.folio.rest.jaxrs.model.Error) Budget(org.folio.rest.acq.model.finance.Budget) HttpException(org.folio.invoices.rest.exceptions.HttpException) Test(org.junit.jupiter.api.Test)

Example 5 with HttpException

use of org.folio.invoices.rest.exceptions.HttpException in project mod-invoice by folio-org.

the class FundAvailabilityHolderValidatorTest method shouldPassValidationWhenBudgetRestrictedAndFinalExpendedValueGreaterThenMaxBudgetExpended.

@Test
void shouldPassValidationWhenBudgetRestrictedAndFinalExpendedValueGreaterThenMaxBudgetExpended() {
    String fiscalYearId = UUID.randomUUID().toString();
    String fundId = UUID.randomUUID().toString();
    String budgetId = UUID.randomUUID().toString();
    String ledgerId = UUID.randomUUID().toString();
    FiscalYear fiscalYear = new FiscalYear().withCurrency("USD").withId(fiscalYearId);
    Fund fund = new Fund().withId(fundId).withName("TestFund").withLedgerId(ledgerId).withCode("FC").withFundStatus(Fund.FundStatus.ACTIVE);
    Budget budget = new Budget().withId(budgetId).withFiscalYearId(fiscalYearId).withFundId(fundId).withAllocated(260d).withTotalFunding(260d).withAvailable(0d).withUnavailable(290d).withEncumbered(250d).withAwaitingPayment(30d).withExpenditures(10d).withAllowableExpenditure(100d).withAllowableExpenditure(110d);
    List<InvoiceWorkflowDataHolder> holders = new ArrayList<>();
    Transaction encumbrance = new Transaction().withId(UUID.randomUUID().toString()).withAmount(250d).withCurrency("USD");
    Transaction linePendingPayment = new Transaction().withAmount(245d).withAwaitingPayment(new AwaitingPayment().withEncumbranceId(encumbrance.getId()).withReleaseEncumbrance(false)).withCurrency("USD");
    InvoiceWorkflowDataHolder holder = new InvoiceWorkflowDataHolder().withFund(fund).withBudget(budget).withRestrictExpenditures(true).withFiscalYear(fiscalYear).withNewTransaction(linePendingPayment).withEncumbrance(encumbrance);
    holders.add(holder);
    HttpException httpException = assertThrows(HttpException.class, () -> fundAvailabilityValidator.validate(holders));
    assertEquals(422, httpException.getCode());
    Error error = httpException.getErrors().getErrors().get(0);
    assertEquals(FUND_CANNOT_BE_PAID.getCode(), error.getCode());
    assertEquals(Collections.singletonList("FC").toString(), error.getParameters().get(0).getValue());
}
Also used : FiscalYear(org.folio.rest.acq.model.finance.FiscalYear) Fund(org.folio.rest.acq.model.finance.Fund) Transaction(org.folio.rest.acq.model.finance.Transaction) InvoiceWorkflowDataHolder(org.folio.models.InvoiceWorkflowDataHolder) ArrayList(java.util.ArrayList) Error(org.folio.rest.jaxrs.model.Error) Budget(org.folio.rest.acq.model.finance.Budget) HttpException(org.folio.invoices.rest.exceptions.HttpException) AwaitingPayment(org.folio.rest.acq.model.finance.AwaitingPayment) Test(org.junit.jupiter.api.Test)

Aggregations

HttpException (org.folio.invoices.rest.exceptions.HttpException)31 Error (org.folio.rest.jaxrs.model.Error)16 CompletableFuture (java.util.concurrent.CompletableFuture)14 Test (org.junit.jupiter.api.Test)13 Invoice (org.folio.rest.jaxrs.model.Invoice)12 InvoiceLine (org.folio.rest.jaxrs.model.InvoiceLine)12 Parameter (org.folio.rest.jaxrs.model.Parameter)11 ArrayList (java.util.ArrayList)10 List (java.util.List)9 RequestContext (org.folio.rest.core.models.RequestContext)9 Errors (org.folio.rest.jaxrs.model.Errors)9 LogManager (org.apache.logging.log4j.LogManager)8 Logger (org.apache.logging.log4j.Logger)8 InvoiceWorkflowDataHolder (org.folio.models.InvoiceWorkflowDataHolder)8 Transaction (org.folio.rest.acq.model.finance.Transaction)8 Collections (java.util.Collections)7 Collectors.toList (java.util.stream.Collectors.toList)7 Fund (org.folio.rest.acq.model.finance.Fund)7 JsonObject (io.vertx.core.json.JsonObject)6 Map (java.util.Map)6