Skip to content

Commit e44b85f

Browse files
committed
Better handle tables with no rows.
There was an exception being thrown in getInvolvedTables when the table had no rows. There was no reason for this.
1 parent d1b56fa commit e44b85f

File tree

3 files changed

+54
-33
lines changed

3 files changed

+54
-33
lines changed

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ plugins {
88
}
99

1010
group = 'com.imsweb'
11-
version = '1.5-SNAPSHOT'
11+
version = '1.5'
1212
description = 'Java client library for staging calculations'
1313

1414
println "Starting build using ${Jvm.current()}"

src/main/java/com/imsweb/decisionengine/DecisionEngine.java

Lines changed: 22 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -354,20 +354,18 @@ protected Set<String> getInvolvedTables(Table table, Set<String> tables) {
354354
if (table == null)
355355
return tables;
356356

357-
if (table.getTableRows() == null || table.getTableRows().isEmpty())
358-
throw new IllegalStateException("Table not initialized");
359-
360357
tables.add(table.getId());
361358

362-
for (TableRow tableRow : table.getTableRows()) {
363-
for (Endpoint endpoint : tableRow.getEndpoints()) {
364-
if (endpoint != null && EndpointType.JUMP.equals(endpoint.getType())) {
365-
// if table has already been visited, don't call getInvolvedTables again; otherwise we could have infinite recursion
366-
if (!tables.contains(endpoint.getValue()))
367-
getInvolvedTables(getProvider().getTable(endpoint.getValue()), tables);
359+
if (table.getTableRows() != null)
360+
for (TableRow tableRow : table.getTableRows()) {
361+
for (Endpoint endpoint : tableRow.getEndpoints()) {
362+
if (endpoint != null && EndpointType.JUMP.equals(endpoint.getType())) {
363+
// if table has already been visited, don't call getInvolvedTables again; otherwise we could have infinite recursion
364+
if (!tables.contains(endpoint.getValue()))
365+
getInvolvedTables(getProvider().getTable(endpoint.getValue()), tables);
366+
}
368367
}
369368
}
370-
}
371369

372370
return tables;
373371
}
@@ -602,15 +600,13 @@ public Result process(Definition definition, Map<String, String> context) {
602600

603601
List<? extends Endpoint> endpoints = matchTable(lookup, context);
604602
if (endpoints == null) {
605-
result.addError(new ErrorBuilder(Boolean.TRUE.equals(input.getUsedForStaging()) ? Type.INVALID_REQUIRED_INPUT : Type.INVALID_NON_REQUIRED_INPUT)
606-
.message("Invalid '" + input.getKey() + "' value (" + (value.isEmpty() ? _BLANK_OUTPUT : value) + ")")
607-
.key(input.getKey()).table(input.getTable())
608-
.build());
603+
result.addError(new ErrorBuilder(Boolean.TRUE.equals(input.getUsedForStaging()) ? Type.INVALID_REQUIRED_INPUT : Type.INVALID_NON_REQUIRED_INPUT).message(
604+
"Invalid '" + input.getKey() + "' value (" + (value.isEmpty() ? _BLANK_OUTPUT : value) + ")").key(input.getKey()).table(input.getTable()).build());
609605

610606
// if the schema error handling is set to FAIL or if the input is required for staging and the error handling is set to FAIL_WHEN_REQUIRED_FOR_STAGING,
611607
// then stop processing and return a failure result
612-
if (Definition.StagingInputErrorHandler.FAIL.equals(definition.getOnInvalidInput()) ||
613-
(Boolean.TRUE.equals(input.getUsedForStaging()) && Definition.StagingInputErrorHandler.FAIL_WHEN_USED_FOR_STAGING.equals(definition.getOnInvalidInput())))
608+
if (Definition.StagingInputErrorHandler.FAIL.equals(definition.getOnInvalidInput()) || (Boolean.TRUE.equals(input.getUsedForStaging())
609+
&& Definition.StagingInputErrorHandler.FAIL_WHEN_USED_FOR_STAGING.equals(definition.getOnInvalidInput())))
614610
stopForBadInput = true;
615611
}
616612
}
@@ -661,10 +657,8 @@ public Result process(Definition definition, Map<String, String> context) {
661657
String mapFromKey = key.getFrom();
662658

663659
if (!context.containsKey(mapFromKey)) {
664-
result.addError(new ErrorBuilder(Type.UNKNOWN_INPUT_MAPPING)
665-
.message("Input mapping '" + mapFromKey + "' does not exist for table '" + tableId + "'")
666-
.key(mapFromKey)
667-
.table(tableId).build());
660+
result.addError(new ErrorBuilder(Type.UNKNOWN_INPUT_MAPPING).message("Input mapping '" + mapFromKey + "' does not exist for table '" + tableId + "'").key(
661+
mapFromKey).table(tableId).build());
668662
continue;
669663
}
670664

@@ -715,10 +709,8 @@ else if (output.getTable() != null) {
715709
List<? extends Endpoint> endpoints = matchTable(lookup, context);
716710
if (endpoints == null) {
717711
String value = context.get(output.getKey());
718-
result.addError(new ErrorBuilder(Type.INVALID_OUTPUT)
719-
.message("Invalid '" + output.getKey() + "' value (" + (value.isEmpty() ? _BLANK_OUTPUT : value) + ")")
720-
.key(output.getKey()).table(output.getTable())
721-
.build());
712+
result.addError(new ErrorBuilder(Type.INVALID_OUTPUT).message("Invalid '" + output.getKey() + "' value (" + (value.isEmpty() ? _BLANK_OUTPUT : value) + ")").key(
713+
output.getKey()).table(output.getTable()).build());
722714
}
723715
}
724716
}
@@ -742,17 +734,15 @@ protected boolean process(String mappingId, String tableId, TablePath path, Resu
742734

743735
Table table = getProvider().getTable(tableId);
744736
if (table == null) {
745-
result.addError(new ErrorBuilder(Type.UNKNOWN_TABLE)
746-
.message("The processing of '" + path.getId() + "' contains a reference to an unknown table: '" + tableId + "'")
747-
.table(tableId).build());
737+
result.addError(new ErrorBuilder(Type.UNKNOWN_TABLE).message("The processing of '" + path.getId() + "' contains a reference to an unknown table: '" + tableId + "'").table(tableId)
738+
.build());
748739
return true;
749740
}
750741

751742
// track the path history to make sure no table is reached twice
752743
if (stack.contains(tableId)) {
753-
result.addError(new ErrorBuilder(Type.INFINITE_LOOP)
754-
.message("The processing of '" + path.getId() + "' has entered an infinite recursive state. Table '" + tableId + "' was accessed multiple times.")
755-
.table(tableId).build());
744+
result.addError(new ErrorBuilder(Type.INFINITE_LOOP).message(
745+
"The processing of '" + path.getId() + "' has entered an infinite recursive state. Table '" + tableId + "' was accessed multiple times.").table(tableId).build());
756746
return true;
757747
}
758748

@@ -765,8 +755,8 @@ protected boolean process(String mappingId, String tableId, TablePath path, Resu
765755
// look for the match in the mapping table; if no match is found, used the table-specific no_match value
766756
List<? extends Endpoint> endpoints = matchTable(table, result.getContext());
767757
if (endpoints == null)
768-
result.addError(new ErrorBuilder(Type.MATCH_NOT_FOUND).message("Match not found in table '" + tableId + "' (" + getTableInputsAsString(table, result.getContext()) + ")")
769-
.table(tableId).build());
758+
result.addError(new ErrorBuilder(Type.MATCH_NOT_FOUND).message("Match not found in table '" + tableId + "' (" + getTableInputsAsString(table, result.getContext()) + ")").table(tableId)
759+
.build());
770760
else {
771761
for (Endpoint endpoint : endpoints) {
772762
if (EndpointType.STOP.equals(endpoint.getType()))

src/test/java/com/imsweb/decisionengine/DecisionEngineTest.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -999,6 +999,37 @@ public void testInvolvedTables() {
999999
Assert.assertTrue(tables.contains("table_exclusion1"));
10001000
}
10011001

1002+
@Test
1003+
public void testInvolvedTablesWhenEmpty() {
1004+
BasicDataProvider provider = new BasicDataProvider();
1005+
1006+
// add empty table
1007+
BasicTable table = new BasicTable("table1");
1008+
table.addColumnDefinition("a", ColumnType.INPUT);
1009+
provider.addTable(table);
1010+
1011+
// add another empty table
1012+
table = new BasicTable("table2");
1013+
table.addColumnDefinition("b", ColumnType.INPUT);
1014+
provider.addTable(table);
1015+
1016+
BasicDefinition def = new BasicDefinition("def1");
1017+
def.setOnInvalidInput(Definition.StagingInputErrorHandler.FAIL);
1018+
def.addInput("a");
1019+
def.addInput("b");
1020+
BasicMapping mapping = new BasicMapping("m1");
1021+
mapping.setTablePaths(Arrays.asList(new BasicTablePath("table1"), new BasicTablePath("table2")));
1022+
def.addMapping(mapping);
1023+
provider.addDefinition(def);
1024+
1025+
DecisionEngine engine = new DecisionEngine(provider);
1026+
1027+
Set<String> tables = engine.getInvolvedTables("def1");
1028+
Assert.assertEquals(2, tables.size());
1029+
Assert.assertTrue(tables.contains("table1"));
1030+
Assert.assertTrue(tables.contains("table2"));
1031+
}
1032+
10021033
@Test
10031034
public void testInvolvedTableRecursion() {
10041035
Set<String> tables = _ENGINE.getInvolvedTables("starting_recursion");

0 commit comments

Comments
 (0)