|
| 1 | +package org.hisp.dhis.rules |
| 2 | + |
| 3 | +import org.hisp.dhis.rules.api.RuleContextRequirements |
| 4 | +import org.hisp.dhis.rules.api.RuleEngine |
| 5 | +import org.hisp.dhis.rules.engine.RuleEngineAnalyzer |
| 6 | +import org.hisp.dhis.rules.models.Option |
| 7 | +import org.hisp.dhis.rules.models.Rule |
| 8 | +import org.hisp.dhis.rules.models.RuleAction |
| 9 | +import org.hisp.dhis.rules.models.RuleValueType |
| 10 | +import org.hisp.dhis.rules.models.RuleVariableCurrentEvent |
| 11 | +import org.hisp.dhis.rules.models.RuleVariableNewestEvent |
| 12 | +import org.hisp.dhis.rules.models.RuleVariableNewestStageEvent |
| 13 | +import org.hisp.dhis.rules.models.RuleVariablePreviousEvent |
| 14 | +import kotlin.test.Test |
| 15 | +import kotlin.test.assertEquals |
| 16 | +import kotlin.test.assertFalse |
| 17 | +import kotlin.test.assertTrue |
| 18 | + |
| 19 | +class RuleEngineAnalyzerTest { |
| 20 | + |
| 21 | + private fun variable( |
| 22 | + name: String, |
| 23 | + field: String = "field_$name", |
| 24 | + ) = RuleVariableCurrentEvent( |
| 25 | + name = name, |
| 26 | + useCodeForOptionSet = false, |
| 27 | + options = emptyList<Option>(), |
| 28 | + field = field, |
| 29 | + fieldType = RuleValueType.TEXT, |
| 30 | + ) |
| 31 | + |
| 32 | + private fun newestEvent(name: String) = RuleVariableNewestEvent( |
| 33 | + name = name, |
| 34 | + useCodeForOptionSet = false, |
| 35 | + options = emptyList<Option>(), |
| 36 | + field = "field_$name", |
| 37 | + fieldType = RuleValueType.TEXT, |
| 38 | + ) |
| 39 | + |
| 40 | + private fun newestStageEvent(name: String) = RuleVariableNewestStageEvent( |
| 41 | + name = name, |
| 42 | + useCodeForOptionSet = false, |
| 43 | + options = emptyList<Option>(), |
| 44 | + field = "field_$name", |
| 45 | + fieldType = RuleValueType.TEXT, |
| 46 | + programStage = "stage1", |
| 47 | + ) |
| 48 | + |
| 49 | + private fun previousEvent(name: String) = RuleVariablePreviousEvent( |
| 50 | + name = name, |
| 51 | + useCodeForOptionSet = false, |
| 52 | + options = emptyList<Option>(), |
| 53 | + field = "field_$name", |
| 54 | + fieldType = RuleValueType.TEXT, |
| 55 | + ) |
| 56 | + |
| 57 | + private fun rule(condition: String, vararg actionData: String?): Rule { |
| 58 | + val actions = actionData.map { RuleAction(it, "DISPLAYTEXT") } |
| 59 | + return Rule(condition, actions) |
| 60 | + } |
| 61 | + |
| 62 | + // --- needsAllEvents = false cases --- |
| 63 | + |
| 64 | + @Test |
| 65 | + fun emptyRulesAndVariablesReturnsFalse() { |
| 66 | + val result = RuleEngineAnalyzer.analyzeContextRequirements(emptyList(), emptyList()) |
| 67 | + assertEquals(RuleContextRequirements.NONE, result) |
| 68 | + assertFalse(result.needsAllEvents) |
| 69 | + } |
| 70 | + |
| 71 | + @Test |
| 72 | + fun unrelatedRulesAndVariablesReturnFalse() { |
| 73 | + val rules = listOf(rule("#{score} > 5", "#{score} + 1")) |
| 74 | + val variables = listOf(variable("score")) |
| 75 | + val result = RuleEngineAnalyzer.analyzeContextRequirements(rules, variables) |
| 76 | + assertFalse(result.needsAllEvents) |
| 77 | + } |
| 78 | + |
| 79 | + @Test |
| 80 | + fun currentEventVariableDoesNotTriggerAllEvents() { |
| 81 | + val rules = listOf(rule("#{current_val} > 0")) |
| 82 | + val variables = listOf(variable("current_val")) |
| 83 | + assertFalse(RuleEngineAnalyzer.analyzeContextRequirements(rules, variables).needsAllEvents) |
| 84 | + } |
| 85 | + |
| 86 | + // --- needsAllEvents = true via env-var --- |
| 87 | + |
| 88 | + @Test |
| 89 | + fun eventCountEnvVarInConditionTriggersAllEvents() { |
| 90 | + val rules = listOf(rule("V{event_count} > 2")) |
| 91 | + assertTrue(RuleEngineAnalyzer.analyzeContextRequirements(rules, emptyList()).needsAllEvents) |
| 92 | + } |
| 93 | + |
| 94 | + @Test |
| 95 | + fun eventCountEnvVarInActionDataTriggersAllEvents() { |
| 96 | + val rules = listOf(rule("true", "V{event_count}")) |
| 97 | + assertTrue(RuleEngineAnalyzer.analyzeContextRequirements(rules, emptyList()).needsAllEvents) |
| 98 | + } |
| 99 | + |
| 100 | + @Test |
| 101 | + fun unrelatedEnvVarDoesNotTriggerAllEvents() { |
| 102 | + val rules = listOf(rule("V{enrollment_date} > '2020-01-01'")) |
| 103 | + assertFalse(RuleEngineAnalyzer.analyzeContextRequirements(rules, emptyList()).needsAllEvents) |
| 104 | + } |
| 105 | + |
| 106 | + // --- needsAllEvents = true via variable type --- |
| 107 | + |
| 108 | + @Test |
| 109 | + fun newestEventVariableTriggersAllEvents() { |
| 110 | + val rules = listOf(rule("#{weight} > 0")) |
| 111 | + val variables = listOf(newestEvent("weight")) |
| 112 | + assertTrue(RuleEngineAnalyzer.analyzeContextRequirements(rules, variables).needsAllEvents) |
| 113 | + } |
| 114 | + |
| 115 | + @Test |
| 116 | + fun newestStageEventVariableTriggersAllEvents() { |
| 117 | + val rules = listOf(rule("#{bp} > 90")) |
| 118 | + val variables = listOf(newestStageEvent("bp")) |
| 119 | + assertTrue(RuleEngineAnalyzer.analyzeContextRequirements(rules, variables).needsAllEvents) |
| 120 | + } |
| 121 | + |
| 122 | + @Test |
| 123 | + fun previousEventVariableTriggersAllEvents() { |
| 124 | + val rules = listOf(rule("#{prev_weight} < #{weight}")) |
| 125 | + val variables = listOf(previousEvent("prev_weight"), variable("weight")) |
| 126 | + assertTrue(RuleEngineAnalyzer.analyzeContextRequirements(rules, variables).needsAllEvents) |
| 127 | + } |
| 128 | + |
| 129 | + @Test |
| 130 | + fun variableDefinedButNotReferencedDoesNotTrigger() { |
| 131 | + // The variable is a "needsAllEvents" type, but it isn't referenced in any rule |
| 132 | + val rules = listOf(rule("#{other} > 0")) |
| 133 | + val variables = listOf(newestEvent("unreferenced"), variable("other")) |
| 134 | + assertFalse(RuleEngineAnalyzer.analyzeContextRequirements(rules, variables).needsAllEvents) |
| 135 | + } |
| 136 | + |
| 137 | + @Test |
| 138 | + fun newestEventVariableReferencedInActionDataTriggers() { |
| 139 | + val rules = listOf(rule("true", "#{latest_score}")) |
| 140 | + val variables = listOf(newestEvent("latest_score")) |
| 141 | + assertTrue(RuleEngineAnalyzer.analyzeContextRequirements(rules, variables).needsAllEvents) |
| 142 | + } |
| 143 | + |
| 144 | + // --- NONE constant --- |
| 145 | + |
| 146 | + @Test |
| 147 | + fun noneConstantHasNeedsAllEventsFalse() { |
| 148 | + assertFalse(RuleContextRequirements.NONE.needsAllEvents) |
| 149 | + } |
| 150 | + |
| 151 | + // --- RuleEngine interface delegation --- |
| 152 | + |
| 153 | + @Test |
| 154 | + fun ruleEngineAnalyzeContextRequirementsDelegatesCorrectly() { |
| 155 | + val rules = listOf(rule("V{event_count} > 2")) |
| 156 | + assertTrue(RuleEngine.getInstance().analyzeContextRequirements(rules, emptyList()).needsAllEvents) |
| 157 | + } |
| 158 | + |
| 159 | + @Test |
| 160 | + fun ruleEngineAnalyzeContextRequirementsReturnsFalseForUnrelatedContext() { |
| 161 | + val rules = listOf(rule("#{score} > 5")) |
| 162 | + val variables = listOf(variable("score")) |
| 163 | + assertFalse(RuleEngine.getInstance().analyzeContextRequirements(rules, variables).needsAllEvents) |
| 164 | + } |
| 165 | +} |
0 commit comments