View Javadoc
1   /**
2    * Copyright 2005-2016 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.setAllowWhitespace(true);
82  		
83  		street1Definition = new AttributeDefinition();
84  		street1Definition.setName("street1");
85  		street1Definition.setValidCharactersConstraint(street1AlphaNumericPatternConstraint);
86  		attributes.add(street1Definition);	
87  		
88  		
89  		street2AlphaNumericPatternConstraint = new AlphaNumericPatternConstraint();
90  		street2AlphaNumericPatternConstraint.setAllowWhitespace(true);
91  		street2AlphaNumericPatternConstraint.setAllowParenthesis(true);
92  		street2AlphaNumericPatternConstraint.setAllowPeriod(true);
93  		street2AlphaNumericPatternConstraint.setAllowUnderscore(true);
94  		
95  		street2Definition = new AttributeDefinition();
96  		street2Definition.setName("street2");
97  		street2Definition.setValidCharactersConstraint(street2AlphaNumericPatternConstraint);
98  		attributes.add(street2Definition);
99  		
100 		postalCodeAlphaNumericPatternConstraint = new AlphaNumericPatternConstraint();
101 		
102 		postalCodeDefinition = new AttributeDefinition();
103 		postalCodeDefinition.setName("postalCode");
104 		postalCodeDefinition.setValidCharactersConstraint(postalCodeAlphaNumericPatternConstraint);
105 		attributes.add(postalCodeDefinition);
106 		
107 		addressEntry.setAttributes(attributes);	
108 	}
109 	
110 	@Test
111 	public void testValueAllValidChars() {
112 		ConstraintValidationResult result = process(washingtonDCAddress, "postalCode", postalCodeAlphaNumericPatternConstraint);
113 		Assert.assertEquals(0, dictionaryValidationResult.getNumberOfWarnings());
114 		Assert.assertEquals(0, dictionaryValidationResult.getNumberOfErrors());
115 		Assert.assertEquals(ErrorLevel.OK, result.getStatus());
116 		Assert.assertEquals(new ValidCharactersConstraintProcessor().getName(), result.getConstraintName());
117 	}
118 
119     @Ignore
120 	@Test
121 	public void testValueNotValidChars() {
122 		ConstraintValidationResult result = process(newYorkNYAddress, "postalCode", postalCodeAlphaNumericPatternConstraint);
123 		Assert.assertEquals(0, dictionaryValidationResult.getNumberOfWarnings());
124 		Assert.assertEquals(1, dictionaryValidationResult.getNumberOfErrors());
125 		Assert.assertEquals(ErrorLevel.ERROR, result.getStatus());
126 		Assert.assertEquals(new ValidCharactersConstraintProcessor().getName(), result.getConstraintName());
127 	}
128 
129 	@Test
130 	public void testValueAllValidCharsAllowWhitespace() {
131 		ConstraintValidationResult result = process(newYorkNYAddress, "street1", street1AlphaNumericPatternConstraint);
132 		Assert.assertEquals(0, dictionaryValidationResult.getNumberOfWarnings());
133 		Assert.assertEquals(0, dictionaryValidationResult.getNumberOfErrors());
134 		Assert.assertEquals(ErrorLevel.OK, result.getStatus());
135 		Assert.assertEquals(new ValidCharactersConstraintProcessor().getName(), result.getConstraintName());
136 	}
137 
138     @Ignore
139 	@Test
140 	public void testValueNotValidCharsAllowWhitespace() {
141 		ConstraintValidationResult result = process(sydneyAUSAddress, "street1", street1AlphaNumericPatternConstraint);
142 		Assert.assertEquals(0, dictionaryValidationResult.getNumberOfWarnings());
143 		Assert.assertEquals(1, dictionaryValidationResult.getNumberOfErrors());
144 		Assert.assertEquals(ErrorLevel.ERROR, result.getStatus());
145 		Assert.assertEquals(new ValidCharactersConstraintProcessor().getName(), result.getConstraintName());
146 	}
147 	
148 	@Test
149 	public void testValueAllValidCharsAllowWhitespaceAndPeriodAndUnderscoreAndParenthesis() {
150 		ConstraintValidationResult result = process(washingtonDCAddress, "street2", street2AlphaNumericPatternConstraint);
151 		Assert.assertEquals(0, dictionaryValidationResult.getNumberOfWarnings());
152 		Assert.assertEquals(0, dictionaryValidationResult.getNumberOfErrors());
153 		Assert.assertEquals(ErrorLevel.OK, result.getStatus());
154 		Assert.assertEquals(new ValidCharactersConstraintProcessor().getName(), result.getConstraintName());
155 	}
156 
157     @Ignore
158 	@Test
159 	public void testValueNotValidCharsAllowWhitespaceAndPeriodAndUnderscoreAndParenthesis() {
160 		ConstraintValidationResult result = process(newYorkNYAddress, "street2", street2AlphaNumericPatternConstraint);
161 		Assert.assertEquals(0, dictionaryValidationResult.getNumberOfWarnings());
162 		Assert.assertEquals(1, dictionaryValidationResult.getNumberOfErrors());
163 		Assert.assertEquals(ErrorLevel.ERROR, result.getStatus());
164 		Assert.assertEquals(new ValidCharactersConstraintProcessor().getName(), result.getConstraintName());
165 	}
166 		
167 	private ConstraintValidationResult process(Object object, String attributeName, ValidCharactersConstraint constraint) {
168 		AttributeValueReader attributeValueReader = new DictionaryObjectAttributeValueReader(object, "org.kuali.rice.kns.datadictionary.validation.MockAddress", addressEntry);
169 		attributeValueReader.setAttributeName(attributeName);
170 		
171 		Object value = attributeValueReader.getValue();
172 		return processor.process(dictionaryValidationResult, value, constraint, attributeValueReader).getFirstConstraintValidationResult();
173 	}
174 }