1
2
3
4
5
6
7
8
9
10
11
12
13
14
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.View;
27 import org.kuali.rice.krad.web.form.UifFormBase;
28
29 import java.util.ArrayList;
30 import java.util.Arrays;
31 import java.util.HashMap;
32 import java.util.List;
33 import java.util.Map;
34
35
36
37
38
39
40 public class ConstraintStateUtilsTest {
41
42
43
44
45 @Test
46 public void testStateValidationUtil() {
47 BaseConstraint genericConstraint = new ValidCharactersConstraint() {};
48 List<String> cStates = new ArrayList<String>();
49 cStates.add("state1");
50 cStates.add("state3>state5");
51 cStates.add("state7+");
52 genericConstraint.setStates(cStates);
53
54 StateMapping genericStateMapping = new StateMappingBase();
55 List<String> states = new ArrayList<String>();
56 states.add("state1");
57 states.add("state2");
58 states.add("state3");
59 states.add("state4");
60 states.add("state5");
61 states.add("state6");
62 states.add("state7");
63 states.add("state8");
64 genericStateMapping.setStates(states);
65
66
67 boolean applies = ConstraintStateUtils.constraintAppliesForState("state1", genericConstraint,
68 genericStateMapping);
69 Assert.assertTrue(applies);
70 Constraint constraintResult = ConstraintStateUtils.getApplicableConstraint(genericConstraint, "state1",
71 genericStateMapping);
72 Assert.assertNotNull(constraintResult);
73
74
75 applies = ConstraintStateUtils.constraintAppliesForState("state2", genericConstraint, genericStateMapping);
76 Assert.assertFalse(applies);
77 constraintResult = ConstraintStateUtils.getApplicableConstraint(genericConstraint, "state2",
78 genericStateMapping);
79 Assert.assertNull(constraintResult);
80
81
82 applies = ConstraintStateUtils.constraintAppliesForState("state3", genericConstraint, genericStateMapping);
83 Assert.assertTrue(applies);
84 constraintResult = ConstraintStateUtils.getApplicableConstraint(genericConstraint, "state3",
85 genericStateMapping);
86 Assert.assertNotNull(constraintResult);
87
88 applies = ConstraintStateUtils.constraintAppliesForState("state4", genericConstraint, genericStateMapping);
89 Assert.assertTrue(applies);
90 constraintResult = ConstraintStateUtils.getApplicableConstraint(genericConstraint, "state4",
91 genericStateMapping);
92 Assert.assertNotNull(constraintResult);
93
94 applies = ConstraintStateUtils.constraintAppliesForState("state5", genericConstraint, genericStateMapping);
95 Assert.assertTrue(applies);
96 constraintResult = ConstraintStateUtils.getApplicableConstraint(genericConstraint, "state5",
97 genericStateMapping);
98 Assert.assertNotNull(constraintResult);
99
100
101 applies = ConstraintStateUtils.constraintAppliesForState("state7", genericConstraint, genericStateMapping);
102 Assert.assertTrue(applies);
103 constraintResult = ConstraintStateUtils.getApplicableConstraint(genericConstraint, "state7",
104 genericStateMapping);
105 Assert.assertNotNull(constraintResult);
106
107 applies = ConstraintStateUtils.constraintAppliesForState("state8", genericConstraint, genericStateMapping);
108 Assert.assertTrue(applies);
109 constraintResult = ConstraintStateUtils.getApplicableConstraint(genericConstraint, "state8",
110 genericStateMapping);
111 Assert.assertNotNull(constraintResult);
112
113
114 applies = ConstraintStateUtils.constraintAppliesForState("fake", genericConstraint, genericStateMapping);
115 Assert.assertFalse(applies);
116 constraintResult = ConstraintStateUtils.getApplicableConstraint(genericConstraint, "notListed",
117 genericStateMapping);
118 Assert.assertNull(constraintResult);
119
120
121 applies = ConstraintStateUtils.constraintAppliesForState(null, genericConstraint, genericStateMapping);
122 Assert.assertTrue(applies);
123 constraintResult = ConstraintStateUtils.getApplicableConstraint(genericConstraint, null, genericStateMapping);
124 Assert.assertNotNull(constraintResult);
125
126 applies = ConstraintStateUtils.constraintAppliesForState("", genericConstraint, genericStateMapping);
127 Assert.assertTrue(applies);
128 constraintResult = ConstraintStateUtils.getApplicableConstraint(genericConstraint, "", genericStateMapping);
129 Assert.assertNotNull(constraintResult);
130
131
132 applies = ConstraintStateUtils.constraintAppliesForState(null, genericConstraint, null);
133 Assert.assertTrue(applies);
134 constraintResult = ConstraintStateUtils.getApplicableConstraint(genericConstraint, null, null);
135 Assert.assertNotNull(constraintResult);
136
137
138 applies = ConstraintStateUtils.constraintAppliesForState("state3", genericConstraint, null);
139 Assert.assertTrue(applies);
140 constraintResult = ConstraintStateUtils.getApplicableConstraint(genericConstraint, "state3", null);
141 Assert.assertNotNull(constraintResult);
142
143
144 try {
145 applies = ConstraintStateUtils.constraintAppliesForState("state3", genericConstraint,
146 new StateMappingBase());
147 } catch (Exception e) {
148 Assert.assertNotNull(e);
149 }
150
151 List<BaseConstraint> constraintStateOverrides = new ArrayList<BaseConstraint>();
152 final List<String> overrideStates = Arrays.asList(new String[]{"state2", "state3"});
153 BaseConstraint override = new ValidCharactersConstraint() {{
154 setStates(overrideStates);
155 }};
156 constraintStateOverrides.add(override);
157
158 genericConstraint.setConstraintStateOverrides(constraintStateOverrides);
159
160
161 constraintResult = ConstraintStateUtils.getApplicableConstraint(genericConstraint, "state1",
162 genericStateMapping);
163 Assert.assertNotNull(constraintResult);
164 Assert.assertEquals(genericConstraint, constraintResult);
165
166
167 constraintResult = ConstraintStateUtils.getApplicableConstraint(genericConstraint, "state3",
168 genericStateMapping);
169 Assert.assertNotNull(constraintResult);
170 Assert.assertEquals(override, constraintResult);
171
172
173 constraintResult = ConstraintStateUtils.getApplicableConstraint(genericConstraint, "state2",
174 genericStateMapping);
175 Assert.assertNotNull(constraintResult);
176 Assert.assertEquals(override, constraintResult);
177
178
179
180 constraintResult = ConstraintStateUtils.getApplicableConstraint(genericConstraint, "state4",
181 genericStateMapping);
182 Assert.assertNotNull(constraintResult);
183 Assert.assertEquals(genericConstraint, constraintResult);
184
185
186 constraintResult = ConstraintStateUtils.getApplicableConstraint(genericConstraint, "state6",
187 genericStateMapping);
188 Assert.assertNull(constraintResult);
189
190 }
191
192
193
194
195 @Test
196 public void testCustomClientsideUtilMethod() {
197 View view = new View();
198 StateMapping genericStateMapping = new StateMappingBase();
199 List<String> states = new ArrayList<String>();
200 states.add("state1");
201 states.add("state2");
202 states.add("state3");
203 states.add("state4");
204 states.add("state5");
205 states.add("state6");
206 states.add("state7");
207 states.add("state8");
208 genericStateMapping.setStates(states);
209 genericStateMapping.setStatePropertyName("state");
210 UifFormBase model = new UifFormBase();
211
212 Map<String, String> customClientStateMap = new HashMap<String, String>();
213 customClientStateMap.put("state1", "state3");
214 customClientStateMap.put("state4", "state8");
215 customClientStateMap.put("state7", "state7");
216 genericStateMapping.setCustomClientSideValidationStates(customClientStateMap);
217 view.setStateMapping(genericStateMapping);
218
219
220 model.setState("state1");
221 String state = ConstraintStateUtils.getClientViewValidationState(model, view);
222 Assert.assertEquals("state3", state);
223 model.setState("state4");
224 state = ConstraintStateUtils.getClientViewValidationState(model, view);
225 Assert.assertEquals("state8", state);
226 model.setState("state7");
227 state = ConstraintStateUtils.getClientViewValidationState(model, view);
228 Assert.assertEquals("state7", state);
229
230
231 model.setState("state2");
232 state = ConstraintStateUtils.getClientViewValidationState(model, view);
233 Assert.assertEquals("state3", state);
234 model.setState("state3");
235 state = ConstraintStateUtils.getClientViewValidationState(model, view);
236 Assert.assertEquals("state4", state);
237 model.setState("state5");
238 state = ConstraintStateUtils.getClientViewValidationState(model, view);
239 Assert.assertEquals("state6", state);
240
241
242 model.setState("state8");
243 state = ConstraintStateUtils.getClientViewValidationState(model, view);
244 Assert.assertEquals("state8", state);
245
246
247
248 model.setState("fake");
249 state = ConstraintStateUtils.getClientViewValidationState(model, view);
250 Assert.assertEquals("fake", state);
251
252 model.setState(null);
253 state = ConstraintStateUtils.getClientViewValidationState(model, view);
254 Assert.assertNull(state);
255
256 model.setState("");
257 state = ConstraintStateUtils.getClientViewValidationState(model, view);
258 Assert.assertEquals("", state);
259
260 model.setState("state1");
261 StateMapping emptyStateMapping = new StateMappingBase();
262 emptyStateMapping.setStatePropertyName("state");
263 view.setStateMapping(emptyStateMapping);
264 state = ConstraintStateUtils.getClientViewValidationState(model, view);
265 Assert.assertEquals("state1", state);
266
267
268 view.setStateMapping(null);
269 state = ConstraintStateUtils.getClientViewValidationState(model, view);
270 Assert.assertNull(state);
271 }
272 }