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.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
38
39
40
41 public class ConstraintStateUtilsTest {
42
43
44
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
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
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
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
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
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
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
133 applies = ConstraintStateUtils.constraintAppliesForState(null, genericConstraint, null);
134 Assert.assertTrue(applies);
135 constraintResult = ConstraintStateUtils.getApplicableConstraint(genericConstraint, null, null);
136 Assert.assertNotNull(constraintResult);
137
138
139 applies = ConstraintStateUtils.constraintAppliesForState("state3", genericConstraint, null);
140 Assert.assertTrue(applies);
141 constraintResult = ConstraintStateUtils.getApplicableConstraint(genericConstraint, "state3", null);
142 Assert.assertNotNull(constraintResult);
143
144
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
162 constraintResult = ConstraintStateUtils.getApplicableConstraint(genericConstraint, "state1",
163 genericStateMapping);
164 Assert.assertNotNull(constraintResult);
165 Assert.assertEquals(genericConstraint, constraintResult);
166
167
168 constraintResult = ConstraintStateUtils.getApplicableConstraint(genericConstraint, "state3",
169 genericStateMapping);
170 Assert.assertNotNull(constraintResult);
171 Assert.assertEquals(override, constraintResult);
172
173
174 constraintResult = ConstraintStateUtils.getApplicableConstraint(genericConstraint, "state2",
175 genericStateMapping);
176 Assert.assertNotNull(constraintResult);
177 Assert.assertEquals(override, constraintResult);
178
179
180
181 constraintResult = ConstraintStateUtils.getApplicableConstraint(genericConstraint, "state4",
182 genericStateMapping);
183 Assert.assertNotNull(constraintResult);
184 Assert.assertEquals(genericConstraint, constraintResult);
185
186
187 constraintResult = ConstraintStateUtils.getApplicableConstraint(genericConstraint, "state6",
188 genericStateMapping);
189 Assert.assertNull(constraintResult);
190
191 }
192
193
194
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
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
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
243 model.setState("state8");
244 state = ConstraintStateUtils.getClientViewValidationState(model, view);
245 Assert.assertEquals("state8", state);
246
247
248
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
269 view.setStateMapping(null);
270 state = ConstraintStateUtils.getClientViewValidationState(model, view);
271 Assert.assertNull(state);
272 }
273 }