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