diff --git a/cwms-data-api/src/main/java/cwms/cda/data/dao/RatingTemplateDao.java b/cwms-data-api/src/main/java/cwms/cda/data/dao/RatingTemplateDao.java index 41d180a8e..a7b60afc6 100644 --- a/cwms-data-api/src/main/java/cwms/cda/data/dao/RatingTemplateDao.java +++ b/cwms-data-api/src/main/java/cwms/cda/data/dao/RatingTemplateDao.java @@ -95,25 +95,11 @@ public Set retrieveRatingTemplates(String office, String templat } @NotNull - private Set buildRatingTemplateSet(ResultQuery query) { - - TableField idField = - AV_RATING_SPEC.AV_RATING_SPEC.RATING_ID; - + private static Set buildRatingTemplateSet(ResultQuery query) { Map> map = new LinkedHashMap<>(); - try (Stream stream = query.fetchStream()) { - stream.forEach(rec -> { - RatingTemplate template = buildRatingTemplate(rec); - String specID = rec.get(idField); - - List list = map.computeIfAbsent(template, k -> new ArrayList<>()); - if (specID != null) { - list.add(specID); - } - }); + stream.forEach(rec -> addTemplate(rec, map)); } - return map.entrySet().stream() .map(entry -> new RatingTemplate.Builder() .fromRatingTemplate(entry.getKey()) @@ -151,15 +137,7 @@ public Optional retrieveRatingTemplate(String office, String tem Map> map = new LinkedHashMap<>(); try (Stream stream = query.fetchStream()) { - stream.forEach(rec -> { - RatingTemplate template = buildRatingTemplate(rec); - String specID = rec.get(specView.RATING_ID); - - List list = map.computeIfAbsent(template, k -> new ArrayList<>()); - if (specID != null) { - list.add(specID); - } - }); + stream.forEach(rec -> addTemplate(rec, map)); } retVal = map.entrySet().stream() @@ -177,8 +155,22 @@ public Optional retrieveRatingTemplate(String office, String tem return retVal.stream().findFirst(); } + static void addTemplate(Record rec, Map> map) { + try { + RatingTemplate template = buildRatingTemplate(rec); + String specID = rec.get(AV_RATING_SPEC.AV_RATING_SPEC.RATING_ID); + List ratingSpecs = map.computeIfAbsent(template, k -> new ArrayList<>()); + if (specID != null) { + ratingSpecs.add(specID); + } + } catch(RuntimeException ex) { + logger.log(Level.WARNING, ex, () -> + "Error transforming rating template table record into RatingTemplate DTO:\n" + rec); + } + } + - private RatingTemplate buildRatingTemplate(Record queryRecord) { + private static RatingTemplate buildRatingTemplate(Record queryRecord) { String indParameters = queryRecord.get(AV_RATING_TEMPLATE.AV_RATING_TEMPLATE.INDEPENDENT_PARAMETERS); String depParameter = @@ -203,7 +195,7 @@ private RatingTemplate buildRatingTemplate(Record queryRecord) { .build(); } - private List buildParameterSpecs(String indParameters, String ratingMethods) { + private static List buildParameterSpecs(String indParameters, String ratingMethods) { List retVal = new ArrayList<>(); String[] indParams = indParameters.split(","); String[] methodsForParam = ratingMethods.split("/"); diff --git a/cwms-data-api/src/test/java/cwms/cda/api/RatingTemplateControllerTestIT.java b/cwms-data-api/src/test/java/cwms/cda/api/RatingTemplateControllerTestIT.java index 1d84cbc26..7beba8b35 100644 --- a/cwms-data-api/src/test/java/cwms/cda/api/RatingTemplateControllerTestIT.java +++ b/cwms-data-api/src/test/java/cwms/cda/api/RatingTemplateControllerTestIT.java @@ -37,16 +37,16 @@ import javax.servlet.http.HttpServletResponse; import mil.army.usace.hec.cwms.rating.io.xml.RatingSpecXmlFactory; -import org.junit.jupiter.api.RepeatedTest; import org.junit.jupiter.api.Tag; import cwms.cda.data.dao.JooqDao; import cwms.cda.formatters.Formats; +import org.junit.jupiter.api.Test; @Tag("integration") class RatingTemplateControllerTestIT extends DataApiTestIT { - @RepeatedTest(value = 30) + @Test void test_create_read_delete() throws Exception { String locationId = "RatingSpecTest"; String officeId = "SPK"; diff --git a/cwms-data-api/src/test/java/cwms/cda/data/dao/RatingTemplateDaoTest.java b/cwms-data-api/src/test/java/cwms/cda/data/dao/RatingTemplateDaoTest.java index 0fadf90f4..46cc94145 100644 --- a/cwms-data-api/src/test/java/cwms/cda/data/dao/RatingTemplateDaoTest.java +++ b/cwms-data-api/src/test/java/cwms/cda/data/dao/RatingTemplateDaoTest.java @@ -24,26 +24,37 @@ package cwms.cda.data.dao; +import static cwms.cda.data.dao.DaoTest.getConnection; +import static cwms.cda.data.dao.DaoTest.getDslContext; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertTrue; + import cwms.cda.data.dto.rating.RatingTemplate; import hec.data.RatingException; import hec.data.cwmsRating.RatingSet; +import java.io.IOException; +import java.sql.Connection; +import java.sql.SQLException; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Set; import mil.army.usace.hec.cwms.rating.io.jdbc.RatingJdbcFactory; import mil.army.usace.hec.cwms.rating.io.xml.RatingXmlFactory; import org.jooq.DSLContext; +import org.jooq.Field; +import org.jooq.Record; +import org.jooq.SQLDialect; +import org.jooq.impl.DSL; import org.junit.jupiter.api.Disabled; import org.junit.jupiter.api.Test; +import usace.cwms.db.jooq.codegen.tables.AV_RATING_SPEC; +import usace.cwms.db.jooq.codegen.tables.AV_RATING_TEMPLATE; -import java.io.IOException; -import java.sql.Connection; -import java.sql.SQLException; -import java.util.Set; - -import static cwms.cda.data.dao.DaoTest.getConnection; -import static cwms.cda.data.dao.DaoTest.getDslContext; -import static org.junit.jupiter.api.Assertions.*; - -@Disabled class RatingTemplateDaoTest { public static final String OFFICE_ID = "SWT"; @@ -52,7 +63,7 @@ class RatingTemplateDaoTest // This is how the test can be run from a unit test. // Must have existing database and specify the -D connection args // It takes 4.2 seconds to run it this way. - @Test + @Disabled("Needs to move to integration test") void testRetrieveRatingTemplates() throws SQLException { Connection c = getConnection(); @@ -119,5 +130,51 @@ private static void storeRatingSet(Connection c, String resource) throws IOExcep RatingJdbcFactory.store(ratingSet,c, true, true); } + @Test + void testInvalidRatingTemplate() { + DSLContext dsl = DSL.using(SQLDialect.ORACLE); + AV_RATING_TEMPLATE view = AV_RATING_TEMPLATE.AV_RATING_TEMPLATE; + List> fields = new ArrayList<>(Arrays.asList(view.fields())); + fields.add(AV_RATING_SPEC.AV_RATING_SPEC.RATING_ID); + Record rec = dsl.newRecord(fields.toArray(new Field[0])); + rec.set(view.DESCRIPTION, "unit test mock"); + rec.set(view.OFFICE_ID, "SPK"); + rec.set(view.VERSION, "TESTING"); + rec.set(AV_RATING_SPEC.AV_RATING_SPEC.RATING_ID, ""); + + + rec.set(view.TEMPLATE_ID, "Elev;Area.TESTING"); + rec.set(view.DEPENDENT_PARAMETER, "Area"); + rec.set(view.INDEPENDENT_PARAMETERS, "Elev"); + rec.set(view.RATING_METHODS, "NULL,LINEAR,NEAREST"); + Map> map = new HashMap<>(); + RatingTemplateDao.addTemplate(rec, map); + assertFalse(map.isEmpty(), "Valid record should not be skipped"); + + rec.set(view.TEMPLATE_ID, "Stage,Speed-Water Index;Flow.TESTING"); + rec.set(view.DEPENDENT_PARAMETER, "Flow"); + rec.set(view.INDEPENDENT_PARAMETERS, "Stage,Speed-Water Index"); + rec.set(view.RATING_METHODS, "NULL,LINEAR,NEAREST/NULL,LINEAR,NEAREST"); + rec.set(AV_RATING_SPEC.AV_RATING_SPEC.RATING_ID, ""); + map = new HashMap<>(); + RatingTemplateDao.addTemplate(rec, map); + assertFalse(map.isEmpty(), "Valid record should not be skipped"); + + rec.set(view.TEMPLATE_ID, "Elev;Area.TESTING"); + rec.set(view.DEPENDENT_PARAMETER, "Area"); + rec.set(view.INDEPENDENT_PARAMETERS, "Elev"); + rec.set(view.RATING_METHODS, "NULL,LINEAR,NEAREST/NULL,LINEAR,NEAREST"); + map = new HashMap<>(); + RatingTemplateDao.addTemplate(rec, map); + assertTrue(map.isEmpty(), "Invalid record should be skipped"); + + rec.set(view.TEMPLATE_ID, "Stage,Speed-Water Index;Flow.TESTING"); + rec.set(view.DEPENDENT_PARAMETER, "Flow"); + rec.set(view.INDEPENDENT_PARAMETERS, "Stage,Speed-Water Index"); + rec.set(view.RATING_METHODS, "NULL,LINEAR,NEAREST"); + map = new HashMap<>(); + RatingTemplateDao.addTemplate(rec, map); + assertTrue(map.isEmpty(), "Invalid record should be skipped"); + } } \ No newline at end of file