View Javadoc
1   /**
2    * Copyright 2005-2014 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.rice.krad.datadictionary.validation.constraint;
17  
18  import java.io.IOException;
19  import java.io.InputStream;
20  import java.util.ArrayList;
21  import java.util.List;
22  import java.util.Properties;
23  
24  import org.junit.Assert;
25  import org.junit.Before;
26  import org.junit.Test;
27  import org.kuali.rice.krad.datadictionary.AttributeDefinition;
28  import org.kuali.rice.krad.datadictionary.BusinessObjectEntry;
29  import org.kuali.rice.krad.datadictionary.validation.AttributeValueReader;
30  import org.kuali.rice.krad.datadictionary.validation.DictionaryObjectAttributeValueReader;
31  import org.kuali.rice.krad.datadictionary.validation.Employee;
32  import org.kuali.rice.krad.datadictionary.validation.ErrorLevel;
33  import org.kuali.rice.krad.datadictionary.validation.constraint.ValidCharactersConstraint;
34  import org.kuali.rice.krad.datadictionary.validation.processor.ValidCharactersConstraintProcessor;
35  import org.kuali.rice.krad.datadictionary.validation.result.ConstraintValidationResult;
36  import org.kuali.rice.krad.datadictionary.validation.result.DictionaryValidationResult;
37  
38  
39  /**
40   * Things this test should check:
41   *
42   * 1. empty value check. (failure) {@link #testValueInvalidEmailAddressEmpty()}
43   * 2. value with valid email address. (success) {@link #testValueValidEmailAddress()}
44   * 3. value with valid email address. (success) {@link #testValueValidEmailAddress1()}
45   * 4. value with valid email address. (success) {@link #testValueValidEmailAddress2()}
46   * 5. value with valid email address. (success) {@link #testValueValidEmailAddress3()}
47   * 6. value with valid email address. (success) {@link #testValueValidEmailAddress4()}
48   * 7. value with valid email address. (success) {@link #testValueValidEmailAddress5()}
49   * 8. value with invalid email address. (failure) {@link #testValueInvalidEmailAddress()}
50   * 9. value with invalid email address. (failure) {@link #testValueInvalidEmailAddress1()}
51   * 10. value with invalid email address. (failure) {@link #testValueInvalidEmailAddress2()}
52   * 11. value with invalid email address. (failure) {@link #testValueInvalidEmailAddress3()}
53   * 12. value with invalid email address. (failure) {@link #testValueInvalidEmailAddress4()}
54   * 13. value with invalid email address. (failure) {@link #testValueInvalidEmailAddress5()}
55   * 14. value with invalid email address. (failure) {@link #testValueInvalidEmailAddress6()}
56   * 15. value with invalid email address. (failure) {@link #testValueInvalidEmailAddress7()}
57   *
58   *
59   * @author Kuali Rice Team (rice.collab@kuali.org)
60   */
61  public class EmailAddressPatternConstraintTest {
62  
63  	private final String PATTERN_CONSTRAINT = "validationPatternRegex.emailAddress";
64  
65  	private AttributeDefinition contactEmailDefinition;
66  
67  	private BusinessObjectEntry addressEntry;
68  	private DictionaryValidationResult dictionaryValidationResult;
69  
70  	private ValidCharactersConstraintProcessor processor;
71  
72  	private Employee validEmailEmployee = new Employee();
73  	private Employee validEmailEmployee1 = new Employee();
74  	private Employee validEmailEmployee2 = new Employee();
75  	private Employee validEmailEmployee3 = new Employee();
76  	private Employee validEmailEmployee4 = new Employee();
77  	private Employee validEmailEmployee5 = new Employee();
78  	private Employee invalidEmailEmployeeEmpty = new Employee();
79  	private Employee invalidEmailEmployee = new Employee();
80  	private Employee invalidEmailEmployee1 = new Employee();
81  	private Employee invalidEmailEmployee2 = new Employee();
82  	private Employee invalidEmailEmployee3 = new Employee();
83  	private Employee invalidEmailEmployee4 = new Employee();
84  	private Employee invalidEmailEmployee5 = new Employee();
85  	private Employee invalidEmailEmployee6 = new Employee();
86  	private Employee invalidEmailEmployee7 = new Employee();
87  
88  	private ConfigurationBasedRegexPatternConstraint contactEmailAddressPatternConstraint;
89  
90  	@Before
91  	public void setUp() throws Exception {
92  
93  		String regex = getProperty(PATTERN_CONSTRAINT);
94  
95  		processor = new ValidCharactersConstraintProcessor();
96  
97  		dictionaryValidationResult = new DictionaryValidationResult();
98  		dictionaryValidationResult.setErrorLevel(ErrorLevel.NOCONSTRAINT);
99  
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@org");
114 		invalidEmailEmployee3.setContactEmail("1@a");
115 		invalidEmailEmployee4.setContactEmail("_@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 }