From 58ecc9a44489c0a6c2dae4f8c332a1c699ec7ac8 Mon Sep 17 00:00:00 2001 From: rma-rripken <89810919+rma-rripken@users.noreply.github.com> Date: Thu, 28 Dec 2023 09:32:02 -0800 Subject: [PATCH] Adding Std text timeseries IT. --- .../StandardTimeSeriesTextDao.java | 32 ++++++- .../StandardTimeSeriesTextDaoTestIT.java | 96 ++++++++++++++++++- 2 files changed, 123 insertions(+), 5 deletions(-) diff --git a/cwms-data-api/src/main/java/cwms/cda/data/dao/texttimeseries/StandardTimeSeriesTextDao.java b/cwms-data-api/src/main/java/cwms/cda/data/dao/texttimeseries/StandardTimeSeriesTextDao.java index 71625baa1..6ca2240f8 100644 --- a/cwms-data-api/src/main/java/cwms/cda/data/dao/texttimeseries/StandardTimeSeriesTextDao.java +++ b/cwms-data-api/src/main/java/cwms/cda/data/dao/texttimeseries/StandardTimeSeriesTextDao.java @@ -17,7 +17,9 @@ import java.util.Arrays; import java.util.Date; import java.util.List; +import java.util.NavigableSet; import java.util.TimeZone; +import java.util.TreeSet; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import org.jooq.DSLContext; @@ -114,7 +116,11 @@ private StandardTextValue retrieve(Connection c, StandardTextId standardTextId) .build(); } - + /** + * This is if you want to store a new standard text id -> value mapping. + * @param standardTextValue + * @param failIfExists + */ public void store(StandardTextValue standardTextValue, boolean failIfExists) { cwms.cda.data.dto.timeseriestext.StandardTextId standardTextId = standardTextValue.getId(); @@ -128,6 +134,28 @@ public void store(StandardTextValue standardTextValue, boolean failIfExists) { }); } + public void store(String officeId, String tsId, StandardTextTimeSeriesRow stdRow, + boolean maxVersion, boolean replaceAll) { + TimeZone timeZone = OracleTypeMap.GMT_TIME_ZONE; + + StandardTextId standardTextId = stdRow.getStandardTextId(); + String textId = standardTextId.getId(); + + Date dateTime = stdRow.getDateTime(); + Date versionDate = stdRow.getVersionDate(); + Long attribute = stdRow.getAttribute(); + + NavigableSet dates = new TreeSet<>(); + dates.add(dateTime); + + connection(dsl, connection -> { + CwmsDbText dbText = CwmsDbServiceLookup.buildCwmsDb(CwmsDbText.class, connection); + dbText.storeTsStdText(connection, tsId, textId, dates, + versionDate, timeZone, maxVersion, replaceAll, + attribute, officeId); + }); + } + /** * Deletes standard text * @@ -211,7 +239,7 @@ public TextTimeSeries retrieveTextTimeSeries( if (standardTextId != null) { stdTextIdMask = standardTextId.getId(); } else { - stdTextIdMask = null; + stdTextIdMask = "*"; } TimeZone timeZone = OracleTypeMap.GMT_TIME_ZONE; diff --git a/cwms-data-api/src/test/java/cwms/cda/data/dao/texttimeseries/StandardTimeSeriesTextDaoTestIT.java b/cwms-data-api/src/test/java/cwms/cda/data/dao/texttimeseries/StandardTimeSeriesTextDaoTestIT.java index 9bcfc925d..5529150d9 100644 --- a/cwms-data-api/src/test/java/cwms/cda/data/dao/texttimeseries/StandardTimeSeriesTextDaoTestIT.java +++ b/cwms-data-api/src/test/java/cwms/cda/data/dao/texttimeseries/StandardTimeSeriesTextDaoTestIT.java @@ -1,16 +1,23 @@ package cwms.cda.data.dao.texttimeseries; - import static cwms.cda.data.dao.DaoTest.getDslContext; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; +import com.google.common.flogger.FluentLogger; import cwms.cda.api.DataApiTestIT; +import cwms.cda.data.dto.timeseriestext.RegularTextTimeSeriesRow; import cwms.cda.data.dto.timeseriestext.StandardTextId; +import cwms.cda.data.dto.timeseriestext.StandardTextTimeSeriesRow; +import cwms.cda.data.dto.timeseriestext.StandardTextValue; import cwms.cda.data.dto.timeseriestext.TextTimeSeries; import fixtures.CwmsDataApiSetupCallback; import java.sql.SQLException; import java.time.ZonedDateTime; +import java.util.Collection; import java.util.Date; import mil.army.usace.hec.test.database.CwmsDatabaseContainer; import org.jooq.DSLContext; @@ -21,6 +28,7 @@ @Tag("integration") class StandardTimeSeriesTextDaoTestIT extends DataApiTestIT { + private static final FluentLogger logger = FluentLogger.forEnclosingClass(); @BeforeAll public static void load_data() throws Exception { loadSqlDataFromResource("cwms/cda/data/sql/store_std_text_timeseries.sql"); @@ -31,6 +39,76 @@ public static void deload_data() throws Exception { loadSqlDataFromResource("cwms/cda/data/sql/delete_std_text_timeseries.sql"); } + @Test + void testCreate() throws SQLException { + CwmsDatabaseContainer databaseLink = CwmsDataApiSetupCallback.getDatabaseLink(); + databaseLink.connection(c -> {// + DSLContext dsl = getDslContext(c, "SPK"); + StandardTimeSeriesTextDao dao = new StandardTimeSeriesTextDao(dsl); + + testCreate(dao); + } + ); + } + + private void testCreate(StandardTimeSeriesTextDao dao) { + String officeId = "SPK"; + String tsId = "First519402.Flow.Inst.1Hour.0.1688755420497"; + + StandardTextId standardTextId = null; + + // The delete script deletes from 2005-02-01 13:30:00 - 2005-02-02 17:00:00' + + ZonedDateTime startZDT = ZonedDateTime.parse("2005-02-01T15:00:00Z"); + ZonedDateTime endZDT = ZonedDateTime.parse("2005-02-01T23:00:00Z"); + + // make sure it doesn't exist + + Date startDate = Date.from(startZDT.toInstant()); + Date endDate = Date.from(endZDT.toInstant()); + Date versionDate = null; + boolean maxVersion = false; + boolean retText = true; + Long minAttr = null; + Long maxAttr = null; + + TextTimeSeries tts = dao.retrieveTextTimeSeries(officeId, tsId, standardTextId, + startDate, endDate, versionDate, + maxVersion, retText, minAttr, maxAttr); + + assertNotNull(tts); + assertEquals(0, tts.getStdRows().size()); + + // create/store + StandardTextId textId = new StandardTextId.Builder() + .withId("A") + .withOfficeId("CWMS").build(); + StandardTextValue stv = new StandardTextValue.Builder() + .withId(textId) + .build(); + + StandardTextTimeSeriesRow row = new StandardTextTimeSeriesRow.Builder() + .withDateTime(startDate) + .withStandardTextId(textId) + .withStandardTextValue(stv) + .build(); + dao.store(officeId, tsId, row, true, true); + + // retrieve and verify + tts = dao.retrieveTextTimeSeries(officeId, tsId, null, + startDate, endDate, versionDate, + maxVersion, retText, minAttr, maxAttr); + + assertNotNull(tts); + Collection stdRows = tts.getStdRows(); + assertFalse(stdRows.isEmpty()); + assertEquals(1, stdRows.size()); +// logger.atInfo().log("got %d rows", stdRows.size()); + StandardTextTimeSeriesRow first = stdRows.iterator().next(); + assertEquals( "A", first.getStandardTextId().getId()); + + } + @Test void testRetrieve() throws SQLException { CwmsDatabaseContainer databaseLink = CwmsDataApiSetupCallback.getDatabaseLink(); @@ -40,8 +118,8 @@ void testRetrieve() throws SQLException { testRetrieve(dao); } - ); - } + ); + } private static void testRetrieve(StandardTimeSeriesTextDao dao) { String officeId = "SPK"; @@ -64,6 +142,18 @@ private static void testRetrieve(StandardTimeSeriesTextDao dao) { maxVersion, retText, minAttr, maxAttr); assertNotNull(tts); + + Collection stdRows = tts.getStdRows(); + assertNotNull(stdRows); + assertFalse(stdRows.isEmpty()); + + StandardTextTimeSeriesRow first = stdRows.iterator().next(); + assertNotNull(first); + assertEquals("E", first.getStandardTextId()); + + Collection regRows = tts.getRegRows(); + assertNull(regRows); + } } \ No newline at end of file