View Javadoc

1   /**
2    * Copyright 2005-2011 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 #testValueInvalidPhoneNumberEmpty()}
43   * 2. value with valid phone number. (success) {@link #testValueValidPhoneNumber()}
44   * 3. value with invalid phone number. (failure) {@link testValueInvalidPhoneNumber()}
45   * 4. value with invalid phone number. (failure) {@link testValueInvalidPhoneNumber1()}
46   * 5. value with invalid phone number. (failure) {@link testValueInvalidPhoneNumber2()}
47   * 6. value with invalid phone number. (failure) {@link testValueInvalidPhoneNumber3()}
48   * 7. value with invalid phone number. (failure) {@link testValueInvalidPhoneNumber4()}
49   * 8. value with invalid phone number. (failure) {@link testValueInvalidPhoneNumber5()}
50   * 9. value with invalid phone number. (failure) {@link testValueInvalidPhoneNumber6()}
51   * 
52   * 
53   * @author Kuali Rice Team (rice.collab@kuali.org) 
54   */
55  public class PhoneNumberPatternConstraintTest {
56  	
57  	private final String PATTERN_CONSTRAINT = "validationPatternRegex.phoneNumber";
58  
59  	private AttributeDefinition contactPhoneDefinition;
60  	
61  	private BusinessObjectEntry addressEntry;
62  	private DictionaryValidationResult dictionaryValidationResult;
63  	
64  	private ValidCharactersConstraintProcessor processor;
65  	
66  	private Employee validPhoneEmployee = new Employee();	
67  	private Employee invalidPhoneEmployeeEmpty = new Employee();
68  	private Employee invalidPhoneEmployee = new Employee();
69  	private Employee invalidPhoneEmployee1 = new Employee();
70  	private Employee invalidPhoneEmployee2 = new Employee();
71  	private Employee invalidPhoneEmployee3 = new Employee();
72  	private Employee invalidPhoneEmployee4 = new Employee();
73  	private Employee invalidPhoneEmployee5 = new Employee();
74  	private Employee invalidPhoneEmployee6 = new Employee();
75  	
76  	private ConfigurationBasedRegexPatternConstraint contactPhoneNumberPatternConstraint;		
77  	
78  	@Before
79  	public void setUp() throws Exception {	
80  		
81  		String regex = getProperty(PATTERN_CONSTRAINT);
82  		
83  		processor = new ValidCharactersConstraintProcessor();
84  		
85  		dictionaryValidationResult = new DictionaryValidationResult();
86  		dictionaryValidationResult.setErrorLevel(ErrorLevel.NOCONSTRAINT);
87  		
88  		addressEntry = new BusinessObjectEntry();		
89  		
90  		List<AttributeDefinition> attributes = new ArrayList<AttributeDefinition>();
91  		
92  		validPhoneEmployee.setContactPhone("056-012-1200");	
93  		invalidPhoneEmployeeEmpty.setContactPhone("");
94  		invalidPhoneEmployee.setContactPhone("09712248474");
95  		invalidPhoneEmployee1.setContactPhone("+19712248474");
96  		invalidPhoneEmployee2.setContactPhone("+1-972-232-3333");
97  		invalidPhoneEmployee3.setContactPhone("+1-(972)-23334444");
98  		invalidPhoneEmployee4.setContactPhone("+1-(972)-1223444 xtn.222");
99  		invalidPhoneEmployee5.setContactPhone("+1 056 012 1200");
100 		invalidPhoneEmployee6.setContactPhone("056\\-012\\-1200");
101 			
102 		contactPhoneNumberPatternConstraint = new ConfigurationBasedRegexPatternConstraint();
103 		contactPhoneNumberPatternConstraint.setValue(regex);
104 
105 		contactPhoneDefinition = new AttributeDefinition();
106 		contactPhoneDefinition.setName("contactPhone");
107 		contactPhoneDefinition.setValidCharactersConstraint(contactPhoneNumberPatternConstraint);
108 		attributes.add(contactPhoneDefinition);									
109 		
110 		addressEntry.setAttributes(attributes);	
111 	}
112 	
113 	@Test
114 	public void testValueInvalidPhoneNumberEmpty() {
115 		ConstraintValidationResult result = process(invalidPhoneEmployeeEmpty, "contactPhone", contactPhoneNumberPatternConstraint);
116 		Assert.assertEquals(0, dictionaryValidationResult.getNumberOfWarnings());
117 		Assert.assertEquals(0, dictionaryValidationResult.getNumberOfErrors());
118 		Assert.assertEquals(ErrorLevel.INAPPLICABLE, result.getStatus());
119 		Assert.assertEquals(new ValidCharactersConstraintProcessor().getName(), result.getConstraintName());
120 	}
121 	
122 	@Test
123 	public void testValueValidPhoneNumber() {
124 		ConstraintValidationResult result = process(validPhoneEmployee, "contactPhone", contactPhoneNumberPatternConstraint);
125 		Assert.assertEquals(0, dictionaryValidationResult.getNumberOfWarnings());
126 		Assert.assertEquals(0, dictionaryValidationResult.getNumberOfErrors());
127 		Assert.assertEquals(ErrorLevel.OK, result.getStatus());
128 		Assert.assertEquals(new ValidCharactersConstraintProcessor().getName(), result.getConstraintName());
129 	}
130 		
131 	
132 	@Test
133 	public void testValueInvalidPhoneNumber() {
134 		ConstraintValidationResult result = process(invalidPhoneEmployee, "contactPhone", contactPhoneNumberPatternConstraint);
135 		Assert.assertEquals(0, dictionaryValidationResult.getNumberOfWarnings());
136 		Assert.assertEquals(1, dictionaryValidationResult.getNumberOfErrors());
137 		Assert.assertEquals(ErrorLevel.ERROR, result.getStatus());
138 		Assert.assertEquals(new ValidCharactersConstraintProcessor().getName(), result.getConstraintName());
139 	}
140 	
141 	@Test
142 	public void testValueInvalidPhoneNumber1() {
143 		ConstraintValidationResult result = process(invalidPhoneEmployee1, "contactPhone", contactPhoneNumberPatternConstraint);
144 		Assert.assertEquals(0, dictionaryValidationResult.getNumberOfWarnings());
145 		Assert.assertEquals(1, dictionaryValidationResult.getNumberOfErrors());
146 		Assert.assertEquals(ErrorLevel.ERROR, result.getStatus());
147 		Assert.assertEquals(new ValidCharactersConstraintProcessor().getName(), result.getConstraintName());
148 	}
149 	
150 	@Test
151 	public void testValueInvalidPhoneNumber2() {
152 		ConstraintValidationResult result = process(invalidPhoneEmployee2, "contactPhone", contactPhoneNumberPatternConstraint);
153 		Assert.assertEquals(0, dictionaryValidationResult.getNumberOfWarnings());
154 		Assert.assertEquals(1, dictionaryValidationResult.getNumberOfErrors());
155 		Assert.assertEquals(ErrorLevel.ERROR, result.getStatus());
156 		Assert.assertEquals(new ValidCharactersConstraintProcessor().getName(), result.getConstraintName());
157 	}
158 	
159 	@Test
160 	public void testValueInvalidPhoneNumber3() {
161 		ConstraintValidationResult result = process(invalidPhoneEmployee3, "contactPhone", contactPhoneNumberPatternConstraint);
162 		Assert.assertEquals(0, dictionaryValidationResult.getNumberOfWarnings());
163 		Assert.assertEquals(1, dictionaryValidationResult.getNumberOfErrors());
164 		Assert.assertEquals(ErrorLevel.ERROR, result.getStatus());
165 		Assert.assertEquals(new ValidCharactersConstraintProcessor().getName(), result.getConstraintName());
166 	}
167 	
168 	@Test
169 	public void testValueInvalidPhoneNumber4() {
170 		ConstraintValidationResult result = process(invalidPhoneEmployee4, "contactPhone", contactPhoneNumberPatternConstraint);
171 		Assert.assertEquals(0, dictionaryValidationResult.getNumberOfWarnings());
172 		Assert.assertEquals(1, dictionaryValidationResult.getNumberOfErrors());
173 		Assert.assertEquals(ErrorLevel.ERROR, result.getStatus());
174 		Assert.assertEquals(new ValidCharactersConstraintProcessor().getName(), result.getConstraintName());
175 	}
176 	
177 	@Test
178 	public void testValueInvalidPhoneNumber5() {
179 		ConstraintValidationResult result = process(invalidPhoneEmployee5, "contactPhone", contactPhoneNumberPatternConstraint);
180 		Assert.assertEquals(0, dictionaryValidationResult.getNumberOfWarnings());
181 		Assert.assertEquals(1, dictionaryValidationResult.getNumberOfErrors());
182 		Assert.assertEquals(ErrorLevel.ERROR, result.getStatus());
183 		Assert.assertEquals(new ValidCharactersConstraintProcessor().getName(), result.getConstraintName());
184 	}
185 	
186 	@Test
187 	public void testValueInvalidPhoneNumber6() {
188 		ConstraintValidationResult result = process(invalidPhoneEmployee6, "contactPhone", contactPhoneNumberPatternConstraint);
189 		Assert.assertEquals(0, dictionaryValidationResult.getNumberOfWarnings());
190 		Assert.assertEquals(1, dictionaryValidationResult.getNumberOfErrors());
191 		Assert.assertEquals(ErrorLevel.ERROR, result.getStatus());
192 		Assert.assertEquals(new ValidCharactersConstraintProcessor().getName(), result.getConstraintName());
193 	}
194 	
195 	private ConstraintValidationResult process(Object object, String attributeName, ValidCharactersConstraint constraint) {
196 		AttributeValueReader attributeValueReader = new DictionaryObjectAttributeValueReader(object, "org.kuali.rice.kns.datadictionary.validation.MockAddress", addressEntry);
197 		attributeValueReader.setAttributeName(attributeName);
198 		
199 		Object value = attributeValueReader.getValue();
200 		return processor.process(dictionaryValidationResult, value, constraint, attributeValueReader).getFirstConstraintValidationResult();
201 	}
202 	
203 	private String getProperty(String key) {
204 		String value = null;
205 		String filePath = "org/kuali/rice/krad/ApplicationResources.properties";
206 		Properties properties = new Properties();
207 		try {			
208 			InputStream in = Thread.currentThread().getContextClassLoader().getResourceAsStream(filePath);
209 			properties.load(in);
210 			value = properties.getProperty(key);			
211 		} catch (IOException e) {		
212 		}
213 		return value;	
214 	}
215 }