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 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (110) |
Complexity: 16 |
Complexity Density: 0.17 |
|
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 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
|
32 |
9
|
@Before... |
33 |
|
public void init() { |
34 |
|
|
35 |
9
|
val = new DefaultValidatorImpl(); |
36 |
9
|
val.setDateParser(new ServerDateParser()); |
37 |
9
|
val.setMessageService(null); |
38 |
9
|
valFactory.setDefaultValidator(val); |
39 |
|
|
40 |
|
} |
41 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
1
PASS
|
|
42 |
1
|
@Test... |
43 |
|
public void testRequired() { |
44 |
|
|
45 |
1
|
List<ValidationResultInfo> results = val.validateObject(buildTestPerson1(), getSimpleStudentObjectStructure()); |
46 |
1
|
assertEquals(results.size(), 1); |
47 |
|
|
48 |
1
|
assertEquals(results.get(0).getErrorLevel(), ValidationResultInfo.ErrorLevel.ERROR); |
49 |
1
|
assertEquals(results.get(0).getMessage(), "validation.required"); |
50 |
|
} |
51 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (6) |
Complexity: 1 |
Complexity Density: 0.17 |
1
PASS
|
|
52 |
1
|
@Test... |
53 |
|
public void testLengthRange() { |
54 |
|
|
55 |
1
|
ConstraintMockPerson p = buildTestPerson1(); |
56 |
1
|
p.setFirstName("thisisaveryveryverylo"); |
57 |
|
|
58 |
1
|
List<ValidationResultInfo> results = val.validateObject(p, getSimpleStudentObjectStructure()); |
59 |
1
|
assertEquals(results.size(), 2); |
60 |
|
|
61 |
1
|
assertEquals(results.get(0).getErrorLevel(), ValidationResultInfo.ErrorLevel.ERROR); |
62 |
1
|
assertEquals(results.get(0).getMessage(), "validation.lengthOutOfRange"); |
63 |
|
} |
64 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (8) |
Complexity: 1 |
Complexity Density: 0.12 |
1
PASS
|
|
65 |
1
|
@Test... |
66 |
|
public void testMinLength() { |
67 |
|
|
68 |
1
|
ConstraintMockPerson p = buildTestPerson1(); |
69 |
1
|
p.setFirstName("t"); |
70 |
|
|
71 |
1
|
ObjectStructureDefinition o1 = getSimpleStudentObjectStructure(); |
72 |
1
|
o1.getAttributes().get(0).setMaxLength(null); |
73 |
|
|
74 |
1
|
List<ValidationResultInfo> results = val.validateObject(p, o1); |
75 |
1
|
assertEquals(results.size(), 2); |
76 |
|
|
77 |
1
|
assertEquals(results.get(0).getErrorLevel(), ValidationResultInfo.ErrorLevel.ERROR); |
78 |
1
|
assertEquals(results.get(0).getMessage(), "validation.minLengthFailed"); |
79 |
|
} |
80 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (8) |
Complexity: 1 |
Complexity Density: 0.12 |
1
PASS
|
|
81 |
1
|
@Test... |
82 |
|
public void testMinDateValue() { |
83 |
1
|
ConstraintMockPerson p = buildTestPerson1(); |
84 |
1
|
ServerDateParser sp = new ServerDateParser(); |
85 |
1
|
p.setDob(sp.parseDate("1960-01-01")); |
86 |
1
|
ObjectStructureDefinition o1 = getSimpleStudentObjectStructure(); |
87 |
|
|
88 |
1
|
List<ValidationResultInfo> results = val.validateObject(p, o1); |
89 |
1
|
assertEquals(results.size(), 1); |
90 |
|
|
91 |
1
|
assertEquals(results.get(0).getErrorLevel(), ValidationResultInfo.ErrorLevel.ERROR); |
92 |
1
|
assertEquals(results.get(0).getMessage(), "validation.minValueFailed"); |
93 |
|
} |
94 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (8) |
Complexity: 1 |
Complexity Density: 0.12 |
1
PASS
|
|
95 |
1
|
@Test... |
96 |
|
public void testMaxLength() { |
97 |
|
|
98 |
1
|
ConstraintMockPerson p = buildTestPerson1(); |
99 |
1
|
p.setFirstName("thisisaveryveryverylo"); |
100 |
|
|
101 |
1
|
ObjectStructureDefinition o1 = getSimpleStudentObjectStructure(); |
102 |
1
|
o1.getAttributes().get(0).setMinLength(0); |
103 |
|
|
104 |
1
|
List<ValidationResultInfo> results = val.validateObject(p, o1); |
105 |
1
|
assertEquals(results.size(), 2); |
106 |
|
|
107 |
1
|
assertEquals(results.get(0).getErrorLevel(), ValidationResultInfo.ErrorLevel.ERROR); |
108 |
1
|
assertEquals(results.get(0).getMessage(), "validation.maxLengthFailed"); |
109 |
|
} |
110 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (7) |
Complexity: 1 |
Complexity Density: 0.14 |
1
PASS
|
|
111 |
1
|
@Test... |
112 |
|
public void testValidChars() { |
113 |
|
|
114 |
1
|
ConstraintMockPerson p = buildTestPerson1(); |
115 |
1
|
p.setFirstName("in$#valid"); |
116 |
|
|
117 |
1
|
ObjectStructureDefinition o1 = getSimpleStudentObjectStructure(); |
118 |
|
|
119 |
1
|
List<ValidationResultInfo> results = val.validateObject(p, o1); |
120 |
1
|
assertEquals(results.size(), 2); |
121 |
|
|
122 |
1
|
assertEquals(results.get(0).getErrorLevel(), |
123 |
|
ValidationResultInfo.ErrorLevel.ERROR); |
124 |
1
|
assertEquals(results.get(0).getMessage(), "validation.validCharsFailed"); |
125 |
|
} |
126 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (7) |
Complexity: 1 |
Complexity Density: 0.14 |
1
PASS
|
|
127 |
1
|
@Test... |
128 |
|
public void testDoubleValueRange() { |
129 |
|
|
130 |
1
|
ConstraintMockPerson p = buildTestPerson2(); |
131 |
1
|
p.setGpa(5.0); |
132 |
|
|
133 |
1
|
ObjectStructureDefinition o1 = getSimpleStudentObjectStructure(); |
134 |
|
|
135 |
1
|
List<ValidationResultInfo> results = val.validateObject(p, o1); |
136 |
1
|
assertEquals(results.size(), 1); |
137 |
|
|
138 |
1
|
assertEquals(results.get(0).getErrorLevel(), ValidationResultInfo.ErrorLevel.ERROR); |
139 |
1
|
assertEquals(results.get(0).getMessage(), "validation.outOfRange"); |
140 |
|
} |
141 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (7) |
Complexity: 1 |
Complexity Density: 0.14 |
1
PASS
|
|
142 |
1
|
@Test... |
143 |
|
public void testNestedCaseConstraint() { |
144 |
|
|
145 |
1
|
ConstraintMockPerson p = buildTestPerson4(); |
146 |
|
|
147 |
1
|
ObjectStructureDefinition o1 = getStudentWithAddressObjectStructure(); |
148 |
|
|
149 |
1
|
List<ValidationResultInfo> results = val.validateObject(p, o1); |
150 |
1
|
assertEquals(2, results.size()); |
151 |
|
|
152 |
1
|
p.getAddress().get(0).setState("ACTIVE"); |
153 |
1
|
results = val.validateObject(p, o1); |
154 |
|
|
155 |
1
|
assertEquals(4, results.size()); |
156 |
|
} |
157 |
|
|
158 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (29) |
Complexity: 1 |
Complexity Density: 0.03 |
1
PASS
|
|
159 |
1
|
@Test... |
160 |
|
public void testNestedStructures() { |
161 |
1
|
ConstraintMockPerson p = buildTestPerson3(); |
162 |
|
|
163 |
1
|
ObjectStructureDefinition o = getStudentWithAddressObjectStructure(); |
164 |
|
|
165 |
1
|
List<ValidationResultInfo> results = val.validateObject(p, o); |
166 |
|
|
167 |
|
|
168 |
|
|
169 |
1
|
System.out.println(results.size() + " errors found"); |
170 |
1
|
for (ValidationResultInfo vri : results) { |
171 |
4
|
System.out.println(vri.getErrorLevel() + " " + vri.getElement() |
172 |
|
+ " " + vri.getMessage()); |
173 |
|
} |
174 |
1
|
assertEquals(4, results.size()); |
175 |
|
|
176 |
1
|
assertEquals(results.get(0).getErrorLevel(), ValidationResultInfo.ErrorLevel.ERROR); |
177 |
1
|
assertEquals(results.get(0).getMessage(), "validation.required"); |
178 |
|
|
179 |
1
|
assertEquals(results.get(1).getErrorLevel(), ValidationResultInfo.ErrorLevel.ERROR); |
180 |
1
|
assertEquals(results.get(1).getMessage(), "validation.validCharsFailed"); |
181 |
|
|
182 |
1
|
assertEquals(results.get(2).getErrorLevel(), ValidationResultInfo.ErrorLevel.ERROR); |
183 |
1
|
assertEquals(results.get(2).getMessage(), "validation.requiresField"); |
184 |
|
|
185 |
1
|
SearchDispatcher searchDispatcher = new SearchDispatcherImpl( |
186 |
|
new MockSearchService()); |
187 |
|
|
188 |
1
|
val.setSearchDispatcher(searchDispatcher); |
189 |
1
|
p.getAddress().get(0).setLine1("something"); |
190 |
1
|
results = val.validateObject(p, o); |
191 |
1
|
System.out.println(results.size() + " errors found"); |
192 |
1
|
for (ValidationResultInfo vri : results) { |
193 |
3
|
System.out.println(vri.getErrorLevel() + " " + vri.getElement() |
194 |
|
+ " " + vri.getMessage()); |
195 |
|
} |
196 |
1
|
assertEquals(3, results.size()); |
197 |
|
|
198 |
1
|
p.getAddress().get(0).setLine2("notrightlookupvalue"); |
199 |
1
|
results = val.validateObject(p, o); |
200 |
1
|
System.out.println(results.size() + " errors found"); |
201 |
1
|
for (ValidationResultInfo vri : results) { |
202 |
2
|
System.out.println(vri.getErrorLevel() + " " + vri.getElement() |
203 |
|
+ " " + vri.getMessage()); |
204 |
|
} |
205 |
1
|
assertEquals(2, results.size()); |
206 |
1
|
assertEquals(results.get(0).getErrorLevel(), |
207 |
|
ValidationResultInfo.ErrorLevel.ERROR); |
208 |
1
|
assertEquals(results.get(0).getMessage(), "validation.lookup"); |
209 |
|
} |
210 |
|
|
|
|
| 8% |
Uncovered Elements: 23 (25) |
Complexity: 13 |
Complexity Density: 0.93 |
|
211 |
|
public class MockSearchService implements SearchService { |
212 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
213 |
0
|
@Override... |
214 |
|
public SearchCriteriaTypeInfo getSearchCriteriaType( |
215 |
|
String searchCriteriaTypeKey) throws DoesNotExistException, |
216 |
|
InvalidParameterException, MissingParameterException, |
217 |
|
OperationFailedException { |
218 |
0
|
return null; |
219 |
|
} |
220 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
221 |
0
|
@Override... |
222 |
|
public List<SearchCriteriaTypeInfo> getSearchCriteriaTypes() |
223 |
|
throws OperationFailedException { |
224 |
0
|
return null; |
225 |
|
} |
226 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
227 |
0
|
@Override... |
228 |
|
public SearchResultTypeInfo getSearchResultType( |
229 |
|
String searchResultTypeKey) throws DoesNotExistException, |
230 |
|
InvalidParameterException, MissingParameterException, |
231 |
|
OperationFailedException { |
232 |
0
|
return null; |
233 |
|
} |
234 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
235 |
0
|
@Override... |
236 |
|
public List<SearchResultTypeInfo> getSearchResultTypes() |
237 |
|
throws OperationFailedException { |
238 |
0
|
return null; |
239 |
|
} |
240 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
241 |
0
|
@Override... |
242 |
|
public SearchTypeInfo getSearchType(String searchTypeKey) |
243 |
|
throws DoesNotExistException, InvalidParameterException, |
244 |
|
MissingParameterException, OperationFailedException { |
245 |
0
|
return null; |
246 |
|
} |
247 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
248 |
1
|
@Override... |
249 |
|
public List<SearchTypeInfo> getSearchTypes() |
250 |
|
throws OperationFailedException { |
251 |
1
|
return null; |
252 |
|
} |
253 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
254 |
0
|
@Override... |
255 |
|
public List<SearchTypeInfo> getSearchTypesByCriteria( |
256 |
|
String searchCriteriaTypeKey) throws DoesNotExistException, |
257 |
|
InvalidParameterException, MissingParameterException, |
258 |
|
OperationFailedException { |
259 |
0
|
return null; |
260 |
|
} |
261 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
262 |
0
|
@Override... |
263 |
|
public List<SearchTypeInfo> getSearchTypesByResult( |
264 |
|
String searchResultTypeKey) throws DoesNotExistException, |
265 |
|
InvalidParameterException, MissingParameterException, |
266 |
|
OperationFailedException { |
267 |
0
|
return null; |
268 |
|
} |
269 |
|
|
|
|
| 0% |
Uncovered Elements: 8 (8) |
Complexity: 5 |
Complexity Density: 0.83 |
|
270 |
0
|
@Override... |
271 |
|
public SearchResult search(SearchRequest searchRequest) |
272 |
|
throws MissingParameterException { |
273 |
0
|
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 |
0
|
SearchResult result = new SearchResult(); |
280 |
0
|
SearchResultRow row = new SearchResultRow(); |
281 |
0
|
result.getRows().add(row); |
282 |
0
|
return result; |
283 |
|
} |
284 |
0
|
return null; |
285 |
|
} |
286 |
|
|
287 |
|
} |
288 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
289 |
6
|
public ConstraintMockPerson buildTestPerson1() {... |
290 |
6
|
return ValidatorMockObjectGenerator.buildTestPerson1(); |
291 |
|
} |
292 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
293 |
1
|
public ConstraintMockPerson buildTestPerson2() {... |
294 |
1
|
return ValidatorMockObjectGenerator.buildTestPerson2(); |
295 |
|
} |
296 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
297 |
1
|
public ConstraintMockPerson buildTestPerson3() {... |
298 |
1
|
return ValidatorMockObjectGenerator.buildTestPerson3(); |
299 |
|
} |
300 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
301 |
1
|
public ConstraintMockPerson buildTestPerson4() {... |
302 |
1
|
return ValidatorMockObjectGenerator.buildTestPerson4(); |
303 |
|
} |
304 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
305 |
7
|
public ObjectStructureDefinition getSimpleStudentObjectStructure() {... |
306 |
7
|
return dictionaryDelegate.getObjectStructure("simpleStudent"); |
307 |
|
} |
308 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
309 |
2
|
public ObjectStructureDefinition getStudentWithAddressObjectStructure() {... |
310 |
2
|
return dictionaryDelegate.getObjectStructure("studentWithAddress"); |
311 |
|
} |
312 |
|
} |