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