View Javadoc

1   /**
2    * Copyright 2005-2011 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
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.ArrayList;
23  import java.util.Arrays;
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.AgendaTreeEntry;
42  import org.kuali.rice.krms.framework.engine.BasicAgenda;
43  import org.kuali.rice.krms.framework.engine.BasicAgendaTree;
44  import org.kuali.rice.krms.framework.engine.BasicAgendaTreeEntry;
45  import org.kuali.rice.krms.framework.engine.BasicContext;
46  import org.kuali.rice.krms.framework.engine.BasicRule;
47  import org.kuali.rice.krms.framework.engine.ComparableTermBasedProposition;
48  import org.kuali.rice.krms.framework.engine.expression.ComparisonOperator;
49  import org.kuali.rice.krms.framework.engine.Context;
50  import org.kuali.rice.krms.framework.engine.ContextProvider;
51  import org.kuali.rice.krms.framework.engine.Proposition;
52  import org.kuali.rice.krms.framework.engine.ProviderBasedEngine;
53  import org.kuali.rice.krms.framework.engine.ResultLogger;
54  import org.kuali.rice.krms.framework.engine.Rule;
55  
56  public class AgendaTest {
57  	private static final ResultLogger LOG = ResultLogger.getInstance();
58  
59  	@Before
60  	public void setUp() {
61  		ActionMock.resetActionsFired();
62  	}
63  
64  	// totalCostTerm will resolve to the Integer value 5
65  	private Proposition trueProp = new ComparableTermBasedProposition(ComparisonOperator.GREATER_THAN, totalCostTerm, Integer.valueOf(1));
66  	private Proposition falseProp = new ComparableTermBasedProposition(ComparisonOperator.GREATER_THAN, totalCostTerm, Integer.valueOf(1000));
67  	
68  	@Test
69  	public void testAllRulesAgenda() {
70  
71  		Rule rule1 = new BasicRule("r1", trueProp, Collections.<Action>singletonList(new ActionMock("a1")));
72  		Rule rule2 = new BasicRule("r2", falseProp, Collections.<Action>singletonList(new ActionMock("a2")));
73  		Rule rule3 = new BasicRule("r3", trueProp, Collections.<Action>singletonList(new ActionMock("a3")));
74  		
75  		AgendaTreeEntry entry1 = new BasicAgendaTreeEntry(rule1);
76  		AgendaTreeEntry entry2 = new BasicAgendaTreeEntry(rule2);
77  		AgendaTreeEntry entry3 = new BasicAgendaTreeEntry(rule3);
78  		BasicAgendaTree agendaTree = new BasicAgendaTree(entry1, entry2, entry3); 
79  		Agenda agenda = new BasicAgenda(Collections.singletonMap(AgendaDefinition.Constants.EVENT, "test"), agendaTree);
80  		
81  		execute(agenda);
82  
83  		assertTrue(ActionMock.actionFired("a1"));
84  		assertFalse(ActionMock.actionFired("a2"));
85  		assertTrue(ActionMock.actionFired("a3"));
86  	}
87  	
88  	@Test
89  	public void testIfTrueSubAgenda() {
90  
91  		Rule rule1 = new BasicRule("r1", trueProp, Collections.<Action>singletonList(new ActionMock("a1")));
92  		Rule rule2 = new BasicRule("r2", falseProp, Collections.<Action>singletonList(new ActionMock("a2")));
93  		Rule subRule1 = new BasicRule("r1s1", trueProp, Collections.<Action>singletonList(new ActionMock("a3")));
94  		
95  		BasicAgendaTree subAgendaTree1 = new BasicAgendaTree(new BasicAgendaTreeEntry(subRule1));
96  		BasicAgendaTree agendaTree1 = new BasicAgendaTree(new BasicAgendaTreeEntry(rule1, subAgendaTree1, null)); 
97  		Agenda agenda1 = new BasicAgenda(Collections.singletonMap(AgendaDefinition.Constants.EVENT, "test"), agendaTree1);
98  		
99  		execute(agenda1);
100 
101 		assertTrue(ActionMock.actionFired("a1"));
102 		assertTrue(ActionMock.actionFired("a3"));
103 		
104 		// RESET
105 		ActionMock.resetActionsFired();
106 		
107 		BasicAgendaTree subAgendaTree2 = new BasicAgendaTree(new BasicAgendaTreeEntry(subRule1));
108 		BasicAgendaTree agendaTree2 = new BasicAgendaTree(new BasicAgendaTreeEntry(rule2, subAgendaTree2, null)); 
109 		Agenda agenda2 = new BasicAgenda(Collections.singletonMap(AgendaDefinition.Constants.EVENT, "test"), agendaTree2);
110 		
111 		execute(agenda2);
112 
113 		assertFalse(ActionMock.actionFired("a2"));
114 		assertFalse(ActionMock.actionFired("a3"));
115 	}
116 
117 	@Test
118 	public void testIfFalseSubAgenda() {
119 
120 		Rule rule1 = new BasicRule("r1", trueProp, Collections.<Action>singletonList(new ActionMock("a1")));
121 		Rule rule2 = new BasicRule("r2", falseProp, Collections.<Action>singletonList(new ActionMock("a2")));
122 		Rule subRule1 = new BasicRule("r1s1", trueProp, Collections.<Action>singletonList(new ActionMock("a3")));
123 		
124 		BasicAgendaTree subAgendaTree1 = new BasicAgendaTree(new BasicAgendaTreeEntry(subRule1));
125 		BasicAgendaTree agendaTree1 = new BasicAgendaTree(new BasicAgendaTreeEntry(rule1, null, subAgendaTree1)); 
126 		Agenda agenda1 = new BasicAgenda(Collections.singletonMap(AgendaDefinition.Constants.EVENT, "test"), agendaTree1);
127 		
128 		execute(agenda1);
129 
130 		assertTrue(ActionMock.actionFired("a1"));
131 		assertFalse(ActionMock.actionFired("a3"));
132 		
133 		// RESET
134 		ActionMock.resetActionsFired();
135 		
136 		BasicAgendaTree subAgendaTree2 = new BasicAgendaTree(new BasicAgendaTreeEntry(subRule1));
137 		BasicAgendaTree agendaTree2 = new BasicAgendaTree(new BasicAgendaTreeEntry(rule2, null, subAgendaTree2)); 
138 		Agenda agenda2 = new BasicAgenda(Collections.singletonMap(AgendaDefinition.Constants.EVENT, "test"), agendaTree2);
139 		
140 		execute(agenda2);
141 
142 		assertFalse(ActionMock.actionFired("a2"));
143 		assertTrue(ActionMock.actionFired("a3"));
144 	}
145 	
146 	@Test
147 	public void testAfterAgenda() {
148 
149 		Rule rule1 = new BasicRule("r1", trueProp, Collections.<Action>singletonList(new ActionMock("a1")));
150 		Rule rule2 = new BasicRule("r2", falseProp, Collections.<Action>singletonList(new ActionMock("a2")));
151 		Rule subRule1 = new BasicRule("r1s1", trueProp, Collections.<Action>singletonList(new ActionMock("a3")));
152 		
153 		BasicAgendaTree agendaTree1 = new BasicAgendaTree(new BasicAgendaTreeEntry(rule1), new BasicAgendaTreeEntry(subRule1)); 
154 		Agenda agenda1 = new BasicAgenda(Collections.singletonMap(AgendaDefinition.Constants.EVENT, "test"), agendaTree1);
155 		
156 		execute(agenda1);
157 
158 		assertTrue(ActionMock.actionFired("a1"));
159 		assertTrue(ActionMock.actionFired("a3"));
160 		
161 		// RESET
162 		ActionMock.resetActionsFired();
163 		
164 		BasicAgendaTree agendaTree2 = new BasicAgendaTree(new BasicAgendaTreeEntry(rule2), new BasicAgendaTreeEntry(subRule1)); 
165 		Agenda agenda2 = new BasicAgenda(Collections.singletonMap(AgendaDefinition.Constants.EVENT, "test"), agendaTree2);
166 		
167 		execute(agenda2);
168 
169 		assertFalse(ActionMock.actionFired("a2"));
170 		assertTrue(ActionMock.actionFired("a3"));
171 	}
172 
173     /**
174      * Make sure agenda qualifier matching is based on the provided qualifiers
175      * see https://jira.kuali.org/browse/KULRICE-6098
176      */
177     public void testQualifierMissingFromAgenda() {
178         // RESET
179 		ActionMock.resetActionsFired();
180 
181         Rule rule1 = new BasicRule("r1", trueProp, Collections.<Action>singletonList(new ActionMock("a1")));
182 
183         BasicAgendaTree agendaTree1 = new BasicAgendaTree(new BasicAgendaTreeEntry(rule1));
184         Agenda agenda1 = new BasicAgenda(Collections.<String, String>emptyMap(), agendaTree1);
185 
186         // this shouldn't select any agendas, so no rules will really get executed
187         execute(agenda1, Collections.singletonMap(AgendaDefinition.Constants.EVENT, "test"));
188 
189         // Expected: the agenda didn't get selected, so the action didn't fire
190         assertFalse("the agenda should not have been selected and executed", ActionMock.actionFired("a1"));
191     }
192 
193     /**
194      * execute the engine against a trivial context containing the given agenda.
195      * a default agenda qualifier of Event=test will be used.
196      * @param agenda
197      */
198     private void execute(Agenda agenda) {
199         execute(agenda, Collections.singletonMap(AgendaDefinition.Constants.EVENT, "test"));
200     }
201 
202     /**
203      * execute the engine against a trivial context containing the given agenda.
204      * the given agenda qualifier will be used.
205      * @param agenda
206      * @param agendaQualifiers
207      */
208 	private void execute(Agenda agenda, Map<String, String> agendaQualifiers) {
209 		Map<String, String> contextQualifiers = new HashMap<String, String>();
210 		contextQualifiers.put("docTypeName", "Proposal");
211 
212 		List<TermResolver<?>> testResolvers = new ArrayList<TermResolver<?>>();
213 		testResolvers.add(testResolver);
214 		
215 		Context context = new BasicContext(Arrays.asList(agenda), testResolvers);
216 		ContextProvider contextProvider = new ManualContextProvider(context);
217 		
218 		SelectionCriteria selectionCriteria = SelectionCriteria.createCriteria(null, contextQualifiers,
219                 agendaQualifiers);
220 		
221 		ProviderBasedEngine engine = new ProviderBasedEngine();
222 		engine.setContextProvider(contextProvider);
223 
224 		// Set execution options to log execution
225 		ExecutionOptions executionOptions = new ExecutionOptions().setFlag(ExecutionFlag.LOG_EXECUTION, true);
226 		
227 		EngineResults results = engine.execute(selectionCriteria, Facts.EMPTY_FACTS, executionOptions);
228 		assertNotNull(results);
229 	}
230 	
231 	private static final Term totalCostTerm = new Term("totalCost");
232 	
233 	private static final TermResolver<Integer> testResolver = new TermResolverMock<Integer>(totalCostTerm.getName(), 10);
234 }