View Javadoc
1   /**
2    * Copyright 2005-2014 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.setMessageKey("validate.dummykey");
91          totalBalanceFixedPointPatternConstraint.setValidationMessageParams( new ArrayList<String>());
92  		totalBalanceFixedPointPatternConstraint.setPrecision(6);
93  		totalBalanceFixedPointPatternConstraint.setScale(2);
94  
95  		totalBalanceDefinition = new AttributeDefinition();
96  		totalBalanceDefinition.setName("totalBalance");
97  		totalBalanceDefinition.setValidCharactersConstraint(totalBalanceFixedPointPatternConstraint);
98  		attributes.add(totalBalanceDefinition);
99  
100 		debitBalanceFixedPointPatternConstraint = new FixedPointPatternConstraint();
101         debitBalanceFixedPointPatternConstraint.setMessageKey("validate.dummykey");
102         debitBalanceFixedPointPatternConstraint.setValidationMessageParams( new ArrayList<String>());
103 		debitBalanceFixedPointPatternConstraint.setPrecision(6);
104 		debitBalanceFixedPointPatternConstraint.setScale(2);
105 		debitBalanceFixedPointPatternConstraint.setAllowNegative(true);
106 
107 		debitBalanceDefinition = new AttributeDefinition();
108 		debitBalanceDefinition.setName("debitBalance");
109 		debitBalanceDefinition.setValidCharactersConstraint(debitBalanceFixedPointPatternConstraint);
110 		attributes.add(debitBalanceDefinition);
111 
112 		creditBalanceFixedPointPatternConstraint = new FixedPointPatternConstraint();
113         creditBalanceFixedPointPatternConstraint.setMessageKey("validate.dummykey");
114         creditBalanceFixedPointPatternConstraint.setValidationMessageParams( new ArrayList<String>());
115 		creditBalanceFixedPointPatternConstraint.setPrecision(0);
116 		creditBalanceFixedPointPatternConstraint.setScale(0);
117 
118 		creditBalanceDefinition = new AttributeDefinition();
119 		creditBalanceDefinition.setName("creditBalance");
120 		creditBalanceDefinition.setValidCharactersConstraint(creditBalanceFixedPointPatternConstraint);
121 		attributes.add(creditBalanceDefinition);
122 
123 		creditBalanceFixedPointPatternConstraint1 = new FixedPointPatternConstraint();
124         creditBalanceFixedPointPatternConstraint1.setMessageKey("validate.dummykey");
125         creditBalanceFixedPointPatternConstraint1.setValidationMessageParams( new ArrayList<String>());
126 		creditBalanceFixedPointPatternConstraint1.setPrecision(2);
127 		creditBalanceFixedPointPatternConstraint1.setScale(3);
128 
129 		creditBalanceFixedPointPatternConstraint2 = new FixedPointPatternConstraint();
130         creditBalanceFixedPointPatternConstraint2.setMessageKey("validate.dummykey");
131         creditBalanceFixedPointPatternConstraint2.setValidationMessageParams( new ArrayList<String>());
132 		creditBalanceFixedPointPatternConstraint2.setPrecision(-2);
133 		creditBalanceFixedPointPatternConstraint2.setScale(-3);
134 
135 		addressEntry.setAttributes(attributes);
136 	}
137 
138 	@Test
139 	public void testValueEmpty() {
140 		ConstraintValidationResult result = process(wellsFargoAccount, "totalBalance", totalBalanceFixedPointPatternConstraint);
141 		Assert.assertEquals(0, dictionaryValidationResult.getNumberOfWarnings());
142 		Assert.assertEquals(0, dictionaryValidationResult.getNumberOfErrors());
143 		Assert.assertEquals(ErrorLevel.INAPPLICABLE, result.getStatus());
144 		Assert.assertEquals(new ValidCharactersConstraintProcessor().getName(), result.getConstraintName());
145 	}
146 
147 	@Test
148 	public void testValueValidPositiveNumber() {
149 		ConstraintValidationResult result = process(iciciAccount, "totalBalance", totalBalanceFixedPointPatternConstraint);
150 		Assert.assertEquals(0, dictionaryValidationResult.getNumberOfWarnings());
151 		Assert.assertEquals(0, dictionaryValidationResult.getNumberOfErrors());
152 		Assert.assertEquals(ErrorLevel.OK, result.getStatus());
153 		Assert.assertEquals(new ValidCharactersConstraintProcessor().getName(), result.getConstraintName());
154 	}
155 
156 	@Test
157 	public void testValueInvalidPositiveNumber() {
158 		ConstraintValidationResult result = process(citibankAccount, "totalBalance", totalBalanceFixedPointPatternConstraint);
159 		Assert.assertEquals(0, dictionaryValidationResult.getNumberOfWarnings());
160 		Assert.assertEquals(1, dictionaryValidationResult.getNumberOfErrors());
161 		Assert.assertEquals(ErrorLevel.ERROR, result.getStatus());
162 		Assert.assertEquals(new ValidCharactersConstraintProcessor().getName(), result.getConstraintName());
163 	}
164 
165 	@Test
166 	public void testValueInvalidNegativeNumber() {
167 		ConstraintValidationResult result = process(morganAccount, "totalBalance", totalBalanceFixedPointPatternConstraint);
168 		Assert.assertEquals(0, dictionaryValidationResult.getNumberOfWarnings());
169 		Assert.assertEquals(1, dictionaryValidationResult.getNumberOfErrors());
170 		Assert.assertEquals(ErrorLevel.ERROR, result.getStatus());
171 		Assert.assertEquals(new ValidCharactersConstraintProcessor().getName(), result.getConstraintName());
172 	}
173 
174 	@Test
175 	public void testValueValidNegativeNumber() {
176 		ConstraintValidationResult result = process(morganAccount, "debitBalance", debitBalanceFixedPointPatternConstraint);
177 		Assert.assertEquals(0, dictionaryValidationResult.getNumberOfWarnings());
178 		Assert.assertEquals(0, dictionaryValidationResult.getNumberOfErrors());
179 		Assert.assertEquals(ErrorLevel.OK, result.getStatus());
180 		Assert.assertEquals(new ValidCharactersConstraintProcessor().getName(), result.getConstraintName());
181 	}
182 
183 	@Test
184 	public void testValueInvalidNegativeNumber1() {
185 		ConstraintValidationResult result = process(iciciAccount, "debitBalance", debitBalanceFixedPointPatternConstraint);
186 		Assert.assertEquals(0, dictionaryValidationResult.getNumberOfWarnings());
187 		Assert.assertEquals(1, dictionaryValidationResult.getNumberOfErrors());
188 		Assert.assertEquals(ErrorLevel.ERROR, result.getStatus());
189 		Assert.assertEquals(new ValidCharactersConstraintProcessor().getName(), result.getConstraintName());
190 	}
191 
192 	@Test
193 	public void testValueInvalidPositiveNumber1() {
194 		ConstraintValidationResult result = process(citibankAccount, "debitBalance", debitBalanceFixedPointPatternConstraint);
195 		Assert.assertEquals(0, dictionaryValidationResult.getNumberOfWarnings());
196 		Assert.assertEquals(1, dictionaryValidationResult.getNumberOfErrors());
197 		Assert.assertEquals(ErrorLevel.ERROR, result.getStatus());
198 		Assert.assertEquals(new ValidCharactersConstraintProcessor().getName(), result.getConstraintName());
199 	}
200 
201 	@Test
202 	public void testValueValidNegativeNumber1() {
203 		ConstraintValidationResult result = process(wellsFargoAccount, "debitBalance", debitBalanceFixedPointPatternConstraint);
204 		Assert.assertEquals(0, dictionaryValidationResult.getNumberOfWarnings());
205 		Assert.assertEquals(0, dictionaryValidationResult.getNumberOfErrors());
206 		Assert.assertEquals(ErrorLevel.OK, result.getStatus());
207 		Assert.assertEquals(new ValidCharactersConstraintProcessor().getName(), result.getConstraintName());
208 	}
209 
210 	@Test(expected=RuntimeException.class)
211 	public void testZeroPrecisionAndScale() {
212 		ConstraintValidationResult result = process(wellsFargoAccount, "creditBalance", creditBalanceFixedPointPatternConstraint);
213 		Assert.assertEquals(0, dictionaryValidationResult.getNumberOfWarnings());
214 		Assert.assertEquals(0, dictionaryValidationResult.getNumberOfErrors());
215 		Assert.assertEquals(ErrorLevel.OK, result.getStatus());
216 		Assert.assertEquals(new ValidCharactersConstraintProcessor().getName(), result.getConstraintName());
217 	}
218 
219 	@Test(expected=RuntimeException.class)
220 	public void testPrecisionLessThanScale() {
221 		ConstraintValidationResult result = process(wellsFargoAccount, "creditBalance", creditBalanceFixedPointPatternConstraint1);
222 		Assert.assertEquals(0, dictionaryValidationResult.getNumberOfWarnings());
223 		Assert.assertEquals(0, dictionaryValidationResult.getNumberOfErrors());
224 		Assert.assertEquals(ErrorLevel.OK, result.getStatus());
225 		Assert.assertEquals(new ValidCharactersConstraintProcessor().getName(), result.getConstraintName());
226 	}
227 
228 	@Test(expected=RuntimeException.class)
229 	public void testNegativePrecisionAndScale() {
230 		ConstraintValidationResult result = process(wellsFargoAccount, "creditBalance", creditBalanceFixedPointPatternConstraint2);
231 		Assert.assertEquals(0, dictionaryValidationResult.getNumberOfWarnings());
232 		Assert.assertEquals(0, dictionaryValidationResult.getNumberOfErrors());
233 		Assert.assertEquals(ErrorLevel.OK, result.getStatus());
234 		Assert.assertEquals(new ValidCharactersConstraintProcessor().getName(), result.getConstraintName());
235 	}
236 
237 	private ConstraintValidationResult process(Object object, String attributeName, ValidCharactersConstraint constraint) {
238 		AttributeValueReader attributeValueReader = new DictionaryObjectAttributeValueReader(object, "org.kuali.rice.kns.datadictionary.validation.MockAddress", addressEntry);
239 		attributeValueReader.setAttributeName(attributeName);
240 
241 		Object value = attributeValueReader.getValue();
242 		return processor.process(dictionaryValidationResult, value, constraint, attributeValueReader).getFirstConstraintValidationResult();
243 	}
244 }