Skip to content

Commit b18b033

Browse files
authored
Merge pull request #123 from imsweb/find-matching-table-row-npe
findMatchingTableRow is not null safe
2 parents dd71da3 + 080fbc5 commit b18b033

File tree

2 files changed

+30
-12
lines changed

2 files changed

+30
-12
lines changed

lib/src/main/java/com/imsweb/staging/engine/DecisionEngine.java

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -130,20 +130,22 @@ public static Integer findMatchingTableRow(Table table, Map<String, String> cont
130130
if (context == null)
131131
throw new IllegalStateException(_CONTEXT_MISSING_MESSAGE);
132132

133-
for (int i = 0; i < table.getTableRows().size(); i++) {
134-
boolean matchAll = true;
135-
for (ColumnDefinition col : table.getColumnDefinitions()) {
136-
if (ColumnType.INPUT.equals(col.getType()) && (keysToMatch == null || keysToMatch.contains(col.getKey())))
137-
matchAll = testMatch(table.getTableRows().get(i).getColumnInput(col.getKey()), context.get(col.getKey()), context);
133+
if (table.getTableRows() != null) {
134+
for (int i = 0; i < table.getTableRows().size(); i++) {
135+
boolean matchAll = true;
136+
for (ColumnDefinition col : table.getColumnDefinitions()) {
137+
if (ColumnType.INPUT.equals(col.getType()) && (keysToMatch == null || keysToMatch.contains(col.getKey())))
138+
matchAll = testMatch(table.getTableRows().get(i).getColumnInput(col.getKey()), context.get(col.getKey()), context);
139+
140+
if (!matchAll)
141+
break;
142+
}
138143

139-
if (!matchAll)
144+
// if all inputs match, we are done
145+
if (matchAll) {
146+
rowIndex = i;
140147
break;
141-
}
142-
143-
// if all inputs match, we are done
144-
if (matchAll) {
145-
rowIndex = i;
146-
break;
148+
}
147149
}
148150
}
149151

lib/src/test/java/com/imsweb/staging/engine/DecisionEngineTest.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -488,6 +488,22 @@ void testMatchOnSpecificKeys() {
488488
.containsExactly(tuple(EndpointType.MATCH, "LINE1", "result"));
489489
}
490490

491+
@Test
492+
void testMatchTableWithNoRows() {
493+
InMemoryDataProvider provider = new InMemoryDataProvider("Test", "1.0");
494+
StagingTable table = new StagingTable("testNoRows");
495+
table.addColumnDefinition("a", ColumnType.INPUT);
496+
table.addColumnDefinition("result", ColumnType.ENDPOINT);
497+
provider.addTable(table);
498+
499+
Table tableMissing = provider.getTable("testNoRows");
500+
501+
Map<String, String> input = new HashMap<>();
502+
input.put("a", "");
503+
List<? extends Endpoint> endpoints = DecisionEngine.matchTable(tableMissing, input);
504+
assertNull(endpoints);
505+
}
506+
491507
@Test
492508
void testMatchMissingVsAll() {
493509
InMemoryDataProvider provider = new InMemoryDataProvider("Test", "1.0");

0 commit comments

Comments
 (0)