Search in sources :

Example 16 with Voucher

use of org.folio.rest.jaxrs.model.Voucher in project mod-invoice by folio-org.

the class MockServer method handleGetVouchers.

private void handleGetVouchers(RoutingContext ctx) {
    logger.info("handleGetVoucher got: {}?{}", ctx.request().path(), ctx.request().query());
    String queryParam = StringUtils.trimToEmpty(ctx.request().getParam(QUERY));
    String tenant = ctx.request().getHeader(OKAPI_HEADER_TENANT);
    String invoiceId = EMPTY;
    if (queryParam.contains(INVOICE_ID)) {
        invoiceId = queryParam.split(INVOICE_ID + "==")[1];
    }
    if (EXCEPTION_CASE_BATCH_VOUCHER_EXPORT_GENERATION.equals(queryParam)) {
        VoucherCollection voucherCollection = new VoucherCollection();
        voucherCollection.withVouchers(new ArrayList<>());
        JsonObject vouchersJson = JsonObject.mapFrom(voucherCollection);
        addServerRqRsData(HttpMethod.GET, VOUCHERS_STORAGE, vouchersJson);
        serverResponse(ctx, 200, APPLICATION_JSON, vouchersJson.encode());
        return;
    }
    if (queryParam.contains("batchGroupId") && queryParam.contains("voucherDate")) {
        invoiceId = "c0d08448-347b-418a-8c2f-5fb50248d67e";
    }
    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<Voucher>> getFromFile = () -> {
            try {
                return new JsonObject(getMockData(VOUCHERS_LIST_PATH)).mapTo(VoucherCollection.class).getVouchers();
            } catch (IOException e) {
                return Collections.emptyList();
            }
        };
        VoucherCollection voucherCollection = new VoucherCollection();
        List<Voucher> vouchers = getMockEntries(VOUCHERS_STORAGE, Voucher.class).orElseGet(getFromFile);
        Function<Voucher, String> invoiceIdGetter = Voucher::getInvoiceId;
        voucherCollection.setVouchers(filterEntriesByStringValue(invoiceId, vouchers, invoiceIdGetter));
        voucherCollection.setTotalRecords(voucherCollection.getVouchers().size());
        JsonObject vouchersJson = JsonObject.mapFrom(voucherCollection);
        logger.info(vouchersJson.encodePrettily());
        addServerRqRsData(HttpMethod.GET, VOUCHERS_STORAGE, vouchersJson);
        serverResponse(ctx, 200, APPLICATION_JSON, vouchersJson.encode());
    }
}
Also used : VoucherCollection(org.folio.rest.jaxrs.model.VoucherCollection) Function(java.util.function.Function) 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) Voucher(org.folio.rest.jaxrs.model.Voucher) BatchVoucher(org.folio.rest.jaxrs.model.BatchVoucher)

Example 17 with Voucher

use of org.folio.rest.jaxrs.model.Voucher in project mod-invoice by folio-org.

the class MockServer method handleGetVoucherById.

private void handleGetVoucherById(RoutingContext ctx) {
    logger.info("handleGetVoucherById 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 voucher = getMockEntry(VOUCHERS_STORAGE, id).orElseGet(getJsonObjectFromFile(VOUCHER_MOCK_DATA_PATH, id));
        if (voucher != null) {
            // validate content against schema
            Voucher voucherSchema = voucher.mapTo(Voucher.class);
            voucherSchema.setId(id);
            voucher = JsonObject.mapFrom(voucherSchema);
            addServerRqRsData(HttpMethod.GET, VOUCHERS_STORAGE, voucher);
            serverResponse(ctx, Response.Status.OK.getStatusCode(), APPLICATION_JSON, voucher.encodePrettily());
        } else {
            serverResponse(ctx, Response.Status.NOT_FOUND.getStatusCode(), TEXT_PLAIN, id);
        }
    }
}
Also used : JsonObject(io.vertx.core.json.JsonObject) Voucher(org.folio.rest.jaxrs.model.Voucher) BatchVoucher(org.folio.rest.jaxrs.model.BatchVoucher)

Example 18 with Voucher

use of org.folio.rest.jaxrs.model.Voucher in project mod-invoice by folio-org.

the class InvoicesApiTest method verifySuccessfulPut.

private void verifySuccessfulPut(List<InvoiceLine> invoiceLines, Invoice reqData, String id, String jsonBody, Headers headers) {
    verifyPut(String.format(INVOICE_ID_PATH, id), jsonBody, headers, "", 204);
    Invoice updatedInvoice = serverRqRs.get(INVOICES, HttpMethod.PUT).get(0).mapTo(Invoice.class);
    List<JsonObject> vouchersCreated = serverRqRs.get(VOUCHERS_STORAGE, HttpMethod.POST);
    assertThat(vouchersCreated, notNullValue());
    assertThat(vouchersCreated, hasSize(1));
    Voucher voucherCreated = vouchersCreated.get(0).mapTo(Voucher.class);
    assertThat(voucherCreated.getSystemCurrency(), equalTo(DEFAULT_SYSTEM_CURRENCY));
    assertThat(voucherCreated.getVoucherNumber(), equalTo(VOUCHER_NUMBER_VALUE));
    List<JsonObject> fundsSearches = serverRqRs.get(FUNDS, HttpMethod.GET);
    List<Fund> funds = fundsSearches.get(0).mapTo(FundCollection.class).getFunds();
    verifyTransitionToApproved(voucherCreated, invoiceLines, updatedInvoice, getExpectedVoucherLinesQuantity(funds));
    checkVoucherAcqUnitIdsList(voucherCreated, reqData);
}
Also used : Invoice(org.folio.rest.jaxrs.model.Invoice) Fund(org.folio.rest.acq.model.finance.Fund) JsonObject(io.vertx.core.json.JsonObject) FundCollection(org.folio.rest.acq.model.finance.FundCollection) Voucher(org.folio.rest.jaxrs.model.Voucher)

Example 19 with Voucher

use of org.folio.rest.jaxrs.model.Voucher in project mod-invoice by folio-org.

the class InvoicesApiTest method testTransitionFromOpenToApprovedWithMixedTypesFundDistributionsAndWithLockTotalWhichEqualToDecimalTotal.

@Test
void testTransitionFromOpenToApprovedWithMixedTypesFundDistributionsAndWithLockTotalWhichEqualToDecimalTotal() {
    Invoice reqData = getMockAsJson(OPEN_INVOICE_SAMPLE_PATH).mapTo(Invoice.class);
    String id = reqData.getId();
    InvoiceLine invoiceLine = getMinimalContentInvoiceLine(id);
    invoiceLine.setSubTotal(100.18d);
    invoiceLine.setId(UUID.randomUUID().toString());
    FundDistribution percentageDistribution = new FundDistribution().withFundId(EXISTING_FUND_ID).withCode(null).withDistributionType(FundDistribution.DistributionType.PERCENTAGE).withValue(50d);
    FundDistribution amountDistribution = new FundDistribution().withFundId(EXISTING_FUND_ID).withDistributionType(AMOUNT).withValue(50.09d);
    invoiceLine.getFundDistributions().addAll(Arrays.asList(percentageDistribution, amountDistribution));
    addMockEntry(INVOICE_LINES, JsonObject.mapFrom(invoiceLine));
    reqData.setStatus(Invoice.Status.APPROVED);
    reqData.setLockTotal(100.18d);
    String jsonBody = JsonObject.mapFrom(reqData).encode();
    Headers headers = prepareHeaders(X_OKAPI_URL, X_OKAPI_TENANT, X_OKAPI_TOKEN, X_OKAPI_USER_ID);
    verifyPut(String.format(INVOICE_ID_PATH, id), jsonBody, headers, "", 204);
    // Verify that expected number of external calls made
    assertThat(getInvoiceRetrievals(), hasSize(1));
    assertThat(getInvoiceLineSearches(), hasSize(1));
    Invoice updatedInvoice = serverRqRs.get(INVOICES, HttpMethod.PUT).get(0).mapTo(Invoice.class);
    List<JsonObject> vouchersCreated = serverRqRs.get(VOUCHERS_STORAGE, HttpMethod.POST);
    assertThat(vouchersCreated, notNullValue());
    assertThat(vouchersCreated, hasSize(1));
    Voucher voucherCreated = vouchersCreated.get(0).mapTo(Voucher.class);
    assertThat(voucherCreated.getVoucherNumber(), equalTo(TEST_PREFIX + VOUCHER_NUMBER_VALUE));
    assertThat(voucherCreated.getSystemCurrency(), equalTo(DEFAULT_SYSTEM_CURRENCY));
    List<JsonObject> fundsSearches = serverRqRs.get(FUNDS, HttpMethod.GET);
    List<Fund> funds = fundsSearches.get(0).mapTo(FundCollection.class).getFunds();
    verifyTransitionToApproved(voucherCreated, Collections.singletonList(invoiceLine), updatedInvoice, getExpectedVoucherLinesQuantity(funds));
    checkVoucherAcqUnitIdsList(voucherCreated, reqData);
}
Also used : FundDistribution(org.folio.rest.jaxrs.model.FundDistribution) Invoice(org.folio.rest.jaxrs.model.Invoice) Fund(org.folio.rest.acq.model.finance.Fund) InvoiceLine(org.folio.rest.jaxrs.model.InvoiceLine) Headers(io.restassured.http.Headers) JsonObject(io.vertx.core.json.JsonObject) FundCollection(org.folio.rest.acq.model.finance.FundCollection) Matchers.containsString(org.hamcrest.Matchers.containsString) Voucher(org.folio.rest.jaxrs.model.Voucher) Test(org.junit.jupiter.api.Test)

Example 20 with Voucher

use of org.folio.rest.jaxrs.model.Voucher in project mod-invoice by folio-org.

the class InvoicesApiTest method testTransitionFromOpenToApprovedWithAmountTypeFundDistributions.

@Test
void testTransitionFromOpenToApprovedWithAmountTypeFundDistributions() {
    logger.info("=== Test transition invoice to Approved ===");
    List<InvoiceLine> invoiceLines = getMockAsJson(INVOICE_LINES_LIST_PATH).mapTo(InvoiceLineCollection.class).getInvoiceLines();
    Invoice reqData = getMockAsJson(OPEN_INVOICE_SAMPLE_PATH).mapTo(Invoice.class);
    String id = reqData.getId();
    invoiceLines.forEach(invoiceLine -> {
        invoiceLine.setId(UUID.randomUUID().toString());
        invoiceLine.setInvoiceId(id);
        // prepare fund distributions
        BigDecimal total = BigDecimal.valueOf(invoiceLine.getSubTotal()).add(BigDecimal.valueOf(invoiceLine.getAdjustmentsTotal()));
        double fundDistrValue = total.divide(BigDecimal.valueOf(invoiceLine.getFundDistributions().size()), 2, RoundingMode.HALF_EVEN).doubleValue();
        invoiceLine.getFundDistributions().forEach(fundDistribution -> fundDistribution.withDistributionType(AMOUNT).withCode(null).setValue(fundDistrValue));
        var invoiceLineSum = invoiceLine.getFundDistributions().stream().map(FundDistribution::getValue).map(BigDecimal::valueOf).reduce(BigDecimal::add).get();
        var reminder = total.subtract(invoiceLineSum);
        var fd1Value = BigDecimal.valueOf(invoiceLine.getFundDistributions().get(0).getValue());
        invoiceLine.getFundDistributions().get(0).setValue(fd1Value.add(reminder).doubleValue());
        addMockEntry(INVOICE_LINES, JsonObject.mapFrom(invoiceLine));
    });
    reqData.setStatus(Invoice.Status.APPROVED);
    String jsonBody = JsonObject.mapFrom(reqData).encode();
    Headers headers = prepareHeaders(X_OKAPI_URL, X_OKAPI_TENANT, X_OKAPI_TOKEN, X_OKAPI_USER_ID);
    verifyPut(String.format(INVOICE_ID_PATH, id), jsonBody, headers, "", 204);
    // Verify that expected number of external calls made
    assertThat(getInvoiceRetrievals(), hasSize(1));
    assertThat(getInvoiceLineSearches(), hasSize(1));
    Invoice updatedInvoice = serverRqRs.get(INVOICES, HttpMethod.PUT).get(0).mapTo(Invoice.class);
    List<JsonObject> vouchersCreated = serverRqRs.get(VOUCHERS_STORAGE, HttpMethod.POST);
    assertThat(vouchersCreated, notNullValue());
    assertThat(vouchersCreated, hasSize(1));
    Voucher voucherCreated = vouchersCreated.get(0).mapTo(Voucher.class);
    assertThat(voucherCreated.getVoucherNumber(), equalTo(TEST_PREFIX + VOUCHER_NUMBER_VALUE));
    assertThat(voucherCreated.getSystemCurrency(), equalTo(DEFAULT_SYSTEM_CURRENCY));
    verifyTransitionToApproved(voucherCreated, invoiceLines, updatedInvoice, 5);
    verifyVoucherLineWithExpenseClasses(2L);
    checkVoucherAcqUnitIdsList(voucherCreated, reqData);
}
Also used : Invoice(org.folio.rest.jaxrs.model.Invoice) InvoiceLine(org.folio.rest.jaxrs.model.InvoiceLine) Headers(io.restassured.http.Headers) JsonObject(io.vertx.core.json.JsonObject) InvoiceLineCollection(org.folio.rest.jaxrs.model.InvoiceLineCollection) Matchers.containsString(org.hamcrest.Matchers.containsString) BigDecimal(java.math.BigDecimal) Voucher(org.folio.rest.jaxrs.model.Voucher) Test(org.junit.jupiter.api.Test)

Aggregations

Voucher (org.folio.rest.jaxrs.model.Voucher)28 JsonObject (io.vertx.core.json.JsonObject)24 Test (org.junit.jupiter.api.Test)18 Invoice (org.folio.rest.jaxrs.model.Invoice)17 InvoiceLine (org.folio.rest.jaxrs.model.InvoiceLine)15 Vertx (io.vertx.core.Vertx)13 List (java.util.List)12 Map (java.util.Map)12 Collectors.toList (java.util.stream.Collectors.toList)12 RequestContext (org.folio.rest.core.models.RequestContext)11 Context (io.vertx.core.Context)10 IOException (java.io.IOException)10 HashMap (java.util.HashMap)10 VoucherCollection (org.folio.rest.jaxrs.model.VoucherCollection)10 Matchers.containsString (org.hamcrest.Matchers.containsString)10 Headers (io.restassured.http.Headers)9 CompletableFuture (java.util.concurrent.CompletableFuture)9 Fund (org.folio.rest.acq.model.finance.Fund)9 FundDistribution (org.folio.rest.jaxrs.model.FundDistribution)9 FundCollection (org.folio.rest.acq.model.finance.FundCollection)7