View Javadoc

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