From 4138f16c76aa948768cef309aa122c3300bb44f7 Mon Sep 17 00:00:00 2001 From: Azizbek Khushvakov Date: Wed, 4 Dec 2024 15:48:36 +0500 Subject: [PATCH] [MODFIN-381] - Added validation --- ramls/acq-models | 2 +- .../financedata/FinanceDataService.java | 92 ++++++++++++------- 2 files changed, 58 insertions(+), 36 deletions(-) diff --git a/ramls/acq-models b/ramls/acq-models index f09326b4..667cd28d 160000 --- a/ramls/acq-models +++ b/ramls/acq-models @@ -1 +1 @@ -Subproject commit f09326b40c4ce0ea9e27ccd0fd4fcfa792d3c173 +Subproject commit 667cd28d3f0604848e6dc24d1a8e72a956906039 diff --git a/src/main/java/org/folio/services/financedata/FinanceDataService.java b/src/main/java/org/folio/services/financedata/FinanceDataService.java index c8c5ae13..922e50de 100644 --- a/src/main/java/org/folio/services/financedata/FinanceDataService.java +++ b/src/main/java/org/folio/services/financedata/FinanceDataService.java @@ -44,7 +44,8 @@ public Future getFinanceDataWithAcqUnitsRestriction(Str .compose(effectiveQuery -> getFinanceData(effectiveQuery, offset, limit, requestContext)); } - private Future getFinanceData(String query, int offset, int limit, RequestContext requestContext) { + private Future getFinanceData(String query, int offset, int limit, + RequestContext requestContext) { var requestEntry = new RequestEntry(resourcesPath(FINANCE_DATA_STORAGE)) .withOffset(offset) .withLimit(limit) @@ -53,61 +54,82 @@ private Future getFinanceData(String query, int offset, } public Future putFinanceData(FyFinanceDataCollection financeData, RequestContext requestContext) { - validateFinanceData(financeData); + financeData.getFyFinanceData().forEach(this::validateFinanceData); return processAllocationTransaction(financeData, requestContext) .compose(v -> updateFinanceData(financeData, requestContext)) .compose(v -> processLogs(financeData, requestContext)); } - private void validateFinanceData(FyFinanceDataCollection financeData) { - if (financeData.getFyFinanceData() == null || financeData.getFyFinanceData().isEmpty()) { - throw new IllegalArgumentException("Finance data collection is empty"); - } + private void validateFinanceData(FyFinanceData financeData) { + validateFinanceDataFields(financeData); - financeData.getFyFinanceData().forEach(this::validateFinanceDataItem); - } + var allocationChange = financeData.getBudgetAllocationChange(); + var initialAllocation = financeData.getBudgetInitialAllocation(); + var currentAllocation = financeData.getBudgetCurrentAllocation(); + var expectedChange = currentAllocation - initialAllocation; - private void validateFinanceDataItem(FyFinanceData item) { - // Validate fiscal year code format (assuming it should be "FY" followed by 4 digits) - if (item.getFiscalYearCode() != null && !item.getFiscalYearCode().matches("FY\\d{4}")) { - throw new IllegalArgumentException("Invalid fiscal year code format. Expected FY followed by 4 digits."); + if (Math.abs(allocationChange) - expectedChange != 0) { + throw new IllegalArgumentException( + "Allocation change does not match the difference between current and initial allocation"); } - - validateNumericFields(item); - - // Validate that allocation change matches the difference between current and initial allocation - if (item.getAllocationChange() != null && item.getBudgetCurrentAllocation() != null - && item.getBudgetInitialAllocation() != null) { - double expectedChange = item.getBudgetCurrentAllocation() - item.getBudgetInitialAllocation(); - if (Math.abs(item.getAllocationChange() - expectedChange) > 0.01) { // allowing for small floating-point discrepancies - throw new IllegalArgumentException("Allocation change does not match the difference between current and initial allocation"); - } + if (allocationChange < 0 && Math.abs(allocationChange) > initialAllocation) { + throw new IllegalArgumentException("Allocation change cannot be greater than initial allocation"); } } - private void validateNumericFields(FyFinanceData item) { - if (item.getBudgetInitialAllocation() != null && item.getBudgetInitialAllocation() < 0) { - throw new IllegalArgumentException("Budget initial allocation must be non-negative"); + private void validateFinanceDataFields(FyFinanceData financeData) { + if (financeData.getFundId() == null) { + throw new IllegalArgumentException("Fund ID is required"); + } + if (financeData.getFundCode() == null) { + throw new IllegalArgumentException("Fund code is required"); + } + if (financeData.getFundName() == null) { + throw new IllegalArgumentException("Fund name is required"); + } + if (financeData.getFundDescription() == null) { + throw new IllegalArgumentException("Fund description is required"); + } + if (financeData.getFundStatus() == null) { + throw new IllegalArgumentException("Fund status is required"); + } + if (financeData.getBudgetId() == null) { + throw new IllegalArgumentException("Budget ID is required"); + } + if (financeData.getBudgetName() == null) { + throw new IllegalArgumentException("Budget name is required"); + } + if (financeData.getBudgetStatus() == null) { + throw new IllegalArgumentException("Budget status is required"); + } + if (financeData.getBudgetInitialAllocation() == null) { + throw new IllegalArgumentException("Budget initial allocation is required"); + } + if (financeData.getBudgetAllocationChange() == null) { + throw new IllegalArgumentException("Allocation change is required"); + } + if (financeData.getBudgetAllowableExpenditure() == null) { + throw new IllegalArgumentException("Budget allowable expenditure is required"); } - if (item.getBudgetCurrentAllocation() != null && item.getBudgetCurrentAllocation() < 0) { - throw new IllegalArgumentException("Budget current allocation must be non-negative"); + if (financeData.getBudgetAllowableEncumbrance() == null) { + throw new IllegalArgumentException("Budget allowable encumbrance is required"); } - if (item.getBudgetAllowableExpenditure() != null - && (item.getBudgetAllowableExpenditure() < 0 || item.getBudgetAllowableExpenditure() > 100)) { - throw new IllegalArgumentException("Budget allowable expenditure must be between 0 and 100"); + if (financeData.getTransactionDescription() == null) { + throw new IllegalArgumentException("Transaction description is required"); } - if (item.getBudgetAllowableEncumbrance() != null - && (item.getBudgetAllowableEncumbrance() < 0 || item.getBudgetAllowableEncumbrance() > 100)) { - throw new IllegalArgumentException("Budget allowable encumbrance must be between 0 and 100"); + if (financeData.getTransactionTag() == null) { + throw new IllegalArgumentException("Transaction tag is required"); } } - private Future processAllocationTransaction(FyFinanceDataCollection financeData, RequestContext requestContext) { + private Future processAllocationTransaction(FyFinanceDataCollection financeData, + RequestContext requestContext) { return createAllocationTransaction(financeData, requestContext); } - public Future createAllocationTransaction(FyFinanceDataCollection fyFinanceDataCollection, RequestContext requestContext) { + public Future createAllocationTransaction(FyFinanceDataCollection fyFinanceDataCollection, + RequestContext requestContext) { var transactionsFuture = fyFinanceDataCollection.getFyFinanceData().stream() .map(financeData -> createAllocationTransaction(financeData, requestContext)) .toList();