View Javadoc

1   /**
2    * Copyright 2005-2012 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.impl.validation;
17  
18  import org.joda.time.DateTime;
19  import org.junit.Before;
20  import org.junit.Test;
21  import org.kuali.rice.krms.api.engine.ExecutionEnvironment;
22  import org.kuali.rice.krms.api.engine.ExecutionFlag;
23  import org.kuali.rice.krms.api.engine.ExecutionOptions;
24  import org.kuali.rice.krms.api.engine.SelectionCriteria;
25  import org.kuali.rice.krms.api.engine.Term;
26  import org.kuali.rice.krms.api.engine.TermResolutionEngine;
27  import org.kuali.rice.krms.api.repository.agenda.AgendaDefinition;
28  import org.kuali.rice.krms.framework.engine.Action;
29  import org.kuali.rice.krms.framework.engine.BasicExecutionEnvironment;
30  import org.kuali.rice.krms.framework.engine.ComparableTermBasedProposition;
31  import org.kuali.rice.krms.framework.engine.Rule;
32  import org.kuali.rice.krms.framework.engine.TermResolutionEngineImpl;
33  import org.kuali.rice.krms.framework.engine.expression.ComparisonOperator;
34  import org.kuali.rice.krms.framework.type.ValidationRuleType;
35  
36  import java.util.Collections;
37  import java.util.HashMap;
38  import java.util.HashSet;
39  import java.util.Map;
40  import java.util.Set;
41  
42  import static junit.framework.Assert.assertFalse;
43  import static junit.framework.Assert.assertTrue;
44  
45  /**
46   *  Test of the @{link ValidationRule}
47   *
48   * @author Kuali Rice Team (rice.collab@kuali.org)
49   */
50  public class ValidationRuleTest {
51      Term term = new Term("true");
52      final SelectionCriteria testEvent = SelectionCriteria.createCriteria(new DateTime(),
53              Collections.EMPTY_MAP, Collections.singletonMap(AgendaDefinition.Constants.EVENT, "testEvent"));
54      Map<Term, Object> facts = new HashMap<Term, Object>();
55      TermResolutionEngine termResolutionEngine = new TermResolutionEngineImpl();
56  	// Set execution options to log execution
57      ExecutionOptions executionOptions = new ExecutionOptions().setFlag(ExecutionFlag.LOG_EXECUTION, true);
58      private ActionMock actionMock;
59  
60      @Before
61      public void setUp() {
62          actionMock = new ActionMock("a1");
63          actionMock.resetActionsFired();
64          facts.put(term, "true");
65      }
66  
67      @Test
68      public void testValidRulePassesActionDoesntFire() {
69          Rule validationRule = new ValidationRule(ValidationRuleType.VALID, "testValidRulePassesActionDoesntFire",
70                  new ComparableTermBasedProposition(ComparisonOperator.EQUALS, term, "true"), Collections
71                  .<Action>singletonList(actionMock));
72          assertTrue(validationRule.evaluate(new BasicExecutionEnvironment(testEvent, facts, executionOptions,
73                  termResolutionEngine)));
74          assertFalse(actionMock.actionFired("a1"));
75      }
76  
77      @Test
78      public void testValidRuleFailsActionFires() {
79          Rule validationRule = new ValidationRule(ValidationRuleType.VALID, "testValidRuleFailsActionFires",
80                  new ComparableTermBasedProposition(ComparisonOperator.EQUALS, term, "false"), Collections
81                  .<Action>singletonList(actionMock));
82          assertFalse(validationRule.evaluate(new BasicExecutionEnvironment(testEvent, facts, executionOptions,
83                  termResolutionEngine)));
84          assertTrue(actionMock.actionFired("a1"));
85      }
86  
87      @Test
88      public void testInvalidRulePassesActionFires() {
89          Rule validationRule = new ValidationRule(ValidationRuleType.INVALID, "testInvalidRulePassesActionFires",
90                  new ComparableTermBasedProposition(ComparisonOperator.EQUALS, term, "true"), Collections
91                  .<Action>singletonList(actionMock));
92          assertTrue(validationRule.evaluate(new BasicExecutionEnvironment(testEvent, facts, executionOptions,
93                  termResolutionEngine)));
94          assertTrue(actionMock.actionFired("a1"));
95      }
96  
97      @Test
98      public void testInvalidRuleFalseActionDoesntFire() {
99          Rule validationRule = new ValidationRule(ValidationRuleType.INVALID, "testInvalidRuleFalseActionDoesntFire",
100                 new ComparableTermBasedProposition(ComparisonOperator.EQUALS, term, "false"), Collections
101                 .<Action>singletonList(actionMock));
102         assertFalse(validationRule.evaluate(new BasicExecutionEnvironment(testEvent, facts, executionOptions,
103                 termResolutionEngine)));
104         assertFalse(actionMock.actionFired("a1"));
105     }
106 
107     // Copy of org.kuali.rice.krms.framework.ActionMock IntelliJ couldn't find to add dependency
108     private class ActionMock implements Action {
109 
110         private final Set<String> actionsFired = new HashSet<String>();
111 
112         public void resetActionsFired() {
113             actionsFired.clear();
114         }
115 
116         public boolean actionFired(String name) {
117             return actionsFired.contains(name);
118         }
119 
120         public ActionMock(String name) {
121             this.name = name;
122         }
123 
124         private String name;
125 
126         @Override
127         public void execute(ExecutionEnvironment environment) {
128             actionsFired.add(name);
129         }
130 
131         @Override
132         public void executeSimulation(ExecutionEnvironment environment) {
133             throw new UnsupportedOperationException();
134         }
135 
136         public boolean actionFired() {
137             return actionFired(name);
138         }
139     }
140 }