diff --git a/rs-e2e/src/main/java/gov/hhs/cdc/trustedintermediary/rse2e/ruleengine/AssertionRuleEngine.java b/rs-e2e/src/main/java/gov/hhs/cdc/trustedintermediary/rse2e/ruleengine/AssertionRuleEngine.java index a040e67f9..cf5a41178 100644 --- a/rs-e2e/src/main/java/gov/hhs/cdc/trustedintermediary/rse2e/ruleengine/AssertionRuleEngine.java +++ b/rs-e2e/src/main/java/gov/hhs/cdc/trustedintermediary/rse2e/ruleengine/AssertionRuleEngine.java @@ -9,7 +9,9 @@ import java.io.IOException; import java.io.InputStream; import java.util.ArrayList; +import java.util.HashSet; import java.util.List; +import java.util.Set; import javax.inject.Inject; /** @@ -58,21 +60,28 @@ public void ensureRulesLoaded() throws RuleLoaderException { } } - public void runRules(Message outputMessage, Message inputMessage) { + public List getRules() { + return assertionRules; + } + + public Set runRules(Message outputMessage, Message inputMessage) { try { ensureRulesLoaded(); } catch (RuleLoaderException e) { logger.logError("Failed to load rules definitions", e); - return; + return Set.of(); } HapiHL7Message outputHapiMessage = new HapiHL7Message(outputMessage); HapiHL7Message inputHapiMessage = new HapiHL7Message(inputMessage); + Set runRules = new HashSet<>(); for (AssertionRule rule : assertionRules) { if (rule.shouldRun(outputHapiMessage)) { rule.runRule(outputHapiMessage, inputHapiMessage); + runRules.add(rule); } } + return runRules; } } diff --git a/rs-e2e/src/test/groovy/gov/hhs/cdc/trustedintermediary/rse2e/AutomatedTest.groovy b/rs-e2e/src/test/groovy/gov/hhs/cdc/trustedintermediary/rse2e/AutomatedTest.groovy index 295f84784..73c62db06 100644 --- a/rs-e2e/src/test/groovy/gov/hhs/cdc/trustedintermediary/rse2e/AutomatedTest.groovy +++ b/rs-e2e/src/test/groovy/gov/hhs/cdc/trustedintermediary/rse2e/AutomatedTest.groovy @@ -40,6 +40,8 @@ class AutomatedTest extends Specification { FileFetcher localFileFetcher = LocalFileFetcher.getInstance() localFiles = localFileFetcher.fetchFiles() + + engine.ensureRulesLoaded() } def cleanup() { @@ -50,16 +52,19 @@ class AutomatedTest extends Specification { def "test defined assertions on relevant messages"() { given: + def rulesToEvaluate = engine.getRules() def matchedFiles = fileMatcher.matchFiles(azureFiles, localFiles) when: for (messagePair in matchedFiles) { Message inputMessage = messagePair.getKey() as Message Message outputMessage = messagePair.getValue() as Message - engine.runRules(outputMessage, inputMessage) + def evaluatedRules = engine.runRules(outputMessage, inputMessage) + rulesToEvaluate.removeAll(evaluatedRules) } then: + rulesToEvaluate.collect { it.name }.isEmpty() //Check whether all the rules in the assertions file have been run 0 * mockLogger.logError(_ as String, _ as Exception) 0 * mockLogger.logWarning(_ as String) } diff --git a/rs-e2e/src/test/groovy/gov/hhs/cdc/trustedintermediary/rse2e/ruleengine/AssertionRuleEngineTest.groovy b/rs-e2e/src/test/groovy/gov/hhs/cdc/trustedintermediary/rse2e/ruleengine/AssertionRuleEngineTest.groovy index ce7250ca8..c7582c80e 100644 --- a/rs-e2e/src/test/groovy/gov/hhs/cdc/trustedintermediary/rse2e/ruleengine/AssertionRuleEngineTest.groovy +++ b/rs-e2e/src/test/groovy/gov/hhs/cdc/trustedintermediary/rse2e/ruleengine/AssertionRuleEngineTest.groovy @@ -1,6 +1,7 @@ package gov.hhs.cdc.trustedintermediary.rse2e.ruleengine import ca.uhn.hl7v2.model.Message +import gov.hhs.cdc.trustedintermediary.rse2e.external.hapi.HapiHL7Message import gov.hhs.cdc.trustedintermediary.ruleengine.RuleLoader import gov.hhs.cdc.trustedintermediary.ruleengine.RuleLoaderException import gov.hhs.cdc.trustedintermediary.context.TestApplicationContext @@ -37,7 +38,7 @@ class AssertionRuleEngineTest extends Specification { then: 1 * mockRuleLoader.loadRules(_ as InputStream, _ as TypeReference) >> [mockRule] - ruleEngine.assertionRules.size() == 1 + ruleEngine.getRules().size() == 1 } def "ensureRulesLoaded loads rules only once by default"() { @@ -50,7 +51,7 @@ class AssertionRuleEngineTest extends Specification { then: 1 * mockRuleLoader.loadRules(_ as InputStream, _ as TypeReference) >> [mockRule] - ruleEngine.assertionRules.size() == 1 + ruleEngine.getRules().size() == 1 } def "ensureRulesLoaded loads rules only once on multiple threads"() { @@ -101,4 +102,37 @@ class AssertionRuleEngineTest extends Specification { then: 1 * mockLogger.logError(_ as String, exception) } + + def "runRules returns nothing when there are no rules"() { + when: + def result = ruleEngine.runRules(Mock(Message), Mock(Message)) + + then: + result.isEmpty() + } + + def "runRules returns rules that have been run"() { + given: + def rule = new AssertionRule("testRule", [], []) + mockRuleLoader.loadRules(_ as InputStream, _ as TypeReference) >> [rule] + + when: + def result = ruleEngine.runRules(Mock(Message), Mock(Message)) + + then: + result.size() == 1 + result[0] == rule + } + + def "runRules doesn't return rules that shouldn't run"() { + given: + def rule = new AssertionRule("testRule", ["false"], []) + mockRuleLoader.loadRules(_ as InputStream, _ as TypeReference) >> [rule] + + when: + def result = ruleEngine.runRules(Mock(Message), Mock(Message)) + + then: + result.isEmpty() + } }