View Javadoc

1   /**
2    * Copyright 2005-2012 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.Test;
24  import org.kuali.rice.krad.datadictionary.AttributeDefinition;
25  import org.kuali.rice.krad.datadictionary.BusinessObjectEntry;
26  import org.kuali.rice.krad.datadictionary.validation.Address;
27  import org.kuali.rice.krad.datadictionary.validation.AttributeValueReader;
28  import org.kuali.rice.krad.datadictionary.validation.DictionaryObjectAttributeValueReader;
29  import org.kuali.rice.krad.datadictionary.validation.ErrorLevel;
30  import org.kuali.rice.krad.datadictionary.validation.constraint.ValidCharactersConstraint;
31  import org.kuali.rice.krad.datadictionary.validation.processor.ValidCharactersConstraintProcessor;
32  import org.kuali.rice.krad.datadictionary.validation.result.ConstraintValidationResult;
33  import org.kuali.rice.krad.datadictionary.validation.result.DictionaryValidationResult;
34  
35  
36  /**
37   * Things this test should check:
38   * 
39   * 1. value with all valid characters. (success) {@link #testValueAllValidChars()}
40   * 2. value with invalid characters. (failure) {@link #testValueNotValidChars()}
41   * 3. value with all valid characters. Allowing white space.(success) {@link #testValueAllValidCharsAllowWhitespace()}
42   * 
43   * @author Kuali Rice Team (rice.collab@kuali.org) 
44   */
45  public class AnyCharacterPatternConstraintTest {
46  
47  	private AttributeDefinition street1Definition;
48  	private AttributeDefinition street2Definition;	
49  	
50  	private BusinessObjectEntry addressEntry;
51  	private DictionaryValidationResult dictionaryValidationResult;
52  	
53  	private ValidCharactersConstraintProcessor processor;
54  	
55  	private Address washingtonDCAddress = new Address("893	Presidential Ave", "(A_123) Suite 800.", "Washington", "DC", "NHW123A", "USA", null);
56  	private Address newYorkNYAddress = new Address("Presidential Street", "(A-123) Suite 800", "New York", "NY", "ZH 3456", "USA", null);
57  	private Address sydneyAUSAddress = new Address("Presidential Street-Ave.", "Suite_800.", "Sydney", "ZZ", "ZH-5656", "USA", null);
58  	
59  	private AnyCharacterPatternConstraint street1AnyCharacterPatternConstraint;
60  	private AnyCharacterPatternConstraint street2AnyCharacterPatternConstraint;	
61  	
62  	@Before
63  	public void setUp() throws Exception {
64  		
65  		processor = new ValidCharactersConstraintProcessor();
66  		
67  		dictionaryValidationResult = new DictionaryValidationResult();
68  		dictionaryValidationResult.setErrorLevel(ErrorLevel.NOCONSTRAINT);
69  		
70  		addressEntry = new BusinessObjectEntry();
71  		
72  		List<AttributeDefinition> attributes = new ArrayList<AttributeDefinition>();						
73  		
74  		street1AnyCharacterPatternConstraint = new AnyCharacterPatternConstraint();
75  		street1AnyCharacterPatternConstraint.setAllowWhitespace(true);
76  		
77  		street1Definition = new AttributeDefinition();
78  		street1Definition.setName("street1");
79  		street1Definition.setValidCharactersConstraint(street1AnyCharacterPatternConstraint);
80  		attributes.add(street1Definition);	
81  		
82  		
83  		street2AnyCharacterPatternConstraint = new AnyCharacterPatternConstraint();	
84  		
85  		street2Definition = new AttributeDefinition();
86  		street2Definition.setName("street2");
87  		street2Definition.setValidCharactersConstraint(street2AnyCharacterPatternConstraint);
88  		attributes.add(street2Definition);
89  		
90  		addressEntry.setAttributes(attributes);	
91  	}
92  	
93  	@Test
94  	public void testValueAllValidChars() {
95  		ConstraintValidationResult result = process(sydneyAUSAddress, "street2", street2AnyCharacterPatternConstraint);
96  		Assert.assertEquals(0, dictionaryValidationResult.getNumberOfWarnings());
97  		Assert.assertEquals(0, dictionaryValidationResult.getNumberOfErrors());
98  		Assert.assertEquals(ErrorLevel.OK, result.getStatus());
99  		Assert.assertEquals(new ValidCharactersConstraintProcessor().getName(), result.getConstraintName());
100 	}
101 	
102 	@Test
103 	public void testValueNotValidChars() {
104 		ConstraintValidationResult result = process(newYorkNYAddress, "street2", street2AnyCharacterPatternConstraint);
105 		Assert.assertEquals(0, dictionaryValidationResult.getNumberOfWarnings());
106 		Assert.assertEquals(1, dictionaryValidationResult.getNumberOfErrors());
107 		Assert.assertEquals(ErrorLevel.ERROR, result.getStatus());
108 		Assert.assertEquals(new ValidCharactersConstraintProcessor().getName(), result.getConstraintName());
109 	}
110 	
111 	@Test
112 	public void testValueAllValidCharsAllowWhitespace() {
113 		ConstraintValidationResult result = process(washingtonDCAddress, "street1", street1AnyCharacterPatternConstraint);
114 		Assert.assertEquals(0, dictionaryValidationResult.getNumberOfWarnings());
115 		Assert.assertEquals(0, dictionaryValidationResult.getNumberOfErrors());
116 		Assert.assertEquals(ErrorLevel.OK, result.getStatus());
117 		Assert.assertEquals(new ValidCharactersConstraintProcessor().getName(), result.getConstraintName());
118 	}	
119 		
120 	private ConstraintValidationResult process(Object object, String attributeName, ValidCharactersConstraint constraint) {
121 		AttributeValueReader attributeValueReader = new DictionaryObjectAttributeValueReader(object, "org.kuali.rice.kns.datadictionary.validation.MockAddress", addressEntry);
122 		attributeValueReader.setAttributeName(attributeName);
123 		
124 		Object value = attributeValueReader.getValue();
125 		return processor.process(dictionaryValidationResult, value, constraint, attributeValueReader).getFirstConstraintValidationResult();
126 	}
127 }