Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

skip rating templates that are invalid in the database #948

Merged
merged 2 commits into from
Nov 13, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -95,25 +95,11 @@ public Set<RatingTemplate> retrieveRatingTemplates(String office, String templat
}

@NotNull
private Set<RatingTemplate> buildRatingTemplateSet(ResultQuery<? extends Record> query) {

TableField<usace.cwms.db.jooq.codegen.tables.records.AV_RATING_SPEC, String> idField =
AV_RATING_SPEC.AV_RATING_SPEC.RATING_ID;

private static Set<RatingTemplate> buildRatingTemplateSet(ResultQuery<? extends Record> query) {
Map<RatingTemplate, List<String>> map = new LinkedHashMap<>();

try (Stream<? extends Record> stream = query.fetchStream()) {
stream.forEach(rec -> {
RatingTemplate template = buildRatingTemplate(rec);
String specID = rec.get(idField);

List<String> 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())
Expand Down Expand Up @@ -151,15 +137,7 @@ public Optional<RatingTemplate> retrieveRatingTemplate(String office, String tem
Map<RatingTemplate, List<String>> map = new LinkedHashMap<>();

try (Stream<? extends Record> stream = query.fetchStream()) {
stream.forEach(rec -> {
RatingTemplate template = buildRatingTemplate(rec);
String specID = rec.get(specView.RATING_ID);

List<String> list = map.computeIfAbsent(template, k -> new ArrayList<>());
if (specID != null) {
list.add(specID);
}
});
stream.forEach(rec -> addTemplate(rec, map));
}

retVal = map.entrySet().stream()
Expand All @@ -177,8 +155,22 @@ public Optional<RatingTemplate> retrieveRatingTemplate(String office, String tem
return retVal.stream().findFirst();
}

static void addTemplate(Record rec, Map<RatingTemplate, List<String>> map) {
try {
RatingTemplate template = buildRatingTemplate(rec);
String specID = rec.get(AV_RATING_SPEC.AV_RATING_SPEC.RATING_ID);
if (specID != null) {
map.computeIfAbsent(template, k -> new ArrayList<>())
.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 =
Expand All @@ -203,7 +195,7 @@ private RatingTemplate buildRatingTemplate(Record queryRecord) {
.build();
}

private List<ParameterSpec> buildParameterSpecs(String indParameters, String ratingMethods) {
private static List<ParameterSpec> buildParameterSpecs(String indParameters, String ratingMethods) {
List<ParameterSpec> retVal = new ArrayList<>();
String[] indParams = indParameters.split(",");
String[] methodsForParam = ratingMethods.split("/");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -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();
Expand Down Expand Up @@ -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<Field<?>> 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<RatingTemplate, List<String>> 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");
}

}
Loading