001/**
002 * Copyright 2005-2015 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 */
016package org.kuali.rice.krad.datadictionary.validation.constraint;
017
018import java.util.ArrayList;
019import java.util.List;
020
021import org.junit.Assert;
022import org.junit.Before;
023import org.junit.Test;
024import org.kuali.rice.krad.datadictionary.AttributeDefinition;
025import org.kuali.rice.krad.datadictionary.BusinessObjectEntry;
026import org.kuali.rice.krad.datadictionary.validation.Address;
027import org.kuali.rice.krad.datadictionary.validation.AttributeValueReader;
028import org.kuali.rice.krad.datadictionary.validation.DictionaryObjectAttributeValueReader;
029import org.kuali.rice.krad.datadictionary.validation.ErrorLevel;
030import org.kuali.rice.krad.datadictionary.validation.constraint.ValidCharactersConstraint;
031import org.kuali.rice.krad.datadictionary.validation.processor.ValidCharactersConstraintProcessor;
032import org.kuali.rice.krad.datadictionary.validation.result.ConstraintValidationResult;
033import 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 */
046public 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.setValidChars("USA");
077        
078                countryDefinition = new AttributeDefinition();
079                countryDefinition.setName("country");
080                countryDefinition.setValidCharactersConstraint(countryCharsetPatternConstraint);
081                attributes.add(countryDefinition);      
082                
083                
084                postalCodeCharsetPatternConstraint = new CharsetPatternConstraint();    
085                postalCodeCharsetPatternConstraint.setValidChars("(ZH-123456)");
086                
087                postalCodeDefinition = new AttributeDefinition();
088                postalCodeDefinition.setName("postalCode");
089                postalCodeDefinition.setValidCharactersConstraint(postalCodeCharsetPatternConstraint);
090                attributes.add(postalCodeDefinition);
091                
092                addressEntry.setAttributes(attributes); 
093        }
094        
095        @Test
096        public void testValueAllValidChars() {
097                ConstraintValidationResult result = process(newYorkNYAddress, "country", countryCharsetPatternConstraint);
098                Assert.assertEquals(0, dictionaryValidationResult.getNumberOfWarnings());
099                Assert.assertEquals(0, dictionaryValidationResult.getNumberOfErrors());
100                Assert.assertEquals(ErrorLevel.OK, result.getStatus());
101                Assert.assertEquals(new ValidCharactersConstraintProcessor().getName(), result.getConstraintName());
102        }
103        
104        @Test
105        public void testValueNotValidChars() {
106                ConstraintValidationResult result = process(washingtonDCAddress, "country", countryCharsetPatternConstraint);
107                Assert.assertEquals(0, dictionaryValidationResult.getNumberOfWarnings());
108                Assert.assertEquals(1, dictionaryValidationResult.getNumberOfErrors());
109                Assert.assertEquals(ErrorLevel.ERROR, result.getStatus());
110                Assert.assertEquals(new ValidCharactersConstraintProcessor().getName(), result.getConstraintName());
111        }
112        
113        @Test
114        public void test2ValueAllValidChars() {
115                ConstraintValidationResult result = process(sydneyAUSAddress, "postalCode", postalCodeCharsetPatternConstraint);
116                Assert.assertEquals(0, dictionaryValidationResult.getNumberOfWarnings());
117                Assert.assertEquals(0, dictionaryValidationResult.getNumberOfErrors());
118                Assert.assertEquals(ErrorLevel.OK, result.getStatus());
119                Assert.assertEquals(new ValidCharactersConstraintProcessor().getName(), result.getConstraintName());
120        }       
121        
122        @Test
123        public void test2ValueNotValidChars() {
124                ConstraintValidationResult result = process(washingtonDCAddress, "postalCode", postalCodeCharsetPatternConstraint);
125                Assert.assertEquals(0, dictionaryValidationResult.getNumberOfWarnings());
126                Assert.assertEquals(1, dictionaryValidationResult.getNumberOfErrors());
127                Assert.assertEquals(ErrorLevel.ERROR, result.getStatus());
128                Assert.assertEquals(new ValidCharactersConstraintProcessor().getName(), result.getConstraintName());
129        }
130                
131        private ConstraintValidationResult process(Object object, String attributeName, ValidCharactersConstraint constraint) {
132                AttributeValueReader attributeValueReader = new DictionaryObjectAttributeValueReader(object, "org.kuali.rice.kns.datadictionary.validation.MockAddress", addressEntry);
133                attributeValueReader.setAttributeName(attributeName);
134                
135                Object value = attributeValueReader.getValue();
136                return processor.process(dictionaryValidationResult, value, constraint, attributeValueReader).getFirstConstraintValidationResult();
137        }
138}