001    /*
002     * Copyright 2011 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/ecl1.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.util.ArrayList;
019    import java.util.List;
020    
021    import org.junit.Assert;
022    import org.junit.Before;
023    import org.junit.Test;
024    import org.kuali.rice.krad.datadictionary.AttributeDefinition;
025    import org.kuali.rice.krad.datadictionary.BusinessObjectEntry;
026    import org.kuali.rice.krad.datadictionary.validation.Address;
027    import org.kuali.rice.krad.datadictionary.validation.AttributeValueReader;
028    import org.kuali.rice.krad.datadictionary.validation.DictionaryObjectAttributeValueReader;
029    import org.kuali.rice.krad.datadictionary.validation.ErrorLevel;
030    import org.kuali.rice.krad.datadictionary.validation.constraint.ValidCharactersConstraint;
031    import org.kuali.rice.krad.datadictionary.validation.processor.ValidCharactersConstraintProcessor;
032    import org.kuali.rice.krad.datadictionary.validation.result.ConstraintValidationResult;
033    import org.kuali.rice.krad.datadictionary.validation.result.DictionaryValidationResult;
034    
035    
036    /**
037     * Things this test should check:
038     * 
039     * 1. value with all valid characters. (success) {@link #testValueAllValidChars()}
040     * 2. value with invalid characters. (failure) {@link #testValueNotValidChars()}
041     * 3. value with all valid characters. Allowing white space.(success) {@link #testValueAllValidCharsAllowWhitespace()}
042     * 4. value with invalid characters. Allowing white space. (failure) {@link #testValueNotValidCharsAllowWhitespace()}
043     * 5. value with all valid characters. Allowing white space, period, underscore and parenthesis. (success) {@link #testValueAllValidCharsAllowWhitespaceAndPeriodAndUnderscoreAndParenthesis()}
044     * 6. value with invalid characters. Allowing white space, period, underscore and parenthesis. (failure) {@link #testValueNotValidCharsAllowWhitespaceAndPeriodAndUnderscoreAndParenthesis()}
045     * 
046     * @author Kuali Rice Team (rice.collab@kuali.org) 
047     */
048    public class AlphaNumericPatternConstraintTest {
049    
050            private AttributeDefinition street1Definition;
051            private AttributeDefinition street2Definition;  
052            private AttributeDefinition postalCodeDefinition;       
053            
054            private BusinessObjectEntry addressEntry;
055            private DictionaryValidationResult dictionaryValidationResult;
056            
057            private ValidCharactersConstraintProcessor processor;
058            
059            private Address washingtonDCAddress = new Address("893 Presidential Ave", "(A_123) Suite 800.", "Washington", "DC", "NHW123A", "USA", null);
060            private Address newYorkNYAddress = new Address("Presidential Street", "(A-123) Suite 800", "New York", "NY", "ZH 3456", "USA", null);
061            private Address sydneyAUSAddress = new Address("Presidential Street-Ave.", "Suite_800.", "Sydney", "ZZ", "ZH-5656", "USA", null);
062            
063            private AlphaNumericPatternConstraint street1AlphaNumericPatternConstraint;
064            private AlphaNumericPatternConstraint street2AlphaNumericPatternConstraint;     
065            private AlphaNumericPatternConstraint postalCodeAlphaNumericPatternConstraint;  
066            
067            @Before
068            public void setUp() throws Exception {
069                    
070                    processor = new ValidCharactersConstraintProcessor();
071                    
072                    dictionaryValidationResult = new DictionaryValidationResult();
073                    dictionaryValidationResult.setErrorLevel(ErrorLevel.NOCONSTRAINT);
074                    
075                    addressEntry = new BusinessObjectEntry();
076                    
077                    List<AttributeDefinition> attributes = new ArrayList<AttributeDefinition>();                                                
078                    
079                    street1AlphaNumericPatternConstraint = new AlphaNumericPatternConstraint();
080                    street1AlphaNumericPatternConstraint.setAllowWhitespace(true);
081                    
082                    street1Definition = new AttributeDefinition();
083                    street1Definition.setName("street1");
084                    street1Definition.setValidCharactersConstraint(street1AlphaNumericPatternConstraint);
085                    attributes.add(street1Definition);      
086                    
087                    
088                    street2AlphaNumericPatternConstraint = new AlphaNumericPatternConstraint();
089                    street2AlphaNumericPatternConstraint.setAllowWhitespace(true);
090                    street2AlphaNumericPatternConstraint.setAllowParenthesis(true);
091                    street2AlphaNumericPatternConstraint.setAllowPeriod(true);
092                    street2AlphaNumericPatternConstraint.setAllowUnderscore(true);
093                    
094                    street2Definition = new AttributeDefinition();
095                    street2Definition.setName("street2");
096                    street2Definition.setValidCharactersConstraint(street2AlphaNumericPatternConstraint);
097                    attributes.add(street2Definition);
098                    
099                    postalCodeAlphaNumericPatternConstraint = new AlphaNumericPatternConstraint();
100                    
101                    postalCodeDefinition = new AttributeDefinition();
102                    postalCodeDefinition.setName("postalCode");
103                    postalCodeDefinition.setValidCharactersConstraint(postalCodeAlphaNumericPatternConstraint);
104                    attributes.add(postalCodeDefinition);
105                    
106                    addressEntry.setAttributes(attributes); 
107            }
108            
109            @Test
110            public void testValueAllValidChars() {
111                    ConstraintValidationResult result = process(washingtonDCAddress, "postalCode", postalCodeAlphaNumericPatternConstraint);
112                    Assert.assertEquals(0, dictionaryValidationResult.getNumberOfWarnings());
113                    Assert.assertEquals(0, dictionaryValidationResult.getNumberOfErrors());
114                    Assert.assertEquals(ErrorLevel.OK, result.getStatus());
115                    Assert.assertEquals(new ValidCharactersConstraintProcessor().getName(), result.getConstraintName());
116            }
117            
118            @Test
119            public void testValueNotValidChars() {
120                    ConstraintValidationResult result = process(newYorkNYAddress, "postalCode", postalCodeAlphaNumericPatternConstraint);
121                    Assert.assertEquals(0, dictionaryValidationResult.getNumberOfWarnings());
122                    Assert.assertEquals(1, dictionaryValidationResult.getNumberOfErrors());
123                    Assert.assertEquals(ErrorLevel.ERROR, result.getStatus());
124                    Assert.assertEquals(new ValidCharactersConstraintProcessor().getName(), result.getConstraintName());
125            }
126            
127            @Test
128            public void testValueAllValidCharsAllowWhitespace() {
129                    ConstraintValidationResult result = process(newYorkNYAddress, "street1", street1AlphaNumericPatternConstraint);
130                    Assert.assertEquals(0, dictionaryValidationResult.getNumberOfWarnings());
131                    Assert.assertEquals(0, dictionaryValidationResult.getNumberOfErrors());
132                    Assert.assertEquals(ErrorLevel.OK, result.getStatus());
133                    Assert.assertEquals(new ValidCharactersConstraintProcessor().getName(), result.getConstraintName());
134            }
135            
136            @Test
137            public void testValueNotValidCharsAllowWhitespace() {
138                    ConstraintValidationResult result = process(sydneyAUSAddress, "street1", street1AlphaNumericPatternConstraint);
139                    Assert.assertEquals(0, dictionaryValidationResult.getNumberOfWarnings());
140                    Assert.assertEquals(1, dictionaryValidationResult.getNumberOfErrors());
141                    Assert.assertEquals(ErrorLevel.ERROR, result.getStatus());
142                    Assert.assertEquals(new ValidCharactersConstraintProcessor().getName(), result.getConstraintName());
143            }
144            
145            @Test
146            public void testValueAllValidCharsAllowWhitespaceAndPeriodAndUnderscoreAndParenthesis() {
147                    ConstraintValidationResult result = process(washingtonDCAddress, "street2", street2AlphaNumericPatternConstraint);
148                    Assert.assertEquals(0, dictionaryValidationResult.getNumberOfWarnings());
149                    Assert.assertEquals(0, dictionaryValidationResult.getNumberOfErrors());
150                    Assert.assertEquals(ErrorLevel.OK, result.getStatus());
151                    Assert.assertEquals(new ValidCharactersConstraintProcessor().getName(), result.getConstraintName());
152            }
153            
154            @Test
155            public void testValueNotValidCharsAllowWhitespaceAndPeriodAndUnderscoreAndParenthesis() {
156                    ConstraintValidationResult result = process(newYorkNYAddress, "street2", street2AlphaNumericPatternConstraint);
157                    Assert.assertEquals(0, dictionaryValidationResult.getNumberOfWarnings());
158                    Assert.assertEquals(1, dictionaryValidationResult.getNumberOfErrors());
159                    Assert.assertEquals(ErrorLevel.ERROR, result.getStatus());
160                    Assert.assertEquals(new ValidCharactersConstraintProcessor().getName(), result.getConstraintName());
161            }
162                    
163            private ConstraintValidationResult process(Object object, String attributeName, ValidCharactersConstraint constraint) {
164                    AttributeValueReader attributeValueReader = new DictionaryObjectAttributeValueReader(object, "org.kuali.rice.kns.datadictionary.validation.MockAddress", addressEntry);
165                    attributeValueReader.setAttributeName(attributeName);
166                    
167                    Object value = attributeValueReader.getValue();
168                    return processor.process(dictionaryValidationResult, value, constraint, attributeValueReader).getFirstConstraintValidationResult();
169            }
170    }