Search in sources :

Example 11 with RequestContext

use of org.folio.rest.core.models.RequestContext in project mod-invoice by folio-org.

the class InvoiceRetrieveServiceTest method positiveGetInvoicesByChunksTest.

@Test
public void positiveGetInvoicesByChunksTest() throws IOException, ExecutionException, InterruptedException {
    InvoiceService invoiceService = new BaseInvoiceService(new RestClient(), invoiceLineService, orderService);
    InvoiceRetrieveService service = new InvoiceRetrieveService(invoiceService);
    JsonObject vouchersList = new JsonObject(getMockData(VOUCHERS_LIST_PATH));
    List<Voucher> vouchers = vouchersList.getJsonArray("vouchers").stream().map(obj -> ((JsonObject) obj).mapTo(Voucher.class)).collect(toList());
    vouchers.remove(1);
    CompletableFuture<List<InvoiceCollection>> future = service.getInvoicesByChunks(vouchers, new RequestContext(context, okapiHeaders));
    List<InvoiceCollection> lineCollections = future.get();
    Assertions.assertEquals(3, lineCollections.get(0).getInvoices().size());
}
Also used : RestClient(org.folio.rest.core.RestClient) BeforeEach(org.junit.jupiter.api.BeforeEach) Autowired(org.springframework.beans.factory.annotation.Autowired) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) VoucherCollection(org.folio.rest.jaxrs.model.VoucherCollection) Context(io.vertx.core.Context) InvoiceService(org.folio.services.invoice.InvoiceService) Voucher(org.folio.rest.jaxrs.model.Voucher) Map(java.util.Map) RequestContext(org.folio.rest.core.models.RequestContext) InvoiceLineService(org.folio.services.invoice.InvoiceLineService) JsonObject(io.vertx.core.json.JsonObject) OKAPI_URL(org.folio.rest.RestConstants.OKAPI_URL) OrderService(org.folio.services.order.OrderService) InvoiceCollection(org.folio.rest.jaxrs.model.InvoiceCollection) Vertx(io.vertx.core.Vertx) IOException(java.io.IOException) ApiTestSuite.mockPort(org.folio.ApiTestSuite.mockPort) ApiTestBase(org.folio.rest.impl.ApiTestBase) ExecutionException(java.util.concurrent.ExecutionException) Test(org.junit.jupiter.api.Test) Collectors.toList(java.util.stream.Collectors.toList) List(java.util.List) BaseInvoiceService(org.folio.services.invoice.BaseInvoiceService) Assertions(org.junit.jupiter.api.Assertions) Invoice(org.folio.rest.jaxrs.model.Invoice) InvoiceService(org.folio.services.invoice.InvoiceService) BaseInvoiceService(org.folio.services.invoice.BaseInvoiceService) BaseInvoiceService(org.folio.services.invoice.BaseInvoiceService) RestClient(org.folio.rest.core.RestClient) JsonObject(io.vertx.core.json.JsonObject) Voucher(org.folio.rest.jaxrs.model.Voucher) InvoiceCollection(org.folio.rest.jaxrs.model.InvoiceCollection) Collectors.toList(java.util.stream.Collectors.toList) List(java.util.List) RequestContext(org.folio.rest.core.models.RequestContext) Test(org.junit.jupiter.api.Test)

Example 12 with RequestContext

use of org.folio.rest.core.models.RequestContext in project mod-invoice by folio-org.

the class BudgetExpenseClassTest method shouldThrowExceptionWithBudgetExpenseClassNotFoundCodeWhenCheckExpenseClassesWithoutExpenseClasses.

@Test
void shouldThrowExceptionWithBudgetExpenseClassNotFoundCodeWhenCheckExpenseClassesWithoutExpenseClasses() {
    String notAssignedExpenseClassId = UUID.randomUUID().toString();
    String activeExpenseClassId = UUID.randomUUID().toString();
    FundDistribution fundDistributionWithActiveExpenseClass = new FundDistribution().withFundId(UUID.randomUUID().toString()).withExpenseClassId(activeExpenseClassId);
    FundDistribution fundDistributionWithNotAssignedExpenseClass = new FundDistribution().withFundId(UUID.randomUUID().toString()).withExpenseClassId(notAssignedExpenseClassId);
    FundDistribution fundDistributionWithoutExpenseClass = new FundDistribution().withFundId(UUID.randomUUID().toString());
    InvoiceLine invoiceLine = new InvoiceLine().withFundDistributions(Arrays.asList(fundDistributionWithActiveExpenseClass, fundDistributionWithoutExpenseClass));
    List<InvoiceLine> invoiceLines = Collections.singletonList(invoiceLine);
    Adjustment adjustment = new Adjustment().withFundDistributions(Collections.singletonList(fundDistributionWithNotAssignedExpenseClass));
    Invoice invoice = new Invoice().withAdjustments(Collections.singletonList(adjustment));
    BudgetExpenseClass active = new BudgetExpenseClass().withBudgetId(UUID.randomUUID().toString()).withExpenseClassId(activeExpenseClassId).withStatus(BudgetExpenseClass.Status.ACTIVE).withId(UUID.randomUUID().toString());
    Fund noExpenseClassFund = new Fund().withCode("no expense class fund");
    ExpenseClass notAssignedExpenseClass = new ExpenseClass().withName("not assigned class");
    List<InvoiceWorkflowDataHolder> holders = new ArrayList<>();
    InvoiceWorkflowDataHolder holder1 = new InvoiceWorkflowDataHolder().withInvoice(invoice).withInvoiceLine(invoiceLine).withFundDistribution(fundDistributionWithActiveExpenseClass).withBudget(new Budget().withFundId(UUID.randomUUID().toString())).withExpenseClass(new ExpenseClass().withId(activeExpenseClassId));
    InvoiceWorkflowDataHolder holder2 = new InvoiceWorkflowDataHolder().withInvoice(invoice).withInvoiceLine(invoiceLine).withBudget(new Budget().withFundId(UUID.randomUUID().toString())).withFundDistribution(fundDistributionWithoutExpenseClass);
    InvoiceWorkflowDataHolder holder3 = new InvoiceWorkflowDataHolder().withInvoice(invoice).withAdjustment(adjustment).withFundDistribution(fundDistributionWithNotAssignedExpenseClass).withExpenseClass(notAssignedExpenseClass).withBudget(new Budget().withFundId(UUID.randomUUID().toString())).withFund(noExpenseClassFund);
    holders.add(holder1);
    holders.add(holder2);
    holders.add(holder3);
    when(restClient.get(any(RequestEntry.class), any(), any())).thenAnswer(invocation -> {
        RequestEntry requestEntry = invocation.getArgument(0);
        List<BudgetExpenseClass> budgetExpenseClasses = requestEntry.buildEndpoint().contains(notAssignedExpenseClassId) ? Collections.emptyList() : Collections.singletonList(active);
        return CompletableFuture.completedFuture(new BudgetExpenseClassCollection().withBudgetExpenseClasses(budgetExpenseClasses).withTotalRecords(1));
    });
    when(requestContext.getContext()).thenReturn(Vertx.vertx().getOrCreateContext());
    CompletableFuture<List<InvoiceWorkflowDataHolder>> future = budgetExpenseClassService.checkExpenseClasses(holders, requestContext);
    ExecutionException executionException = assertThrows(ExecutionException.class, future::get);
    assertThat(executionException.getCause(), instanceOf(HttpException.class));
    HttpException exception = (HttpException) executionException.getCause();
    assertEquals(400, exception.getCode());
    Errors errors = exception.getErrors();
    Error error = errors.getErrors().get(0);
    assertEquals(BUDGET_EXPENSE_CLASS_NOT_FOUND.getCode(), error.getCode());
    String expenseClassNameFromError = error.getParameters().stream().filter(parameter -> parameter.getKey().equals(EXPENSE_CLASS_NAME)).findFirst().get().getValue();
    assertEquals(notAssignedExpenseClass.getName(), expenseClassNameFromError);
    String fundCodeFromError = error.getParameters().stream().filter(parameter -> parameter.getKey().equals(FUND_CODE)).findFirst().get().getValue();
    assertEquals(noExpenseClassFund.getCode(), fundCodeFromError);
}
Also used : ExpenseClass(org.folio.rest.acq.model.finance.ExpenseClass) BudgetExpenseClass(org.folio.rest.acq.model.finance.BudgetExpenseClass) Assertions.assertThrows(org.junit.jupiter.api.Assertions.assertThrows) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) RestClient(org.folio.rest.core.RestClient) BeforeEach(org.junit.jupiter.api.BeforeEach) Arrays(java.util.Arrays) Mock(org.mockito.Mock) CompletableFuture(java.util.concurrent.CompletableFuture) InvoiceWorkflowDataHolder(org.folio.models.InvoiceWorkflowDataHolder) ArrayList(java.util.ArrayList) HttpException(org.folio.invoices.rest.exceptions.HttpException) MockitoAnnotations(org.mockito.MockitoAnnotations) ExpenseClass(org.folio.rest.acq.model.finance.ExpenseClass) FUND_CODE(org.folio.services.finance.budget.BudgetExpenseClassService.FUND_CODE) BudgetExpenseClass(org.folio.rest.acq.model.finance.BudgetExpenseClass) RequestContext(org.folio.rest.core.models.RequestContext) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) Errors(org.folio.rest.jaxrs.model.Errors) Budget(org.folio.rest.acq.model.finance.Budget) InvoiceLine(org.folio.rest.jaxrs.model.InvoiceLine) INACTIVE_EXPENSE_CLASS(org.folio.invoices.utils.ErrorCodes.INACTIVE_EXPENSE_CLASS) BudgetExpenseClassService(org.folio.services.finance.budget.BudgetExpenseClassService) Vertx(io.vertx.core.Vertx) RequestEntry(org.folio.rest.core.models.RequestEntry) Mockito.when(org.mockito.Mockito.when) UUID(java.util.UUID) Fund(org.folio.rest.acq.model.finance.Fund) FundDistribution(org.folio.rest.jaxrs.model.FundDistribution) Matchers.instanceOf(org.hamcrest.Matchers.instanceOf) ExecutionException(java.util.concurrent.ExecutionException) Test(org.junit.jupiter.api.Test) Error(org.folio.rest.jaxrs.model.Error) List(java.util.List) BudgetExpenseClassCollection(org.folio.rest.acq.model.finance.BudgetExpenseClassCollection) EXPENSE_CLASS_NAME(org.folio.services.finance.budget.BudgetExpenseClassService.EXPENSE_CLASS_NAME) Adjustment(org.folio.rest.jaxrs.model.Adjustment) BUDGET_EXPENSE_CLASS_NOT_FOUND(org.folio.invoices.utils.ErrorCodes.BUDGET_EXPENSE_CLASS_NOT_FOUND) Collections(java.util.Collections) Invoice(org.folio.rest.jaxrs.model.Invoice) Invoice(org.folio.rest.jaxrs.model.Invoice) Adjustment(org.folio.rest.jaxrs.model.Adjustment) BudgetExpenseClass(org.folio.rest.acq.model.finance.BudgetExpenseClass) Fund(org.folio.rest.acq.model.finance.Fund) InvoiceLine(org.folio.rest.jaxrs.model.InvoiceLine) ArrayList(java.util.ArrayList) Error(org.folio.rest.jaxrs.model.Error) RequestEntry(org.folio.rest.core.models.RequestEntry) FundDistribution(org.folio.rest.jaxrs.model.FundDistribution) Errors(org.folio.rest.jaxrs.model.Errors) InvoiceWorkflowDataHolder(org.folio.models.InvoiceWorkflowDataHolder) Budget(org.folio.rest.acq.model.finance.Budget) ArrayList(java.util.ArrayList) List(java.util.List) HttpException(org.folio.invoices.rest.exceptions.HttpException) ExecutionException(java.util.concurrent.ExecutionException) BudgetExpenseClassCollection(org.folio.rest.acq.model.finance.BudgetExpenseClassCollection) Test(org.junit.jupiter.api.Test)

Example 13 with RequestContext

use of org.folio.rest.core.models.RequestContext in project mod-invoice by folio-org.

the class InvoiceCancelService method cancelTransactions.

private CompletableFuture<Void> cancelTransactions(String invoiceId, List<Transaction> transactions, RequestContext requestContext) {
    if (transactions.size() == 0)
        return completedFuture(null);
    transactions.forEach(tr -> tr.setInvoiceCancelled(true));
    InvoiceTransactionSummary summary = buildInvoiceTransactionsSummary(invoiceId, transactions);
    return invoiceTransactionSummaryService.updateInvoiceTransactionSummary(summary, requestContext).thenCompose(s -> baseTransactionService.updateTransactions(transactions, requestContext)).exceptionally(t -> {
        logger.error("Failed to cancel transactions for invoice with id {}", invoiceId, t);
        List<Parameter> parameters = Collections.singletonList(new Parameter().withKey("invoiceId").withValue(invoiceId));
        throw new HttpException(500, CANCEL_TRANSACTIONS_ERROR.toError().withParameters(parameters));
    });
}
Also used : EncumbranceService(org.folio.services.finance.transaction.EncumbranceService) TransactionType(org.folio.rest.acq.model.finance.Transaction.TransactionType) CANNOT_CANCEL_INVOICE(org.folio.invoices.utils.ErrorCodes.CANNOT_CANCEL_INVOICE) PurchaseOrder(org.folio.rest.acq.model.orders.PurchaseOrder) CompletableFuture.completedFuture(java.util.concurrent.CompletableFuture.completedFuture) PoLine(org.folio.rest.acq.model.orders.PoLine) OrderLineService(org.folio.services.order.OrderLineService) CompletableFuture(java.util.concurrent.CompletableFuture) Transaction(org.folio.rest.acq.model.finance.Transaction) TransactionCollection(org.folio.rest.acq.model.finance.TransactionCollection) Objects.requireNonNullElse(java.util.Objects.requireNonNullElse) HttpException(org.folio.invoices.rest.exceptions.HttpException) InvoiceTransactionSummaryService(org.folio.services.finance.transaction.InvoiceTransactionSummaryService) VoucherCommandService(org.folio.services.voucher.VoucherCommandService) InvoiceTransactionSummary(org.folio.rest.acq.model.finance.InvoiceTransactionSummary) RequestContext(org.folio.rest.core.models.RequestContext) BaseTransactionService(org.folio.services.finance.transaction.BaseTransactionService) CREDIT(org.folio.rest.acq.model.finance.Transaction.TransactionType.CREDIT) ERROR_UNRELEASING_ENCUMBRANCES(org.folio.invoices.utils.ErrorCodes.ERROR_UNRELEASING_ENCUMBRANCES) RELEASED(org.folio.rest.acq.model.finance.Encumbrance.Status.RELEASED) PENDING_PAYMENT(org.folio.rest.acq.model.finance.Transaction.TransactionType.PENDING_PAYMENT) OrderService(org.folio.services.order.OrderService) InvoiceLine(org.folio.rest.jaxrs.model.InvoiceLine) Collections.emptyList(java.util.Collections.emptyList) HelperUtils.convertIdsToCqlQuery(org.folio.invoices.utils.HelperUtils.convertIdsToCqlQuery) CANCEL_TRANSACTIONS_ERROR(org.folio.invoices.utils.ErrorCodes.CANCEL_TRANSACTIONS_ERROR) Error(org.folio.rest.jaxrs.model.Error) List(java.util.List) Collectors.toList(java.util.stream.Collectors.toList) Logger(org.apache.logging.log4j.Logger) PAYMENT(org.folio.rest.acq.model.finance.Transaction.TransactionType.PAYMENT) Parameter(org.folio.rest.jaxrs.model.Parameter) UNRELEASED(org.folio.rest.acq.model.finance.Encumbrance.Status.UNRELEASED) LogManager(org.apache.logging.log4j.LogManager) Collections(java.util.Collections) Invoice(org.folio.rest.jaxrs.model.Invoice) Parameter(org.folio.rest.jaxrs.model.Parameter) HttpException(org.folio.invoices.rest.exceptions.HttpException) InvoiceTransactionSummary(org.folio.rest.acq.model.finance.InvoiceTransactionSummary)

Example 14 with RequestContext

use of org.folio.rest.core.models.RequestContext in project mod-invoice by folio-org.

the class InvoiceCancelService method unreleaseEncumbrances.

private CompletableFuture<Void> unreleaseEncumbrances(List<InvoiceLine> invoiceLines, RequestContext requestContext) {
    List<String> poLineIds = invoiceLines.stream().filter(InvoiceLine::getReleaseEncumbrance).map(InvoiceLine::getPoLineId).distinct().collect(toList());
    if (poLineIds.isEmpty())
        return completedFuture(null);
    return orderLineService.getPoLines(queryToGetPoLinesWithRightPaymentStatusByIds(poLineIds), requestContext).thenCompose(poLines -> selectPoLinesWithOpenOrders(poLines, requestContext)).thenCompose(poLines -> unreleaseEncumbrancesForPoLines(poLines, requestContext)).exceptionally(t -> {
        Throwable cause = requireNonNullElse(t.getCause(), t);
        List<Parameter> parameters = Collections.singletonList(new Parameter().withKey("cause").withValue(cause.toString()));
        Error error = ERROR_UNRELEASING_ENCUMBRANCES.toError().withParameters(parameters);
        logger.error(error.getMessage(), cause);
        throw new HttpException(500, error);
    });
}
Also used : EncumbranceService(org.folio.services.finance.transaction.EncumbranceService) TransactionType(org.folio.rest.acq.model.finance.Transaction.TransactionType) CANNOT_CANCEL_INVOICE(org.folio.invoices.utils.ErrorCodes.CANNOT_CANCEL_INVOICE) PurchaseOrder(org.folio.rest.acq.model.orders.PurchaseOrder) CompletableFuture.completedFuture(java.util.concurrent.CompletableFuture.completedFuture) PoLine(org.folio.rest.acq.model.orders.PoLine) OrderLineService(org.folio.services.order.OrderLineService) CompletableFuture(java.util.concurrent.CompletableFuture) Transaction(org.folio.rest.acq.model.finance.Transaction) TransactionCollection(org.folio.rest.acq.model.finance.TransactionCollection) Objects.requireNonNullElse(java.util.Objects.requireNonNullElse) HttpException(org.folio.invoices.rest.exceptions.HttpException) InvoiceTransactionSummaryService(org.folio.services.finance.transaction.InvoiceTransactionSummaryService) VoucherCommandService(org.folio.services.voucher.VoucherCommandService) InvoiceTransactionSummary(org.folio.rest.acq.model.finance.InvoiceTransactionSummary) RequestContext(org.folio.rest.core.models.RequestContext) BaseTransactionService(org.folio.services.finance.transaction.BaseTransactionService) CREDIT(org.folio.rest.acq.model.finance.Transaction.TransactionType.CREDIT) ERROR_UNRELEASING_ENCUMBRANCES(org.folio.invoices.utils.ErrorCodes.ERROR_UNRELEASING_ENCUMBRANCES) RELEASED(org.folio.rest.acq.model.finance.Encumbrance.Status.RELEASED) PENDING_PAYMENT(org.folio.rest.acq.model.finance.Transaction.TransactionType.PENDING_PAYMENT) OrderService(org.folio.services.order.OrderService) InvoiceLine(org.folio.rest.jaxrs.model.InvoiceLine) Collections.emptyList(java.util.Collections.emptyList) HelperUtils.convertIdsToCqlQuery(org.folio.invoices.utils.HelperUtils.convertIdsToCqlQuery) CANCEL_TRANSACTIONS_ERROR(org.folio.invoices.utils.ErrorCodes.CANCEL_TRANSACTIONS_ERROR) Error(org.folio.rest.jaxrs.model.Error) List(java.util.List) Collectors.toList(java.util.stream.Collectors.toList) Logger(org.apache.logging.log4j.Logger) PAYMENT(org.folio.rest.acq.model.finance.Transaction.TransactionType.PAYMENT) Parameter(org.folio.rest.jaxrs.model.Parameter) UNRELEASED(org.folio.rest.acq.model.finance.Encumbrance.Status.UNRELEASED) LogManager(org.apache.logging.log4j.LogManager) Collections(java.util.Collections) Invoice(org.folio.rest.jaxrs.model.Invoice) InvoiceLine(org.folio.rest.jaxrs.model.InvoiceLine) Parameter(org.folio.rest.jaxrs.model.Parameter) Error(org.folio.rest.jaxrs.model.Error) HttpException(org.folio.invoices.rest.exceptions.HttpException)

Example 15 with RequestContext

use of org.folio.rest.core.models.RequestContext in project mod-invoice by folio-org.

the class RestClientTest method initMocks.

@BeforeEach
public void initMocks() {
    MockitoAnnotations.openMocks(this);
    okapiHeaders = new HashMap<>();
    okapiHeaders.put(OKAPI_URL, "http://localhost:" + 8081);
    okapiHeaders.put(X_OKAPI_TOKEN.getName(), X_OKAPI_TOKEN.getValue());
    okapiHeaders.put(X_OKAPI_TENANT.getName(), X_OKAPI_TENANT.getValue());
    okapiHeaders.put(X_OKAPI_USER_ID.getName(), X_OKAPI_USER_ID.getValue());
    requestContext = new RequestContext(ctxMock, okapiHeaders);
}
Also used : RequestContext(org.folio.rest.core.models.RequestContext) BeforeEach(org.junit.jupiter.api.BeforeEach)

Aggregations

RequestContext (org.folio.rest.core.models.RequestContext)165 JsonObject (io.vertx.core.json.JsonObject)109 CompletableFuture (java.util.concurrent.CompletableFuture)105 Map (java.util.Map)100 List (java.util.List)92 Logger (org.apache.logging.log4j.Logger)85 LogManager (org.apache.logging.log4j.LogManager)83 Collections (java.util.Collections)73 ArrayList (java.util.ArrayList)72 Context (io.vertx.core.Context)71 CompletableFuture.completedFuture (java.util.concurrent.CompletableFuture.completedFuture)69 Collectors.toList (java.util.stream.Collectors.toList)68 BeforeEach (org.junit.jupiter.api.BeforeEach)63 CompositePoLine (org.folio.rest.jaxrs.model.CompositePoLine)56 Optional (java.util.Optional)53 StringUtils (org.apache.commons.lang3.StringUtils)53 Autowired (org.springframework.beans.factory.annotation.Autowired)51 Piece (org.folio.rest.jaxrs.model.Piece)49 Error (org.folio.rest.jaxrs.model.Error)46 RequestEntry (org.folio.rest.core.models.RequestEntry)45