Clover Coverage Report - Kuali Student 1.1.0-M10-SNAPSHOT (Aggregated)
Coverage timestamp: Fri Dec 17 2010 05:04:51 EST
235   428   14   16.79
0   289   0.06   14
14     1  
1    
 
  ValidatorTest       Line # 35 235 0% 14 0 100% 1.0
 
  (8)
 
1    /**
2    * Copyright 2010 The Kuali Foundation Licensed under the
3    * Educational Community License, Version 2.0 (the "License"); you may
4    * not use this file except in compliance with the License. You may
5    * obtain a copy of the License at
6    *
7    * http://www.osedu.org/licenses/ECL-2.0
8    *
9    * Unless required by applicable law or agreed to in writing,
10    * software distributed under the License is distributed on an "AS IS"
11    * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
12    * or implied. See the License for the specific language governing
13    * permissions and limitations under the License.
14    */
15   
16    package org.kuali.student.common.validator.old;
17   
18    import static org.junit.Assert.assertEquals;
19   
20    import java.util.ArrayList;
21    import java.util.List;
22   
23    import org.junit.Test;
24    import org.kuali.student.core.dictionary.old.dto.ConstraintDescriptor;
25    import org.kuali.student.core.dictionary.old.dto.ConstraintSelector;
26    import org.kuali.student.core.dictionary.old.dto.Field;
27    import org.kuali.student.core.dictionary.old.dto.FieldDescriptor;
28    import org.kuali.student.core.dictionary.old.dto.ObjectStructure;
29    import org.kuali.student.core.dictionary.old.dto.RequireConstraint;
30    import org.kuali.student.core.dictionary.old.dto.State;
31    import org.kuali.student.core.dictionary.old.dto.Type;
32    import org.kuali.student.core.dictionary.old.dto.ValidCharsConstraint;
33    import org.kuali.student.core.validation.dto.ValidationResultInfo;
34   
 
35    public class ValidatorTest {
36   
37    Validator val = null;
38   
 
39  8 toggle public ValidatorTest() {
40  8 val = new Validator();
41  8 val.setDateParser(new ServerDateParser());
42  8 val.setMessageService(null);
43    }
44   
 
45  1 toggle @Test
46    public void testRequired() {
47   
48  1 List<ValidationResultInfo> results = val.validateTypeStateObject( buildTestPerson1(), buildObjectStructure1());
49  1 assertEquals(results.size(), 1);
50   
51  1 assertEquals(results.get(0).getErrorLevel(), ValidationResultInfo.ErrorLevel.ERROR);
52  1 assertEquals(results.get(0).getMessage(), "validation.required");
53    }
54   
55   
 
56  1 toggle @Test
57    public void testLengthRange() {
58   
59  1 ConstraintMockPerson p = buildTestPerson1();
60  1 p.setFirstName("thisisaveryveryverylo");
61   
62  1 List<ValidationResultInfo> results = val.validateTypeStateObject( p, buildObjectStructure1());
63  1 assertEquals(results.size(), 2);
64   
65  1 assertEquals(results.get(0).getErrorLevel(), ValidationResultInfo.ErrorLevel.ERROR);
66  1 assertEquals(results.get(0).getMessage(), "validation.lengthOutOfRange");
67    }
68   
 
69  1 toggle @Test
70    public void testMinLength() {
71   
72  1 ConstraintMockPerson p = buildTestPerson1();
73  1 p.setFirstName("t");
74   
75  1 ObjectStructure o1 = buildObjectStructure1();
76  1 o1.getType().get(0).getState().get(0).getField().get(0).getConstraintDescriptor().getConstraint().get(0).setMaxLength(null);
77   
78  1 List<ValidationResultInfo> results = val.validateTypeStateObject( p, o1);
79  1 assertEquals(results.size(), 2);
80   
81  1 assertEquals(results.get(0).getErrorLevel(), ValidationResultInfo.ErrorLevel.ERROR);
82  1 assertEquals(results.get(0).getMessage(), "validation.minLengthFailed");
83    }
84   
85   
 
86  1 toggle @Test
87    public void testMinDateValue() {
88  1 ConstraintMockPerson p = buildTestPerson1();
89  1 ServerDateParser sp = new ServerDateParser();
90  1 p.setDob(sp.parseDate("1960-01-01"));
91  1 ObjectStructure o1 = buildObjectStructure1();
92   
93  1 List<ValidationResultInfo> results = val.validateTypeStateObject( p, o1);
94  1 assertEquals(results.size(), 1);
95   
96  1 assertEquals(results.get(0).getErrorLevel(), ValidationResultInfo.ErrorLevel.ERROR);
97  1 assertEquals(results.get(0).getMessage(), "validation.minValueFailed");
98    }
99   
 
100  1 toggle @Test
101    public void testMaxLength() {
102   
103  1 ConstraintMockPerson p = buildTestPerson1();
104  1 p.setFirstName("thisisaveryveryverylo");
105   
106  1 ObjectStructure o1 = buildObjectStructure1();
107  1 o1.getType().get(0).getState().get(0).getField().get(0).getConstraintDescriptor().getConstraint().get(0).setMinLength(0);
108   
109  1 List<ValidationResultInfo> results = val.validateTypeStateObject( p, o1);
110  1 assertEquals(results.size(), 2);
111   
112  1 assertEquals(results.get(0).getErrorLevel(), ValidationResultInfo.ErrorLevel.ERROR);
113  1 assertEquals(results.get(0).getMessage(), "validation.maxLengthFailed");
114    }
115   
 
116  1 toggle @Test
117    public void testValidChars() {
118   
119  1 ConstraintMockPerson p = buildTestPerson1();
120  1 p.setFirstName("in$#valid");
121   
122  1 ObjectStructure o1 = buildObjectStructure1();
123   
124  1 List<ValidationResultInfo> results = val.validateTypeStateObject( p, o1);
125  1 assertEquals(results.size(), 2);
126   
127  1 assertEquals(results.get(0).getErrorLevel(), ValidationResultInfo.ErrorLevel.ERROR);
128  1 assertEquals(results.get(0).getMessage(), "validation.validCharsFailed");
129    }
130   
131   
 
132  1 toggle @Test
133    public void testDoubleValueRange() {
134   
135  1 ConstraintMockPerson p = buildTestPerson2();
136  1 p.setGpa(5.0);
137   
138  1 ObjectStructure o1 = buildObjectStructure1();
139   
140  1 List<ValidationResultInfo> results = val.validateTypeStateObject( p, o1);
141  1 assertEquals(results.size(), 1);
142   
143  1 assertEquals(results.get(0).getErrorLevel(), ValidationResultInfo.ErrorLevel.ERROR);
144  1 assertEquals(results.get(0).getMessage(), "validation.outOfRange");
145    }
146   
 
147  1 toggle @Test
148    public void testNestedStructures() {
149  1 ConstraintMockPerson p = buildTestPerson3();
150   
151  1 ObjectStructure o = buildObjectStructure2();
152   
153  1 List<ValidationResultInfo> results = val.validateTypeStateObject( p, o);
154  1 assertEquals(results.size(), 3);
155   
156  1 assertEquals(results.get(0).getErrorLevel(), ValidationResultInfo.ErrorLevel.ERROR);
157  1 assertEquals(results.get(0).getMessage(), "validation.required");
158   
159  1 assertEquals(results.get(1).getErrorLevel(), ValidationResultInfo.ErrorLevel.ERROR);
160  1 assertEquals(results.get(1).getMessage(), "validation.requiresField");
161  1 assertEquals(results.get(2).getMessage(), "validation.validCharsFailed");
162    }
163   
164   
 
165  6 toggle public ConstraintMockPerson buildTestPerson1() {
166  6 ConstraintMockPerson person = new ConstraintMockPerson();
167   
168  6 person.setFirstName("first");
169  6 person.setLastName("last");
170  6 person.setEmail("first@test.com");
171  6 person.setType("STUDENT");
172  6 person.setState("CREATE");
173  6 person.setId("P1");
174   
175  6 return person;
176    }
177   
178   
 
179  1 toggle public ConstraintMockPerson buildTestPerson2() {
180  1 ConstraintMockPerson person = new ConstraintMockPerson();
181   
182  1 person.setFirstName("first");
183  1 person.setLastName("last");
184  1 person.setEmail("first@test.com");
185  1 person.setType("STUDENT");
186  1 person.setState("CREATE");
187  1 person.setId("P1");
188  1 person.setGpa(3.5);
189  1 ServerDateParser dp = new ServerDateParser();
190  1 person.setDob(dp.parseDate("1978-01-01"));
191   
192   
193  1 return person;
194    }
195   
196   
 
197  1 toggle public ConstraintMockPerson buildTestPerson3() {
198  1 ConstraintMockPerson person = new ConstraintMockPerson();
199   
200  1 person.setFirstName("first");
201  1 person.setLastName("last");
202  1 person.setEmail("first@test.com");
203  1 person.setType("STUDENT");
204  1 person.setState("CREATE");
205  1 person.setId("P1");
206  1 ServerDateParser dp = new ServerDateParser();
207  1 person.setDob(dp.parseDate("1978-01-01"));
208   
209  1 ConstraintMockAddress address = new ConstraintMockAddress();
210  1 address.setState("ACTIVE");
211  1 address.setType("MAILING");
212  1 address.setId("A1");
213  1 address.setCity("TLH");
214  1 address.setStateCode("FL");
215  1 address.setCountry("US");
216  1 address.setLine1("");
217  1 address.setLine2("line2value");
218  1 address.setPostalCode("32306");
219   
220  1 List<ConstraintMockAddress> addressL = new ArrayList<ConstraintMockAddress>();
221  1 addressL.add(address);
222   
223  1 person.setAddress(addressL);
224   
225  1 return person;
226    }
227   
 
228  1 toggle public ObjectStructure buildObjectStructure2() {
229   
230  1 ObjectStructure addrStruct = new ObjectStructure();
231  1 addrStruct.setKey("addressInfo");
232   
233  1 List<Field> fields = new ArrayList<Field>();
234   
235    // Line 1 Field
236  1 Field f1 = new Field();
237  1 f1.setKey("line1");
238   
239  1 FieldDescriptor fd1 = new FieldDescriptor();
240  1 fd1.setDataType("string");
241   
242  1 ConstraintDescriptor cd1 = new ConstraintDescriptor();
243  1 ConstraintSelector cs1 = new ConstraintSelector();
244  1 cs1.setMaxLength("20");
245  1 cs1.setMinLength(2);
246  1 cs1.setMinOccurs(1);
247   
248  1 ValidCharsConstraint vcs1 = new ValidCharsConstraint();
249  1 vcs1.setValue("regex:[a-z]*");
250   
251  1 ConstraintSelector cs12 = new ConstraintSelector();
252  1 cs12.setValidChars(vcs1);
253   
254  1 List<ConstraintSelector> csList1 = new ArrayList<ConstraintSelector>();
255  1 csList1.add(cs1);
256  1 csList1.add(cs12);
257  1 cd1.setConstraint(csList1);
258   
259  1 f1.setFieldDescriptor(fd1);
260  1 f1.setConstraintDescriptor(cd1);
261   
262  1 fields.add(f1);
263   
264   
265    // Line 2 Field
266  1 Field f2 = new Field();
267  1 f2.setKey("line2");
268   
269  1 FieldDescriptor fd2 = new FieldDescriptor();
270  1 fd2.setDataType("string");
271   
272  1 ConstraintDescriptor cd2 = new ConstraintDescriptor();
273  1 ConstraintSelector cs2 = new ConstraintSelector();
274  1 cs2.setMaxLength("20");
275  1 cs2.setMinLength(2);
276  1 cs2.setMinOccurs(0);
277   
278  1 RequireConstraint rc2 = new RequireConstraint();
279  1 rc2.setField("line1");
280  1 List<RequireConstraint> rcList2 = new ArrayList<RequireConstraint>();
281  1 rcList2.add(rc2);
282  1 cs2.setRequireConstraint(rcList2);
283   
284  1 ValidCharsConstraint vcs2 = new ValidCharsConstraint();
285  1 vcs2.setValue("regex:[a-z]*");
286   
287  1 ConstraintSelector cs22 = new ConstraintSelector();
288  1 cs22.setValidChars(vcs1);
289   
290  1 List<ConstraintSelector> csList2 = new ArrayList<ConstraintSelector>();
291  1 csList2.add(cs2);
292  1 csList2.add(cs22);
293  1 cd2.setConstraint(csList2);
294   
295  1 f2.setFieldDescriptor(fd2);
296  1 f2.setConstraintDescriptor(cd2);
297   
298  1 fields.add(f2);
299   
300  1 List<State> states = new ArrayList<State>();
301  1 State s = new State();
302  1 s.setKey("ACTIVE");
303  1 s.setField(fields);
304   
305  1 states.add(s);
306   
307  1 List<Type> types = new ArrayList<Type>();
308  1 Type t = new Type();
309  1 t.setKey("MAILING");
310  1 t.setState(states);
311  1 types.add(t);
312  1 addrStruct.setType(types);
313   
314   
315  1 ObjectStructure objStruct = buildObjectStructure1();
316   
317    // Complex nested obj struct
318  1 Field f3 = new Field();
319  1 f3.setKey("address");
320   
321  1 FieldDescriptor fd3 = new FieldDescriptor();
322  1 fd3.setDataType("complex");
323  1 fd3.setObjectStructure(addrStruct);
324  1 f3.setFieldDescriptor(fd3);
325   
326  1 List<Field> fieldList = objStruct.getType().get(0).getState().get(0).getField();
327  1 fieldList.add(f3);
328  1 objStruct.getType().get(0).getState().get(0).setField(fieldList);
329   
330   
331  1 return objStruct;
332    }
333   
 
334  8 toggle public ObjectStructure buildObjectStructure1() {
335  8 ObjectStructure objStruct = new ObjectStructure();
336  8 objStruct.setKey("personInfo");
337   
338  8 List<Field> fields = new ArrayList<Field>();
339   
340   
341    // Line 1 Field
342  8 Field f1 = new Field();
343  8 f1.setKey("firstName");
344   
345  8 FieldDescriptor fd1 = new FieldDescriptor();
346  8 fd1.setDataType("string");
347   
348  8 ConstraintDescriptor cd1 = new ConstraintDescriptor();
349  8 ConstraintSelector cs1 = new ConstraintSelector();
350  8 cs1.setMaxLength("20");
351  8 cs1.setMinLength(2);
352  8 cs1.setMinOccurs(1);
353   
354  8 ValidCharsConstraint vcs1 = new ValidCharsConstraint();
355  8 vcs1.setValue("regex:[a-z]*");
356   
357  8 ConstraintSelector cs12 = new ConstraintSelector();
358  8 cs12.setValidChars(vcs1);
359   
360  8 List<ConstraintSelector> csList1 = new ArrayList<ConstraintSelector>();
361  8 csList1.add(cs1);
362  8 csList1.add(cs12);
363  8 cd1.setConstraint(csList1);
364   
365  8 f1.setFieldDescriptor(fd1);
366  8 f1.setConstraintDescriptor(cd1);
367   
368  8 fields.add(f1);
369   
370    // DOB Field
371  8 Field f2 = new Field();
372  8 f2.setKey("dob");
373   
374  8 FieldDescriptor fd2 = new FieldDescriptor();
375  8 fd2.setDataType("date");
376   
377  8 ConstraintDescriptor cd2 = new ConstraintDescriptor();
378  8 ConstraintSelector cs2 = new ConstraintSelector();
379  8 cs2.setMinValue("1970-01-01");
380  8 cs2.setMinOccurs(1);
381   
382  8 List<ConstraintSelector> csList2 = new ArrayList<ConstraintSelector>();
383  8 csList2.add(cs2);
384  8 cd2.setConstraint(csList2);
385   
386  8 f2.setFieldDescriptor(fd2);
387  8 f2.setConstraintDescriptor(cd2);
388   
389  8 fields.add(f2);
390   
391   
392    // GAP
393  8 Field f3 = new Field();
394  8 f3.setKey("gpa");
395   
396  8 FieldDescriptor fd3 = new FieldDescriptor();
397  8 fd3.setDataType("double");
398   
399  8 ConstraintDescriptor cd3 = new ConstraintDescriptor();
400  8 ConstraintSelector cs3 = new ConstraintSelector();
401  8 cs3.setMinValue("1.0");
402  8 cs3.setMaxValue("4.0");
403  8 List<ConstraintSelector> csList3 = new ArrayList<ConstraintSelector>();
404  8 csList3.add(cs3);
405  8 cd3.setConstraint(csList3);
406  8 f3.setFieldDescriptor(fd3);
407  8 f3.setConstraintDescriptor(cd3);
408  8 fields.add(f3);
409   
410  8 List<State> states = new ArrayList<State>();
411  8 State s = new State();
412  8 s.setKey("CREATE");
413  8 s.setField(fields);
414   
415  8 states.add(s);
416   
417  8 List<Type> types = new ArrayList<Type>();
418  8 Type t = new Type();
419  8 t.setKey("STUDENT");
420  8 t.setState(states);
421  8 types.add(t);
422   
423   
424  8 objStruct.setType(types);
425   
426  8 return objStruct;
427    }
428    }