View Javadoc

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