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 #testValueInvalidPhoneNumberEmpty()}
043     * 2. value with valid phone number. (success) {@link #testValueValidPhoneNumber()}
044     * 3. value with invalid phone number. (failure) {@link testValueInvalidPhoneNumber()}
045     * 4. value with invalid phone number. (failure) {@link testValueInvalidPhoneNumber1()}
046     * 5. value with invalid phone number. (failure) {@link testValueInvalidPhoneNumber2()}
047     * 6. value with invalid phone number. (failure) {@link testValueInvalidPhoneNumber3()}
048     * 7. value with invalid phone number. (failure) {@link testValueInvalidPhoneNumber4()}
049     * 8. value with invalid phone number. (failure) {@link testValueInvalidPhoneNumber5()}
050     * 9. value with invalid phone number. (failure) {@link testValueInvalidPhoneNumber6()}
051     * 
052     * 
053     * @author Kuali Rice Team (rice.collab@kuali.org) 
054     */
055    public class PhoneNumberPatternConstraintTest {
056            
057            private final String PATTERN_CONSTRAINT = "validationPatternRegex.phoneNumber";
058    
059            private AttributeDefinition contactPhoneDefinition;
060            
061            private BusinessObjectEntry addressEntry;
062            private DictionaryValidationResult dictionaryValidationResult;
063            
064            private ValidCharactersConstraintProcessor processor;
065            
066            private Employee validPhoneEmployee = new Employee();   
067            private Employee invalidPhoneEmployeeEmpty = new Employee();
068            private Employee invalidPhoneEmployee = new Employee();
069            private Employee invalidPhoneEmployee1 = new Employee();
070            private Employee invalidPhoneEmployee2 = new Employee();
071            private Employee invalidPhoneEmployee3 = new Employee();
072            private Employee invalidPhoneEmployee4 = new Employee();
073            private Employee invalidPhoneEmployee5 = new Employee();
074            private Employee invalidPhoneEmployee6 = new Employee();
075            
076            private ConfigurationBasedRegexPatternConstraint contactPhoneNumberPatternConstraint;           
077            
078            @Before
079            public void setUp() throws Exception {  
080                    
081                    String regex = getProperty(PATTERN_CONSTRAINT);
082                    
083                    processor = new ValidCharactersConstraintProcessor();
084                    
085                    dictionaryValidationResult = new DictionaryValidationResult();
086                    dictionaryValidationResult.setErrorLevel(ErrorLevel.NOCONSTRAINT);
087                    
088                    addressEntry = new BusinessObjectEntry();               
089                    
090                    List<AttributeDefinition> attributes = new ArrayList<AttributeDefinition>();
091                    
092                    validPhoneEmployee.setContactPhone("056-012-1200");     
093                    invalidPhoneEmployeeEmpty.setContactPhone("");
094                    invalidPhoneEmployee.setContactPhone("09712248474");
095                    invalidPhoneEmployee1.setContactPhone("+19712248474");
096                    invalidPhoneEmployee2.setContactPhone("+1-972-232-3333");
097                    invalidPhoneEmployee3.setContactPhone("+1-(972)-23334444");
098                    invalidPhoneEmployee4.setContactPhone("+1-(972)-1223444 xtn.222");
099                    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    }