001 /** 002 * Copyright 2005-2012 The Kuali Foundation 003 * 004 * Licensed under the Educational Community License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.opensource.org/licenses/ecl2.php 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 package org.kuali.rice.krad.datadictionary.validation.constraint; 017 018 import java.util.ArrayList; 019 import java.util.List; 020 021 import org.junit.Assert; 022 import org.junit.Before; 023 import org.junit.Ignore; 024 import org.junit.Test; 025 import org.kuali.rice.krad.datadictionary.AttributeDefinition; 026 import org.kuali.rice.krad.datadictionary.BusinessObjectEntry; 027 import org.kuali.rice.krad.datadictionary.validation.Account; 028 import org.kuali.rice.krad.datadictionary.validation.AttributeValueReader; 029 import org.kuali.rice.krad.datadictionary.validation.DictionaryObjectAttributeValueReader; 030 import org.kuali.rice.krad.datadictionary.validation.ErrorLevel; 031 import org.kuali.rice.krad.datadictionary.validation.processor.ValidCharactersConstraintProcessor; 032 import org.kuali.rice.krad.datadictionary.validation.result.ConstraintValidationResult; 033 import org.kuali.rice.krad.datadictionary.validation.result.DictionaryValidationResult; 034 035 036 /** 037 * Things this test should check: 038 * 039 * 1. empty value check. (failure) {@link #testValueEmpty()} 040 * 2. value with valid positive number within specified precision and scale. (success) {@link #testValueValidPositiveNumber()} 041 * 3. value with invalid positive number as per given precision and scale. (failure) {@link testValueInvalidPositiveNumber()} 042 * 4. value with invalid negative number as allowNegative set to false. (failure) {@link testValueInvalidNegativeNumber()} 043 * 5. value with valid negative number within specified precision and scale, with allowNegative set to true. (success) {@link testValueValidNegativeNumber()} 044 * 6. value with invalid negative number as per given precision and scale. (failure) {@link testValueInvalidNegativeNumber1()} 045 * 7. value with invalid positive number as per given precision and scale. (failure) {@link testValueInvalidPositiveNumber1()} 046 * 8. value with valid negative number within specified precision and scale, with allowNegative set to true. (success) {@link testValueValidNegativeNumber1()} 047 * 9. zero precision and scale test. (error) {@link testZeroPrecisionAndScale()} 048 * 10. precision less than scale test. (error) {@link testPrecisionLessThanScale()} 049 * 11. negative precision and scale test. (error) {@link testNegativePrecisionAndScale()} 050 * 051 * 052 * 053 * @author Kuali Rice Team (rice.collab@kuali.org) 054 */ 055 public class FixedPointPatternConstraintTest { 056 057 private AttributeDefinition totalBalanceDefinition; 058 private AttributeDefinition debitBalanceDefinition; 059 private AttributeDefinition creditBalanceDefinition; 060 061 private BusinessObjectEntry addressEntry; 062 private DictionaryValidationResult dictionaryValidationResult; 063 064 private ValidCharactersConstraintProcessor processor; 065 066 private Account iciciAccount = new Account("11111111","ICICI","5000.00","15000","-10000"); 067 private Account citibankAccount = new Account("22222222","Citi Bank","15000.70","14999.70","1."); 068 private Account wellsFargoAccount = new Account("33333333","Wells Fargo","",".25","-.25"); 069 private Account morganAccount = new Account("44444444","J P Morgan","-1000.00","1000.00","-2000.00"); 070 071 private FixedPointPatternConstraint totalBalanceFixedPointPatternConstraint; 072 private FixedPointPatternConstraint debitBalanceFixedPointPatternConstraint; 073 private FixedPointPatternConstraint creditBalanceFixedPointPatternConstraint; 074 private FixedPointPatternConstraint creditBalanceFixedPointPatternConstraint1; 075 private FixedPointPatternConstraint creditBalanceFixedPointPatternConstraint2; 076 077 @Before 078 public void setUp() throws Exception { 079 080 processor = new ValidCharactersConstraintProcessor(); 081 082 dictionaryValidationResult = new DictionaryValidationResult(); 083 dictionaryValidationResult.setErrorLevel(ErrorLevel.NOCONSTRAINT); 084 085 addressEntry = new BusinessObjectEntry(); 086 087 List<AttributeDefinition> attributes = new ArrayList<AttributeDefinition>(); 088 089 totalBalanceFixedPointPatternConstraint = new FixedPointPatternConstraint(); 090 totalBalanceFixedPointPatternConstraint.setPrecision(6); 091 totalBalanceFixedPointPatternConstraint.setScale(2); 092 093 totalBalanceDefinition = new AttributeDefinition(); 094 totalBalanceDefinition.setName("totalBalance"); 095 totalBalanceDefinition.setValidCharactersConstraint(totalBalanceFixedPointPatternConstraint); 096 attributes.add(totalBalanceDefinition); 097 098 debitBalanceFixedPointPatternConstraint = new FixedPointPatternConstraint(); 099 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 }