use of org.folio.invoices.utils.ResourcePathResolver.VOUCHERS_STORAGE in project mod-invoice by folio-org.
the class InvoicesApiTest method testInvoiceTransitionApprovedWithOddNumberOfPennies.
@Test
void testInvoiceTransitionApprovedWithOddNumberOfPennies() {
logger.info("=== Test transition invoice to Approved with odd number of pennies ===");
InvoiceLine invoiceLine = getMockAsJson(INVOICE_LINES_LIST_PATH).mapTo(InvoiceLineCollection.class).getInvoiceLines().get(0);
Invoice invoice = getMockAsJson(OPEN_INVOICE_SAMPLE_PATH).mapTo(Invoice.class);
invoice.getAdjustments().clear();
invoice.getAdjustments().add(createAdjustment(Prorate.BY_LINE, Type.AMOUNT, 4d));
String id = invoice.getId();
invoiceLine.setId(UUID.randomUUID().toString());
invoiceLine.setInvoiceId(id);
var conversionFactor = 3d;
invoice.setExchangeRate(conversionFactor);
List<FundDistribution> fundDistrList = new ArrayList<>();
invoiceLine.setSubTotal(20.01d);
invoiceLine.getAdjustments().clear();
fundDistrList.add(new FundDistribution().withFundId("1d1574f1-9196-4a57-8d1f-3b2e4309eb81").withDistributionType(PERCENTAGE).withValue(50d));
fundDistrList.add(new FundDistribution().withFundId("55f48dc6-efa7-4cfe-bc7c-4786efe493e3").withDistributionType(PERCENTAGE).withValue(50d));
invoiceLine.setFundDistributions(fundDistrList);
addMockEntry(INVOICE_LINES, JsonObject.mapFrom(invoiceLine));
invoice.setStatus(Invoice.Status.APPROVED);
String jsonBody = JsonObject.mapFrom(invoice).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, Collections.singletonList(invoiceLine), updatedInvoice, 2);
checkVoucherAcqUnitIdsList(voucherCreated, invoice);
List<Transaction> pendingPayments = serverRqRs.get(FINANCE_PENDING_PAYMENTS, HttpMethod.POST).stream().map(transaction -> transaction.mapTo(Transaction.class)).collect(toList());
Double transactionsAmount = pendingPayments.stream().map(tr -> Money.of(tr.getAmount(), tr.getCurrency())).reduce(Money::add).get().getNumber().doubleValue();
InvoiceLine invLineWithRecalculatedTotals = serverRqRs.get(INVOICE_LINES, HttpMethod.PUT).get(0).mapTo(InvoiceLine.class);
assertEquals(invLineWithRecalculatedTotals.getTotal(), transactionsAmount);
// check voucher totals
Voucher voucher = serverRqRs.get(VOUCHERS_STORAGE, HttpMethod.POST).stream().map(transaction -> transaction.mapTo(Voucher.class)).collect(toList()).get(0);
List<VoucherLine> voucherLines = serverRqRs.get(VOUCHER_LINES, HttpMethod.POST).stream().map(transaction -> transaction.mapTo(VoucherLine.class)).collect(toList());
Double voucherLinesTotal = voucherLines.stream().map(vLine -> BigDecimal.valueOf(vLine.getAmount())).reduce(BigDecimal::add).get().doubleValue();
var expectedVoucherAmount = Money.of(invLineWithRecalculatedTotals.getTotal(), invoice.getCurrency()).multiply(conversionFactor).getNumber().doubleValue();
assertEquals(expectedVoucherAmount, voucher.getAmount());
assertEquals(expectedVoucherAmount, voucherLinesTotal);
}
Aggregations