001/** 002 * Copyright 2005-2016 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 */ 016package org.kuali.rice.krad.datadictionary.validation.constraint; 017 018import java.io.IOException; 019import java.io.InputStream; 020import java.util.ArrayList; 021import java.util.List; 022import java.util.Properties; 023 024import org.junit.Assert; 025import org.junit.Before; 026import org.junit.Test; 027import org.kuali.rice.krad.datadictionary.AttributeDefinition; 028import org.kuali.rice.krad.datadictionary.BusinessObjectEntry; 029import org.kuali.rice.krad.datadictionary.validation.AttributeValueReader; 030import org.kuali.rice.krad.datadictionary.validation.DictionaryObjectAttributeValueReader; 031import org.kuali.rice.krad.datadictionary.validation.Employee; 032import org.kuali.rice.krad.datadictionary.validation.ErrorLevel; 033import org.kuali.rice.krad.datadictionary.validation.constraint.ValidCharactersConstraint; 034import org.kuali.rice.krad.datadictionary.validation.processor.ValidCharactersConstraintProcessor; 035import org.kuali.rice.krad.datadictionary.validation.result.ConstraintValidationResult; 036import 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 */ 061public 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.setMessageKey("validate.dummykey"); 122 contactEmailAddressPatternConstraint.setValidationMessageParams( new ArrayList<String>()); 123 contactEmailAddressPatternConstraint.setValue(regex); 124 125 contactEmailDefinition = new AttributeDefinition(); 126 contactEmailDefinition.setName("contactEmail"); 127 contactEmailDefinition.setValidCharactersConstraint(contactEmailAddressPatternConstraint); 128 attributes.add(contactEmailDefinition); 129 130 addressEntry.setAttributes(attributes); 131 } 132 133 @Test 134 public void testValueInvalidEmailAddressEmpty() { 135 ConstraintValidationResult result = process(invalidEmailEmployeeEmpty, "contactEmail", contactEmailAddressPatternConstraint); 136 Assert.assertEquals(0, dictionaryValidationResult.getNumberOfWarnings()); 137 Assert.assertEquals(0, dictionaryValidationResult.getNumberOfErrors()); 138 Assert.assertEquals(ErrorLevel.INAPPLICABLE, result.getStatus()); 139 Assert.assertEquals(new ValidCharactersConstraintProcessor().getName(), result.getConstraintName()); 140 } 141 142 @Test 143 public void testValueValidEmailAddress() { 144 ConstraintValidationResult result = process(validEmailEmployee, "contactEmail", contactEmailAddressPatternConstraint); 145 Assert.assertEquals(0, dictionaryValidationResult.getNumberOfWarnings()); 146 Assert.assertEquals(0, dictionaryValidationResult.getNumberOfErrors()); 147 Assert.assertEquals(ErrorLevel.OK, result.getStatus()); 148 Assert.assertEquals(new ValidCharactersConstraintProcessor().getName(), result.getConstraintName()); 149 } 150 151 @Test 152 public void testValueValidEmailAddress1() { 153 ConstraintValidationResult result = process(validEmailEmployee1, "contactEmail", contactEmailAddressPatternConstraint); 154 Assert.assertEquals(0, dictionaryValidationResult.getNumberOfWarnings()); 155 Assert.assertEquals(0, dictionaryValidationResult.getNumberOfErrors()); 156 Assert.assertEquals(ErrorLevel.OK, result.getStatus()); 157 Assert.assertEquals(new ValidCharactersConstraintProcessor().getName(), result.getConstraintName()); 158 } 159 160 @Test 161 public void testValueValidEmailAddress2() { 162 ConstraintValidationResult result = process(validEmailEmployee2, "contactEmail", contactEmailAddressPatternConstraint); 163 Assert.assertEquals(0, dictionaryValidationResult.getNumberOfWarnings()); 164 Assert.assertEquals(0, dictionaryValidationResult.getNumberOfErrors()); 165 Assert.assertEquals(ErrorLevel.OK, result.getStatus()); 166 Assert.assertEquals(new ValidCharactersConstraintProcessor().getName(), result.getConstraintName()); 167 } 168 169 @Test 170 public void testValueValidEmailAddress3() { 171 ConstraintValidationResult result = process(validEmailEmployee3, "contactEmail", contactEmailAddressPatternConstraint); 172 Assert.assertEquals(0, dictionaryValidationResult.getNumberOfWarnings()); 173 Assert.assertEquals(0, dictionaryValidationResult.getNumberOfErrors()); 174 Assert.assertEquals(ErrorLevel.OK, result.getStatus()); 175 Assert.assertEquals(new ValidCharactersConstraintProcessor().getName(), result.getConstraintName()); 176 } 177 178 @Test 179 public void testValueValidEmailAddress4() { 180 ConstraintValidationResult result = process(validEmailEmployee4, "contactEmail", contactEmailAddressPatternConstraint); 181 Assert.assertEquals(0, dictionaryValidationResult.getNumberOfWarnings()); 182 Assert.assertEquals(0, dictionaryValidationResult.getNumberOfErrors()); 183 Assert.assertEquals(ErrorLevel.OK, result.getStatus()); 184 Assert.assertEquals(new ValidCharactersConstraintProcessor().getName(), result.getConstraintName()); 185 } 186 187 @Test 188 public void testValueValidEmailAddress5() { 189 ConstraintValidationResult result = process(validEmailEmployee5, "contactEmail", contactEmailAddressPatternConstraint); 190 Assert.assertEquals(0, dictionaryValidationResult.getNumberOfWarnings()); 191 Assert.assertEquals(0, dictionaryValidationResult.getNumberOfErrors()); 192 Assert.assertEquals(ErrorLevel.OK, result.getStatus()); 193 Assert.assertEquals(new ValidCharactersConstraintProcessor().getName(), result.getConstraintName()); 194 } 195 196 @Test 197 public void testValueInvalidEmailAddress() { 198 ConstraintValidationResult result = process(invalidEmailEmployee, "contactEmail", contactEmailAddressPatternConstraint); 199 Assert.assertEquals(0, dictionaryValidationResult.getNumberOfWarnings()); 200 Assert.assertEquals(1, dictionaryValidationResult.getNumberOfErrors()); 201 Assert.assertEquals(ErrorLevel.ERROR, result.getStatus()); 202 Assert.assertEquals(new ValidCharactersConstraintProcessor().getName(), result.getConstraintName()); 203 } 204 205 @Test 206 public void testValueInvalidEmailAddress1() { 207 ConstraintValidationResult result = process(invalidEmailEmployee1, "contactEmail", contactEmailAddressPatternConstraint); 208 Assert.assertEquals(0, dictionaryValidationResult.getNumberOfWarnings()); 209 Assert.assertEquals(1, dictionaryValidationResult.getNumberOfErrors()); 210 Assert.assertEquals(ErrorLevel.ERROR, result.getStatus()); 211 Assert.assertEquals(new ValidCharactersConstraintProcessor().getName(), result.getConstraintName()); 212 } 213 214 @Test 215 public void testValueInvalidEmailAddress2() { 216 ConstraintValidationResult result = process(invalidEmailEmployee2, "contactEmail", contactEmailAddressPatternConstraint); 217 Assert.assertEquals(0, dictionaryValidationResult.getNumberOfWarnings()); 218 Assert.assertEquals(1, dictionaryValidationResult.getNumberOfErrors()); 219 Assert.assertEquals(ErrorLevel.ERROR, result.getStatus()); 220 Assert.assertEquals(new ValidCharactersConstraintProcessor().getName(), result.getConstraintName()); 221 } 222 223 @Test 224 public void testValueInvalidEmailAddress3() { 225 ConstraintValidationResult result = process(invalidEmailEmployee3, "contactEmail", contactEmailAddressPatternConstraint); 226 Assert.assertEquals(0, dictionaryValidationResult.getNumberOfWarnings()); 227 Assert.assertEquals(1, dictionaryValidationResult.getNumberOfErrors()); 228 Assert.assertEquals(ErrorLevel.ERROR, result.getStatus()); 229 Assert.assertEquals(new ValidCharactersConstraintProcessor().getName(), result.getConstraintName()); 230 } 231 232 @Test 233 public void testValueInvalidEmailAddress4() { 234 ConstraintValidationResult result = process(invalidEmailEmployee4, "contactEmail", contactEmailAddressPatternConstraint); 235 Assert.assertEquals(0, dictionaryValidationResult.getNumberOfWarnings()); 236 Assert.assertEquals(1, dictionaryValidationResult.getNumberOfErrors()); 237 Assert.assertEquals(ErrorLevel.ERROR, result.getStatus()); 238 Assert.assertEquals(new ValidCharactersConstraintProcessor().getName(), result.getConstraintName()); 239 } 240 241 @Test 242 public void testValueInvalidEmailAddress5() { 243 ConstraintValidationResult result = process(invalidEmailEmployee5, "contactEmail", contactEmailAddressPatternConstraint); 244 Assert.assertEquals(0, dictionaryValidationResult.getNumberOfWarnings()); 245 Assert.assertEquals(1, dictionaryValidationResult.getNumberOfErrors()); 246 Assert.assertEquals(ErrorLevel.ERROR, result.getStatus()); 247 Assert.assertEquals(new ValidCharactersConstraintProcessor().getName(), result.getConstraintName()); 248 } 249 250 @Test 251 public void testValueInvalidEmailAddress6() { 252 ConstraintValidationResult result = process(invalidEmailEmployee6, "contactEmail", contactEmailAddressPatternConstraint); 253 Assert.assertEquals(0, dictionaryValidationResult.getNumberOfWarnings()); 254 Assert.assertEquals(1, dictionaryValidationResult.getNumberOfErrors()); 255 Assert.assertEquals(ErrorLevel.ERROR, result.getStatus()); 256 Assert.assertEquals(new ValidCharactersConstraintProcessor().getName(), result.getConstraintName()); 257 } 258 259 @Test 260 public void testValueInvalidEmailAddress7() { 261 ConstraintValidationResult result = process(invalidEmailEmployee7, "contactEmail", contactEmailAddressPatternConstraint); 262 Assert.assertEquals(0, dictionaryValidationResult.getNumberOfWarnings()); 263 Assert.assertEquals(1, dictionaryValidationResult.getNumberOfErrors()); 264 Assert.assertEquals(ErrorLevel.ERROR, result.getStatus()); 265 Assert.assertEquals(new ValidCharactersConstraintProcessor().getName(), result.getConstraintName()); 266 } 267 268 private ConstraintValidationResult process(Object object, String attributeName, ValidCharactersConstraint constraint) { 269 AttributeValueReader attributeValueReader = new DictionaryObjectAttributeValueReader(object, "org.kuali.rice.kns.datadictionary.validation.MockAddress", addressEntry); 270 attributeValueReader.setAttributeName(attributeName); 271 272 Object value = attributeValueReader.getValue(); 273 return processor.process(dictionaryValidationResult, value, constraint, attributeValueReader).getFirstConstraintValidationResult(); 274 } 275 276 private String getProperty(String key) { 277 String value = null; 278 String filePath = "org/kuali/rice/krad/ApplicationResources.properties"; 279 Properties properties = new Properties(); 280 try { 281 InputStream in = Thread.currentThread().getContextClassLoader().getResourceAsStream(filePath); 282 properties.load(in); 283 value = properties.getProperty(key); 284 } catch (IOException e) { 285 } 286 return value; 287 } 288}