From 0a7238d267bcc8918742a9f361d728b535c5a232 Mon Sep 17 00:00:00 2001 From: Zachary Olson Date: Fri, 26 Jul 2024 12:16:11 -0700 Subject: [PATCH] Updated transfer type checking, fixed test cases --- .../AccountingCreateController.java | 40 +++++++++---------- .../WaterSupplyAccountingControllerIT.java | 4 +- .../cwms/cda/api/pump_accounting.json | 4 +- 3 files changed, 23 insertions(+), 25 deletions(-) diff --git a/cwms-data-api/src/main/java/cwms/cda/api/watersupply/AccountingCreateController.java b/cwms-data-api/src/main/java/cwms/cda/api/watersupply/AccountingCreateController.java index 821889600..9b83324e2 100644 --- a/cwms-data-api/src/main/java/cwms/cda/api/watersupply/AccountingCreateController.java +++ b/cwms-data-api/src/main/java/cwms/cda/api/watersupply/AccountingCreateController.java @@ -48,11 +48,12 @@ import io.javalin.plugin.openapi.annotations.OpenApiParam; import io.javalin.plugin.openapi.annotations.OpenApiRequestBody; import io.javalin.plugin.openapi.annotations.OpenApiResponse; +import java.util.List; +import javax.servlet.http.HttpServletResponse; import org.jetbrains.annotations.NotNull; import org.jooq.DSLContext; -import javax.servlet.http.HttpServletResponse; -import java.util.Iterator; -import java.util.List; + + public class AccountingCreateController implements Handler { @@ -110,31 +111,28 @@ public void handle(@NotNull Context ctx) { WaterSupplyAccounting.class); WaterSupplyAccountingDao waterSupplyAccountingDao = getWaterSupplyAccountingDao(dsl); LookupTypeDao lookupTypeDao = new LookupTypeDao(dsl); - List lookupList = lookupTypeDao.retrieveLookupTypes("AT_PHYSICAL_TRANSFER_TYPE", "PHYS_TRANS_TYPE", office); + List lookupList = lookupTypeDao + .retrieveLookupTypes("AT_PHYSICAL_TRANSFER_TYPE", "PHYS_TRANS_TYPE", office); - if (!searchForTransferType(accounting, lookupList)) { - ctx.status(HttpServletResponse.SC_BAD_REQUEST).json("No such transfer type found."); - return; + for (PumpAccounting pumpAccounting : accounting.getPumpAccounting()) { + if (!searchForTransferType(pumpAccounting, lookupList)) { + ctx.status(HttpServletResponse.SC_BAD_REQUEST).json("No matching transfer type found " + + "for an accounting entry."); + return; + } } - waterSupplyAccountingDao.storeAccounting(accounting); ctx.status(HttpServletResponse.SC_CREATED).json(contractId + " created successfully"); } } - private boolean searchForTransferType(WaterSupplyAccounting accounting, List lookupTypes) { - Iterator pumpAccountingIterator = accounting.getPumpAccounting().iterator(); - Iterator lookupTypeIterator = lookupTypes.iterator(); - while (pumpAccountingIterator.hasNext()) { - PumpAccounting pumpAccounting = pumpAccountingIterator.next(); - while (lookupTypeIterator.hasNext()) { - LookupType lookupType = lookupTypeIterator.next(); - if (pumpAccounting.getTransferType().getActive() == lookupType.getActive() - && pumpAccounting.getTransferType().getOfficeId().equals(lookupType.getOfficeId()) - && pumpAccounting.getTransferType().getTooltip().equals(lookupType.getTooltip()) - && pumpAccounting.getTransferType().getDisplayValue().equals(lookupType.getDisplayValue())) { - return true; - } + private boolean searchForTransferType(PumpAccounting accounting, List lookupTypes) { + for (LookupType lookupType : lookupTypes) { + if (accounting.getTransferType().getActive() == lookupType.getActive() + && accounting.getTransferType().getOfficeId().equals(lookupType.getOfficeId()) + && accounting.getTransferType().getTooltip().equals(lookupType.getTooltip()) + && accounting.getTransferType().getDisplayValue().equals(lookupType.getDisplayValue())) { + return true; } } return false; diff --git a/cwms-data-api/src/test/java/cwms/cda/api/WaterSupplyAccountingControllerIT.java b/cwms-data-api/src/test/java/cwms/cda/api/WaterSupplyAccountingControllerIT.java index 8a2e7c680..0b47238f1 100644 --- a/cwms-data-api/src/test/java/cwms/cda/api/WaterSupplyAccountingControllerIT.java +++ b/cwms-data-api/src/test/java/cwms/cda/api/WaterSupplyAccountingControllerIT.java @@ -234,8 +234,8 @@ void testCreateRetrieveWaterAccounting() throws Exception { .body("[0].water-user.project-id.office-id", equalTo(WATER_SUPPLY_ACCOUNTING.getWaterUser().getProjectId().getOfficeId())) .body("[0].water-user.water-right", equalTo(WATER_SUPPLY_ACCOUNTING.getWaterUser().getWaterRight())) .body("[0].pump-accounting[0].transfer-type.display-value", equalTo(testTransferType.getDisplayValue())) - .body("[0].pump-accounting[0].pump-location.name", equalTo(WATER_SUPPLY_ACCOUNTING.getPumpAccounting().get(0).getPumpLocation().getName())) - .body("[0].pump-accounting[1].pump-location.name", equalTo(WATER_SUPPLY_ACCOUNTING.getPumpAccounting().get(1).getPumpLocation().getName())) + .body("[0].pump-accounting[1].pump-location.name", equalTo(WATER_SUPPLY_ACCOUNTING.getPumpAccounting().get(0).getPumpLocation().getName())) + .body("[0].pump-accounting[0].pump-location.name", equalTo(WATER_SUPPLY_ACCOUNTING.getPumpAccounting().get(1).getPumpLocation().getName())) ; } diff --git a/cwms-data-api/src/test/resources/cwms/cda/api/pump_accounting.json b/cwms-data-api/src/test/resources/cwms/cda/api/pump_accounting.json index ca9c6ec8c..669b45056 100644 --- a/cwms-data-api/src/test/resources/cwms/cda/api/pump_accounting.json +++ b/cwms-data-api/src/test/resources/cwms/cda/api/pump_accounting.json @@ -31,8 +31,8 @@ }, "transfer-type": { "office-id": "SWT", - "display-value": "Test Transfer Type 2", - "tooltip": "Test Tool Tip 2", + "display-value": "Test Transfer Type", + "tooltip": "Test Tool Tip", "active": true }, "flow": 2.0,