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