View Javadoc

1   package org.kuali.student.common.validator;
2   
3   import static org.junit.Assert.assertEquals;
4   
5   import java.util.List;
6   
7   import org.junit.Before;
8   import org.junit.Test;
9   import org.kuali.student.core.dictionary.dto.ObjectStructureDefinition;
10  import org.kuali.student.core.dictionary.service.impl.DictionaryServiceImpl;
11  import org.kuali.student.core.exceptions.DoesNotExistException;
12  import org.kuali.student.core.exceptions.InvalidParameterException;
13  import org.kuali.student.core.exceptions.MissingParameterException;
14  import org.kuali.student.core.exceptions.OperationFailedException;
15  import org.kuali.student.core.search.dto.SearchCriteriaTypeInfo;
16  import org.kuali.student.core.search.dto.SearchRequest;
17  import org.kuali.student.core.search.dto.SearchResult;
18  import org.kuali.student.core.search.dto.SearchResultRow;
19  import org.kuali.student.core.search.dto.SearchResultTypeInfo;
20  import org.kuali.student.core.search.dto.SearchTypeInfo;
21  import org.kuali.student.core.search.service.SearchDispatcher;
22  import org.kuali.student.core.search.service.SearchService;
23  import org.kuali.student.core.search.service.impl.SearchDispatcherImpl;
24  import org.kuali.student.core.validation.dto.ValidationResultInfo;
25  
26  public class TestValidator {
27  	DefaultValidatorImpl val = null;
28  	ValidatorFactory valFactory = new ValidatorFactory();
29  	DictionaryServiceImpl dictionaryDelegate = new DictionaryServiceImpl("classpath:test-validator-context.xml");
30  	
31  	@Before
32  	public void init() {
33  		
34  		val = new DefaultValidatorImpl();
35  		val.setDateParser(new ServerDateParser());
36  		val.setMessageService(null);
37  		valFactory.setDefaultValidator(val);
38  
39  	}
40  		
41  	@Test     
42      public void testRequired() {
43      	    	
44      	List<ValidationResultInfo> results = val.validateObject( buildTestPerson1(), getSimpleStudentObjectStructure());
45      	assertEquals(results.size(), 1);
46  
47      	assertEquals(results.get(0).getErrorLevel(), ValidationResultInfo.ErrorLevel.ERROR);
48      	assertEquals(results.get(0).getMessage(), "validation.required");
49      }
50      
51  
52      @Test     
53      public void testLengthRange() {
54      	
55      	ConstraintMockPerson p = buildTestPerson1();
56      	p.setFirstName("thisisaveryveryverylo");
57      	
58      	List<ValidationResultInfo> results = val.validateObject( p, getSimpleStudentObjectStructure());
59      	assertEquals(results.size(), 2);
60  
61      	assertEquals(results.get(0).getErrorLevel(), ValidationResultInfo.ErrorLevel.ERROR);
62      	assertEquals(results.get(0).getMessage(), "validation.lengthOutOfRange");
63      }
64      
65      @Test     
66      public void testMinLength() {
67      	
68      	ConstraintMockPerson p = buildTestPerson1();
69      	p.setFirstName("t");
70  
71      	ObjectStructureDefinition o1 = getSimpleStudentObjectStructure();
72      	o1.getAttributes().get(0).setMaxLength(null);
73      	
74      	List<ValidationResultInfo> results = val.validateObject( p, o1);    
75      	assertEquals(results.size(), 2);
76  
77      	assertEquals(results.get(0).getErrorLevel(), ValidationResultInfo.ErrorLevel.ERROR);
78      	assertEquals(results.get(0).getMessage(), "validation.minLengthFailed");
79      }
80      
81  
82      @Test
83      public void testMinDateValue() {
84      	ConstraintMockPerson p = buildTestPerson1();
85      	ServerDateParser sp = new ServerDateParser();
86      	p.setDob(sp.parseDate("1960-01-01"));
87      	ObjectStructureDefinition o1 = getSimpleStudentObjectStructure();
88      	
89      	List<ValidationResultInfo> results = val.validateObject( p, o1);    
90      	assertEquals(results.size(), 1);
91  
92      	assertEquals(results.get(0).getErrorLevel(), ValidationResultInfo.ErrorLevel.ERROR);
93      	assertEquals(results.get(0).getMessage(), "validation.minValueFailed");    	
94      }
95      
96      @Test     
97      public void testMaxLength() {
98      	
99      	ConstraintMockPerson p = buildTestPerson1();
100     	p.setFirstName("thisisaveryveryverylo");
101 
102     	ObjectStructureDefinition o1 = getSimpleStudentObjectStructure();
103     	o1.getAttributes().get(0).setMinLength(0);
104     	
105     	List<ValidationResultInfo> results = val.validateObject( p, o1);    
106     	assertEquals(results.size(), 2);
107 
108     	assertEquals(results.get(0).getErrorLevel(), ValidationResultInfo.ErrorLevel.ERROR);
109     	assertEquals(results.get(0).getMessage(), "validation.maxLengthFailed");
110     }
111     
112     @Test     
113     public void testValidChars() {
114     	    	
115     	ConstraintMockPerson p = buildTestPerson1();
116     	p.setFirstName("in$#valid");
117 
118     	ObjectStructureDefinition o1 = getSimpleStudentObjectStructure();
119     	
120     	List<ValidationResultInfo> results = val.validateObject( p, o1);    
121     	assertEquals(results.size(), 2);
122 
123     	assertEquals(results.get(0).getErrorLevel(), ValidationResultInfo.ErrorLevel.ERROR);
124     	assertEquals(results.get(0).getMessage(), "validation.validCharsFailed");
125     }
126 
127 
128     @Test     
129     public void testDoubleValueRange() {
130     	
131     	ConstraintMockPerson p = buildTestPerson2();
132     	p.setGpa(5.0);
133 
134     	ObjectStructureDefinition o1 = getSimpleStudentObjectStructure();
135     	
136     	List<ValidationResultInfo> results = val.validateObject( p, o1);    
137     	assertEquals(results.size(), 1);
138 
139     	assertEquals(results.get(0).getErrorLevel(), ValidationResultInfo.ErrorLevel.ERROR);
140     	assertEquals(results.get(0).getMessage(), "validation.outOfRange");
141     }
142     
143     @Test
144     public void testNestedStructures() {    	
145     	ConstraintMockPerson p = buildTestPerson3();
146 
147     	ObjectStructureDefinition o = getStudentWithAddressObjectStructure();
148     	
149     	List<ValidationResultInfo> results = val.validateObject( p, o);
150 //     ERROR address/0/line1 validation.required
151 //     ERROR address/0/line2 validation.validCharsFailed
152 //     ERROR address/0/line2 validation.requiresField
153 //     ERROR address/0/province validation.required
154 //     ERROR address/0/postalCode validation.lengthOutOfRange
155 //     ERROR address/0/type validation.lengthOutOfRange
156 //     ERROR address/0/state validation.lengthOutOfRange
157      System.out.println (results.size () + " errors found");
158      for (ValidationResultInfo vri : results)
159      {
160       System.out.println (vri.getErrorLevel () + " " + vri.getElement () + " " + vri.getMessage ());
161      }
162     	assertEquals(7, results.size());
163 
164     	assertEquals(results.get(0).getErrorLevel(), ValidationResultInfo.ErrorLevel.ERROR);
165     	assertEquals(results.get(0).getMessage(), "validation.required");
166 
167     	assertEquals(results.get(1).getErrorLevel(), ValidationResultInfo.ErrorLevel.ERROR);
168     	assertEquals(results.get(1).getMessage(), "validation.validCharsFailed");    	
169     	assertEquals(results.get(2).getMessage(), "validation.requiresField");
170     	SearchDispatcher searchDispatcher= new SearchDispatcherImpl(new MockSearchService());
171     	
172     	val.setSearchDispatcher(searchDispatcher);
173     	p.getAddress().get(0).setLine1("something");
174     	results = val.validateObject( p, o);
175      System.out.println (results.size () + " errors found");
176      for (ValidationResultInfo vri : results)
177      {
178       System.out.println (vri.getErrorLevel () + " " + vri.getElement () + " " + vri.getMessage ());
179      }
180     	assertEquals(6, results.size());
181 
182     	p.getAddress().get(0).setLine2("notrightlookupvalue");
183     	results = val.validateObject( p, o); 
184      System.out.println (results.size () + " errors found");
185      for (ValidationResultInfo vri : results)
186      {
187       System.out.println (vri.getErrorLevel () + " " + vri.getElement () + " " + vri.getMessage ());
188      }
189     	assertEquals(5, results.size());
190     	assertEquals(results.get(0).getErrorLevel(), ValidationResultInfo.ErrorLevel.ERROR);
191     	assertEquals(results.get(0).getMessage(), "validation.lookup");   
192     }
193     
194     public class MockSearchService implements SearchService{
195 
196 		@Override
197 		public SearchCriteriaTypeInfo getSearchCriteriaType(
198 				String searchCriteriaTypeKey) throws DoesNotExistException,
199 				InvalidParameterException, MissingParameterException,
200 				OperationFailedException {
201 			return null;
202 		}
203 
204 		@Override
205 		public List<SearchCriteriaTypeInfo> getSearchCriteriaTypes()
206 				throws OperationFailedException {
207 			return null;
208 		}
209 
210 		@Override
211 		public SearchResultTypeInfo getSearchResultType(
212 				String searchResultTypeKey) throws DoesNotExistException,
213 				InvalidParameterException, MissingParameterException,
214 				OperationFailedException {
215 			return null;
216 		}
217 
218 		@Override
219 		public List<SearchResultTypeInfo> getSearchResultTypes()
220 				throws OperationFailedException {
221 			return null;
222 		}
223 
224 		@Override
225 		public SearchTypeInfo getSearchType(String searchTypeKey)
226 				throws DoesNotExistException, InvalidParameterException,
227 				MissingParameterException, OperationFailedException {
228 			return null;
229 		}
230 
231 		@Override
232 		public List<SearchTypeInfo> getSearchTypes()
233 				throws OperationFailedException {
234 			return null;
235 		}
236 
237 		@Override
238 		public List<SearchTypeInfo> getSearchTypesByCriteria(
239 				String searchCriteriaTypeKey) throws DoesNotExistException,
240 				InvalidParameterException, MissingParameterException,
241 				OperationFailedException {
242 			return null;
243 		}
244 
245 		@Override
246 		public List<SearchTypeInfo> getSearchTypesByResult(
247 				String searchResultTypeKey) throws DoesNotExistException,
248 				InvalidParameterException, MissingParameterException,
249 				OperationFailedException {
250 			return null;
251 		}
252 
253 		@Override
254 		public SearchResult search(SearchRequest searchRequest)
255 				throws MissingParameterException {
256 			if(searchRequest!=null && searchRequest.getParams()!=null
257 					&&"param1".equals(searchRequest.getParams().get(0).getKey())
258 					&&"line2value".equals(searchRequest.getParams().get(0).getValue())
259 					){
260 				SearchResult result = new SearchResult();
261 				SearchResultRow row = new SearchResultRow();
262 				result.getRows().add(row);
263 				return result;
264 			}
265 			return null;
266 		}
267     	
268     }
269     
270     public ConstraintMockPerson buildTestPerson1() {
271     	return ValidatorMockObjectGenerator.buildTestPerson1();
272     }
273 
274     
275     public ConstraintMockPerson buildTestPerson2() {
276     	return ValidatorMockObjectGenerator.buildTestPerson2();
277     }
278 
279     
280     public ConstraintMockPerson buildTestPerson3() {
281     	return ValidatorMockObjectGenerator.buildTestPerson3();
282     }
283     
284     public ObjectStructureDefinition getSimpleStudentObjectStructure() {
285     	return dictionaryDelegate.getObjectStructure("simpleStudent");
286     }
287 
288     public ObjectStructureDefinition getStudentWithAddressObjectStructure() {
289     	return dictionaryDelegate.getObjectStructure("studentWithAddress");
290     }
291 }