View Javadoc

1   /**
2    * Copyright 2005-2011 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   * 
42   * @author Kuali Rice Team (rice.collab@kuali.org) 
43   */
44  public class NumericPatternConstraintTest {
45  
46  	private AttributeDefinition postalCodeDefinition;	
47  	
48  	private BusinessObjectEntry addressEntry;
49  	private DictionaryValidationResult dictionaryValidationResult;
50  	
51  	private ValidCharactersConstraintProcessor processor;
52  		
53  	private Address newYorkNYAddress = new Address("Presidential Street", "(A-123) Suite 800", "New York", "NY", "ZH 3456", "USA", null);
54  	private Address sydneyAUSAddress = new Address("Presidential Street-Ave.", "Suite_800.", "Sydney", "ZZ", "999", "USA", null);
55  	
56  	private NumericPatternConstraint postalCodeNumericPatternConstraint;	
57  	
58  	@Before
59  	public void setUp() throws Exception {
60  		
61  		processor = new ValidCharactersConstraintProcessor();
62  		
63  		dictionaryValidationResult = new DictionaryValidationResult();
64  		dictionaryValidationResult.setErrorLevel(ErrorLevel.NOCONSTRAINT);
65  		
66  		addressEntry = new BusinessObjectEntry();
67  		
68  		List<AttributeDefinition> attributes = new ArrayList<AttributeDefinition>();						
69  		
70  		postalCodeNumericPatternConstraint = new NumericPatternConstraint();
71  		
72  		postalCodeDefinition = new AttributeDefinition();
73  		postalCodeDefinition.setName("postalCode");
74  		postalCodeDefinition.setValidCharactersConstraint(postalCodeNumericPatternConstraint);
75  		attributes.add(postalCodeDefinition);
76  		
77  		addressEntry.setAttributes(attributes);	
78  	}
79  	
80  	@Test
81  	public void testValueAllValidChars() {
82  		ConstraintValidationResult result = process(sydneyAUSAddress, "postalCode", postalCodeNumericPatternConstraint);
83  		Assert.assertEquals(0, dictionaryValidationResult.getNumberOfWarnings());
84  		Assert.assertEquals(0, dictionaryValidationResult.getNumberOfErrors());
85  		Assert.assertEquals(ErrorLevel.OK, result.getStatus());
86  		Assert.assertEquals(new ValidCharactersConstraintProcessor().getName(), result.getConstraintName());
87  	}
88  	
89  	@Test
90  	public void testValueNotValidChars() {
91  		ConstraintValidationResult result = process(newYorkNYAddress, "postalCode", postalCodeNumericPatternConstraint);
92  		Assert.assertEquals(0, dictionaryValidationResult.getNumberOfWarnings());
93  		Assert.assertEquals(1, dictionaryValidationResult.getNumberOfErrors());
94  		Assert.assertEquals(ErrorLevel.ERROR, result.getStatus());
95  		Assert.assertEquals(new ValidCharactersConstraintProcessor().getName(), result.getConstraintName());
96  	}	
97  		
98  	private ConstraintValidationResult process(Object object, String attributeName, ValidCharactersConstraint constraint) {
99  		AttributeValueReader attributeValueReader = new DictionaryObjectAttributeValueReader(object, "org.kuali.rice.kns.datadictionary.validation.MockAddress", addressEntry);
100 		attributeValueReader.setAttributeName(attributeName);
101 		
102 		Object value = attributeValueReader.getValue();
103 		return processor.process(dictionaryValidationResult, value, constraint, attributeValueReader).getFirstConstraintValidationResult();
104 	}
105 }