001 package org.kuali.student.r2.common.validator; 002 003 import org.junit.Before; 004 import org.junit.Test; 005 import org.kuali.student.common.test.util.ContextInfoTestUtility; 006 import org.kuali.student.r1.common.dictionary.dto.ObjectStructureDefinition; 007 import org.kuali.student.r1.common.dictionary.service.impl.DictionaryServiceImpl; 008 import org.kuali.student.r1.common.validator.ServerDateParser; 009 import org.kuali.student.r2.common.class1.search.SearchServiceDispatcherImpl; 010 import org.kuali.student.r2.common.dto.ContextInfo; 011 import org.kuali.student.r2.common.dto.ValidationResultInfo; 012 import org.kuali.student.r2.common.exceptions.*; 013 import org.kuali.student.r2.core.search.dto.*; 014 import org.kuali.student.r2.core.search.service.SearchService; 015 import org.kuali.student.r2.core.class1.type.dto.TypeInfo; 016 017 import javax.jws.WebParam; 018 import java.util.ArrayList; 019 import java.util.List; 020 021 import static org.junit.Assert.assertEquals; 022 023 public class TestValidator { 024 DefaultValidatorImpl val = null; 025 ValidatorFactory valFactory = new ValidatorFactory(); 026 DictionaryServiceImpl dictionaryDelegate = new DictionaryServiceImpl( 027 "classpath:test-validator-context.xml"); 028 029 @Before 030 public void init() { 031 032 val = new DefaultValidatorImpl(); 033 val.setDateParser(new ServerDateParser()); 034 val.setMessageService(null); 035 valFactory.setDefaultValidator(val); 036 037 } 038 039 @Test 040 public void testRequired() { 041 042 List<ValidationResultInfo> results = val.validateObject(buildTestPerson1(), getSimpleStudentObjectStructure(), ContextInfoTestUtility.getEnglishContextInfo()); 043 assertEquals(results.size(), 1); 044 045 assertEquals(results.get(0).getErrorLevel(), ValidationResultInfo.ErrorLevel.ERROR); 046 assertEquals(results.get(0).getMessage(), "validation.required"); 047 } 048 049 @Test 050 public void testLengthRange() { 051 052 ConstraintMockPerson p = buildTestPerson1(); 053 p.setFirstName("thisisaveryveryverylo"); 054 055 List<ValidationResultInfo> results = val.validateObject(p, getSimpleStudentObjectStructure(), ContextInfoTestUtility.getEnglishContextInfo()); 056 assertEquals(results.size(), 2); 057 058 assertEquals(results.get(0).getErrorLevel(), ValidationResultInfo.ErrorLevel.ERROR); 059 assertEquals(results.get(0).getMessage(), "validation.lengthOutOfRange"); 060 } 061 062 @Test 063 public void testMinLength() { 064 065 ConstraintMockPerson p = buildTestPerson1(); 066 p.setFirstName("t"); 067 068 ObjectStructureDefinition o1 = getSimpleStudentObjectStructure(); 069 o1.getAttributes().get(0).setMaxLength(null); 070 071 List<ValidationResultInfo> results = val.validateObject(p, o1, ContextInfoTestUtility.getEnglishContextInfo()); 072 assertEquals(results.size(), 2); 073 074 assertEquals(results.get(0).getErrorLevel(), ValidationResultInfo.ErrorLevel.ERROR); 075 assertEquals(results.get(0).getMessage(), "validation.minLengthFailed"); 076 } 077 078 @Test 079 public void testMinDateValue() { 080 ConstraintMockPerson p = buildTestPerson1(); 081 ServerDateParser sp = new ServerDateParser(); 082 p.setDob(sp.parseDate("1960-01-01")); 083 ObjectStructureDefinition o1 = getSimpleStudentObjectStructure(); 084 085 List<ValidationResultInfo> results = val.validateObject(p, o1, ContextInfoTestUtility.getEnglishContextInfo()); 086 assertEquals(results.size(), 1); 087 088 assertEquals(results.get(0).getErrorLevel(), ValidationResultInfo.ErrorLevel.ERROR); 089 assertEquals(results.get(0).getMessage(), "validation.minValueFailed"); 090 } 091 092 @Test 093 public void testMaxLength() { 094 095 ConstraintMockPerson p = buildTestPerson1(); 096 p.setFirstName("thisisaveryveryverylo"); 097 098 ObjectStructureDefinition o1 = getSimpleStudentObjectStructure(); 099 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 }