001    /**
002     * Copyright 2005-2013 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.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. (success) {@link #test2ValueAllValidChars()}
042     * 4. value with invalid characters. (failure) {@link #test2ValueNotValidChars()}
043     *
044     * @author Kuali Rice Team (rice.collab@kuali.org)
045     */
046    public class CharsetPatternConstraintTest {
047    
048            private AttributeDefinition countryDefinition;
049            private AttributeDefinition postalCodeDefinition;
050    
051            private BusinessObjectEntry addressEntry;
052            private DictionaryValidationResult dictionaryValidationResult;
053    
054            private ValidCharactersConstraintProcessor processor;
055    
056            private Address washingtonDCAddress = new Address("893  Presidential Ave", "(A_123) Suite 800.", "Washington", "DC", "NHW123A", "United States of America", null);
057            private Address newYorkNYAddress = new Address("Presidential Street", "(A-123) Suite 800", "New York", "NY", "ZH 3456", "USA", null);
058            private Address sydneyAUSAddress = new Address("Presidential Street-Ave.", "Suite_800.", "Sydney", "ZZ", "(ZH-5656)", "USA", null);
059    
060            private CharsetPatternConstraint countryCharsetPatternConstraint;
061            private CharsetPatternConstraint postalCodeCharsetPatternConstraint;
062    
063            @Before
064            public void setUp() throws Exception {
065    
066                    processor = new ValidCharactersConstraintProcessor();
067    
068                    dictionaryValidationResult = new DictionaryValidationResult();
069                    dictionaryValidationResult.setErrorLevel(ErrorLevel.NOCONSTRAINT);
070    
071                    addressEntry = new BusinessObjectEntry();
072    
073                    List<AttributeDefinition> attributes = new ArrayList<AttributeDefinition>();
074    
075                    countryCharsetPatternConstraint = new CharsetPatternConstraint();
076            countryCharsetPatternConstraint.setMessageKey("validate.dummykey");
077            countryCharsetPatternConstraint.setValidationMessageParams( new ArrayList<String>());
078                    countryCharsetPatternConstraint.setValidChars("USA");
079    
080                    countryDefinition = new AttributeDefinition();
081                    countryDefinition.setName("country");
082                    countryDefinition.setValidCharactersConstraint(countryCharsetPatternConstraint);
083                    attributes.add(countryDefinition);
084    
085    
086                    postalCodeCharsetPatternConstraint = new CharsetPatternConstraint();
087            countryCharsetPatternConstraint.setMessageKey("validate.dummykey");
088            countryCharsetPatternConstraint.setValidationMessageParams( new ArrayList<String>());
089                    postalCodeCharsetPatternConstraint.setValidChars("(ZH-123456)");
090    
091                    postalCodeDefinition = new AttributeDefinition();
092                    postalCodeDefinition.setName("postalCode");
093                    postalCodeDefinition.setValidCharactersConstraint(postalCodeCharsetPatternConstraint);
094                    attributes.add(postalCodeDefinition);
095    
096                    addressEntry.setAttributes(attributes);
097            }
098    
099            @Test
100            public void testValueAllValidChars() {
101                    ConstraintValidationResult result = process(newYorkNYAddress, "country", countryCharsetPatternConstraint);
102                    Assert.assertEquals(0, dictionaryValidationResult.getNumberOfWarnings());
103                    Assert.assertEquals(0, dictionaryValidationResult.getNumberOfErrors());
104                    Assert.assertEquals(ErrorLevel.OK, result.getStatus());
105                    Assert.assertEquals(new ValidCharactersConstraintProcessor().getName(), result.getConstraintName());
106            }
107    
108            @Test
109            public void testValueNotValidChars() {
110                    ConstraintValidationResult result = process(washingtonDCAddress, "country", countryCharsetPatternConstraint);
111                    Assert.assertEquals(0, dictionaryValidationResult.getNumberOfWarnings());
112                    Assert.assertEquals(1, dictionaryValidationResult.getNumberOfErrors());
113                    Assert.assertEquals(ErrorLevel.ERROR, result.getStatus());
114                    Assert.assertEquals(new ValidCharactersConstraintProcessor().getName(), result.getConstraintName());
115            }
116    
117            @Test
118            public void test2ValueAllValidChars() {
119                    ConstraintValidationResult result = process(sydneyAUSAddress, "postalCode", postalCodeCharsetPatternConstraint);
120                    Assert.assertEquals(0, dictionaryValidationResult.getNumberOfWarnings());
121                    Assert.assertEquals(0, dictionaryValidationResult.getNumberOfErrors());
122                    Assert.assertEquals(ErrorLevel.OK, result.getStatus());
123                    Assert.assertEquals(new ValidCharactersConstraintProcessor().getName(), result.getConstraintName());
124            }
125    
126            @Test
127            public void test2ValueNotValidChars() {
128                    ConstraintValidationResult result = process(washingtonDCAddress, "postalCode", postalCodeCharsetPatternConstraint);
129                    Assert.assertEquals(0, dictionaryValidationResult.getNumberOfWarnings());
130                    Assert.assertEquals(1, dictionaryValidationResult.getNumberOfErrors());
131                    Assert.assertEquals(ErrorLevel.ERROR, result.getStatus());
132                    Assert.assertEquals(new ValidCharactersConstraintProcessor().getName(), result.getConstraintName());
133            }
134    
135            private ConstraintValidationResult process(Object object, String attributeName, ValidCharactersConstraint constraint) {
136                    AttributeValueReader attributeValueReader = new DictionaryObjectAttributeValueReader(object, "org.kuali.rice.kns.datadictionary.validation.MockAddress", addressEntry);
137                    attributeValueReader.setAttributeName(attributeName);
138    
139                    Object value = attributeValueReader.getValue();
140                    return processor.process(dictionaryValidationResult, value, constraint, attributeValueReader).getFirstConstraintValidationResult();
141            }
142    }