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