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 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (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 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
|
31 |
8
|
@Before... |
32 |
|
public void init() { |
33 |
|
|
34 |
8
|
val = new DefaultValidatorImpl(); |
35 |
8
|
val.setDateParser(new ServerDateParser()); |
36 |
8
|
val.setMessageService(null); |
37 |
8
|
valFactory.setDefaultValidator(val); |
38 |
|
|
39 |
|
} |
40 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
1
PASS
|
|
41 |
1
|
@Test ... |
42 |
|
public void testRequired() { |
43 |
|
|
44 |
1
|
List<ValidationResultInfo> results = val.validateObject( buildTestPerson1(), getSimpleStudentObjectStructure()); |
45 |
1
|
assertEquals(results.size(), 1); |
46 |
|
|
47 |
1
|
assertEquals(results.get(0).getErrorLevel(), ValidationResultInfo.ErrorLevel.ERROR); |
48 |
1
|
assertEquals(results.get(0).getMessage(), "validation.required"); |
49 |
|
} |
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 |
|
|
81 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (8) |
Complexity: 1 |
Complexity Density: 0.12 |
1
PASS
|
|
82 |
1
|
@Test... |
83 |
|
public void testMinDateValue() { |
84 |
1
|
ConstraintMockPerson p = buildTestPerson1(); |
85 |
1
|
ServerDateParser sp = new ServerDateParser(); |
86 |
1
|
p.setDob(sp.parseDate("1960-01-01")); |
87 |
1
|
ObjectStructureDefinition o1 = getSimpleStudentObjectStructure(); |
88 |
|
|
89 |
1
|
List<ValidationResultInfo> results = val.validateObject( p, o1); |
90 |
1
|
assertEquals(results.size(), 1); |
91 |
|
|
92 |
1
|
assertEquals(results.get(0).getErrorLevel(), ValidationResultInfo.ErrorLevel.ERROR); |
93 |
1
|
assertEquals(results.get(0).getMessage(), "validation.minValueFailed"); |
94 |
|
} |
95 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (8) |
Complexity: 1 |
Complexity Density: 0.12 |
1
PASS
|
|
96 |
1
|
@Test ... |
97 |
|
public void testMaxLength() { |
98 |
|
|
99 |
1
|
ConstraintMockPerson p = buildTestPerson1(); |
100 |
1
|
p.setFirstName("thisisaveryveryverylo"); |
101 |
|
|
102 |
1
|
ObjectStructureDefinition o1 = getSimpleStudentObjectStructure(); |
103 |
1
|
o1.getAttributes().get(0).setMinLength(0); |
104 |
|
|
105 |
1
|
List<ValidationResultInfo> results = val.validateObject( p, o1); |
106 |
1
|
assertEquals(results.size(), 2); |
107 |
|
|
108 |
1
|
assertEquals(results.get(0).getErrorLevel(), ValidationResultInfo.ErrorLevel.ERROR); |
109 |
1
|
assertEquals(results.get(0).getMessage(), "validation.maxLengthFailed"); |
110 |
|
} |
111 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (7) |
Complexity: 1 |
Complexity Density: 0.14 |
1
PASS
|
|
112 |
1
|
@Test ... |
113 |
|
public void testValidChars() { |
114 |
|
|
115 |
1
|
ConstraintMockPerson p = buildTestPerson1(); |
116 |
1
|
p.setFirstName("in$#valid"); |
117 |
|
|
118 |
1
|
ObjectStructureDefinition o1 = getSimpleStudentObjectStructure(); |
119 |
|
|
120 |
1
|
List<ValidationResultInfo> results = val.validateObject( p, o1); |
121 |
1
|
assertEquals(results.size(), 2); |
122 |
|
|
123 |
1
|
assertEquals(results.get(0).getErrorLevel(), ValidationResultInfo.ErrorLevel.ERROR); |
124 |
1
|
assertEquals(results.get(0).getMessage(), "validation.validCharsFailed"); |
125 |
|
} |
126 |
|
|
127 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (7) |
Complexity: 1 |
Complexity Density: 0.14 |
1
PASS
|
|
128 |
1
|
@Test ... |
129 |
|
public void testDoubleValueRange() { |
130 |
|
|
131 |
1
|
ConstraintMockPerson p = buildTestPerson2(); |
132 |
1
|
p.setGpa(5.0); |
133 |
|
|
134 |
1
|
ObjectStructureDefinition o1 = getSimpleStudentObjectStructure(); |
135 |
|
|
136 |
1
|
List<ValidationResultInfo> results = val.validateObject( p, o1); |
137 |
1
|
assertEquals(results.size(), 1); |
138 |
|
|
139 |
1
|
assertEquals(results.get(0).getErrorLevel(), ValidationResultInfo.ErrorLevel.ERROR); |
140 |
1
|
assertEquals(results.get(0).getMessage(), "validation.outOfRange"); |
141 |
|
} |
142 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (28) |
Complexity: 1 |
Complexity Density: 0.04 |
1
PASS
|
|
143 |
1
|
@Test... |
144 |
|
public void testNestedStructures() { |
145 |
1
|
ConstraintMockPerson p = buildTestPerson3(); |
146 |
|
|
147 |
1
|
ObjectStructureDefinition o = getStudentWithAddressObjectStructure(); |
148 |
|
|
149 |
1
|
List<ValidationResultInfo> results = val.validateObject( p, o); |
150 |
|
|
151 |
|
|
152 |
|
|
153 |
|
|
154 |
|
|
155 |
|
|
156 |
|
|
157 |
1
|
System.out.println (results.size () + " errors found"); |
158 |
1
|
for (ValidationResultInfo vri : results) |
159 |
|
{ |
160 |
7
|
System.out.println (vri.getErrorLevel () + " " + vri.getElement () + " " + vri.getMessage ()); |
161 |
|
} |
162 |
1
|
assertEquals(7, results.size()); |
163 |
|
|
164 |
1
|
assertEquals(results.get(0).getErrorLevel(), ValidationResultInfo.ErrorLevel.ERROR); |
165 |
1
|
assertEquals(results.get(0).getMessage(), "validation.required"); |
166 |
|
|
167 |
1
|
assertEquals(results.get(1).getErrorLevel(), ValidationResultInfo.ErrorLevel.ERROR); |
168 |
1
|
assertEquals(results.get(1).getMessage(), "validation.validCharsFailed"); |
169 |
1
|
assertEquals(results.get(2).getMessage(), "validation.requiresField"); |
170 |
1
|
SearchDispatcher searchDispatcher= new SearchDispatcherImpl(new MockSearchService()); |
171 |
|
|
172 |
1
|
val.setSearchDispatcher(searchDispatcher); |
173 |
1
|
p.getAddress().get(0).setLine1("something"); |
174 |
1
|
results = val.validateObject( p, o); |
175 |
1
|
System.out.println (results.size () + " errors found"); |
176 |
1
|
for (ValidationResultInfo vri : results) |
177 |
|
{ |
178 |
6
|
System.out.println (vri.getErrorLevel () + " " + vri.getElement () + " " + vri.getMessage ()); |
179 |
|
} |
180 |
1
|
assertEquals(6, results.size()); |
181 |
|
|
182 |
1
|
p.getAddress().get(0).setLine2("notrightlookupvalue"); |
183 |
1
|
results = val.validateObject( p, o); |
184 |
1
|
System.out.println (results.size () + " errors found"); |
185 |
1
|
for (ValidationResultInfo vri : results) |
186 |
|
{ |
187 |
5
|
System.out.println (vri.getErrorLevel () + " " + vri.getElement () + " " + vri.getMessage ()); |
188 |
|
} |
189 |
1
|
assertEquals(5, results.size()); |
190 |
1
|
assertEquals(results.get(0).getErrorLevel(), ValidationResultInfo.ErrorLevel.ERROR); |
191 |
1
|
assertEquals(results.get(0).getMessage(), "validation.lookup"); |
192 |
|
} |
193 |
|
|
|
|
| 8% |
Uncovered Elements: 23 (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 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
231 |
1
|
@Override... |
232 |
|
public List<SearchTypeInfo> getSearchTypes() |
233 |
|
throws OperationFailedException { |
234 |
1
|
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 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
270 |
6
|
public ConstraintMockPerson buildTestPerson1() {... |
271 |
6
|
return ValidatorMockObjectGenerator.buildTestPerson1(); |
272 |
|
} |
273 |
|
|
274 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
275 |
1
|
public ConstraintMockPerson buildTestPerson2() {... |
276 |
1
|
return ValidatorMockObjectGenerator.buildTestPerson2(); |
277 |
|
} |
278 |
|
|
279 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
280 |
1
|
public ConstraintMockPerson buildTestPerson3() {... |
281 |
1
|
return ValidatorMockObjectGenerator.buildTestPerson3(); |
282 |
|
} |
283 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
284 |
7
|
public ObjectStructureDefinition getSimpleStudentObjectStructure() {... |
285 |
7
|
return dictionaryDelegate.getObjectStructure("simpleStudent"); |
286 |
|
} |
287 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
288 |
1
|
public ObjectStructureDefinition getStudentWithAddressObjectStructure() {... |
289 |
1
|
return dictionaryDelegate.getObjectStructure("studentWithAddress"); |
290 |
|
} |
291 |
|
} |