Search in sources :

Example 1 with BudgetExpenseClassCollection

use of org.folio.rest.acq.model.finance.BudgetExpenseClassCollection 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 2 with BudgetExpenseClassCollection

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

the class MockServer method handleGetBudgetExpenseClass.

private void handleGetBudgetExpenseClass(RoutingContext ctx) {
    logger.info("handleGetBudgetExpenseClass: " + ctx.request().path());
    try {
        BudgetExpenseClassCollection expenseClassCollection = new JsonObject(getMockData(BUDGET_EXPENSE_CLASSES_MOCK_DATA_PATH)).mapTo(BudgetExpenseClassCollection.class);
        JsonObject entries = JsonObject.mapFrom(expenseClassCollection);
        serverResponse(ctx, 200, APPLICATION_JSON, entries.encodePrettily());
    } catch (IOException exception) {
        serverResponse(ctx, 500, APPLICATION_JSON, INTERNAL_SERVER_ERROR.getReasonPhrase());
    }
}
Also used : JsonObject(io.vertx.core.json.JsonObject) IOException(java.io.IOException) BudgetExpenseClassCollection(org.folio.rest.acq.model.finance.BudgetExpenseClassCollection)

Example 3 with BudgetExpenseClassCollection

use of org.folio.rest.acq.model.finance.BudgetExpenseClassCollection in project mod-invoice by folio-org.

the class MockServer method handleGetBudgetExpenseClass.

private void handleGetBudgetExpenseClass(RoutingContext ctx) {
    logger.info("handle BudgetExpenseClasses got: {}?{}", ctx.request().path(), ctx.request().query());
    String tenant = ctx.request().getHeader(OKAPI_HEADER_TENANT);
    String queryParam = StringUtils.trimToEmpty(ctx.request().getParam(QUERY));
    addServerRqQuery(BUDGET_EXPENSE_CLASSES, queryParam);
    if (queryParam.contains(BAD_QUERY)) {
        serverResponse(ctx, 400, APPLICATION_JSON, Response.Status.BAD_REQUEST.getReasonPhrase());
    } else if (queryParam.contains(ID_FOR_INTERNAL_SERVER_ERROR)) {
        serverResponse(ctx, 500, APPLICATION_JSON, Response.Status.INTERNAL_SERVER_ERROR.getReasonPhrase());
    } else {
        // By default return 0
        BudgetExpenseClassCollection budgetExpenseClasses = new BudgetExpenseClassCollection().withTotalRecords(0);
        if (queryParam.contains(EXPENSE_CLASS_ID)) {
            String expenseClassId = queryParam.split(EXPENSE_CLASS_ID + "==")[1];
            Supplier<BudgetExpenseClassCollection> getFromFile = () -> {
                try {
                    return new JsonObject(getMockData(BUDGET_EXPENSE_CLASS_COLLECTION)).mapTo(BudgetExpenseClassCollection.class);
                } catch (IOException e) {
                    return new BudgetExpenseClassCollection().withTotalRecords(0);
                }
            };
            budgetExpenseClasses.withBudgetExpenseClasses(getFromFile.get().getBudgetExpenseClasses().stream().filter(bec -> bec.getExpenseClassId().equals(expenseClassId)).collect(toList()));
            budgetExpenseClasses.withTotalRecords(budgetExpenseClasses.getBudgetExpenseClasses().size());
        }
        JsonObject budgetExpenseClassesJson = JsonObject.mapFrom(budgetExpenseClasses);
        logger.info(budgetExpenseClassesJson.encodePrettily());
        addServerRqRsData(HttpMethod.GET, BUDGET_EXPENSE_CLASSES, budgetExpenseClassesJson);
        serverResponse(ctx, 200, APPLICATION_JSON, budgetExpenseClassesJson.encodePrettily());
    }
}
Also used : BAD_QUERY(org.folio.rest.impl.InvoicesApiTest.BAD_QUERY) HttpServer(io.vertx.core.http.HttpServer) Header(io.restassured.http.Header) HashBasedTable(com.google.common.collect.HashBasedTable) StringUtils(org.apache.commons.lang3.StringUtils) TransactionCollection(org.folio.rest.acq.model.finance.TransactionCollection) CompositePurchaseOrder(org.folio.rest.acq.model.orders.CompositePurchaseOrder) VoucherLine(org.folio.rest.acq.model.VoucherLine) ExpenseClass(org.folio.rest.acq.model.finance.ExpenseClass) Voucher(org.folio.rest.jaxrs.model.Voucher) Matcher(java.util.regex.Matcher) ProfileSnapshotWrapper(org.folio.rest.jaxrs.model.ProfileSnapshotWrapper) VOUCHER_NUMBER_VALUE(org.folio.rest.impl.ApiTestBase.VOUCHER_NUMBER_VALUE) JsonObject(io.vertx.core.json.JsonObject) VOUCHERS_LIST_PATH(org.folio.rest.impl.VouchersApiTest.VOUCHERS_LIST_PATH) EXISTING_VENDOR_INV_NO(org.folio.rest.impl.InvoicesApiTest.EXISTING_VENDOR_INV_NO) ID_FOR_INTERNAL_SERVER_ERROR(org.folio.rest.impl.ApiTestBase.ID_FOR_INTERNAL_SERVER_ERROR) Budget(org.folio.rest.acq.model.finance.Budget) INVOICE_LINES_MOCK_DATA_PATH(org.folio.rest.impl.InvoiceLinesApiTest.INVOICE_LINES_MOCK_DATA_PATH) BatchVoucherExport(org.folio.rest.jaxrs.model.BatchVoucherExport) INVOICE_CONFIG_MODULE_NAME(org.folio.rest.impl.InvoiceHelper.INVOICE_CONFIG_MODULE_NAME) CompositePoLine(org.folio.rest.acq.model.orders.CompositePoLine) Ledger(org.folio.rest.acq.model.finance.Ledger) Fund(org.folio.rest.acq.model.finance.Fund) Logger(org.apache.logging.log4j.Logger) OrderInvoiceRelationshipCollection(org.folio.rest.acq.model.orders.OrderInvoiceRelationshipCollection) ID_FOR_INTERNAL_SERVER_ERROR_PUT(org.folio.rest.impl.ApiTestBase.ID_FOR_INTERNAL_SERVER_ERROR_PUT) INVOICE_MOCK_DATA_PATH(org.folio.rest.impl.InvoicesApiTest.INVOICE_MOCK_DATA_PATH) ApiTestBase.getMockData(org.folio.rest.impl.ApiTestBase.getMockData) CONFIGURATION_ID(org.folio.rest.impl.BatchVoucherExportConfigCredentialsTest.CONFIGURATION_ID) TEXT_PLAIN(javax.ws.rs.core.MediaType.TEXT_PLAIN) Invoice(org.folio.rest.jaxrs.model.Invoice) SYSTEM_CONFIG_MODULE_NAME(org.folio.rest.impl.InvoiceHelper.SYSTEM_CONFIG_MODULE_NAME) FOLIO_INVOICE_NUMBER_VALUE(org.folio.rest.impl.ApiTestBase.FOLIO_INVOICE_NUMBER_VALUE) ALL_UNITS_CQL(org.folio.invoices.utils.HelperUtils.ALL_UNITS_CQL) ExportConfig(org.folio.rest.jaxrs.model.ExportConfig) IS_DELETED_PROP(org.folio.invoices.utils.HelperUtils.IS_DELETED_PROP) BATCH_VOUCHER_EXPORTS_LIST_PATH(org.folio.rest.impl.BatchVoucherExportsApiTest.BATCH_VOUCHER_EXPORTS_LIST_PATH) Transaction(org.folio.rest.acq.model.finance.Transaction) Supplier(java.util.function.Supplier) BudgetCollection(org.folio.rest.acq.model.finance.BudgetCollection) ArrayList(java.util.ArrayList) BATCH_VOUCHER_EXPORTS_MOCK_DATA_PATH(org.folio.rest.impl.BatchVoucherExportsApiTest.BATCH_VOUCHER_EXPORTS_MOCK_DATA_PATH) InvoiceLineCollection(org.folio.rest.jaxrs.model.InvoiceLineCollection) BatchVoucher(org.folio.rest.jaxrs.model.BatchVoucher) ExchangeRate(org.folio.rest.acq.model.finance.ExchangeRate) ExpenseClassCollection(org.folio.rest.acq.model.finance.ExpenseClassCollection) VOUCHER_MOCK_DATA_PATH(org.folio.rest.impl.VouchersApiTest.VOUCHER_MOCK_DATA_PATH) VOUCHER_LINES_MOCK_DATA_PATH(org.folio.rest.impl.VoucherLinesApiTest.VOUCHER_LINES_MOCK_DATA_PATH) VoucherLineCollection(org.folio.rest.acq.model.VoucherLineCollection) INTERNAL_SERVER_ERROR(javax.ws.rs.core.Response.Status.INTERNAL_SERVER_ERROR) InvoiceDocument(org.folio.rest.jaxrs.model.InvoiceDocument) AcquisitionsUnit(org.folio.rest.acq.model.units.AcquisitionsUnit) Vertx(io.vertx.core.Vertx) IOException(java.io.IOException) CompositeFund(org.folio.rest.acq.model.finance.CompositeFund) ExecutionException(java.util.concurrent.ExecutionException) DAYS(java.time.temporal.ChronoUnit.DAYS) BudgetExpenseClassCollection(org.folio.rest.acq.model.finance.BudgetExpenseClassCollection) UnixFakeFileSystem(org.mockftpserver.fake.filesystem.UnixFakeFileSystem) BATCH_VOUCHER_EXPORT_CONFIGS_SAMPLE_PATH(org.folio.rest.impl.BatchVoucherExportConfigTest.BATCH_VOUCHER_EXPORT_CONFIGS_SAMPLE_PATH) AcquisitionsUnitCollection(org.folio.rest.acq.model.units.AcquisitionsUnitCollection) BASE_MOCK_DATA_PATH(org.folio.rest.impl.ApiTestBase.BASE_MOCK_DATA_PATH) BATCH_VOUCHER_EXPORT_CONFIG_CREDENTIALS_SAMPLE_PATH_WITH_ID(org.folio.rest.impl.BatchVoucherExportConfigCredentialsTest.BATCH_VOUCHER_EXPORT_CONFIG_CREDENTIALS_SAMPLE_PATH_WITH_ID) VOUCHER_NUMBER_PREFIX_CONFIG(org.folio.services.voucher.VoucherCommandService.VOUCHER_NUMBER_PREFIX_CONFIG) Date(java.util.Date) TimeoutException(java.util.concurrent.TimeoutException) Router(io.vertx.ext.web.Router) LOCALE_SETTINGS(org.folio.rest.impl.InvoiceHelper.LOCALE_SETTINGS) FakeFtpServer(org.mockftpserver.fake.FakeFtpServer) RoutingContext(io.vertx.ext.web.RoutingContext) BodyHandler(io.vertx.ext.web.handler.BodyHandler) SequenceNumber(org.folio.rest.acq.model.SequenceNumber) APPLICATION_XML(javax.ws.rs.core.MediaType.APPLICATION_XML) FUND_ID_WITH_NOT_ACTIVE_BUDGET(org.folio.rest.impl.InvoicesApiTest.FUND_ID_WITH_NOT_ACTIVE_BUDGET) DocumentCollection(org.folio.rest.jaxrs.model.DocumentCollection) BATCH_VOUCHER_EXPORT_CONFIG_BAD_CREDENTIALS_SAMPLE_PATH_WITH_ID(org.folio.rest.impl.BatchVoucherExportConfigCredentialsTest.BATCH_VOUCHER_EXPORT_CONFIG_BAD_CREDENTIALS_SAMPLE_PATH_WITH_ID) BatchVoucherExportCollection(org.folio.rest.jaxrs.model.BatchVoucherExportCollection) INVOICE_SAMPLE_DOCUMENTS_PATH(org.folio.rest.impl.DocumentsApiTest.INVOICE_SAMPLE_DOCUMENTS_PATH) Assert.fail(org.junit.Assert.fail) APPLICATION_JSON(javax.ws.rs.core.MediaType.APPLICATION_JSON) BATCH_GROUPS_LIST_PATH(org.folio.rest.impl.BatchGroupsApiTest.BATCH_GROUPS_LIST_PATH) EXPENSE_CLASSES_LIST_PATH(org.folio.rest.impl.InvoicesApiTest.EXPENSE_CLASSES_LIST_PATH) InvoiceCollection(org.folio.rest.jaxrs.model.InvoiceCollection) InvoiceLine(org.folio.rest.jaxrs.model.InvoiceLine) INVOICE_LINE_NUMBER_VALUE(org.folio.rest.impl.ApiTestBase.INVOICE_LINE_NUMBER_VALUE) HttpHeaders(io.vertx.core.http.HttpHeaders) PROTECTED_READ_ONLY_TENANT(org.folio.rest.impl.ApiTestBase.PROTECTED_READ_ONLY_TENANT) UUID(java.util.UUID) Instant(java.time.Instant) BatchGroup(org.folio.rest.acq.model.BatchGroup) INVOICE_DOCUMENTS_SAMPLE_PATH(org.folio.rest.impl.DocumentsApiTest.INVOICE_DOCUMENTS_SAMPLE_PATH) List(java.util.List) FiscalYearCollection(org.folio.rest.acq.model.finance.FiscalYearCollection) Response(javax.ws.rs.core.Response) AcquisitionsUnitMembershipCollection(org.folio.rest.acq.model.units.AcquisitionsUnitMembershipCollection) StreamEx(one.util.streamex.StreamEx) Optional(java.util.Optional) Pattern(java.util.regex.Pattern) UserAccount(org.mockftpserver.fake.UserAccount) FileSystem(org.mockftpserver.fake.filesystem.FileSystem) Json(io.vertx.core.json.Json) Credentials(org.folio.rest.jaxrs.model.Credentials) Document(org.folio.rest.jaxrs.model.Document) OKAPI_HEADER_TENANT(org.folio.rest.RestVerticle.OKAPI_HEADER_TENANT) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) FiscalYear(org.folio.rest.acq.model.finance.FiscalYear) VoucherCollection(org.folio.rest.jaxrs.model.VoucherCollection) Function(java.util.function.Function) LedgerCollection(org.folio.rest.acq.model.finance.LedgerCollection) ExportConfigCollection(org.folio.rest.jaxrs.model.ExportConfigCollection) EXISTING_LEDGER_ID(org.folio.rest.impl.InvoicesApiTest.EXISTING_LEDGER_ID) EMPTY(org.apache.commons.lang3.StringUtils.EMPTY) VOUCHER_NUMBER_CONFIG_NAME(org.folio.services.voucher.VoucherCommandService.VOUCHER_NUMBER_CONFIG_NAME) Config(org.folio.rest.jaxrs.model.Config) ACQUISITIONS_UNIT_ID(org.folio.rest.impl.ProtectionHelper.ACQUISITIONS_UNIT_ID) BATCH_GROUP_MOCK_DATA_PATH(org.folio.rest.impl.BatchGroupsApiTest.BATCH_GROUP_MOCK_DATA_PATH) BATCH_VOUCHER_MOCK_DATA_PATH(org.folio.rest.impl.BatchVoucherImplTest.BATCH_VOUCHER_MOCK_DATA_PATH) Configs(org.folio.rest.jaxrs.model.Configs) ID_DOES_NOT_EXIST(org.folio.rest.impl.ApiTestBase.ID_DOES_NOT_EXIST) EXPENSE_CLASSES_MOCK_DATA_PATH(org.folio.rest.impl.InvoicesApiTest.EXPENSE_CLASSES_MOCK_DATA_PATH) Iterator(java.util.Iterator) ResourcePathResolver(org.folio.invoices.utils.ResourcePathResolver) DirectoryEntry(org.mockftpserver.fake.filesystem.DirectoryEntry) BatchGroupCollection(org.folio.rest.acq.model.BatchGroupCollection) INVOICE_ID(org.folio.invoices.utils.HelperUtils.INVOICE_ID) TimeUnit(java.util.concurrent.TimeUnit) Collectors.toList(java.util.stream.Collectors.toList) HttpMethod(io.vertx.core.http.HttpMethod) QUERY_PARAM_START_WITH(org.folio.invoices.utils.HelperUtils.QUERY_PARAM_START_WITH) HttpStatus(org.folio.HttpStatus) FundCollection(org.folio.rest.acq.model.finance.FundCollection) Collections(java.util.Collections) Table(com.google.common.collect.Table) LogManager(org.apache.logging.log4j.LogManager) BATCH_VOUCHER_EXPORT_CONFIG_SAMPLE_PATH(org.folio.rest.impl.BatchVoucherExportConfigTest.BATCH_VOUCHER_EXPORT_CONFIG_SAMPLE_PATH) JsonObject(io.vertx.core.json.JsonObject) Supplier(java.util.function.Supplier) IOException(java.io.IOException) BudgetExpenseClassCollection(org.folio.rest.acq.model.finance.BudgetExpenseClassCollection)

Example 4 with BudgetExpenseClassCollection

use of org.folio.rest.acq.model.finance.BudgetExpenseClassCollection in project mod-invoice by folio-org.

the class BudgetExpenseClassTest method shouldThrowExceptionWithInactiveExpenseClassCodeWhenCheckExpenseClassesWithInactiveExpenseClass.

@Test
void shouldThrowExceptionWithInactiveExpenseClassCodeWhenCheckExpenseClassesWithInactiveExpenseClass() {
    String inactiveExpenseClassId = UUID.randomUUID().toString();
    String activeExpenseClassId = UUID.randomUUID().toString();
    FundDistribution fundDistributionWithInactiveExpenseClass = new FundDistribution().withFundId(UUID.randomUUID().toString()).withExpenseClassId(inactiveExpenseClassId);
    FundDistribution fundDistributionWithActiveExpenseClass = new FundDistribution().withFundId(UUID.randomUUID().toString()).withExpenseClassId(activeExpenseClassId);
    FundDistribution fundDistributionWithoutExpenseClass = new FundDistribution().withFundId(UUID.randomUUID().toString());
    InvoiceLine invoiceLine = new InvoiceLine().withFundDistributions(Arrays.asList(fundDistributionWithInactiveExpenseClass, fundDistributionWithoutExpenseClass));
    List<InvoiceLine> invoiceLines = Collections.singletonList(invoiceLine);
    Adjustment adjustment = new Adjustment().withFundDistributions(Collections.singletonList(fundDistributionWithActiveExpenseClass));
    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());
    BudgetExpenseClass inactive = new BudgetExpenseClass().withBudgetId(UUID.randomUUID().toString()).withExpenseClassId(inactiveExpenseClassId).withStatus(BudgetExpenseClass.Status.INACTIVE).withId(UUID.randomUUID().toString());
    Fund inactiveExpenseClassFund = new Fund().withCode("inactive fund");
    ExpenseClass inactiveExpenseClass = new ExpenseClass().withName("inactive class");
    List<InvoiceWorkflowDataHolder> holders = new ArrayList<>();
    InvoiceWorkflowDataHolder holder1 = new InvoiceWorkflowDataHolder().withInvoice(invoice).withInvoiceLine(invoiceLine).withFundDistribution(fundDistributionWithInactiveExpenseClass).withExpenseClass(inactiveExpenseClass).withBudget(new Budget().withFundId(UUID.randomUUID().toString())).withFund(inactiveExpenseClassFund);
    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(fundDistributionWithActiveExpenseClass).withBudget(new Budget().withFundId(UUID.randomUUID().toString())).withExpenseClass(new ExpenseClass().withId(activeExpenseClassId));
    holders.add(holder1);
    holders.add(holder2);
    holders.add(holder3);
    when(restClient.get(any(RequestEntry.class), any(), any())).thenAnswer(invocation -> {
        RequestEntry requestEntry = invocation.getArgument(0);
        BudgetExpenseClass bec = requestEntry.buildEndpoint().contains(activeExpenseClassId) ? active : inactive;
        return CompletableFuture.completedFuture(new BudgetExpenseClassCollection().withBudgetExpenseClasses(Collections.singletonList(bec)).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(INACTIVE_EXPENSE_CLASS.getCode(), error.getCode());
    String expenseClassNameFromError = error.getParameters().stream().filter(parameter -> parameter.getKey().equals(EXPENSE_CLASS_NAME)).findFirst().get().getValue();
    assertEquals(inactiveExpenseClass.getName(), expenseClassNameFromError);
    String fundCodeFromError = error.getParameters().stream().filter(parameter -> parameter.getKey().equals(FUND_CODE)).findFirst().get().getValue();
    assertEquals(inactiveExpenseClassFund.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)

Aggregations

BudgetExpenseClassCollection (org.folio.rest.acq.model.finance.BudgetExpenseClassCollection)4 Vertx (io.vertx.core.Vertx)3 ArrayList (java.util.ArrayList)3 Collections (java.util.Collections)3 List (java.util.List)3 UUID (java.util.UUID)3 CompletableFuture (java.util.concurrent.CompletableFuture)3 ExecutionException (java.util.concurrent.ExecutionException)3 Budget (org.folio.rest.acq.model.finance.Budget)3 ExpenseClass (org.folio.rest.acq.model.finance.ExpenseClass)3 Fund (org.folio.rest.acq.model.finance.Fund)3 Invoice (org.folio.rest.jaxrs.model.Invoice)3 InvoiceLine (org.folio.rest.jaxrs.model.InvoiceLine)3 JsonObject (io.vertx.core.json.JsonObject)2 IOException (java.io.IOException)2 Arrays (java.util.Arrays)2 HashBasedTable (com.google.common.collect.HashBasedTable)1 Table (com.google.common.collect.Table)1 Header (io.restassured.http.Header)1 HttpHeaders (io.vertx.core.http.HttpHeaders)1