From e8e85bac937ff8a5169b627c3a13e3f8c49116a9 Mon Sep 17 00:00:00 2001 From: zack-rma Date: Mon, 12 Aug 2024 13:59:02 -0700 Subject: [PATCH] Updated Accounting DTO and tests --- .../watersupply/WaterSupplyAccounting.java | 3 + .../WaterSupplyAccountingTest.java | 134 ++++++++++++++++++ 2 files changed, 137 insertions(+) create mode 100644 cwms-data-api/src/test/java/cwms/cda/data/dto/watersupply/WaterSupplyAccountingTest.java diff --git a/cwms-data-api/src/main/java/cwms/cda/data/dto/watersupply/WaterSupplyAccounting.java b/cwms-data-api/src/main/java/cwms/cda/data/dto/watersupply/WaterSupplyAccounting.java index 9b43e8446..554f1a531 100644 --- a/cwms-data-api/src/main/java/cwms/cda/data/dto/watersupply/WaterSupplyAccounting.java +++ b/cwms-data-api/src/main/java/cwms/cda/data/dto/watersupply/WaterSupplyAccounting.java @@ -27,6 +27,7 @@ package cwms.cda.data.dto.watersupply; import com.fasterxml.jackson.annotation.JsonInclude; +import com.fasterxml.jackson.annotation.JsonProperty; import com.fasterxml.jackson.databind.PropertyNamingStrategies; import com.fasterxml.jackson.databind.annotation.JsonDeserialize; import com.fasterxml.jackson.databind.annotation.JsonNaming; @@ -43,7 +44,9 @@ @FormattableWith(contentType = Formats.JSONV1, formatter = JsonV1.class) @JsonNaming(PropertyNamingStrategies.KebabCaseStrategy.class) public final class WaterSupplyAccounting extends CwmsDTOBase { + @JsonProperty(required = true) private final String contractName; + @JsonProperty(required = true) private final WaterUser waterUser; private final List pumpAccounting; diff --git a/cwms-data-api/src/test/java/cwms/cda/data/dto/watersupply/WaterSupplyAccountingTest.java b/cwms-data-api/src/test/java/cwms/cda/data/dto/watersupply/WaterSupplyAccountingTest.java new file mode 100644 index 000000000..716c20d9f --- /dev/null +++ b/cwms-data-api/src/test/java/cwms/cda/data/dto/watersupply/WaterSupplyAccountingTest.java @@ -0,0 +1,134 @@ +/* + * + * MIT License + * + * Copyright (c) 2024 Hydrologic Engineering Center + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER + * DEALINGS IN THE + * SOFTWARE. + */ + +package cwms.cda.data.dto.watersupply; + +import static org.junit.jupiter.api.Assertions.*; + +import cwms.cda.api.errors.FieldException; +import cwms.cda.data.dto.CwmsId; +import cwms.cda.data.dto.LookupType; +import cwms.cda.formatters.Formats; +import cwms.cda.helpers.DTOMatch; +import org.junit.jupiter.api.Test; +import org.testcontainers.shaded.org.apache.commons.io.IOUtils; +import java.io.InputStream; +import java.nio.charset.StandardCharsets; +import java.time.Instant; +import java.util.ArrayList; +import java.util.List; + + +class WaterSupplyAccountingTest { + private static final String OFFICE = "SPK"; + + @Test + void testWaterSupplyAccountingSerializationRoundTrip() { + WaterUser user = new WaterUser.Builder().withEntityName("Test Entity") + .withProjectId(new CwmsId.Builder() + .withOfficeId(OFFICE) + .withName("Test Location") + .build()) + .withWaterRight("Test Water Right").build(); + WaterSupplyAccounting waterSupplyAccounting = new WaterSupplyAccounting.Builder() + .withWaterUser(user).withContractName("Test Contract").withPumpAccounting(buildPumpAccounting()).build(); + String serialized = Formats.format(Formats.parseHeader(Formats.JSONV1, WaterSupplyAccounting.class), + waterSupplyAccounting); + WaterSupplyAccounting deserialized = Formats.parseContent(Formats.parseHeader(Formats.JSONV1, + WaterSupplyAccounting.class), serialized, WaterSupplyAccounting.class); + DTOMatch.assertMatch(waterSupplyAccounting, deserialized); + } + + @Test + void testWaterSupplyAccountingSerializationRoundTripFromFile() throws Exception { + WaterSupplyAccounting WaterSupplyAccounting = new WaterSupplyAccounting.Builder() + .withWaterUser(new WaterUser.Builder() + .withEntityName("Test Entity").withProjectId(new CwmsId.Builder().withOfficeId(OFFICE) + .withName("Test Location").build()) + .withWaterRight("Test Water Right").build()) + .withContractName("Test Contract") + .withPumpAccounting(buildPumpAccounting()).build(); + InputStream resource = this.getClass().getResourceAsStream( + "/cwms/cda/data/dto/watersupply/water_supply_accounting.json"); + assertNotNull(resource); + String serialized = IOUtils.toString(resource, StandardCharsets.UTF_8); + WaterSupplyAccounting deserialized = Formats.parseContent(Formats.parseHeader(Formats.JSONV1, + WaterSupplyAccounting.class), serialized, WaterSupplyAccounting.class); + DTOMatch.assertMatch(WaterSupplyAccounting, deserialized); + } + + + @Test + void testValidate() { + assertAll( + () -> { + WaterSupplyAccounting WaterSupplyAccounting = new WaterSupplyAccounting.Builder() + .withWaterUser(new WaterUser.Builder().withEntityName("Test Entity") + .withProjectId(new CwmsId.Builder().withOfficeId(OFFICE) + .withName("Test Location").build()) + .withWaterRight("Test Water Right").build()).withContractName("Test Contract") + .build(); + assertDoesNotThrow(WaterSupplyAccounting::validate, "Expected validation to pass"); + }, + () -> { + WaterSupplyAccounting WaterSupplyAccounting = new WaterSupplyAccounting.Builder() + .withWaterUser(new WaterUser.Builder() + .withEntityName("Test Entity").withProjectId(new CwmsId.Builder().withOfficeId(OFFICE) + .withName("Test Location").build()) + .withWaterRight("Test Water Right").build()) + .withContractName(null) + .build(); + assertThrows(FieldException.class, WaterSupplyAccounting::validate, "Expected validation to " + + "fail due to null contract name"); + }, + () -> { + WaterSupplyAccounting WaterSupplyAccounting = new WaterSupplyAccounting.Builder() + .withContractName("Test Contract") + .build(); + assertThrows(FieldException.class, WaterSupplyAccounting::validate, "Expected validation to " + + "fail due to null water user"); + } + ); + } + + + private static List buildPumpAccounting() { + List pumpList = new ArrayList<>(); + PumpAccounting pumpAccounting = new PumpAccounting.Builder() + .withPumpLocation(new CwmsId.Builder().withOfficeId(OFFICE).withName("Test Pump").build()) + .withTransferType(new LookupType.Builder().withActive(true).withTooltip("Test Tool Tip").withOfficeId(OFFICE) + .withDisplayValue("Test Transfer Type").build()).withFlow(1.0) + .withTransferDate(Instant.ofEpochMilli(10000012648000L)).withComment("Test Comment").build(); + pumpList.add(pumpAccounting); + PumpAccounting pumpAccounting2 = new PumpAccounting.Builder() + .withPumpLocation(new CwmsId.Builder().withOfficeId(OFFICE).withName("Test Pump 2").build()) + .withTransferType(new LookupType.Builder().withActive(true).withTooltip("Test Tool Tip 2").withOfficeId(OFFICE) + .withDisplayValue("Test Transfer Type 2").build()).withFlow(2.0) + .withTransferDate(Instant.ofEpochMilli(10000012648000L)).withComment("Test Comment 2").build(); + pumpList.add(pumpAccounting2); + return pumpList; + } +}