1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.rice.krms.framework;
17
18 import static junit.framework.Assert.assertFalse;
19 import static junit.framework.Assert.assertNotNull;
20 import static junit.framework.Assert.assertTrue;
21
22 import java.util.Arrays;
23 import java.util.Collection;
24 import java.util.Collections;
25 import java.util.HashMap;
26 import java.util.List;
27 import java.util.Map;
28
29 import org.junit.Before;
30 import org.junit.Test;
31 import org.kuali.rice.krms.api.engine.EngineResults;
32 import org.kuali.rice.krms.api.engine.ExecutionOptions;
33 import org.kuali.rice.krms.api.engine.ExecutionFlag;
34 import org.kuali.rice.krms.api.engine.Facts;
35 import org.kuali.rice.krms.api.engine.SelectionCriteria;
36 import org.kuali.rice.krms.api.engine.Term;
37 import org.kuali.rice.krms.api.engine.TermResolver;
38 import org.kuali.rice.krms.api.repository.agenda.AgendaDefinition;
39 import org.kuali.rice.krms.framework.engine.Action;
40 import org.kuali.rice.krms.framework.engine.Agenda;
41 import org.kuali.rice.krms.framework.engine.BasicAgenda;
42 import org.kuali.rice.krms.framework.engine.BasicAgendaTree;
43 import org.kuali.rice.krms.framework.engine.BasicAgendaTreeEntry;
44 import org.kuali.rice.krms.framework.engine.BasicContext;
45 import org.kuali.rice.krms.framework.engine.BasicRule;
46 import org.kuali.rice.krms.framework.engine.CollectionOfComparablesTermBasedProposition;
47 import org.kuali.rice.krms.framework.engine.CollectionOperator;
48 import org.kuali.rice.krms.framework.engine.Context;
49 import org.kuali.rice.krms.framework.engine.ContextProvider;
50 import org.kuali.rice.krms.framework.engine.Proposition;
51 import org.kuali.rice.krms.framework.engine.ProviderBasedEngine;
52 import org.kuali.rice.krms.framework.engine.ResultLogger;
53 import org.kuali.rice.krms.framework.engine.Rule;
54 import org.kuali.rice.krms.framework.engine.expression.ComparisonOperator;
55 import org.kuali.rice.krms.framework.engine.expression.ComparisonOperatorServiceImpl;
56
57 public class CollectionOfComparablesTermBasedPropositionTest {
58 private static final ResultLogger LOG = ResultLogger.getInstance();
59
60 private ComparisonOperator operatorGreaterThan;
61
62 @Before
63 public void setUp() {
64 operatorGreaterThan = ComparisonOperator.GREATER_THAN;
65 operatorGreaterThan.setComparisonOperatorService(ComparisonOperatorServiceImpl.getInstance());
66
67 ActionMock.resetActionsFired();
68 }
69
70 @Test
71 public void allPropositionTest() {
72
73
74
75
76 assertRuleTrue(Arrays.asList(100f, 1000f), CollectionOperator.ALL, operatorGreaterThan, 1f);
77
78 assertRuleTrue(Arrays.asList(100f), CollectionOperator.ALL, operatorGreaterThan, 1f);
79
80 assertRuleTrue(Arrays.<Float>asList(), CollectionOperator.ALL, operatorGreaterThan, 1f);
81
82
83 assertRuleFalse(Arrays.asList(100f, 1000f), CollectionOperator.ALL, operatorGreaterThan, 10000f);
84 assertRuleFalse(Arrays.asList(100f, 1000f), CollectionOperator.ALL, operatorGreaterThan, 500f);
85 assertRuleFalse(Arrays.asList(1000f, 100f), CollectionOperator.ALL, operatorGreaterThan, 500f);
86 assertRuleFalse(Arrays.asList(100f), CollectionOperator.ALL, operatorGreaterThan, 10000f);
87
88 }
89
90 @Test
91 public void oneOrMorePropositionTest() {
92
93
94
95
96 assertRuleTrue(Arrays.asList(100f, 1000f), CollectionOperator.ONE_OR_MORE, operatorGreaterThan, 500f);
97 assertRuleTrue(Arrays.asList(1000f, 100f), CollectionOperator.ONE_OR_MORE, operatorGreaterThan, 500f);
98 assertRuleTrue(Arrays.asList(1000f), CollectionOperator.ONE_OR_MORE, operatorGreaterThan, 500f);
99
100
101 assertRuleFalse(Arrays.asList(1000f, 2000f), CollectionOperator.ONE_OR_MORE, operatorGreaterThan, 5000f);
102 assertRuleFalse(Arrays.asList(2000f, 1000f), CollectionOperator.ONE_OR_MORE, operatorGreaterThan, 5000f);
103 assertRuleFalse(Arrays.asList(1000f), CollectionOperator.ONE_OR_MORE, operatorGreaterThan, 5000f);
104 assertRuleFalse(Arrays.<Float>asList(), CollectionOperator.ONE_OR_MORE, operatorGreaterThan, 5000f);
105
106 }
107
108 @Test
109 public void nonePropositionTest() {
110
111
112
113
114 assertRuleTrue(Arrays.asList(100f, 1000f), CollectionOperator.NONE, operatorGreaterThan, 5000f);
115 assertRuleTrue(Arrays.asList(1000f), CollectionOperator.NONE, operatorGreaterThan, 5000f);
116 assertRuleTrue(Arrays.<Float>asList(), CollectionOperator.NONE, operatorGreaterThan, 5000f);
117
118
119 assertRuleFalse(Arrays.asList(1000f, 7000f), CollectionOperator.NONE, operatorGreaterThan, 5000f);
120 assertRuleFalse(Arrays.asList(7000f, 1000f), CollectionOperator.NONE, operatorGreaterThan, 5000f);
121 assertRuleFalse(Arrays.asList(7000f), CollectionOperator.NONE, operatorGreaterThan, 5000f);
122
123 }
124
125
126
127
128 private void execute(Agenda agenda, TermResolver ... termResolvers) {
129 Map<String, String> contextQualifiers = new HashMap<String, String>();
130 contextQualifiers.put("docTypeName", "Proposal");
131
132 List<TermResolver<?>> testResolvers = Arrays.<TermResolver<?>>asList(termResolvers);
133
134 Context context = new BasicContext(Arrays.asList(agenda), testResolvers);
135 ContextProvider contextProvider = new ManualContextProvider(context);
136
137 SelectionCriteria selectionCriteria = SelectionCriteria.createCriteria(null, contextQualifiers,
138 Collections.singletonMap(AgendaDefinition.Constants.EVENT, "test"));
139
140 ProviderBasedEngine engine = new ProviderBasedEngine();
141 engine.setContextProvider(contextProvider);
142
143
144 ExecutionOptions executionOptions = new ExecutionOptions().setFlag(ExecutionFlag.LOG_EXECUTION, true);
145
146 EngineResults results = engine.execute(selectionCriteria, Facts.EMPTY_FACTS, executionOptions);
147 assertNotNull(results);
148 }
149
150 private void assertRuleTrue(List<Float> termValue,
151 CollectionOperator collectionOper,
152 ComparisonOperator comparisonOper, Float compareValue) {
153 assertRule(termValue, collectionOper, comparisonOper, compareValue, true);
154 }
155
156 private void assertRuleFalse(List<Float> termValue,
157 CollectionOperator collectionOper,
158 ComparisonOperator comparisonOper, Float compareValue) {
159 assertRule(termValue, collectionOper, comparisonOper, compareValue, false);
160 }
161
162 private void assertRule(List<Float> termValue,
163 CollectionOperator collectionOper,
164 ComparisonOperator comparisonOper, Float compareValue, boolean assertTrue) {
165
166 boolean actionFired = executeTestAgenda(termValue, collectionOper, comparisonOper, compareValue).actionFired();
167
168 if (assertTrue) {
169 assertTrue(actionFired);
170 } else {
171 assertFalse(actionFired);
172 }
173
174 ActionMock.resetActionsFired();
175 }
176
177
178
179
180
181
182
183
184
185
186 private ActionMock executeTestAgenda(List<Float> termValue,
187 CollectionOperator collectionOper,
188 ComparisonOperator comparisonOper, Float compareValue) {
189 Term expensesTerm = new Term("expenses");
190 TermResolver<Collection<Float>> expensesResolver = new TermResolverMock<Collection<Float>>(expensesTerm.getName(), termValue);
191
192 Proposition prop1 = new CollectionOfComparablesTermBasedProposition(collectionOper, comparisonOper, expensesTerm, compareValue);
193 ActionMock prop1Action = new ActionMock("prop1Action");
194
195 Rule rule = new BasicRule("ALL", prop1, Collections.<Action>singletonList(prop1Action));
196
197 BasicAgendaTree agendaTree = new BasicAgendaTree(new BasicAgendaTreeEntry(rule));
198 Agenda agenda = new BasicAgenda(Collections.singletonMap(AgendaDefinition.Constants.EVENT, "test"), agendaTree);
199
200 execute(agenda, expensesResolver);
201 return prop1Action;
202 }
203
204 }