View Javadoc

1   /**
2    * Copyright 2005-2013 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.rice.krad.datadictionary.validation.constraint;
17  
18  import java.util.ArrayList;
19  import java.util.List;
20  
21  import org.junit.Assert;
22  import org.junit.Before;
23  import org.junit.Ignore;
24  import org.junit.Test;
25  import org.kuali.rice.krad.datadictionary.AttributeDefinition;
26  import org.kuali.rice.krad.datadictionary.BusinessObjectEntry;
27  import org.kuali.rice.krad.datadictionary.validation.Address;
28  import org.kuali.rice.krad.datadictionary.validation.AttributeValueReader;
29  import org.kuali.rice.krad.datadictionary.validation.DictionaryObjectAttributeValueReader;
30  import org.kuali.rice.krad.datadictionary.validation.ErrorLevel;
31  import org.kuali.rice.krad.datadictionary.validation.constraint.ValidCharactersConstraint;
32  import org.kuali.rice.krad.datadictionary.validation.processor.ValidCharactersConstraintProcessor;
33  import org.kuali.rice.krad.datadictionary.validation.result.ConstraintValidationResult;
34  import org.kuali.rice.krad.datadictionary.validation.result.DictionaryValidationResult;
35  
36  
37  /**
38   * Things this test should check:
39   *
40   * 1. value with all valid characters. (success) {@link #testValueAllValidChars()}
41   * 2. value with invalid characters. (failure) {@link #testValueNotValidChars()}
42   * 3. value with all valid characters. Allowing white space.(success) {@link #testValueAllValidCharsAllowWhitespace()}
43   * 4. value with invalid characters. Allowing white space. (failure) {@link #testValueNotValidCharsAllowWhitespace()}
44   * 5. value with all valid characters. Allowing white space, period, underscore and parenthesis. (success) {@link #testValueAllValidCharsAllowWhitespaceAndPeriodAndUnderscoreAndParenthesis()}
45   * 6. value with invalid characters. Allowing white space, period, underscore and parenthesis. (failure) {@link #testValueNotValidCharsAllowWhitespaceAndPeriodAndUnderscoreAndParenthesis()}
46   *
47   * @author Kuali Rice Team (rice.collab@kuali.org)
48   */
49  public class AlphaNumericPatternConstraintTest {
50  
51  	private AttributeDefinition street1Definition;
52  	private AttributeDefinition street2Definition;
53  	private AttributeDefinition postalCodeDefinition;
54  
55  	private BusinessObjectEntry addressEntry;
56  	private DictionaryValidationResult dictionaryValidationResult;
57  
58  	private ValidCharactersConstraintProcessor processor;
59  
60  	private Address washingtonDCAddress = new Address("893 Presidential Ave", "(A_123) Suite 800.", "Washington", "DC", "NHW123A", "USA", null);
61  	private Address newYorkNYAddress = new Address("Presidential Street", "(A-123) Suite 800", "New York", "NY", "ZH 3456", "USA", null);
62  	private Address sydneyAUSAddress = new Address("Presidential Street-Ave.", "Suite_800.", "Sydney", "ZZ", "ZH-5656", "USA", null);
63  
64  	private AlphaNumericPatternConstraint street1AlphaNumericPatternConstraint;
65  	private AlphaNumericPatternConstraint street2AlphaNumericPatternConstraint;
66  	private AlphaNumericPatternConstraint postalCodeAlphaNumericPatternConstraint;
67  
68  	@Before
69  	public void setUp() throws Exception {
70  
71  		processor = new ValidCharactersConstraintProcessor();
72  
73  		dictionaryValidationResult = new DictionaryValidationResult();
74  		dictionaryValidationResult.setErrorLevel(ErrorLevel.NOCONSTRAINT);
75  
76  		addressEntry = new BusinessObjectEntry();
77  
78  		List<AttributeDefinition> attributes = new ArrayList<AttributeDefinition>();
79  
80  		street1AlphaNumericPatternConstraint = new AlphaNumericPatternConstraint();
81          street1AlphaNumericPatternConstraint.setMessageKey("validate.dummykey");
82          street1AlphaNumericPatternConstraint.setValidationMessageParams( new ArrayList<String>());
83  		street1AlphaNumericPatternConstraint.setAllowWhitespace(true);
84  
85  		street1Definition = new AttributeDefinition();
86  		street1Definition.setName("street1");
87  		street1Definition.setValidCharactersConstraint(street1AlphaNumericPatternConstraint);
88  		attributes.add(street1Definition);
89  
90  
91  		street2AlphaNumericPatternConstraint = new AlphaNumericPatternConstraint();
92          street2AlphaNumericPatternConstraint.setMessageKey("validate.dummykey");
93          street2AlphaNumericPatternConstraint.setValidationMessageParams( new ArrayList<String>());
94  		street2AlphaNumericPatternConstraint.setAllowWhitespace(true);
95  		street2AlphaNumericPatternConstraint.setAllowParenthesis(true);
96  		street2AlphaNumericPatternConstraint.setAllowPeriod(true);
97  		street2AlphaNumericPatternConstraint.setAllowUnderscore(true);
98  
99  		street2Definition = new AttributeDefinition();
100 		street2Definition.setName("street2");
101 		street2Definition.setValidCharactersConstraint(street2AlphaNumericPatternConstraint);
102 		attributes.add(street2Definition);
103 
104 		postalCodeAlphaNumericPatternConstraint = new AlphaNumericPatternConstraint();
105         postalCodeAlphaNumericPatternConstraint.setMessageKey("validate.dummykey");
106         postalCodeAlphaNumericPatternConstraint.setValidationMessageParams( new ArrayList<String>());
107 
108 		postalCodeDefinition = new AttributeDefinition();
109 		postalCodeDefinition.setName("postalCode");
110 		postalCodeDefinition.setValidCharactersConstraint(postalCodeAlphaNumericPatternConstraint);
111 		attributes.add(postalCodeDefinition);
112 
113 		addressEntry.setAttributes(attributes);
114 	}
115 
116 	@Test
117 	public void testValueAllValidChars() {
118 		ConstraintValidationResult result = process(washingtonDCAddress, "postalCode", postalCodeAlphaNumericPatternConstraint);
119 		Assert.assertEquals(0, dictionaryValidationResult.getNumberOfWarnings());
120 		Assert.assertEquals(0, dictionaryValidationResult.getNumberOfErrors());
121 		Assert.assertEquals(ErrorLevel.OK, result.getStatus());
122 		Assert.assertEquals(new ValidCharactersConstraintProcessor().getName(), result.getConstraintName());
123 	}
124 
125 	@Test
126 	public void testValueNotValidChars() {
127 		ConstraintValidationResult result = process(newYorkNYAddress, "postalCode", postalCodeAlphaNumericPatternConstraint);
128 		Assert.assertEquals(0, dictionaryValidationResult.getNumberOfWarnings());
129 		Assert.assertEquals(1, dictionaryValidationResult.getNumberOfErrors());
130 		Assert.assertEquals(ErrorLevel.ERROR, result.getStatus());
131 		Assert.assertEquals(new ValidCharactersConstraintProcessor().getName(), result.getConstraintName());
132 	}
133 
134 	@Test
135 	public void testValueAllValidCharsAllowWhitespace() {
136 		ConstraintValidationResult result = process(newYorkNYAddress, "street1", street1AlphaNumericPatternConstraint);
137 		Assert.assertEquals(0, dictionaryValidationResult.getNumberOfWarnings());
138 		Assert.assertEquals(0, dictionaryValidationResult.getNumberOfErrors());
139 		Assert.assertEquals(ErrorLevel.OK, result.getStatus());
140 		Assert.assertEquals(new ValidCharactersConstraintProcessor().getName(), result.getConstraintName());
141 	}
142 
143 	@Test
144 	public void testValueNotValidCharsAllowWhitespace() {
145 		ConstraintValidationResult result = process(sydneyAUSAddress, "street1", street1AlphaNumericPatternConstraint);
146 		Assert.assertEquals(0, dictionaryValidationResult.getNumberOfWarnings());
147 		Assert.assertEquals(1, dictionaryValidationResult.getNumberOfErrors());
148 		Assert.assertEquals(ErrorLevel.ERROR, result.getStatus());
149 		Assert.assertEquals(new ValidCharactersConstraintProcessor().getName(), result.getConstraintName());
150 	}
151 
152 	@Test
153 	public void testValueAllValidCharsAllowWhitespaceAndPeriodAndUnderscoreAndParenthesis() {
154 		ConstraintValidationResult result = process(washingtonDCAddress, "street2", street2AlphaNumericPatternConstraint);
155 		Assert.assertEquals(0, dictionaryValidationResult.getNumberOfWarnings());
156 		Assert.assertEquals(0, dictionaryValidationResult.getNumberOfErrors());
157 		Assert.assertEquals(ErrorLevel.OK, result.getStatus());
158 		Assert.assertEquals(new ValidCharactersConstraintProcessor().getName(), result.getConstraintName());
159 	}
160 
161 	@Test
162 	public void testValueNotValidCharsAllowWhitespaceAndPeriodAndUnderscoreAndParenthesis() {
163 		ConstraintValidationResult result = process(newYorkNYAddress, "street2", street2AlphaNumericPatternConstraint);
164 		Assert.assertEquals(0, dictionaryValidationResult.getNumberOfWarnings());
165 		Assert.assertEquals(1, dictionaryValidationResult.getNumberOfErrors());
166 		Assert.assertEquals(ErrorLevel.ERROR, result.getStatus());
167 		Assert.assertEquals(new ValidCharactersConstraintProcessor().getName(), result.getConstraintName());
168 	}
169 
170 	private ConstraintValidationResult process(Object object, String attributeName, ValidCharactersConstraint constraint) {
171 		AttributeValueReader attributeValueReader = new DictionaryObjectAttributeValueReader(object, "org.kuali.rice.kns.datadictionary.validation.MockAddress", addressEntry);
172 		attributeValueReader.setAttributeName(attributeName);
173 
174 		Object value = attributeValueReader.getValue();
175 		return processor.process(dictionaryValidationResult, value, constraint, attributeValueReader).getFirstConstraintValidationResult();
176 	}
177 }