001    /**
002     * Copyright 2005-2014 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.Address;
030    import org.kuali.rice.krad.datadictionary.validation.AttributeValueReader;
031    import org.kuali.rice.krad.datadictionary.validation.DictionaryObjectAttributeValueReader;
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. value with valid zipcode. (success) {@link #testValueValidZipcode()}
043     * 2. value with valid zipcode. (success) {@link #testValueValidZipcode1()}
044     * 3. value with invalid zipcode. (failure) {@link #testValueInvalidZipcode()}
045     * 4. value with invalid zipcode. (failure) {@link #testValueInvalidZipcode1()}
046     * 5. value with invalid zipcode. (failure) {@link #testValueInvalidZipcode2()}
047     *
048     * @author Kuali Rice Team (rice.collab@kuali.org)
049     */
050    public class ZipcodePatternConstraintTest {
051    
052            private final String PATTERN_CONSTRAINT = "validationPatternRegex.zipcode";
053    
054            private AttributeDefinition postalCodeDefinition;
055    
056            private BusinessObjectEntry addressEntry;
057            private DictionaryValidationResult dictionaryValidationResult;
058    
059            private ValidCharactersConstraintProcessor processor;
060    
061            private Address washingtonDCAddress = new Address("893 Presidential Ave", "(A_123) Suite 800.", "Washington", "DC", "12345", "USA", null);
062            private Address newYorkNYAddress = new Address("Presidential Street", "(A-123) Suite 800", "New York", "NY", "12345-1234", "USA", null);
063            private Address sydneySDAddress = new Address("Presidential Street-Ave.", "Suite_800.", "Sydney", "SD", "ZH-123456", "Australia", null);
064            private Address holandHDAddress = new Address("Presidential Street-Ave.", "Suite_800.", "Holand", "HD", "12345-123", "Holand", null);
065            private Address bombayMHAddress = new Address("Presidential Street-Ave.", "Suite_800.", "Bombay", "MH", "380002", "India", null);
066    
067            private ConfigurationBasedRegexPatternConstraint postalCodePatternConstraint;
068    
069            @Before
070            public void setUp() throws Exception {
071    
072                    String regex = getProperty(PATTERN_CONSTRAINT);
073    
074                    processor = new ValidCharactersConstraintProcessor();
075    
076                    dictionaryValidationResult = new DictionaryValidationResult();
077                    dictionaryValidationResult.setErrorLevel(ErrorLevel.NOCONSTRAINT);
078    
079                    addressEntry = new BusinessObjectEntry();
080    
081                    List<AttributeDefinition> attributes = new ArrayList<AttributeDefinition>();
082    
083                    postalCodePatternConstraint = new ConfigurationBasedRegexPatternConstraint();
084            postalCodePatternConstraint.setMessageKey("validate.dummykey");
085            postalCodePatternConstraint.setValidationMessageParams( new ArrayList<String>());
086                    postalCodePatternConstraint.setValue(regex);
087    
088                    postalCodeDefinition = new AttributeDefinition();
089                    postalCodeDefinition.setName("postalCode");
090                    postalCodeDefinition.setValidCharactersConstraint(postalCodePatternConstraint);
091                    attributes.add(postalCodeDefinition);
092    
093                    addressEntry.setAttributes(attributes);
094            }
095    
096            @Test
097            public void testValueValidZipcode() {
098                    ConstraintValidationResult result = process(washingtonDCAddress, "postalCode", postalCodePatternConstraint);
099                    Assert.assertEquals(0, dictionaryValidationResult.getNumberOfWarnings());
100                    Assert.assertEquals(0, dictionaryValidationResult.getNumberOfErrors());
101                    Assert.assertEquals(ErrorLevel.OK, result.getStatus());
102                    Assert.assertEquals(new ValidCharactersConstraintProcessor().getName(), result.getConstraintName());
103            }
104    
105            @Test
106            public void testValueValidZipcode1() {
107                    ConstraintValidationResult result = process(newYorkNYAddress, "postalCode", postalCodePatternConstraint);
108                    Assert.assertEquals(0, dictionaryValidationResult.getNumberOfWarnings());
109                    Assert.assertEquals(0, dictionaryValidationResult.getNumberOfErrors());
110                    Assert.assertEquals(ErrorLevel.OK, result.getStatus());
111                    Assert.assertEquals(new ValidCharactersConstraintProcessor().getName(), result.getConstraintName());
112            }
113    
114            @Test
115            public void testValueInvalidZipcode() {
116                    ConstraintValidationResult result = process(sydneySDAddress, "postalCode", postalCodePatternConstraint);
117                    Assert.assertEquals(0, dictionaryValidationResult.getNumberOfWarnings());
118                    Assert.assertEquals(1, dictionaryValidationResult.getNumberOfErrors());
119                    Assert.assertEquals(ErrorLevel.ERROR, result.getStatus());
120                    Assert.assertEquals(new ValidCharactersConstraintProcessor().getName(), result.getConstraintName());
121            }
122    
123            @Test
124            public void testValueInvalidZipcode1() {
125                    ConstraintValidationResult result = process(holandHDAddress, "postalCode", postalCodePatternConstraint);
126                    Assert.assertEquals(0, dictionaryValidationResult.getNumberOfWarnings());
127                    Assert.assertEquals(1, dictionaryValidationResult.getNumberOfErrors());
128                    Assert.assertEquals(ErrorLevel.ERROR, result.getStatus());
129                    Assert.assertEquals(new ValidCharactersConstraintProcessor().getName(), result.getConstraintName());
130            }
131    
132            @Test
133            public void testValueInvalidZipcode2() {
134                    ConstraintValidationResult result = process(bombayMHAddress, "postalCode", postalCodePatternConstraint);
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            private ConstraintValidationResult process(Object object, String attributeName, ValidCharactersConstraint constraint) {
142                    AttributeValueReader attributeValueReader = new DictionaryObjectAttributeValueReader(object, "org.kuali.rice.kns.datadictionary.validation.MockAddress", addressEntry);
143                    attributeValueReader.setAttributeName(attributeName);
144    
145                    Object value = attributeValueReader.getValue();
146                    return processor.process(dictionaryValidationResult, value, constraint, attributeValueReader).getFirstConstraintValidationResult();
147            }
148    
149            private String getProperty(String key) {
150                    String value = null;
151                    String filePath = "org/kuali/rice/krad/ApplicationResources.properties";
152                    Properties properties = new Properties();
153                    try {
154                            InputStream in = Thread.currentThread().getContextClassLoader().getResourceAsStream(filePath);
155                            properties.load(in);
156                            value = properties.getProperty(key);
157                    } catch (IOException e) {
158                    }
159                    return value;
160            }
161    }