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());
}
}
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);
}
}
}
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);
}
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);
}
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);
}
Aggregations