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.util.ArrayList;
019 import java.util.List;
020
021 import org.junit.Assert;
022 import org.junit.Before;
023 import org.junit.Ignore;
024 import org.junit.Test;
025 import org.kuali.rice.krad.datadictionary.AttributeDefinition;
026 import org.kuali.rice.krad.datadictionary.BusinessObjectEntry;
027 import org.kuali.rice.krad.datadictionary.validation.Address;
028 import org.kuali.rice.krad.datadictionary.validation.AttributeValueReader;
029 import org.kuali.rice.krad.datadictionary.validation.DictionaryObjectAttributeValueReader;
030 import org.kuali.rice.krad.datadictionary.validation.ErrorLevel;
031 import org.kuali.rice.krad.datadictionary.validation.constraint.AlphaPatternConstraint;
032 import org.kuali.rice.krad.datadictionary.validation.constraint.ValidCharactersConstraint;
033 import org.kuali.rice.krad.datadictionary.validation.processor.ValidCharactersConstraintProcessor;
034 import org.kuali.rice.krad.datadictionary.validation.result.ConstraintValidationResult;
035 import org.kuali.rice.krad.datadictionary.validation.result.DictionaryValidationResult;
036
037
038 /**
039 * Things this test should check:
040 *
041 * 1. value with all valid characters.(success) {@link #testValueAllValidChars()}
042 * 2. value with invalid characters. (failure) {@link #testValueNotValidChars()}
043 * 3. value with all valid characters. Allowing white space.(success) {@link #testValueAllValidCharsAllowWhitespace()}
044 * 4. value with invalid characters Allowing white space. (failure) {@link #testValueNotValidCharsAllowWhitespace()}
045 * 5. value with invalid characters(Special Characters) Allowing white space. (failure) {@link #testValueWithSpecialCharsAllowWhitespace()}
046 *
047 * @author Kuali Rice Team (rice.collab@kuali.org)
048 */
049 public class AlphaPatternConstraintTest {
050
051 private AttributeDefinition street1Definition;
052 private AttributeDefinition cityDefinition;
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 AlphaPatternConstraint street1AlphaPatternConstraint;
064 private AlphaPatternConstraint cityAlphaPatternConstraint;
065
066 @Before
067 public void setUp() throws Exception {
068
069 processor = new ValidCharactersConstraintProcessor();
070
071 dictionaryValidationResult = new DictionaryValidationResult();
072 dictionaryValidationResult.setErrorLevel(ErrorLevel.NOCONSTRAINT);
073
074 addressEntry = new BusinessObjectEntry();
075
076 List<AttributeDefinition> attributes = new ArrayList<AttributeDefinition>();
077
078 cityAlphaPatternConstraint = new AlphaPatternConstraint();
079 cityAlphaPatternConstraint.setMessageKey("validate.dummykey");
080 cityAlphaPatternConstraint.setValidationMessageParams( new ArrayList<String>());
081
082 cityDefinition = new AttributeDefinition();
083 cityDefinition.setName("city");
084 cityDefinition.setValidCharactersConstraint(cityAlphaPatternConstraint);
085 attributes.add(cityDefinition);
086
087 street1AlphaPatternConstraint = new AlphaPatternConstraint();
088 street1AlphaPatternConstraint.setMessageKey("validate.dummykey");
089 street1AlphaPatternConstraint.setValidationMessageParams( new ArrayList<String>());
090 street1AlphaPatternConstraint.setAllowWhitespace(true);
091
092 street1Definition = new AttributeDefinition();
093 street1Definition.setName("street1");
094 street1Definition.setValidCharactersConstraint(street1AlphaPatternConstraint);
095 attributes.add(street1Definition);
096
097 addressEntry.setAttributes(attributes);
098 }
099
100 @Test
101 public void testValueAllValidChars() {
102 ConstraintValidationResult result = process(washingtonDCAddress, "city", cityAlphaPatternConstraint);
103 Assert.assertEquals(0, dictionaryValidationResult.getNumberOfWarnings());
104 Assert.assertEquals(0, dictionaryValidationResult.getNumberOfErrors());
105 Assert.assertEquals(ErrorLevel.OK, result.getStatus());
106 Assert.assertEquals(new ValidCharactersConstraintProcessor().getName(), result.getConstraintName());
107 }
108
109 @Test
110 public void testValueNotValidChars() {
111 ConstraintValidationResult result = process(newYorkNYAddress, "city", cityAlphaPatternConstraint);
112 Assert.assertEquals(0, dictionaryValidationResult.getNumberOfWarnings());
113 Assert.assertEquals(1, dictionaryValidationResult.getNumberOfErrors());
114 Assert.assertEquals(ErrorLevel.ERROR, result.getStatus());
115 Assert.assertEquals(new ValidCharactersConstraintProcessor().getName(), result.getConstraintName());
116 }
117
118 @Test
119 public void testValueAllValidCharsAllowWhitespace() {
120 ConstraintValidationResult result = process(newYorkNYAddress, "street1", street1AlphaPatternConstraint);
121 Assert.assertEquals(0, dictionaryValidationResult.getNumberOfWarnings());
122 Assert.assertEquals(0, dictionaryValidationResult.getNumberOfErrors());
123 Assert.assertEquals(ErrorLevel.OK, result.getStatus());
124 Assert.assertEquals(new ValidCharactersConstraintProcessor().getName(), result.getConstraintName());
125 }
126
127 @Test
128 public void testValueNotValidCharsAllowWhitespace() {
129 ConstraintValidationResult result = process(washingtonDCAddress, "street1", street1AlphaPatternConstraint);
130 Assert.assertEquals(0, dictionaryValidationResult.getNumberOfWarnings());
131 Assert.assertEquals(1, dictionaryValidationResult.getNumberOfErrors());
132 Assert.assertEquals(ErrorLevel.ERROR, result.getStatus());
133 Assert.assertEquals(new ValidCharactersConstraintProcessor().getName(), result.getConstraintName());
134 }
135
136 @Test
137 public void testValueWithSpecialCharsAllowWhitespace() {
138 ConstraintValidationResult result = process(sydneyAUSAddress, "street1", street1AlphaPatternConstraint);
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 private ConstraintValidationResult process(Object object, String attributeName, ValidCharactersConstraint constraint) {
146 AttributeValueReader attributeValueReader = new DictionaryObjectAttributeValueReader(object, "org.kuali.rice.kns.datadictionary.validation.MockAddress", addressEntry);
147 attributeValueReader.setAttributeName(attributeName);
148
149 Object value = attributeValueReader.getValue();
150 return processor.process(dictionaryValidationResult, value, constraint, attributeValueReader).getFirstConstraintValidationResult();
151 }
152 }