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.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. (success) {@link #test2ValueAllValidChars()}
42   * 4. value with invalid characters. (failure) {@link #test2ValueNotValidChars()}
43   *
44   * @author Kuali Rice Team (rice.collab@kuali.org)
45   */
46  public class CharsetPatternConstraintTest {
47  
48  	private AttributeDefinition countryDefinition;
49  	private AttributeDefinition postalCodeDefinition;
50  
51  	private BusinessObjectEntry addressEntry;
52  	private DictionaryValidationResult dictionaryValidationResult;
53  
54  	private ValidCharactersConstraintProcessor processor;
55  
56  	private Address washingtonDCAddress = new Address("893	Presidential Ave", "(A_123) Suite 800.", "Washington", "DC", "NHW123A", "United States of America", null);
57  	private Address newYorkNYAddress = new Address("Presidential Street", "(A-123) Suite 800", "New York", "NY", "ZH 3456", "USA", null);
58  	private Address sydneyAUSAddress = new Address("Presidential Street-Ave.", "Suite_800.", "Sydney", "ZZ", "(ZH-5656)", "USA", null);
59  
60  	private CharsetPatternConstraint countryCharsetPatternConstraint;
61  	private CharsetPatternConstraint postalCodeCharsetPatternConstraint;
62  
63  	@Before
64  	public void setUp() throws Exception {
65  
66  		processor = new ValidCharactersConstraintProcessor();
67  
68  		dictionaryValidationResult = new DictionaryValidationResult();
69  		dictionaryValidationResult.setErrorLevel(ErrorLevel.NOCONSTRAINT);
70  
71  		addressEntry = new BusinessObjectEntry();
72  
73  		List<AttributeDefinition> attributes = new ArrayList<AttributeDefinition>();
74  
75  		countryCharsetPatternConstraint = new CharsetPatternConstraint();
76          countryCharsetPatternConstraint.setMessageKey("validate.dummykey");
77          countryCharsetPatternConstraint.setValidationMessageParams( new ArrayList<String>());
78  		countryCharsetPatternConstraint.setValidChars("USA");
79  
80  		countryDefinition = new AttributeDefinition();
81  		countryDefinition.setName("country");
82  		countryDefinition.setValidCharactersConstraint(countryCharsetPatternConstraint);
83  		attributes.add(countryDefinition);
84  
85  
86  		postalCodeCharsetPatternConstraint = new CharsetPatternConstraint();
87          countryCharsetPatternConstraint.setMessageKey("validate.dummykey");
88          countryCharsetPatternConstraint.setValidationMessageParams( new ArrayList<String>());
89  		postalCodeCharsetPatternConstraint.setValidChars("(ZH-123456)");
90  
91  		postalCodeDefinition = new AttributeDefinition();
92  		postalCodeDefinition.setName("postalCode");
93  		postalCodeDefinition.setValidCharactersConstraint(postalCodeCharsetPatternConstraint);
94  		attributes.add(postalCodeDefinition);
95  
96  		addressEntry.setAttributes(attributes);
97  	}
98  
99  	@Test
100 	public void testValueAllValidChars() {
101 		ConstraintValidationResult result = process(newYorkNYAddress, "country", countryCharsetPatternConstraint);
102 		Assert.assertEquals(0, dictionaryValidationResult.getNumberOfWarnings());
103 		Assert.assertEquals(0, dictionaryValidationResult.getNumberOfErrors());
104 		Assert.assertEquals(ErrorLevel.OK, result.getStatus());
105 		Assert.assertEquals(new ValidCharactersConstraintProcessor().getName(), result.getConstraintName());
106 	}
107 
108 	@Test
109 	public void testValueNotValidChars() {
110 		ConstraintValidationResult result = process(washingtonDCAddress, "country", countryCharsetPatternConstraint);
111 		Assert.assertEquals(0, dictionaryValidationResult.getNumberOfWarnings());
112 		Assert.assertEquals(1, dictionaryValidationResult.getNumberOfErrors());
113 		Assert.assertEquals(ErrorLevel.ERROR, result.getStatus());
114 		Assert.assertEquals(new ValidCharactersConstraintProcessor().getName(), result.getConstraintName());
115 	}
116 
117 	@Test
118 	public void test2ValueAllValidChars() {
119 		ConstraintValidationResult result = process(sydneyAUSAddress, "postalCode", postalCodeCharsetPatternConstraint);
120 		Assert.assertEquals(0, dictionaryValidationResult.getNumberOfWarnings());
121 		Assert.assertEquals(0, dictionaryValidationResult.getNumberOfErrors());
122 		Assert.assertEquals(ErrorLevel.OK, result.getStatus());
123 		Assert.assertEquals(new ValidCharactersConstraintProcessor().getName(), result.getConstraintName());
124 	}
125 
126 	@Test
127 	public void test2ValueNotValidChars() {
128 		ConstraintValidationResult result = process(washingtonDCAddress, "postalCode", postalCodeCharsetPatternConstraint);
129 		Assert.assertEquals(0, dictionaryValidationResult.getNumberOfWarnings());
130 		Assert.assertEquals(1, dictionaryValidationResult.getNumberOfErrors());
131 		Assert.assertEquals(ErrorLevel.ERROR, result.getStatus());
132 		Assert.assertEquals(new ValidCharactersConstraintProcessor().getName(), result.getConstraintName());
133 	}
134 
135 	private ConstraintValidationResult process(Object object, String attributeName, ValidCharactersConstraint constraint) {
136 		AttributeValueReader attributeValueReader = new DictionaryObjectAttributeValueReader(object, "org.kuali.rice.kns.datadictionary.validation.MockAddress", addressEntry);
137 		attributeValueReader.setAttributeName(attributeName);
138 
139 		Object value = attributeValueReader.getValue();
140 		return processor.process(dictionaryValidationResult, value, constraint, attributeValueReader).getFirstConstraintValidationResult();
141 	}
142 }