diff --git a/cwms-data-api/src/main/java/cwms/cda/data/dao/location/kind/EmbankmentDao.java b/cwms-data-api/src/main/java/cwms/cda/data/dao/location/kind/EmbankmentDao.java new file mode 100644 index 000000000..bc3f6550e --- /dev/null +++ b/cwms-data-api/src/main/java/cwms/cda/data/dao/location/kind/EmbankmentDao.java @@ -0,0 +1,128 @@ +/* + * 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.dao.location.kind; + + +import cwms.cda.api.errors.NotFoundException; +import cwms.cda.data.dao.DeleteRule; +import cwms.cda.data.dao.JooqDao; +import cwms.cda.data.dto.location.kind.Embankment; +import org.jooq.Configuration; +import org.jooq.DSLContext; +import org.jooq.impl.DSL; +import usace.cwms.db.dao.util.OracleTypeMap; +import usace.cwms.db.jooq.codegen.packages.CWMS_EMBANK_PACKAGE; +import usace.cwms.db.jooq.codegen.udt.records.EMBANKMENT_OBJ_T; +import usace.cwms.db.jooq.codegen.udt.records.LOCATION_REF_T; + +import java.util.List; + +import static cwms.cda.data.dao.location.kind.LocationUtil.*; +import static java.util.stream.Collectors.toList; + +public class EmbankmentDao extends JooqDao { + + public EmbankmentDao(DSLContext dsl) { + super(dsl); + } + + public List retrieveEmbankments(String projectLocationId, String officeId) { + return connectionResult(dsl, conn -> { + LOCATION_REF_T locationRefT = getLocationRef(projectLocationId, officeId); + return CWMS_EMBANK_PACKAGE.call_RETRIEVE_EMBANKMENTS(DSL.using(conn).configuration(), locationRefT) + .stream() + .map(EmbankmentDao::map) + .collect(toList()); + }); + } + + public Embankment retrieveEmbankment(String locationId, String officeId) { + return connectionResult(dsl, conn -> { + LOCATION_REF_T locationRefT = getLocationRef(locationId, officeId); + Configuration configuration = DSL.using(conn).configuration(); + EMBANKMENT_OBJ_T embankmentObjT = CWMS_EMBANK_PACKAGE.call_RETRIEVE_EMBANKMENT(configuration, locationRefT); + if (embankmentObjT == null) { + throw new NotFoundException("Embankment: " + officeId + "." + locationId + " not found"); + } + return map(embankmentObjT); + }); + } + + static Embankment map(EMBANKMENT_OBJ_T embankment) { + return new Embankment.Builder() + .withStructureLength(embankment.getSTRUCTURE_LENGTH()) + .withTopWidth(embankment.getTOP_WIDTH()) + .withUnitsId(embankment.getUNITS_ID()) + .withUpstreamSideSlope(embankment.getUPSTREAM_SIDESLOPE()) + .withDownstreamSideSlope(embankment.getDOWNSTREAM_SIDESLOPE()) + .withProjectOfficeId(embankment.getPROJECT_LOCATION_REF().getOFFICE_ID()) + .withProjectId(getLocationId(embankment.getPROJECT_LOCATION_REF())) + .withUpstreamProtType(getLookupType(embankment.getUPSTREAM_PROT_TYPE())) + .withDownstreamProtType(getLookupType(embankment.getDOWNSTREAM_PROT_TYPE())) + .withLocation(getLocation(embankment.getEMBANKMENT_LOCATION())) + .withHeightMax(embankment.getHEIGHT_MAX()) + .withStructureType(getLookupType(embankment.getSTRUCTURE_TYPE())) + .build(); + } + + static EMBANKMENT_OBJ_T map(Embankment embankment) { + EMBANKMENT_OBJ_T retval = new EMBANKMENT_OBJ_T(); + retval.setSTRUCTURE_LENGTH(embankment.getStructureLength()); + retval.setTOP_WIDTH(embankment.getTopWidth()); + retval.setUNITS_ID(embankment.getUnitsId()); + retval.setUPSTREAM_SIDESLOPE(embankment.getUpstreamSideSlope()); + retval.setDOWNSTREAM_SIDESLOPE(embankment.getDownstreamSideSlope()); + retval.setPROJECT_LOCATION_REF(getLocationRef(embankment.getProjectId(), embankment.getProjectOfficeId())); + retval.setUPSTREAM_PROT_TYPE(getLookupType(embankment.getUpstreamProtType())); + retval.setDOWNSTREAM_PROT_TYPE(getLookupType(embankment.getDownstreamProtType())); + retval.setEMBANKMENT_LOCATION(getLocation(embankment.getLocation())); + retval.setHEIGHT_MAX(embankment.getHeightMax()); + retval.setSTRUCTURE_TYPE(getLookupType(embankment.getStructureType())); + return retval; + } + + public void storeEmbankment(Embankment embankment, boolean failIfExists) { + connection(dsl, conn -> { + setOffice(conn, embankment.getLocation().getOfficeId()); + CWMS_EMBANK_PACKAGE.call_STORE_EMBANKMENT(DSL.using(conn).configuration(), map(embankment), + OracleTypeMap.formatBool(failIfExists)); + }); + } + + public void deleteEmbankment(String locationId, String officeId, DeleteRule deleteRule) { + connection(dsl, conn -> { + setOffice(conn, officeId); + CWMS_EMBANK_PACKAGE.call_DELETE_EMBANKMENT(DSL.using(conn).configuration(), locationId, + deleteRule.getRule(), officeId); + }); + } + + public void renameEmbankment(String officeId, String oldId, String newId) { + connection(dsl, conn -> { + setOffice(conn, officeId); + CWMS_EMBANK_PACKAGE.call_RENAME_EMBANKMENT(DSL.using(conn).configuration(), oldId, + newId, officeId); + }); + } +} diff --git a/cwms-data-api/src/main/java/cwms/cda/data/dao/location/kind/LocationUtil.java b/cwms-data-api/src/main/java/cwms/cda/data/dao/location/kind/LocationUtil.java new file mode 100644 index 000000000..d5004e915 --- /dev/null +++ b/cwms-data-api/src/main/java/cwms/cda/data/dao/location/kind/LocationUtil.java @@ -0,0 +1,152 @@ +/* + * 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.dao.location.kind; + +import cwms.cda.api.enums.Nation; +import cwms.cda.data.dto.Location; +import cwms.cda.data.dto.LookupType; +import usace.cwms.db.dao.util.OracleTypeMap; +import usace.cwms.db.jooq.codegen.udt.records.LOCATION_OBJ_T; +import usace.cwms.db.jooq.codegen.udt.records.LOCATION_REF_T; +import usace.cwms.db.jooq.codegen.udt.records.LOOKUP_TYPE_OBJ_T; + +import java.time.ZoneId; +import java.util.Optional; + +final class LocationUtil { + + private LocationUtil() { + throw new AssertionError("Utility class"); + } + + static String getLocationId(LOCATION_REF_T ref) { + String locationId = null; + if(ref != null) { + locationId = ref.getBASE_LOCATION_ID(); + String sub = ref.getSUB_LOCATION_ID(); + if (sub != null && !sub.isEmpty()) { + locationId += "-" + sub; + } + } + return locationId; + } + + static LOCATION_REF_T getLocationRef(String locationId, String officeId) { + LOCATION_REF_T retval = null; + if(locationId != null && !locationId.isEmpty()) { + retval = new LOCATION_REF_T(); + String[] split = locationId.split("-"); + retval.setBASE_LOCATION_ID(split[0]); + if(split.length > 1) { + retval.setSUB_LOCATION_ID(split[1]); + } + retval.setOFFICE_ID(officeId); + } + return retval; + } + + static LookupType getLookupType(LOOKUP_TYPE_OBJ_T lookupType) { + LookupType retval = null; + if(lookupType != null) { + retval = new LookupType.Builder() + .withOfficeId(lookupType.getOFFICE_ID()) + .withActive(OracleTypeMap.parseBool(lookupType.getACTIVE())) + .withDisplayValue(lookupType.getDISPLAY_VALUE()) + .withTooltip(lookupType.getTOOLTIP()) + .build(); + } + return retval; + } + + static LOOKUP_TYPE_OBJ_T getLookupType(LookupType lookupType) { + LOOKUP_TYPE_OBJ_T retval = null; + if(lookupType != null) { + retval = new LOOKUP_TYPE_OBJ_T(); + retval.setOFFICE_ID(lookupType.getOfficeId()); + retval.setACTIVE(OracleTypeMap.formatBool(lookupType.getActive())); + retval.setDISPLAY_VALUE(lookupType.getDisplayValue()); + retval.setTOOLTIP(lookupType.getTooltip()); + } + return retval; + } + + static Location getLocation(LOCATION_OBJ_T location) { + Location retval = null; + if(location != null) { + retval = new Location.Builder(getLocationId(location.getLOCATION_REF()), + location.getLOCATION_KIND_ID(), + ZoneId.of(location.getTIME_ZONE_NAME()), + OracleTypeMap.buildDouble(location.getLATITUDE()), + OracleTypeMap.buildDouble(location.getLONGITUDE()), + location.getHORIZONTAL_DATUM(), + location.getLOCATION_REF().getOFFICE_ID()) + .withActive(OracleTypeMap.parseBool(location.getACTIVE_FLAG())) + .withDescription(location.getDESCRIPTION()) + .withElevation(OracleTypeMap.buildDouble(location.getELEVATION())) + .withCountyName(location.getCOUNTY_NAME()) + .withBoundingOfficeId(location.getBOUNDING_OFFICE_ID()) + .withNation(Nation.nationForName(location.getNATION_ID())) + .withMapLabel(location.getMAP_LABEL()) + .withPublicName(location.getPUBLIC_NAME()) + .withPublishedLatitude(OracleTypeMap.buildDouble(location.getPUBLISHED_LATITUDE())) + .withPublishedLongitude(OracleTypeMap.buildDouble(location.getPUBLISHED_LONGITUDE())) + .withVerticalDatum(location.getVERTICAL_DATUM()) + .withLongName(location.getLONG_NAME()) + .withStateInitial(location.getSTATE_INITIAL()) + .withLocationType(location.getLOCATION_TYPE()) + .build(); + } + return retval; + } + + static LOCATION_OBJ_T getLocation(Location location) { + LOCATION_OBJ_T retval = null; + if(location != null) { + retval = new LOCATION_OBJ_T(); + retval.setLOCATION_REF(getLocationRef(location.getName(), location.getOfficeId())); + retval.setLOCATION_KIND_ID(location.getLocationKind()); + retval.setTIME_ZONE_NAME(location.getTimezoneName()); + retval.setLATITUDE(OracleTypeMap.toBigDecimal(location.getLatitude())); + retval.setLONGITUDE(OracleTypeMap.toBigDecimal(location.getLongitude())); + retval.setHORIZONTAL_DATUM(location.getHorizontalDatum()); + retval.setACTIVE_FLAG(OracleTypeMap.formatBool(location.getActive())); + retval.setDESCRIPTION(location.getDescription()); + retval.setELEVATION(OracleTypeMap.toBigDecimal(location.getElevation())); + retval.setELEV_UNIT_ID("m"); + retval.setCOUNTY_NAME(location.getCountyName()); + retval.setBOUNDING_OFFICE_ID(location.getBoundingOfficeId()); + retval.setNATION_ID(Optional.ofNullable(location.getNation()).map(Nation::getName).orElse(null)); + retval.setMAP_LABEL(location.getMapLabel()); + retval.setPUBLIC_NAME(location.getPublicName()); + retval.setPUBLISHED_LATITUDE(OracleTypeMap.toBigDecimal(location.getPublishedLatitude())); + retval.setPUBLISHED_LONGITUDE(OracleTypeMap.toBigDecimal(location.getPublishedLongitude())); + retval.setVERTICAL_DATUM(location.getVerticalDatum()); + retval.setLONG_NAME(location.getLongName()); + retval.setSTATE_INITIAL(location.getStateInitial()); + retval.setLOCATION_TYPE(location.getLocationType()); + } + return retval; + } +} diff --git a/cwms-data-api/src/test/java/cwms/cda/data/dao/location/kind/EmbankmentDaoIT.java b/cwms-data-api/src/test/java/cwms/cda/data/dao/location/kind/EmbankmentDaoIT.java new file mode 100644 index 000000000..dbe7d8ae2 --- /dev/null +++ b/cwms-data-api/src/test/java/cwms/cda/data/dao/location/kind/EmbankmentDaoIT.java @@ -0,0 +1,223 @@ +/* + * 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.dao.location.kind; + +import cwms.cda.api.DataApiTestIT; +import cwms.cda.api.enums.Nation; +import cwms.cda.api.errors.NotFoundException; +import cwms.cda.data.dao.DeleteRule; +import cwms.cda.data.dao.LocationsDaoImpl; +import cwms.cda.data.dto.Location; +import cwms.cda.data.dto.LookupType; +import cwms.cda.data.dto.location.kind.Embankment; +import fixtures.CwmsDataApiSetupCallback; +import mil.army.usace.hec.test.database.CwmsDatabaseContainer; +import org.jooq.DSLContext; +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Tag; +import org.junit.jupiter.api.Test; +import usace.cwms.db.jooq.codegen.packages.CWMS_PROJECT_PACKAGE; +import usace.cwms.db.jooq.codegen.udt.records.PROJECT_OBJ_T; + +import java.io.IOException; +import java.math.BigDecimal; +import java.sql.Timestamp; +import java.time.Instant; +import java.time.ZoneId; + +import static cwms.cda.data.dao.DaoTest.getDslContext; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; + +@Tag("integration") +final class EmbankmentDaoIT extends DataApiTestIT { + + private static final Location PROJECT_LOC = buildProjectLocation(); + private static final Location EMBANKMENT_LOC = buildEmbankmentLocation(); + + @BeforeAll + public static void setup() throws Exception { + CwmsDatabaseContainer databaseLink = CwmsDataApiSetupCallback.getDatabaseLink(); + databaseLink.connection(c -> { + DSLContext context = getDslContext(c, databaseLink.getOfficeId()); + LocationsDaoImpl locationsDao = new LocationsDaoImpl(context); + try { + PROJECT_OBJ_T projectObjT = buildProject(); + CWMS_PROJECT_PACKAGE.call_STORE_PROJECT(context.configuration(), projectObjT, "T"); + locationsDao.storeLocation(EMBANKMENT_LOC); + } catch (IOException e) { + throw new RuntimeException(e); + } + }); + } + + @AfterAll + public static void tearDown() throws Exception { + + CwmsDatabaseContainer databaseLink = CwmsDataApiSetupCallback.getDatabaseLink(); + databaseLink.connection(c -> { + DSLContext context = getDslContext(c, databaseLink.getOfficeId()); + LocationsDaoImpl locationsDao = new LocationsDaoImpl(context); + locationsDao.deleteLocation(EMBANKMENT_LOC.getName(), databaseLink.getOfficeId()); + CWMS_PROJECT_PACKAGE.call_DELETE_PROJECT(context.configuration(), PROJECT_LOC.getName(), + DeleteRule.DELETE_ALL.getRule(), databaseLink.getOfficeId()); + locationsDao.deleteLocation(PROJECT_LOC.getName(), databaseLink.getOfficeId()); + }); + } + + @Test + void testRoundTrip() throws Exception { + CwmsDatabaseContainer databaseLink = CwmsDataApiSetupCallback.getDatabaseLink(); + databaseLink.connection(c -> { + DSLContext context = getDslContext(c, databaseLink.getOfficeId()); + EmbankmentDao embankmentDao = new EmbankmentDao(context); + Embankment embankment = buildTestEmbankment(); + embankmentDao.storeEmbankment(embankment, false); + String embankmentId = embankment.getLocation().getName(); + String embankmentOfficeId = embankment.getLocation().getOfficeId(); + Embankment retrievedEmbankment = embankmentDao.retrieveEmbankment(embankmentId, + embankmentOfficeId); + assertEquals(embankment, retrievedEmbankment); + retrievedEmbankment = embankmentDao.retrieveEmbankments(embankment.getProjectId(), + embankment.getProjectOfficeId()).get(0); + assertEquals(embankment, retrievedEmbankment); + embankmentDao.deleteEmbankment(embankmentId, embankmentOfficeId, DeleteRule.DELETE_ALL); + assertThrows(NotFoundException.class, () -> embankmentDao.retrieveEmbankment(embankmentId, + embankmentOfficeId)); + }); + } + + @Test + void testRename() throws Exception { + CwmsDatabaseContainer databaseLink = CwmsDataApiSetupCallback.getDatabaseLink(); + databaseLink.connection(c -> { + DSLContext context = getDslContext(c, databaseLink.getOfficeId()); + EmbankmentDao embankmentDao = new EmbankmentDao(context); + Embankment embankment = buildTestEmbankment(); + embankmentDao.storeEmbankment(embankment, false); + String originalId = embankment.getLocation().getName(); + String office = embankment.getLocation().getOfficeId(); + String newId = embankment.getLocation().getName() + "New"; + embankmentDao.renameEmbankment(office, originalId, newId); + assertThrows(NotFoundException.class, () -> embankmentDao.retrieveEmbankment(originalId, office)); + Embankment retrievedEmbankment = embankmentDao.retrieveEmbankment(newId, office); + assertEquals(newId, retrievedEmbankment.getLocation().getName()); + embankmentDao.deleteEmbankment(newId, office, DeleteRule.DELETE_ALL); + }); + } + + private static Embankment buildTestEmbankment() { + return new Embankment.Builder() + .withLocation(EMBANKMENT_LOC) + .withHeightMax(5.0) + .withProjectId(PROJECT_LOC.getName()) + .withProjectOfficeId(PROJECT_LOC.getOfficeId()) + .withStructureLength(10.0) + .withStructureType(new LookupType.Builder() + .withOfficeId("CWMS") + .withDisplayValue("Rolled Earth-Filled") + .withTooltip("An embankment formed by compacted earth") + .withActive(true) + .build()) + .withDownstreamProtType(new LookupType.Builder() + .withOfficeId("CWMS") + .withDisplayValue("Concrete Arch Facing") + .withTooltip("Protected by the faces of the concrete arches") + .withActive(true) + .build()) + .withUpstreamProtType(new LookupType.Builder() + .withOfficeId("CWMS") + .withDisplayValue("Concrete Blanket") + .withTooltip("Protected by blanket of concrete") + .withActive(true) + .build()) + .withUpstreamSideSlope(15.0) + .withUnitsId("m") + .withTopWidth(20.0) + .withStructureLength(25.0) + .withDownstreamSideSlope(90.0) + .build(); + } + + private static Location buildProjectLocation() { + String officeId = CwmsDataApiSetupCallback.getDatabaseLink().getOfficeId(); + return new Location.Builder("PROJECT", "PROJECT", ZoneId.of("UTC"), + 38.5613824, -121.7298432, "NVGD29", officeId) + .withElevation(10.0) + .withLocationType("SITE") + .withCountyName("Sacramento") + .withNation(Nation.US) + .withActive(true) + .withStateInitial("CA") + .withPublishedLatitude(38.5613824) + .withPublishedLongitude(-121.7298432) + .withBoundingOfficeId(officeId) + .withLongName("UNITED STATES") + .withDescription("for testing") + .build(); + } + + private static Location buildEmbankmentLocation() { + String officeId = CwmsDataApiSetupCallback.getDatabaseLink().getOfficeId(); + return new Location.Builder("PROJECT-EMBANKMENT_LOC", "EMBANKMENT", ZoneId.of("UTC"), + 38.5613824, -121.7298432, "NVGD29", officeId) + .withElevation(10.0) + .withLocationType("SITE") + .withCountyName("Sacramento") + .withNation(Nation.US) + .withActive(true) + .withStateInitial("CA") + .withBoundingOfficeId(officeId) + .withPublishedLatitude(38.5613824) + .withPublishedLongitude(-121.7298432) + .withLongName("UNITED STATES") + .withDescription("for testing") + .build(); + } + + private static PROJECT_OBJ_T buildProject() { + PROJECT_OBJ_T retval = new PROJECT_OBJ_T(); + retval.setPROJECT_LOCATION(LocationUtil.getLocation(PROJECT_LOC)); + retval.setPUMP_BACK_LOCATION(null); + retval.setNEAR_GAGE_LOCATION(null); + retval.setAUTHORIZING_LAW(null); + retval.setCOST_YEAR(Timestamp.from(Instant.now())); + retval.setFEDERAL_COST(BigDecimal.ONE); + retval.setNONFEDERAL_COST(BigDecimal.TEN); + retval.setFEDERAL_OM_COST(BigDecimal.ZERO); + retval.setNONFEDERAL_OM_COST(BigDecimal.valueOf(15.0)); + retval.setCOST_UNITS_ID("$"); + retval.setREMARKS("TEST RESERVOIR PROJECT"); + retval.setPROJECT_OWNER("CDA"); + retval.setHYDROPOWER_DESCRIPTION("HYDRO DESCRIPTION"); + retval.setSEDIMENTATION_DESCRIPTION("SEDIMENTATION DESCRIPTION"); + retval.setDOWNSTREAM_URBAN_DESCRIPTION("DOWNSTREAM URBAN DESCRIPTION"); + retval.setBANK_FULL_CAPACITY_DESCRIPTION("BANK FULL CAPACITY DESCRIPTION"); + retval.setYIELD_TIME_FRAME_START(Timestamp.from(Instant.now())); + retval.setYIELD_TIME_FRAME_END(Timestamp.from(Instant.now())); + return retval; + } +} diff --git a/cwms-data-api/src/test/java/cwms/cda/data/dao/location/kind/EmbankmentDaoTest.java b/cwms-data-api/src/test/java/cwms/cda/data/dao/location/kind/EmbankmentDaoTest.java new file mode 100644 index 000000000..03633b289 --- /dev/null +++ b/cwms-data-api/src/test/java/cwms/cda/data/dao/location/kind/EmbankmentDaoTest.java @@ -0,0 +1,97 @@ +/* + * 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.dao.location.kind; + +import cwms.cda.api.enums.Nation; +import cwms.cda.data.dto.Location; +import cwms.cda.data.dto.LookupType; +import cwms.cda.data.dto.location.kind.Embankment; +import org.junit.jupiter.api.Test; +import usace.cwms.db.jooq.codegen.udt.records.EMBANKMENT_OBJ_T; + +import java.time.ZoneId; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +final class EmbankmentDaoTest { + + @Test + void testJooqTypeConversion() { + Embankment expected = buildTestEmbankment(); + EMBANKMENT_OBJ_T embankmentObjT = EmbankmentDao.map(expected); + Embankment embankment = EmbankmentDao.map(embankmentObjT); + assertEquals(expected, embankment, "Conversion of Embankment to jOOQ type and back failed."); + } + + private Embankment buildTestEmbankment() { + return new Embankment.Builder() + .withLocation(buildTestLocation()) + .withHeightMax(5.0) + .withProjectId("PROJECT") + .withProjectOfficeId("LRD") + .withStructureLength(10.0) + .withStructureType(new LookupType.Builder() + .withOfficeId("CWMS") + .withDisplayValue("STRUCT") + .withTooltip("TOOLTIP_STRUCT") + .withActive(true) + .build()) + .withDownstreamProtType(new LookupType.Builder() + .withOfficeId("SPK") + .withDisplayValue("DOWNSTREAM_PROT") + .withTooltip("TOOLTIP_DOWNSTREAM_PROT") + .withActive(false) + .build()) + .withUpstreamProtType(new LookupType.Builder() + .withOfficeId("LRL") + .withDisplayValue("UPSTREAM_PROT") + .withTooltip("TOOLTIP_UPSTREAM_PROT") + .withActive(true) + .build()) + .withUpstreamSideSlope(15.0) + .withUnitsId("ft") + .withTopWidth(20.0) + .withStructureLength(25.0) + .withDownstreamSideSlope(90.0) + .build(); + } + + private Location buildTestLocation() { + return new Location.Builder("TEST_LOCATION2", "EMBANKMENT", ZoneId.of("UTC"), + 50.0, 50.0, "NVGD29", "LRL") + .withElevation(10.0) + .withLocationType("SITE") + .withCountyName("Sacramento") + .withNation(Nation.US) + .withActive(true) + .withStateInitial("CA") + .withBoundingOfficeId("LRL") + .withLongName("TEST_LOCATION") + .withPublishedLatitude(50.0) + .withPublishedLongitude(50.0) + .withDescription("for testing") + .build(); + } +} diff --git a/cwms-data-api/src/test/java/cwms/cda/data/dao/location/kind/LocationUtilTest.java b/cwms-data-api/src/test/java/cwms/cda/data/dao/location/kind/LocationUtilTest.java new file mode 100644 index 000000000..f76ec43b7 --- /dev/null +++ b/cwms-data-api/src/test/java/cwms/cda/data/dao/location/kind/LocationUtilTest.java @@ -0,0 +1,100 @@ +/* + * 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.dao.location.kind; + +import cwms.cda.api.enums.Nation; +import cwms.cda.data.dto.Location; +import cwms.cda.data.dto.LookupType; +import org.junit.jupiter.api.Test; +import usace.cwms.db.jooq.codegen.udt.records.LOCATION_OBJ_T; +import usace.cwms.db.jooq.codegen.udt.records.LOCATION_REF_T; +import usace.cwms.db.jooq.codegen.udt.records.LOOKUP_TYPE_OBJ_T; + +import java.time.ZoneId; + +import static org.junit.jupiter.api.Assertions.*; + +final class LocationUtilTest { + + @Test + void testLocationObjConversion() { + Location expected = buildTestLocation(); + LOCATION_OBJ_T locationObj = LocationUtil.getLocation(expected); + Location location = LocationUtil.getLocation(locationObj); + assertEquals(expected, location, "Conversion of Location to jOOQ type and back failed."); + } + + @Test + void testLocationRefBaseSub() { + LOCATION_REF_T locationRef = LocationUtil.getLocationRef("BASE-SUB", "SPK"); + assertAll(() -> assertEquals("SPK", locationRef.getOFFICE_ID()), + () -> assertEquals("BASE", locationRef.getBASE_LOCATION_ID()), + () -> assertEquals("SUB", locationRef.getSUB_LOCATION_ID())); + assertEquals("BASE-SUB", LocationUtil.getLocationId(locationRef)); + } + + @Test + void testLocationRefBaseOnly() { + LOCATION_REF_T locationRef = LocationUtil.getLocationRef("BASE", "SPK"); + assertAll(() -> assertEquals("SPK", locationRef.getOFFICE_ID()), + () -> assertEquals("BASE", locationRef.getBASE_LOCATION_ID()), + () -> assertNull(locationRef.getSUB_LOCATION_ID())); + assertEquals("BASE", LocationUtil.getLocationId(locationRef)); + } + + @Test + void testLookupTypeObjConversion() { + LookupType expected = buildTestLookupType(); + LOOKUP_TYPE_OBJ_T lookupTypeObjT = LocationUtil.getLookupType(expected); + LookupType lookupType = LocationUtil.getLookupType(lookupTypeObjT); + assertEquals(expected, lookupType, "Conversion of LookupType to jOOQ type and back failed."); + } + + private LookupType buildTestLookupType() { + return new LookupType.Builder() + .withActive(true) + .withDisplayValue("display") + .withOfficeId("SPK") + .withTooltip("tooltip") + .build(); + } + + private Location buildTestLocation() { + return new Location.Builder("TEST_LOCATION2", "EMBANKMENT", ZoneId.of("UTC"), + 50.0, 50.0, "NVGD29", "LRL") + .withElevation(10.0) + .withLocationType("SITE") + .withCountyName("Sacramento") + .withNation(Nation.US) + .withActive(true) + .withStateInitial("CA") + .withBoundingOfficeId("LRL") + .withLongName("TEST_LOCATION") + .withPublishedLatitude(50.0) + .withPublishedLongitude(50.0) + .withDescription("for testing") + .build(); + } +}