View Javadoc

1   /**
2    * Copyright 2005-2012 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@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 }