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.krad.util;
17  
18  import org.junit.Assert;
19  import org.junit.Test;
20  import org.kuali.rice.krad.datadictionary.state.StateMapping;
21  import org.kuali.rice.krad.datadictionary.state.StateMappingBase;
22  import org.kuali.rice.krad.datadictionary.validation.constraint.BaseConstraint;
23  import org.kuali.rice.krad.datadictionary.validation.constraint.Constraint;
24  import org.kuali.rice.krad.datadictionary.validation.constraint.ValidCharactersConstraint;
25  import org.kuali.rice.krad.uif.util.ConstraintStateUtils;
26  import org.kuali.rice.krad.uif.view.FormView;
27  import org.kuali.rice.krad.uif.view.View;
28  import org.kuali.rice.krad.web.form.UifFormBase;
29  
30  import java.util.ArrayList;
31  import java.util.Arrays;
32  import java.util.HashMap;
33  import java.util.List;
34  import java.util.Map;
35  
36  /**
37   * Tests for constraintStateUtils class to insure correct logic for applicable constraints in state based validation
38   *
39   * @author Kuali Rice Team (rice.collab@kuali.org)
40   */
41  public class ConstraintStateUtilsTest {
42  
43      /**
44       * Testing state based validation utility methods that the service uses to determine applicable constraints
45       */
46      @Test
47      public void testStateValidationUtil() {
48          BaseConstraint genericConstraint = new ValidCharactersConstraint() {};
49          List<String> cStates = new ArrayList<String>();
50          cStates.add("state1");
51          cStates.add("state3>state5");
52          cStates.add("state7+");
53          genericConstraint.setStates(cStates);
54  
55          StateMapping genericStateMapping = new StateMappingBase();
56          List<String> states = new ArrayList<String>();
57          states.add("state1");
58          states.add("state2");
59          states.add("state3");
60          states.add("state4");
61          states.add("state5");
62          states.add("state6");
63          states.add("state7");
64          states.add("state8");
65          genericStateMapping.setStates(states);
66  
67          //constraint applies for state1
68          boolean applies = ConstraintStateUtils.constraintAppliesForState("state1", genericConstraint,
69                  genericStateMapping);
70          Assert.assertTrue(applies);
71          Constraint constraintResult = ConstraintStateUtils.getApplicableConstraint(genericConstraint, "state1",
72                  genericStateMapping);
73          Assert.assertNotNull(constraintResult);
74  
75          //constraint does not apply for state2
76          applies = ConstraintStateUtils.constraintAppliesForState("state2", genericConstraint, genericStateMapping);
77          Assert.assertFalse(applies);
78          constraintResult = ConstraintStateUtils.getApplicableConstraint(genericConstraint, "state2",
79                  genericStateMapping);
80          Assert.assertNull(constraintResult);
81  
82          //constraint applies for state3 through state5
83          applies = ConstraintStateUtils.constraintAppliesForState("state3", genericConstraint, genericStateMapping);
84          Assert.assertTrue(applies);
85          constraintResult = ConstraintStateUtils.getApplicableConstraint(genericConstraint, "state3",
86                  genericStateMapping);
87          Assert.assertNotNull(constraintResult);
88  
89          applies = ConstraintStateUtils.constraintAppliesForState("state4", genericConstraint, genericStateMapping);
90          Assert.assertTrue(applies);
91          constraintResult = ConstraintStateUtils.getApplicableConstraint(genericConstraint, "state4",
92                  genericStateMapping);
93          Assert.assertNotNull(constraintResult);
94  
95          applies = ConstraintStateUtils.constraintAppliesForState("state5", genericConstraint, genericStateMapping);
96          Assert.assertTrue(applies);
97          constraintResult = ConstraintStateUtils.getApplicableConstraint(genericConstraint, "state5",
98                  genericStateMapping);
99          Assert.assertNotNull(constraintResult);
100 
101         //constraint applies for state7 and after
102         applies = ConstraintStateUtils.constraintAppliesForState("state7", genericConstraint, genericStateMapping);
103         Assert.assertTrue(applies);
104         constraintResult = ConstraintStateUtils.getApplicableConstraint(genericConstraint, "state7",
105                 genericStateMapping);
106         Assert.assertNotNull(constraintResult);
107 
108         applies = ConstraintStateUtils.constraintAppliesForState("state8", genericConstraint, genericStateMapping);
109         Assert.assertTrue(applies);
110         constraintResult = ConstraintStateUtils.getApplicableConstraint(genericConstraint, "state8",
111                 genericStateMapping);
112         Assert.assertNotNull(constraintResult);
113 
114         //constraint does not apply for non-listed states or the empty state
115         applies = ConstraintStateUtils.constraintAppliesForState("fake", genericConstraint, genericStateMapping);
116         Assert.assertFalse(applies);
117         constraintResult = ConstraintStateUtils.getApplicableConstraint(genericConstraint, "notListed",
118                 genericStateMapping);
119         Assert.assertNull(constraintResult);
120 
121         //constraint applies if no state (state validation not setup or "stateless")
122         applies = ConstraintStateUtils.constraintAppliesForState(null, genericConstraint, genericStateMapping);
123         Assert.assertTrue(applies);
124         constraintResult = ConstraintStateUtils.getApplicableConstraint(genericConstraint, null, genericStateMapping);
125         Assert.assertNotNull(constraintResult);
126 
127         applies = ConstraintStateUtils.constraintAppliesForState("", genericConstraint, genericStateMapping);
128         Assert.assertTrue(applies);
129         constraintResult = ConstraintStateUtils.getApplicableConstraint(genericConstraint, "", genericStateMapping);
130         Assert.assertNotNull(constraintResult);
131 
132         //applies for stateless situations
133         applies = ConstraintStateUtils.constraintAppliesForState(null, genericConstraint, null);
134         Assert.assertTrue(applies);
135         constraintResult = ConstraintStateUtils.getApplicableConstraint(genericConstraint, null, null);
136         Assert.assertNotNull(constraintResult);
137 
138         //constraint applies if null state mapping (state validation not setup or "stateless")
139         applies = ConstraintStateUtils.constraintAppliesForState("state3", genericConstraint, null);
140         Assert.assertTrue(applies);
141         constraintResult = ConstraintStateUtils.getApplicableConstraint(genericConstraint, "state3", null);
142         Assert.assertNotNull(constraintResult);
143 
144         //throws error if stateMapping does not contain the current state (bad state mapping object)
145         try {
146             applies = ConstraintStateUtils.constraintAppliesForState("state3", genericConstraint,
147                     new StateMappingBase());
148         } catch (Exception e) {
149             Assert.assertNotNull(e);
150         }
151 
152         List<BaseConstraint> constraintStateOverrides = new ArrayList<BaseConstraint>();
153         final List<String> overrideStates = Arrays.asList(new String[]{"state2", "state3"});
154         BaseConstraint override = new ValidCharactersConstraint() {{
155             setStates(overrideStates);
156         }};
157         constraintStateOverrides.add(override);
158 
159         genericConstraint.setConstraintStateOverrides(constraintStateOverrides);
160 
161         //should return back the same constraint
162         constraintResult = ConstraintStateUtils.getApplicableConstraint(genericConstraint, "state1",
163                 genericStateMapping);
164         Assert.assertNotNull(constraintResult);
165         Assert.assertEquals(genericConstraint, constraintResult);
166 
167         //should return back the override (testing common case)
168         constraintResult = ConstraintStateUtils.getApplicableConstraint(genericConstraint, "state3",
169                 genericStateMapping);
170         Assert.assertNotNull(constraintResult);
171         Assert.assertEquals(override, constraintResult);
172 
173         //should return back the override (testing where constraint does not apply but override does)
174         constraintResult = ConstraintStateUtils.getApplicableConstraint(genericConstraint, "state2",
175                 genericStateMapping);
176         Assert.assertNotNull(constraintResult);
177         Assert.assertEquals(override, constraintResult);
178 
179         //should return back the same constraint (testing where overrides are configured but none match
180         //but the constraint is applicable for this state)
181         constraintResult = ConstraintStateUtils.getApplicableConstraint(genericConstraint, "state4",
182                 genericStateMapping);
183         Assert.assertNotNull(constraintResult);
184         Assert.assertEquals(genericConstraint, constraintResult);
185 
186         //should return back null (testing where the constrain overrides and the constraint do not match state)
187         constraintResult = ConstraintStateUtils.getApplicableConstraint(genericConstraint, "state6",
188                 genericStateMapping);
189         Assert.assertNull(constraintResult);
190 
191     }
192 
193     /**
194      * Test for getClientViewValidationState method that returns the state the client should validate for
195      */
196     @Test
197     public void testCustomClientsideUtilMethod() {
198         View view = new FormView();
199         StateMapping genericStateMapping = new StateMappingBase();
200         List<String> states = new ArrayList<String>();
201         states.add("state1");
202         states.add("state2");
203         states.add("state3");
204         states.add("state4");
205         states.add("state5");
206         states.add("state6");
207         states.add("state7");
208         states.add("state8");
209         genericStateMapping.setStates(states);
210         genericStateMapping.setStatePropertyName("state");
211         UifFormBase model = new UifFormBase();
212 
213         Map<String, String> customClientStateMap = new HashMap<String, String>();
214         customClientStateMap.put("state1", "state3");
215         customClientStateMap.put("state4", "state8");
216         customClientStateMap.put("state7", "state7");
217         genericStateMapping.setCustomClientSideValidationStates(customClientStateMap);
218         view.setStateMapping(genericStateMapping);
219 
220         //custom
221         model.setState("state1");
222         String state = ConstraintStateUtils.getClientViewValidationState(model, view);
223         Assert.assertEquals("state3", state);
224         model.setState("state4");
225         state = ConstraintStateUtils.getClientViewValidationState(model, view);
226         Assert.assertEquals("state8", state);
227         model.setState("state7");
228         state = ConstraintStateUtils.getClientViewValidationState(model, view);
229         Assert.assertEquals("state7", state);
230 
231         //should return next state
232         model.setState("state2");
233         state = ConstraintStateUtils.getClientViewValidationState(model, view);
234         Assert.assertEquals("state3", state);
235         model.setState("state3");
236         state = ConstraintStateUtils.getClientViewValidationState(model, view);
237         Assert.assertEquals("state4", state);
238         model.setState("state5");
239         state = ConstraintStateUtils.getClientViewValidationState(model, view);
240         Assert.assertEquals("state6", state);
241 
242         //should return state8 (no next state)
243         model.setState("state8");
244         state = ConstraintStateUtils.getClientViewValidationState(model, view);
245         Assert.assertEquals("state8", state);
246 
247         //should back same state sent in - bad or no configurations, if then used for get applicableConstraint
248         //will get back the appropriate constraint
249         model.setState("fake");
250         state = ConstraintStateUtils.getClientViewValidationState(model, view);
251         Assert.assertEquals("fake", state);
252 
253         model.setState(null);
254         state = ConstraintStateUtils.getClientViewValidationState(model, view);
255         Assert.assertNull(state);
256 
257         model.setState("");
258         state = ConstraintStateUtils.getClientViewValidationState(model, view);
259         Assert.assertEquals("", state);
260 
261         model.setState("state1");
262         StateMapping emptyStateMapping = new StateMappingBase();
263         emptyStateMapping.setStatePropertyName("state");
264         view.setStateMapping(emptyStateMapping);
265         state = ConstraintStateUtils.getClientViewValidationState(model, view);
266         Assert.assertEquals("state1", state);
267 
268         //return back null state when StateMapping is not configured
269         view.setStateMapping(null);
270         state = ConstraintStateUtils.getClientViewValidationState(model, view);
271         Assert.assertNull(state);
272     }
273 }