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.Account;
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.processor.ValidCharactersConstraintProcessor;
31  import org.kuali.rice.krad.datadictionary.validation.result.ConstraintValidationResult;
32  import org.kuali.rice.krad.datadictionary.validation.result.DictionaryValidationResult;
33  
34  
35  /**
36   * Things this test should check:
37   * 
38   * 1. empty value check. (failure) {@link #testValueEmpty()}
39   * 2. value with valid positive number within specified precision and scale. (success) {@link #testValueValidPositiveNumber()}
40   * 3. value with invalid positive number as per given precision and scale. (failure) {@link testValueInvalidPositiveNumber()}
41   * 4. value with invalid negative number as allowNegative set to false. (failure) {@link testValueInvalidNegativeNumber()}
42   * 5. value with valid negative number within specified precision and scale, with allowNegative set to true. (success) {@link testValueValidNegativeNumber()}
43   * 6. value with invalid negative number as per given precision and scale. (failure) {@link testValueInvalidNegativeNumber1()}
44   * 7. value with invalid positive number as per given precision and scale. (failure) {@link testValueInvalidPositiveNumber1()}
45   * 8. value with valid negative number within specified precision and scale, with allowNegative set to true. (success) {@link testValueValidNegativeNumber1()}
46   * 9. zero precision and scale test. (error) {@link testZeroPrecisionAndScale()}
47   * 10. precision less than scale test. (error) {@link testPrecisionLessThanScale()}
48   * 11. negative precision and scale test. (error) {@link testNegativePrecisionAndScale()}
49   * 
50   * 
51   * 
52   * @author Kuali Rice Team (rice.collab@kuali.org) 
53   */
54  public class FixedPointPatternConstraintTest {
55  
56  	private AttributeDefinition totalBalanceDefinition;	
57  	private AttributeDefinition debitBalanceDefinition;	
58  	private AttributeDefinition creditBalanceDefinition;
59  	
60  	private BusinessObjectEntry addressEntry;
61  	private DictionaryValidationResult dictionaryValidationResult;
62  	
63  	private ValidCharactersConstraintProcessor processor;
64  		
65  	private Account iciciAccount = new Account("11111111","ICICI","5000.00","15000","-10000");
66  	private Account citibankAccount = new Account("22222222","Citi Bank","15000.70","14999.70","1.");
67  	private Account wellsFargoAccount = new Account("33333333","Wells Fargo","",".25","-.25");
68  	private Account morganAccount = new Account("44444444","J P Morgan","-1000.00","1000.00","-2000.00");
69  	
70  	private FixedPointPatternConstraint totalBalanceFixedPointPatternConstraint;	
71  	private FixedPointPatternConstraint debitBalanceFixedPointPatternConstraint;
72  	private FixedPointPatternConstraint creditBalanceFixedPointPatternConstraint;
73  	private FixedPointPatternConstraint creditBalanceFixedPointPatternConstraint1;
74  	private FixedPointPatternConstraint creditBalanceFixedPointPatternConstraint2;
75  	
76  	@Before
77  	public void setUp() throws Exception {
78  		
79  		processor = new ValidCharactersConstraintProcessor();
80  		
81  		dictionaryValidationResult = new DictionaryValidationResult();
82  		dictionaryValidationResult.setErrorLevel(ErrorLevel.NOCONSTRAINT);
83  		
84  		addressEntry = new BusinessObjectEntry();
85  		
86  		List<AttributeDefinition> attributes = new ArrayList<AttributeDefinition>();						
87  		
88  		totalBalanceFixedPointPatternConstraint = new FixedPointPatternConstraint();
89  		totalBalanceFixedPointPatternConstraint.setPrecision(6);
90  		totalBalanceFixedPointPatternConstraint.setScale(2);
91  		
92  		totalBalanceDefinition = new AttributeDefinition();
93  		totalBalanceDefinition.setName("totalBalance");
94  		totalBalanceDefinition.setValidCharactersConstraint(totalBalanceFixedPointPatternConstraint);
95  		attributes.add(totalBalanceDefinition);
96  		
97  		debitBalanceFixedPointPatternConstraint = new FixedPointPatternConstraint();
98  		debitBalanceFixedPointPatternConstraint.setPrecision(6);
99  		debitBalanceFixedPointPatternConstraint.setScale(2);
100 		debitBalanceFixedPointPatternConstraint.setAllowNegative(true);
101 		
102 		debitBalanceDefinition = new AttributeDefinition();
103 		debitBalanceDefinition.setName("debitBalance");
104 		debitBalanceDefinition.setValidCharactersConstraint(debitBalanceFixedPointPatternConstraint);
105 		attributes.add(debitBalanceDefinition);
106 		
107 		creditBalanceFixedPointPatternConstraint = new FixedPointPatternConstraint();
108 		creditBalanceFixedPointPatternConstraint.setPrecision(0);
109 		creditBalanceFixedPointPatternConstraint.setScale(0);
110 		
111 		creditBalanceDefinition = new AttributeDefinition();
112 		creditBalanceDefinition.setName("creditBalance");
113 		creditBalanceDefinition.setValidCharactersConstraint(creditBalanceFixedPointPatternConstraint);
114 		attributes.add(creditBalanceDefinition);
115 		
116 		creditBalanceFixedPointPatternConstraint1 = new FixedPointPatternConstraint();
117 		creditBalanceFixedPointPatternConstraint1.setPrecision(2);
118 		creditBalanceFixedPointPatternConstraint1.setScale(3);
119 				
120 		creditBalanceFixedPointPatternConstraint2 = new FixedPointPatternConstraint();
121 		creditBalanceFixedPointPatternConstraint2.setPrecision(-2);
122 		creditBalanceFixedPointPatternConstraint2.setScale(-3);		
123 		
124 		addressEntry.setAttributes(attributes);	
125 	}
126 	
127 	@Test
128 	public void testValueEmpty() {
129 		ConstraintValidationResult result = process(wellsFargoAccount, "totalBalance", totalBalanceFixedPointPatternConstraint);
130 		Assert.assertEquals(0, dictionaryValidationResult.getNumberOfWarnings());
131 		Assert.assertEquals(0, dictionaryValidationResult.getNumberOfErrors());
132 		Assert.assertEquals(ErrorLevel.INAPPLICABLE, result.getStatus());
133 		Assert.assertEquals(new ValidCharactersConstraintProcessor().getName(), result.getConstraintName());
134 	}	
135 	
136 	@Test
137 	public void testValueValidPositiveNumber() {
138 		ConstraintValidationResult result = process(iciciAccount, "totalBalance", totalBalanceFixedPointPatternConstraint);
139 		Assert.assertEquals(0, dictionaryValidationResult.getNumberOfWarnings());
140 		Assert.assertEquals(0, dictionaryValidationResult.getNumberOfErrors());
141 		Assert.assertEquals(ErrorLevel.OK, result.getStatus());
142 		Assert.assertEquals(new ValidCharactersConstraintProcessor().getName(), result.getConstraintName());
143 	}
144 	
145 	@Test
146 	public void testValueInvalidPositiveNumber() {
147 		ConstraintValidationResult result = process(citibankAccount, "totalBalance", totalBalanceFixedPointPatternConstraint);
148 		Assert.assertEquals(0, dictionaryValidationResult.getNumberOfWarnings());
149 		Assert.assertEquals(1, dictionaryValidationResult.getNumberOfErrors());
150 		Assert.assertEquals(ErrorLevel.ERROR, result.getStatus());
151 		Assert.assertEquals(new ValidCharactersConstraintProcessor().getName(), result.getConstraintName());
152 	}	
153 	
154 	@Test
155 	public void testValueInvalidNegativeNumber() {
156 		ConstraintValidationResult result = process(morganAccount, "totalBalance", totalBalanceFixedPointPatternConstraint);
157 		Assert.assertEquals(0, dictionaryValidationResult.getNumberOfWarnings());
158 		Assert.assertEquals(1, dictionaryValidationResult.getNumberOfErrors());
159 		Assert.assertEquals(ErrorLevel.ERROR, result.getStatus());
160 		Assert.assertEquals(new ValidCharactersConstraintProcessor().getName(), result.getConstraintName());
161 	}	
162 	
163 	@Test
164 	public void testValueValidNegativeNumber() {
165 		ConstraintValidationResult result = process(morganAccount, "debitBalance", debitBalanceFixedPointPatternConstraint);
166 		Assert.assertEquals(0, dictionaryValidationResult.getNumberOfWarnings());
167 		Assert.assertEquals(0, dictionaryValidationResult.getNumberOfErrors());
168 		Assert.assertEquals(ErrorLevel.OK, result.getStatus());
169 		Assert.assertEquals(new ValidCharactersConstraintProcessor().getName(), result.getConstraintName());
170 	}
171 	
172 	@Test
173 	public void testValueInvalidNegativeNumber1() {
174 		ConstraintValidationResult result = process(iciciAccount, "debitBalance", debitBalanceFixedPointPatternConstraint);
175 		Assert.assertEquals(0, dictionaryValidationResult.getNumberOfWarnings());
176 		Assert.assertEquals(1, dictionaryValidationResult.getNumberOfErrors());
177 		Assert.assertEquals(ErrorLevel.ERROR, result.getStatus());
178 		Assert.assertEquals(new ValidCharactersConstraintProcessor().getName(), result.getConstraintName());
179 	}
180 	
181 	@Test
182 	public void testValueInvalidPositiveNumber1() {
183 		ConstraintValidationResult result = process(citibankAccount, "debitBalance", debitBalanceFixedPointPatternConstraint);
184 		Assert.assertEquals(0, dictionaryValidationResult.getNumberOfWarnings());
185 		Assert.assertEquals(1, dictionaryValidationResult.getNumberOfErrors());
186 		Assert.assertEquals(ErrorLevel.ERROR, result.getStatus());
187 		Assert.assertEquals(new ValidCharactersConstraintProcessor().getName(), result.getConstraintName());
188 	}
189 	
190 	@Test
191 	public void testValueValidNegativeNumber1() {
192 		ConstraintValidationResult result = process(wellsFargoAccount, "debitBalance", debitBalanceFixedPointPatternConstraint);
193 		Assert.assertEquals(0, dictionaryValidationResult.getNumberOfWarnings());
194 		Assert.assertEquals(0, dictionaryValidationResult.getNumberOfErrors());
195 		Assert.assertEquals(ErrorLevel.OK, result.getStatus());
196 		Assert.assertEquals(new ValidCharactersConstraintProcessor().getName(), result.getConstraintName());
197 	}	
198 	
199 	@Test(expected=java.util.regex.PatternSyntaxException.class)
200 	public void testZeroPrecisionAndScale() {
201 		ConstraintValidationResult result = process(wellsFargoAccount, "creditBalance", creditBalanceFixedPointPatternConstraint);
202 		Assert.assertEquals(0, dictionaryValidationResult.getNumberOfWarnings());
203 		Assert.assertEquals(0, dictionaryValidationResult.getNumberOfErrors());
204 		Assert.assertEquals(ErrorLevel.OK, result.getStatus());
205 		Assert.assertEquals(new ValidCharactersConstraintProcessor().getName(), result.getConstraintName());
206 	}
207 	
208 	@Test(expected=java.util.regex.PatternSyntaxException.class)
209 	public void testPrecisionLessThanScale() {
210 		ConstraintValidationResult result = process(wellsFargoAccount, "creditBalance", creditBalanceFixedPointPatternConstraint1);
211 		Assert.assertEquals(0, dictionaryValidationResult.getNumberOfWarnings());
212 		Assert.assertEquals(0, dictionaryValidationResult.getNumberOfErrors());
213 		Assert.assertEquals(ErrorLevel.OK, result.getStatus());
214 		Assert.assertEquals(new ValidCharactersConstraintProcessor().getName(), result.getConstraintName());
215 	}
216 	
217 	@Test(expected=java.util.regex.PatternSyntaxException.class)
218 	public void testNegativePrecisionAndScale() {
219 		ConstraintValidationResult result = process(wellsFargoAccount, "creditBalance", creditBalanceFixedPointPatternConstraint2);
220 		Assert.assertEquals(0, dictionaryValidationResult.getNumberOfWarnings());
221 		Assert.assertEquals(0, dictionaryValidationResult.getNumberOfErrors());
222 		Assert.assertEquals(ErrorLevel.OK, result.getStatus());
223 		Assert.assertEquals(new ValidCharactersConstraintProcessor().getName(), result.getConstraintName());
224 	}
225 	
226 	private ConstraintValidationResult process(Object object, String attributeName, ValidCharactersConstraint constraint) {
227 		AttributeValueReader attributeValueReader = new DictionaryObjectAttributeValueReader(object, "org.kuali.rice.kns.datadictionary.validation.MockAddress", addressEntry);
228 		attributeValueReader.setAttributeName(attributeName);
229 		
230 		Object value = attributeValueReader.getValue();
231 		return processor.process(dictionaryValidationResult, value, constraint, attributeValueReader).getFirstConstraintValidationResult();
232 	}
233 }