001 /** 002 * Copyright 2005-2012 The Kuali Foundation 003 * 004 * Licensed under the Educational Community License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.opensource.org/licenses/ecl2.php 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 package org.kuali.rice.krad.datadictionary.validation.constraint; 017 018 import java.io.IOException; 019 import java.io.InputStream; 020 import java.util.ArrayList; 021 import java.util.List; 022 import java.util.Properties; 023 024 import org.junit.Assert; 025 import org.junit.Before; 026 import org.junit.Test; 027 import org.kuali.rice.krad.datadictionary.AttributeDefinition; 028 import org.kuali.rice.krad.datadictionary.BusinessObjectEntry; 029 import org.kuali.rice.krad.datadictionary.validation.AttributeValueReader; 030 import org.kuali.rice.krad.datadictionary.validation.DictionaryObjectAttributeValueReader; 031 import org.kuali.rice.krad.datadictionary.validation.Employee; 032 import org.kuali.rice.krad.datadictionary.validation.ErrorLevel; 033 import org.kuali.rice.krad.datadictionary.validation.constraint.ValidCharactersConstraint; 034 import org.kuali.rice.krad.datadictionary.validation.processor.ValidCharactersConstraintProcessor; 035 import org.kuali.rice.krad.datadictionary.validation.result.ConstraintValidationResult; 036 import org.kuali.rice.krad.datadictionary.validation.result.DictionaryValidationResult; 037 038 039 /** 040 * Things this test should check: 041 * 042 * 1. empty value check. (failure) {@link #testValueInvalidEmailAddressEmpty()} 043 * 2. value with valid email address. (success) {@link #testValueValidEmailAddress()} 044 * 3. value with valid email address. (success) {@link #testValueValidEmailAddress1()} 045 * 4. value with valid email address. (success) {@link #testValueValidEmailAddress2()} 046 * 5. value with valid email address. (success) {@link #testValueValidEmailAddress3()} 047 * 6. value with valid email address. (success) {@link #testValueValidEmailAddress4()} 048 * 7. value with valid email address. (success) {@link #testValueValidEmailAddress5()} 049 * 8. value with invalid email address. (failure) {@link testValueInvalidEmailAddress()} 050 * 9. value with invalid email address. (failure) {@link testValueInvalidEmailAddress1()} 051 * 10. value with invalid email address. (failure) {@link testValueInvalidEmailAddress2()} 052 * 11. value with invalid email address. (failure) {@link testValueInvalidEmailAddress3()} 053 * 12. value with invalid email address. (failure) {@link testValueInvalidEmailAddress4()} 054 * 13. value with invalid email address. (failure) {@link testValueInvalidEmailAddress5()} 055 * 14. value with invalid email address. (failure) {@link testValueInvalidEmailAddress6()} 056 * 15. value with invalid email address. (failure) {@link testValueInvalidEmailAddress7()} 057 * 058 * 059 * @author Kuali Rice Team (rice.collab@kuali.org) 060 */ 061 public class EmailAddressPatternConstraintTest { 062 063 private final String PATTERN_CONSTRAINT = "validationPatternRegex.emailAddress"; 064 065 private AttributeDefinition contactEmailDefinition; 066 067 private BusinessObjectEntry addressEntry; 068 private DictionaryValidationResult dictionaryValidationResult; 069 070 private ValidCharactersConstraintProcessor processor; 071 072 private Employee validEmailEmployee = new Employee(); 073 private Employee validEmailEmployee1 = new Employee(); 074 private Employee validEmailEmployee2 = new Employee(); 075 private Employee validEmailEmployee3 = new Employee(); 076 private Employee validEmailEmployee4 = new Employee(); 077 private Employee validEmailEmployee5 = new Employee(); 078 private Employee invalidEmailEmployeeEmpty = new Employee(); 079 private Employee invalidEmailEmployee = new Employee(); 080 private Employee invalidEmailEmployee1 = new Employee(); 081 private Employee invalidEmailEmployee2 = new Employee(); 082 private Employee invalidEmailEmployee3 = new Employee(); 083 private Employee invalidEmailEmployee4 = new Employee(); 084 private Employee invalidEmailEmployee5 = new Employee(); 085 private Employee invalidEmailEmployee6 = new Employee(); 086 private Employee invalidEmailEmployee7 = new Employee(); 087 088 private ConfigurationBasedRegexPatternConstraint contactEmailAddressPatternConstraint; 089 090 @Before 091 public void setUp() throws Exception { 092 093 String regex = getProperty(PATTERN_CONSTRAINT); 094 095 processor = new ValidCharactersConstraintProcessor(); 096 097 dictionaryValidationResult = new DictionaryValidationResult(); 098 dictionaryValidationResult.setErrorLevel(ErrorLevel.NOCONSTRAINT); 099 100 addressEntry = new BusinessObjectEntry(); 101 102 List<AttributeDefinition> attributes = new ArrayList<AttributeDefinition>(); 103 104 validEmailEmployee.setContactEmail("ww5@a.b.c.org"); 105 validEmailEmployee1.setContactEmail("something.else@a2.com"); 106 validEmailEmployee2.setContactEmail("something_else@something.else.com"); 107 validEmailEmployee3.setContactEmail("something-else@et-tu.com"); 108 validEmailEmployee4.setContactEmail("dmerkal@gmail.com"); 109 validEmailEmployee5.setContactEmail("m.modi@gmail.com"); 110 invalidEmailEmployeeEmpty.setContactEmail(""); 111 invalidEmailEmployee.setContactEmail("@a.b.c.org"); 112 invalidEmailEmployee1.setContactEmail("a"); 113 invalidEmailEmployee2.setContactEmail("1@a.b.c.org"); 114 invalidEmailEmployee3.setContactEmail("1@org"); 115 invalidEmailEmployee4.setContactEmail("1@a"); 116 invalidEmailEmployee5.setContactEmail(".@a.org"); 117 invalidEmailEmployee6.setContactEmail("_@a.org"); 118 invalidEmailEmployee7.setContactEmail("something@a.o-rg"); 119 120 contactEmailAddressPatternConstraint = new ConfigurationBasedRegexPatternConstraint(); 121 contactEmailAddressPatternConstraint.setValue(regex); 122 123 contactEmailDefinition = new AttributeDefinition(); 124 contactEmailDefinition.setName("contactEmail"); 125 contactEmailDefinition.setValidCharactersConstraint(contactEmailAddressPatternConstraint); 126 attributes.add(contactEmailDefinition); 127 128 addressEntry.setAttributes(attributes); 129 } 130 131 @Test 132 public void testValueInvalidEmailAddressEmpty() { 133 ConstraintValidationResult result = process(invalidEmailEmployeeEmpty, "contactEmail", contactEmailAddressPatternConstraint); 134 Assert.assertEquals(0, dictionaryValidationResult.getNumberOfWarnings()); 135 Assert.assertEquals(0, dictionaryValidationResult.getNumberOfErrors()); 136 Assert.assertEquals(ErrorLevel.INAPPLICABLE, result.getStatus()); 137 Assert.assertEquals(new ValidCharactersConstraintProcessor().getName(), result.getConstraintName()); 138 } 139 140 @Test 141 public void testValueValidEmailAddress() { 142 ConstraintValidationResult result = process(validEmailEmployee, "contactEmail", contactEmailAddressPatternConstraint); 143 Assert.assertEquals(0, dictionaryValidationResult.getNumberOfWarnings()); 144 Assert.assertEquals(0, dictionaryValidationResult.getNumberOfErrors()); 145 Assert.assertEquals(ErrorLevel.OK, result.getStatus()); 146 Assert.assertEquals(new ValidCharactersConstraintProcessor().getName(), result.getConstraintName()); 147 } 148 149 @Test 150 public void testValueValidEmailAddress1() { 151 ConstraintValidationResult result = process(validEmailEmployee1, "contactEmail", contactEmailAddressPatternConstraint); 152 Assert.assertEquals(0, dictionaryValidationResult.getNumberOfWarnings()); 153 Assert.assertEquals(0, dictionaryValidationResult.getNumberOfErrors()); 154 Assert.assertEquals(ErrorLevel.OK, result.getStatus()); 155 Assert.assertEquals(new ValidCharactersConstraintProcessor().getName(), result.getConstraintName()); 156 } 157 158 @Test 159 public void testValueValidEmailAddress2() { 160 ConstraintValidationResult result = process(validEmailEmployee2, "contactEmail", contactEmailAddressPatternConstraint); 161 Assert.assertEquals(0, dictionaryValidationResult.getNumberOfWarnings()); 162 Assert.assertEquals(0, dictionaryValidationResult.getNumberOfErrors()); 163 Assert.assertEquals(ErrorLevel.OK, result.getStatus()); 164 Assert.assertEquals(new ValidCharactersConstraintProcessor().getName(), result.getConstraintName()); 165 } 166 167 @Test 168 public void testValueValidEmailAddress3() { 169 ConstraintValidationResult result = process(validEmailEmployee3, "contactEmail", contactEmailAddressPatternConstraint); 170 Assert.assertEquals(0, dictionaryValidationResult.getNumberOfWarnings()); 171 Assert.assertEquals(0, dictionaryValidationResult.getNumberOfErrors()); 172 Assert.assertEquals(ErrorLevel.OK, result.getStatus()); 173 Assert.assertEquals(new ValidCharactersConstraintProcessor().getName(), result.getConstraintName()); 174 } 175 176 @Test 177 public void testValueValidEmailAddress4() { 178 ConstraintValidationResult result = process(validEmailEmployee4, "contactEmail", contactEmailAddressPatternConstraint); 179 Assert.assertEquals(0, dictionaryValidationResult.getNumberOfWarnings()); 180 Assert.assertEquals(0, dictionaryValidationResult.getNumberOfErrors()); 181 Assert.assertEquals(ErrorLevel.OK, result.getStatus()); 182 Assert.assertEquals(new ValidCharactersConstraintProcessor().getName(), result.getConstraintName()); 183 } 184 185 @Test 186 public void testValueValidEmailAddress5() { 187 ConstraintValidationResult result = process(validEmailEmployee5, "contactEmail", contactEmailAddressPatternConstraint); 188 Assert.assertEquals(0, dictionaryValidationResult.getNumberOfWarnings()); 189 Assert.assertEquals(0, dictionaryValidationResult.getNumberOfErrors()); 190 Assert.assertEquals(ErrorLevel.OK, result.getStatus()); 191 Assert.assertEquals(new ValidCharactersConstraintProcessor().getName(), result.getConstraintName()); 192 } 193 194 @Test 195 public void testValueInvalidEmailAddress() { 196 ConstraintValidationResult result = process(invalidEmailEmployee, "contactEmail", contactEmailAddressPatternConstraint); 197 Assert.assertEquals(0, dictionaryValidationResult.getNumberOfWarnings()); 198 Assert.assertEquals(1, dictionaryValidationResult.getNumberOfErrors()); 199 Assert.assertEquals(ErrorLevel.ERROR, result.getStatus()); 200 Assert.assertEquals(new ValidCharactersConstraintProcessor().getName(), result.getConstraintName()); 201 } 202 203 @Test 204 public void testValueInvalidEmailAddress1() { 205 ConstraintValidationResult result = process(invalidEmailEmployee1, "contactEmail", contactEmailAddressPatternConstraint); 206 Assert.assertEquals(0, dictionaryValidationResult.getNumberOfWarnings()); 207 Assert.assertEquals(1, dictionaryValidationResult.getNumberOfErrors()); 208 Assert.assertEquals(ErrorLevel.ERROR, result.getStatus()); 209 Assert.assertEquals(new ValidCharactersConstraintProcessor().getName(), result.getConstraintName()); 210 } 211 212 @Test 213 public void testValueInvalidEmailAddress2() { 214 ConstraintValidationResult result = process(invalidEmailEmployee2, "contactEmail", contactEmailAddressPatternConstraint); 215 Assert.assertEquals(0, dictionaryValidationResult.getNumberOfWarnings()); 216 Assert.assertEquals(1, dictionaryValidationResult.getNumberOfErrors()); 217 Assert.assertEquals(ErrorLevel.ERROR, result.getStatus()); 218 Assert.assertEquals(new ValidCharactersConstraintProcessor().getName(), result.getConstraintName()); 219 } 220 221 @Test 222 public void testValueInvalidEmailAddress3() { 223 ConstraintValidationResult result = process(invalidEmailEmployee3, "contactEmail", contactEmailAddressPatternConstraint); 224 Assert.assertEquals(0, dictionaryValidationResult.getNumberOfWarnings()); 225 Assert.assertEquals(1, dictionaryValidationResult.getNumberOfErrors()); 226 Assert.assertEquals(ErrorLevel.ERROR, result.getStatus()); 227 Assert.assertEquals(new ValidCharactersConstraintProcessor().getName(), result.getConstraintName()); 228 } 229 230 @Test 231 public void testValueInvalidEmailAddress4() { 232 ConstraintValidationResult result = process(invalidEmailEmployee4, "contactEmail", contactEmailAddressPatternConstraint); 233 Assert.assertEquals(0, dictionaryValidationResult.getNumberOfWarnings()); 234 Assert.assertEquals(1, dictionaryValidationResult.getNumberOfErrors()); 235 Assert.assertEquals(ErrorLevel.ERROR, result.getStatus()); 236 Assert.assertEquals(new ValidCharactersConstraintProcessor().getName(), result.getConstraintName()); 237 } 238 239 @Test 240 public void testValueInvalidEmailAddress5() { 241 ConstraintValidationResult result = process(invalidEmailEmployee5, "contactEmail", contactEmailAddressPatternConstraint); 242 Assert.assertEquals(0, dictionaryValidationResult.getNumberOfWarnings()); 243 Assert.assertEquals(1, dictionaryValidationResult.getNumberOfErrors()); 244 Assert.assertEquals(ErrorLevel.ERROR, result.getStatus()); 245 Assert.assertEquals(new ValidCharactersConstraintProcessor().getName(), result.getConstraintName()); 246 } 247 248 @Test 249 public void testValueInvalidEmailAddress6() { 250 ConstraintValidationResult result = process(invalidEmailEmployee6, "contactEmail", contactEmailAddressPatternConstraint); 251 Assert.assertEquals(0, dictionaryValidationResult.getNumberOfWarnings()); 252 Assert.assertEquals(1, dictionaryValidationResult.getNumberOfErrors()); 253 Assert.assertEquals(ErrorLevel.ERROR, result.getStatus()); 254 Assert.assertEquals(new ValidCharactersConstraintProcessor().getName(), result.getConstraintName()); 255 } 256 257 @Test 258 public void testValueInvalidEmailAddress7() { 259 ConstraintValidationResult result = process(invalidEmailEmployee7, "contactEmail", contactEmailAddressPatternConstraint); 260 Assert.assertEquals(0, dictionaryValidationResult.getNumberOfWarnings()); 261 Assert.assertEquals(1, dictionaryValidationResult.getNumberOfErrors()); 262 Assert.assertEquals(ErrorLevel.ERROR, result.getStatus()); 263 Assert.assertEquals(new ValidCharactersConstraintProcessor().getName(), result.getConstraintName()); 264 } 265 266 private ConstraintValidationResult process(Object object, String attributeName, ValidCharactersConstraint constraint) { 267 AttributeValueReader attributeValueReader = new DictionaryObjectAttributeValueReader(object, "org.kuali.rice.kns.datadictionary.validation.MockAddress", addressEntry); 268 attributeValueReader.setAttributeName(attributeName); 269 270 Object value = attributeValueReader.getValue(); 271 return processor.process(dictionaryValidationResult, value, constraint, attributeValueReader).getFirstConstraintValidationResult(); 272 } 273 274 private String getProperty(String key) { 275 String value = null; 276 String filePath = "org/kuali/rice/krad/ApplicationResources.properties"; 277 Properties properties = new Properties(); 278 try { 279 InputStream in = Thread.currentThread().getContextClassLoader().getResourceAsStream(filePath); 280 properties.load(in); 281 value = properties.getProperty(key); 282 } catch (IOException e) { 283 } 284 return value; 285 } 286 }