View Javadoc

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