View Javadoc

1   /**
2    * Copyright 2005-2014 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.AlphaPatternConstraint;
32  import org.kuali.rice.krad.datadictionary.validation.constraint.ValidCharactersConstraint;
33  import org.kuali.rice.krad.datadictionary.validation.processor.ValidCharactersConstraintProcessor;
34  import org.kuali.rice.krad.datadictionary.validation.result.ConstraintValidationResult;
35  import org.kuali.rice.krad.datadictionary.validation.result.DictionaryValidationResult;
36  
37  
38  /**
39   * Things this test should check:
40   *
41   * 1. value with all valid characters.(success) {@link #testValueAllValidChars()}
42   * 2. value with invalid characters. (failure) {@link #testValueNotValidChars()}
43   * 3. value with all valid characters. Allowing white space.(success) {@link #testValueAllValidCharsAllowWhitespace()}
44   * 4. value with invalid characters Allowing white space. (failure) {@link #testValueNotValidCharsAllowWhitespace()}
45   * 5. value with invalid characters(Special Characters) Allowing white space. (failure) {@link #testValueWithSpecialCharsAllowWhitespace()}
46   *
47   * @author Kuali Rice Team (rice.collab@kuali.org)
48   */
49  public class AlphaPatternConstraintTest {
50  
51  	private AttributeDefinition street1Definition;
52  	private AttributeDefinition cityDefinition;
53  
54  	private BusinessObjectEntry addressEntry;
55  	private DictionaryValidationResult dictionaryValidationResult;
56  
57  	private ValidCharactersConstraintProcessor processor;
58  
59  	private Address washingtonDCAddress = new Address("893 Presidential Ave", "(A_123) Suite 800.", "Washington", "DC", "NHW123A", "USA", null);
60  	private Address newYorkNYAddress = new Address("Presidential Street", "(A-123) Suite 800", "New York", "NY", "ZH 3456", "USA", null);
61  	private Address sydneyAUSAddress = new Address("Presidential Street-Ave.", "Suite_800.", "Sydney", "ZZ", "ZH-5656", "USA", null);
62  
63  	private AlphaPatternConstraint street1AlphaPatternConstraint;
64  	private AlphaPatternConstraint cityAlphaPatternConstraint;
65  
66  	@Before
67  	public void setUp() throws Exception {
68  
69  		processor = new ValidCharactersConstraintProcessor();
70  
71  		dictionaryValidationResult = new DictionaryValidationResult();
72  		dictionaryValidationResult.setErrorLevel(ErrorLevel.NOCONSTRAINT);
73  
74  		addressEntry = new BusinessObjectEntry();
75  
76  		List<AttributeDefinition> attributes = new ArrayList<AttributeDefinition>();
77  
78  		cityAlphaPatternConstraint = new AlphaPatternConstraint();
79          cityAlphaPatternConstraint.setMessageKey("validate.dummykey");
80          cityAlphaPatternConstraint.setValidationMessageParams( new ArrayList<String>());
81  
82  		cityDefinition = new AttributeDefinition();
83  		cityDefinition.setName("city");
84  		cityDefinition.setValidCharactersConstraint(cityAlphaPatternConstraint);
85  		attributes.add(cityDefinition);
86  
87  		street1AlphaPatternConstraint = new AlphaPatternConstraint();
88          street1AlphaPatternConstraint.setMessageKey("validate.dummykey");
89          street1AlphaPatternConstraint.setValidationMessageParams( new ArrayList<String>());
90  		street1AlphaPatternConstraint.setAllowWhitespace(true);
91  
92  		street1Definition = new AttributeDefinition();
93  		street1Definition.setName("street1");
94  		street1Definition.setValidCharactersConstraint(street1AlphaPatternConstraint);
95  		attributes.add(street1Definition);
96  
97  		addressEntry.setAttributes(attributes);
98  	}
99  
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 }