use of org.folio.rest.core.models.RequestContext in project mod-invoice by folio-org.
the class InvoiceRetrieveServiceTest method positiveGetInvoicesByChunksTest.
@Test
public void positiveGetInvoicesByChunksTest() throws IOException, ExecutionException, InterruptedException {
InvoiceService invoiceService = new BaseInvoiceService(new RestClient(), invoiceLineService, orderService);
InvoiceRetrieveService service = new InvoiceRetrieveService(invoiceService);
JsonObject vouchersList = new JsonObject(getMockData(VOUCHERS_LIST_PATH));
List<Voucher> vouchers = vouchersList.getJsonArray("vouchers").stream().map(obj -> ((JsonObject) obj).mapTo(Voucher.class)).collect(toList());
vouchers.remove(1);
CompletableFuture<List<InvoiceCollection>> future = service.getInvoicesByChunks(vouchers, new RequestContext(context, okapiHeaders));
List<InvoiceCollection> lineCollections = future.get();
Assertions.assertEquals(3, lineCollections.get(0).getInvoices().size());
}
use of org.folio.rest.core.models.RequestContext 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);
}
use of org.folio.rest.core.models.RequestContext in project mod-invoice by folio-org.
the class InvoiceCancelService method cancelTransactions.
private CompletableFuture<Void> cancelTransactions(String invoiceId, List<Transaction> transactions, RequestContext requestContext) {
if (transactions.size() == 0)
return completedFuture(null);
transactions.forEach(tr -> tr.setInvoiceCancelled(true));
InvoiceTransactionSummary summary = buildInvoiceTransactionsSummary(invoiceId, transactions);
return invoiceTransactionSummaryService.updateInvoiceTransactionSummary(summary, requestContext).thenCompose(s -> baseTransactionService.updateTransactions(transactions, requestContext)).exceptionally(t -> {
logger.error("Failed to cancel transactions for invoice with id {}", invoiceId, t);
List<Parameter> parameters = Collections.singletonList(new Parameter().withKey("invoiceId").withValue(invoiceId));
throw new HttpException(500, CANCEL_TRANSACTIONS_ERROR.toError().withParameters(parameters));
});
}
use of org.folio.rest.core.models.RequestContext in project mod-invoice by folio-org.
the class InvoiceCancelService method unreleaseEncumbrances.
private CompletableFuture<Void> unreleaseEncumbrances(List<InvoiceLine> invoiceLines, RequestContext requestContext) {
List<String> poLineIds = invoiceLines.stream().filter(InvoiceLine::getReleaseEncumbrance).map(InvoiceLine::getPoLineId).distinct().collect(toList());
if (poLineIds.isEmpty())
return completedFuture(null);
return orderLineService.getPoLines(queryToGetPoLinesWithRightPaymentStatusByIds(poLineIds), requestContext).thenCompose(poLines -> selectPoLinesWithOpenOrders(poLines, requestContext)).thenCompose(poLines -> unreleaseEncumbrancesForPoLines(poLines, requestContext)).exceptionally(t -> {
Throwable cause = requireNonNullElse(t.getCause(), t);
List<Parameter> parameters = Collections.singletonList(new Parameter().withKey("cause").withValue(cause.toString()));
Error error = ERROR_UNRELEASING_ENCUMBRANCES.toError().withParameters(parameters);
logger.error(error.getMessage(), cause);
throw new HttpException(500, error);
});
}
use of org.folio.rest.core.models.RequestContext in project mod-invoice by folio-org.
the class RestClientTest method initMocks.
@BeforeEach
public void initMocks() {
MockitoAnnotations.openMocks(this);
okapiHeaders = new HashMap<>();
okapiHeaders.put(OKAPI_URL, "http://localhost:" + 8081);
okapiHeaders.put(X_OKAPI_TOKEN.getName(), X_OKAPI_TOKEN.getValue());
okapiHeaders.put(X_OKAPI_TENANT.getName(), X_OKAPI_TENANT.getValue());
okapiHeaders.put(X_OKAPI_USER_ID.getName(), X_OKAPI_USER_ID.getValue());
requestContext = new RequestContext(ctxMock, okapiHeaders);
}
Aggregations