Search in sources :

Example 1 with ExpenseClass

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

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

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

the class ExpenseClassRetrieveService method getExpenseClassById.

public CompletableFuture<ExpenseClass> getExpenseClassById(String id, RequestContext requestContext) {
    RequestEntry requestEntry = new RequestEntry(EXPENSE_CLASS_BY_ID_ENDPOINT).withId(id);
    return restClient.get(requestEntry, requestContext, ExpenseClass.class).exceptionally(t -> {
        Throwable cause = t.getCause() == null ? t : t.getCause();
        if (HelperUtils.isNotFound(cause)) {
            List<Parameter> parameters = Collections.singletonList(new Parameter().withValue(id).withKey("expenseClass"));
            cause = new HttpException(404, EXPENSE_CLASS_NOT_FOUND.toError().withParameters(parameters));
        }
        throw new CompletionException(cause);
    });
}
Also used : ExpenseClass(org.folio.rest.acq.model.finance.ExpenseClass) CompletionException(java.util.concurrent.CompletionException) Parameter(org.folio.rest.jaxrs.model.Parameter) HttpException(org.folio.invoices.rest.exceptions.HttpException) RequestEntry(org.folio.rest.core.models.RequestEntry)

Example 4 with ExpenseClass

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

the class MockServer method handleExpenseClasses.

private void handleExpenseClasses(RoutingContext ctx) {
    logger.info("handleExpenseClasses got: {}?{}", ctx.request().path(), ctx.request().query());
    String queryParam = StringUtils.trimToEmpty(ctx.request().getParam(QUERY));
    String tenant = ctx.request().getHeader(OKAPI_HEADER_TENANT);
    if (queryParam.contains(BAD_QUERY)) {
        serverResponse(ctx, 400, APPLICATION_JSON, Response.Status.BAD_REQUEST.getReasonPhrase());
    } else if (queryParam.contains(ID_FOR_INTERNAL_SERVER_ERROR) || GET_VOUCHERS_ERROR_TENANT.equals(tenant)) {
        serverResponse(ctx, 500, APPLICATION_JSON, Response.Status.INTERNAL_SERVER_ERROR.getReasonPhrase());
    } else {
        Supplier<List<ExpenseClass>> getFromFile = () -> {
            try {
                return new JsonObject(getMockData(EXPENSE_CLASSES_LIST_PATH)).mapTo(ExpenseClassCollection.class).getExpenseClasses();
            } catch (IOException e) {
                return Collections.emptyList();
            }
        };
        ExpenseClassCollection expenseClassCollection = new ExpenseClassCollection();
        List<ExpenseClass> expenseClasses = getMockEntries(ResourcePathResolver.resourcesPath(EXPENSE_CLASSES_URL), ExpenseClass.class).orElseGet(getFromFile);
        expenseClassCollection.withExpenseClasses(expenseClasses);
        JsonObject expenseClassesJson = JsonObject.mapFrom(expenseClassCollection);
        logger.info(expenseClassesJson.encodePrettily());
        addServerRqRsData(HttpMethod.GET, BATCH_GROUPS, expenseClassesJson);
        serverResponse(ctx, 200, APPLICATION_JSON, expenseClassesJson.encode());
    }
}
Also used : ExpenseClass(org.folio.rest.acq.model.finance.ExpenseClass) JsonObject(io.vertx.core.json.JsonObject) Supplier(java.util.function.Supplier) ArrayList(java.util.ArrayList) List(java.util.List) Collectors.toList(java.util.stream.Collectors.toList) IOException(java.io.IOException) ExpenseClassCollection(org.folio.rest.acq.model.finance.ExpenseClassCollection) BudgetExpenseClassCollection(org.folio.rest.acq.model.finance.BudgetExpenseClassCollection)

Example 5 with ExpenseClass

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

the class MockServer method handleExpenseClassesById.

private void handleExpenseClassesById(RoutingContext ctx) {
    logger.info("handleExpenseClassesById got: GET " + ctx.request().path());
    String id = ctx.request().getParam(AbstractHelper.ID);
    logger.info("id: " + id);
    if (ID_FOR_INTERNAL_SERVER_ERROR.equals(id)) {
        serverResponse(ctx, 500, APPLICATION_JSON, Response.Status.INTERNAL_SERVER_ERROR.getReasonPhrase());
    } else {
        JsonObject expenseClass = getMockEntry(EXPENSE_CLASSES_URL, id).orElseGet(getJsonObjectFromFile(EXPENSE_CLASSES_MOCK_DATA_PATH, id));
        if (expenseClass != null) {
            // validate content against schema
            ExpenseClass expenseClassSchema = expenseClass.mapTo(ExpenseClass.class);
            expenseClassSchema.setId(id);
            expenseClass = JsonObject.mapFrom(expenseClassSchema);
            addServerRqRsData(HttpMethod.GET, EXPENSE_CLASSES_URL, expenseClass);
            serverResponse(ctx, 200, APPLICATION_JSON, expenseClass.encodePrettily());
        } else {
            serverResponse(ctx, 404, APPLICATION_JSON, id);
        }
    }
}
Also used : ExpenseClass(org.folio.rest.acq.model.finance.ExpenseClass) JsonObject(io.vertx.core.json.JsonObject)

Aggregations

ExpenseClass (org.folio.rest.acq.model.finance.ExpenseClass)7 HttpException (org.folio.invoices.rest.exceptions.HttpException)4 ArrayList (java.util.ArrayList)3 List (java.util.List)3 CompletableFuture (java.util.concurrent.CompletableFuture)3 ExecutionException (java.util.concurrent.ExecutionException)3 BudgetExpenseClass (org.folio.rest.acq.model.finance.BudgetExpenseClass)3 BudgetExpenseClassCollection (org.folio.rest.acq.model.finance.BudgetExpenseClassCollection)3 Fund (org.folio.rest.acq.model.finance.Fund)3 RequestEntry (org.folio.rest.core.models.RequestEntry)3 Error (org.folio.rest.jaxrs.model.Error)3 Errors (org.folio.rest.jaxrs.model.Errors)3 Vertx (io.vertx.core.Vertx)2 JsonObject (io.vertx.core.json.JsonObject)2 Arrays (java.util.Arrays)2 Collections (java.util.Collections)2 UUID (java.util.UUID)2 BUDGET_EXPENSE_CLASS_NOT_FOUND (org.folio.invoices.utils.ErrorCodes.BUDGET_EXPENSE_CLASS_NOT_FOUND)2 INACTIVE_EXPENSE_CLASS (org.folio.invoices.utils.ErrorCodes.INACTIVE_EXPENSE_CLASS)2 InvoiceWorkflowDataHolder (org.folio.models.InvoiceWorkflowDataHolder)2