View Javadoc

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