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