001package org.kuali.student.r2.common.validator;
002
003import static org.junit.Assert.assertEquals;
004
005import java.util.ArrayList;
006import java.util.List;
007
008import org.junit.Before;
009import org.junit.Test;
010import org.kuali.student.common.test.util.ContextInfoTestUtility;
011import org.kuali.student.r1.common.dictionary.dto.ObjectStructureDefinition;
012import org.kuali.student.r1.common.dictionary.service.impl.DictionaryServiceImpl;
013import org.kuali.student.r2.common.class1.search.SearchServiceDispatcherImpl;
014import org.kuali.student.r2.common.class1.type.dto.TypeInfo;
015import org.kuali.student.r2.common.dto.ContextInfo;
016import org.kuali.student.r2.common.exceptions.*;
017import org.kuali.student.r2.common.search.dto.*;
018import org.kuali.student.r2.common.search.service.SearchService;
019import org.kuali.student.r2.common.dto.ValidationResultInfo;
020
021import org.kuali.student.r1.common.validator.ServerDateParser;
022
023import javax.jws.WebParam;
024
025public class TestValidator {
026        DefaultValidatorImpl val = null;
027        ValidatorFactory valFactory = new ValidatorFactory();
028        DictionaryServiceImpl dictionaryDelegate = new DictionaryServiceImpl(
029                        "classpath:test-validator-context.xml");
030
031        @Before
032        public void init() {
033
034                val = new DefaultValidatorImpl();
035                val.setDateParser(new ServerDateParser());
036                val.setMessageService(null);
037                valFactory.setDefaultValidator(val);
038
039        }
040
041        @Test
042        public void testRequired() {
043
044                List<ValidationResultInfo> results = val.validateObject(buildTestPerson1(), getSimpleStudentObjectStructure(), ContextInfoTestUtility.getEnglishContextInfo());
045                assertEquals(results.size(), 1);
046
047                assertEquals(results.get(0).getErrorLevel(), ValidationResultInfo.ErrorLevel.ERROR);
048                assertEquals(results.get(0).getMessage(), "validation.required");
049        }
050
051        @Test
052        public void testLengthRange() {
053
054                ConstraintMockPerson p = buildTestPerson1();
055                p.setFirstName("thisisaveryveryverylo");
056
057                List<ValidationResultInfo> results = val.validateObject(p, getSimpleStudentObjectStructure(), ContextInfoTestUtility.getEnglishContextInfo());
058                assertEquals(results.size(), 2);
059
060                assertEquals(results.get(0).getErrorLevel(), ValidationResultInfo.ErrorLevel.ERROR);
061                assertEquals(results.get(0).getMessage(), "validation.lengthOutOfRange");
062        }
063
064        @Test
065        public void testMinLength() {
066
067                ConstraintMockPerson p = buildTestPerson1();
068                p.setFirstName("t");
069
070                ObjectStructureDefinition o1 = getSimpleStudentObjectStructure();
071                o1.getAttributes().get(0).setMaxLength(null);
072
073                List<ValidationResultInfo> results = val.validateObject(p, o1, ContextInfoTestUtility.getEnglishContextInfo());
074                assertEquals(results.size(), 2);
075
076                assertEquals(results.get(0).getErrorLevel(), ValidationResultInfo.ErrorLevel.ERROR);
077                assertEquals(results.get(0).getMessage(), "validation.minLengthFailed");
078        }
079
080        @Test
081        public void testMinDateValue() {
082                ConstraintMockPerson p = buildTestPerson1();
083                ServerDateParser sp = new ServerDateParser();
084                p.setDob(sp.parseDate("1960-01-01"));
085                ObjectStructureDefinition o1 = getSimpleStudentObjectStructure();
086
087                List<ValidationResultInfo> results = val.validateObject(p, o1, ContextInfoTestUtility.getEnglishContextInfo());
088                assertEquals(results.size(), 1);
089
090                assertEquals(results.get(0).getErrorLevel(), ValidationResultInfo.ErrorLevel.ERROR);
091                assertEquals(results.get(0).getMessage(), "validation.minValueFailed");
092        }
093
094        @Test
095        public void testMaxLength() {
096
097                ConstraintMockPerson p = buildTestPerson1();
098                p.setFirstName("thisisaveryveryverylo");
099
100                ObjectStructureDefinition o1 = getSimpleStudentObjectStructure();
101                o1.getAttributes().get(0).setMinLength(0);
102
103                List<ValidationResultInfo> results = val.validateObject(p, o1, ContextInfoTestUtility.getEnglishContextInfo());
104                assertEquals(results.size(), 2);
105
106                assertEquals(results.get(0).getErrorLevel(), ValidationResultInfo.ErrorLevel.ERROR);
107                assertEquals(results.get(0).getMessage(), "validation.maxLengthFailed");
108        }
109
110        @Test
111        public void testValidChars() {
112
113                ConstraintMockPerson p = buildTestPerson1();
114                p.setFirstName("in$#valid");
115
116                ObjectStructureDefinition o1 = getSimpleStudentObjectStructure();
117
118                List<ValidationResultInfo> results = val.validateObject(p, o1, ContextInfoTestUtility.getEnglishContextInfo());
119                assertEquals(results.size(), 2);
120
121                assertEquals(results.get(0).getErrorLevel(),
122                                ValidationResultInfo.ErrorLevel.ERROR);
123                assertEquals(results.get(0).getMessage(), "validation.validCharsFailed");
124        }
125
126        @Test
127        public void testDoubleValueRange() {
128
129                ConstraintMockPerson p = buildTestPerson2();
130                p.setGpa(5.0);
131
132                ObjectStructureDefinition o1 = getSimpleStudentObjectStructure();
133
134                List<ValidationResultInfo> results = val.validateObject(p, o1, ContextInfoTestUtility.getEnglishContextInfo());
135                assertEquals(results.size(), 1);
136
137                assertEquals(results.get(0).getErrorLevel(), ValidationResultInfo.ErrorLevel.ERROR);
138                assertEquals(results.get(0).getMessage(), "validation.outOfRange");
139        }
140
141        @Test
142        public void testNestedCaseConstraint() {
143
144                ConstraintMockPerson p = buildTestPerson4();
145
146                ObjectStructureDefinition o1 = getStudentWithAddressObjectStructure();
147
148                List<ValidationResultInfo> results = val.validateObject(p, o1, ContextInfoTestUtility.getEnglishContextInfo());
149                assertEquals(2, results.size());                                
150        
151                p.getAddress().get(0).setState("ACTIVE");
152                results = val.validateObject(p, o1, ContextInfoTestUtility.getEnglishContextInfo());
153                
154                assertEquals(4, results.size());
155        }
156                
157
158        @Test
159        public void testNestedStructures() {
160                ConstraintMockPerson p = buildTestPerson3();
161
162                ObjectStructureDefinition o = getStudentWithAddressObjectStructure();
163
164                List<ValidationResultInfo> results = val.validateObject(p, o, ContextInfoTestUtility.getEnglishContextInfo());
165                // ERROR address/0/line1 validation.required
166                // ERROR address/0/line2 validation.validCharsFailed
167                // ERROR address/0/line2 validation.requiresField
168                System.out.println(results.size() + " errors found");
169                for (ValidationResultInfo vri : results) {
170                        System.out.println(vri.getErrorLevel() + " " + vri.getElement()
171                                        + " " + vri.getMessage());
172                }
173                assertEquals(4, results.size());
174
175                assertEquals(results.get(0).getErrorLevel(), ValidationResultInfo.ErrorLevel.ERROR);
176                assertEquals(results.get(0).getMessage(), "validation.required");
177
178                assertEquals(results.get(1).getErrorLevel(), ValidationResultInfo.ErrorLevel.ERROR);
179                assertEquals(results.get(1).getMessage(), "validation.validCharsFailed");
180
181                assertEquals(results.get(2).getErrorLevel(), ValidationResultInfo.ErrorLevel.ERROR);            
182                assertEquals(results.get(2).getMessage(), "validation.requiresField");
183
184        SearchServiceDispatcherImpl searchDispatcher = new SearchServiceDispatcherImpl();
185        List<SearchService> searchServices = new ArrayList<SearchService>();
186        searchServices.add(new MockSearchService());
187        searchDispatcher.setSearchServices(searchServices);
188
189                val.setSearchDispatcher(searchDispatcher);
190                p.getAddress().get(0).setLine1("something");
191                results = val.validateObject(p, o, ContextInfoTestUtility.getEnglishContextInfo());
192                System.out.println(results.size() + " errors found");
193                for (ValidationResultInfo vri : results) {
194                        System.out.println(vri.getErrorLevel() + " " + vri.getElement()
195                                        + " " + vri.getMessage());
196                }
197                assertEquals(3, results.size());
198
199                p.getAddress().get(0).setLine2("notrightlookupvalue");
200                results = val.validateObject(p, o, ContextInfoTestUtility.getEnglishContextInfo());
201                System.out.println(results.size() + " errors found");
202                for (ValidationResultInfo vri : results) {
203                        System.out.println(vri.getErrorLevel() + " " + vri.getElement()
204                                        + " " + vri.getMessage());
205                }
206                assertEquals(2, results.size());
207                assertEquals(results.get(0).getErrorLevel(),
208                                ValidationResultInfo.ErrorLevel.ERROR);
209                assertEquals(results.get(0).getMessage(), "validation.lookup");
210        }
211
212        public class MockSearchService implements SearchService {
213
214                @Override
215        public List<TypeInfo> getSearchTypes(@WebParam(name = "contextInfo") ContextInfo contextInfo) throws InvalidParameterException, MissingParameterException, OperationFailedException {
216            return null;  //To change body of implemented methods use File | Settings | File Templates.
217        }
218
219        @Override
220        public TypeInfo getSearchType(@WebParam(name = "searchTypeKey") String searchTypeKey, @WebParam(name = "contextInfo") ContextInfo contextInfo) throws DoesNotExistException, InvalidParameterException, MissingParameterException, OperationFailedException {
221            return null;  //To change body of implemented methods use File | Settings | File Templates.
222        }
223
224        @Override
225        public List<TypeInfo> getSearchTypesByResult(@WebParam(name = "searchResultTypeKey") String searchResultTypeKey, @WebParam(name = "contextInfo") ContextInfo contextInfo) throws DoesNotExistException, InvalidParameterException, MissingParameterException, OperationFailedException {
226            return null;  //To change body of implemented methods use File | Settings | File Templates.
227        }
228
229        @Override
230        public List<TypeInfo> getSearchTypesByCriteria(@WebParam(name = "searchCriteriaTypeKey") String searchCriteriaTypeKey, @WebParam(name = "contextInfo") ContextInfo contextInfo) throws DoesNotExistException, InvalidParameterException, MissingParameterException, OperationFailedException {
231            return null;  //To change body of implemented methods use File | Settings | File Templates.
232        }
233
234        @Override
235        public List<TypeInfo> getSearchResultTypes(@WebParam(name = "contextInfo") ContextInfo contextInfo) throws InvalidParameterException, MissingParameterException, OperationFailedException {
236            return null;  //To change body of implemented methods use File | Settings | File Templates.
237        }
238
239        @Override
240        public List<TypeInfo> getSearchCriteriaTypes(@WebParam(name = "contextInfo") ContextInfo contextInfo) throws InvalidParameterException, MissingParameterException, OperationFailedException {
241            return null;  //To change body of implemented methods use File | Settings | File Templates.
242        }
243
244        @Override
245        public SearchResultInfo search(SearchRequestInfo searchRequestInfo, @WebParam(name = "contextInfo") ContextInfo contextInfo) throws MissingParameterException, OperationFailedException, PermissionDeniedException {
246            if (searchRequestInfo != null
247                    && searchRequestInfo.getParams() != null
248                    && "param1".equals(searchRequestInfo.getParams().get(0)
249                    .getKey())
250                    && "line2value".equals(searchRequestInfo.getParams().get(0)
251                    .getValues().get(0))) {
252                SearchResultInfo result = new SearchResultInfo();
253                SearchResultRowInfo row = new SearchResultRowInfo();
254                result.getRows().add(row);
255                return result;
256            }
257            return null;
258        }
259    }
260
261        public ConstraintMockPerson buildTestPerson1() {
262                return ValidatorMockObjectGenerator.buildTestPerson1();
263        }
264
265        public ConstraintMockPerson buildTestPerson2() {
266                return ValidatorMockObjectGenerator.buildTestPerson2();
267        }
268
269        public ConstraintMockPerson buildTestPerson3() {
270                return ValidatorMockObjectGenerator.buildTestPerson3();
271        }
272
273        public ConstraintMockPerson buildTestPerson4() {
274                return ValidatorMockObjectGenerator.buildTestPerson4();
275        }
276
277        public ObjectStructureDefinition getSimpleStudentObjectStructure() {
278                return dictionaryDelegate.getObjectStructure("simpleStudent");
279        }
280
281        public ObjectStructureDefinition getStudentWithAddressObjectStructure() {
282                return dictionaryDelegate.getObjectStructure("studentWithAddress");
283        }
284}