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.io.IOException; 019 import java.io.InputStream; 020 import java.util.ArrayList; 021 import java.util.List; 022 import java.util.Properties; 023 024 import org.junit.Assert; 025 import org.junit.Before; 026 import org.junit.Ignore; 027 import org.junit.Test; 028 import org.kuali.rice.krad.datadictionary.AttributeDefinition; 029 import org.kuali.rice.krad.datadictionary.BusinessObjectEntry; 030 import org.kuali.rice.krad.datadictionary.validation.Account; 031 import org.kuali.rice.krad.datadictionary.validation.AttributeValueReader; 032 import org.kuali.rice.krad.datadictionary.validation.DictionaryObjectAttributeValueReader; 033 import org.kuali.rice.krad.datadictionary.validation.ErrorLevel; 034 import org.kuali.rice.krad.datadictionary.validation.constraint.ValidCharactersConstraint; 035 import org.kuali.rice.krad.datadictionary.validation.processor.ValidCharactersConstraintProcessor; 036 import org.kuali.rice.krad.datadictionary.validation.result.ConstraintValidationResult; 037 import org.kuali.rice.krad.datadictionary.validation.result.DictionaryValidationResult; 038 039 040 /** 041 * Things this test should check: 042 * 043 * 1. empty value check. (failure) {@link #testValueEmpty()} 044 * 2. value with valid positive number. (success) {@link #testValueValidPositiveNumber()} 045 * 3. value with invalid negative number as allowNegative set to false. (failure) {@link testValueInvalidNegativeNumber()} 046 * 4. value with valid negative number as allowNegative set to true. (success) {@link testValueValidNegativeNumber()} 047 * 5. value with invalid negative number as allowNegative set to true.. (failure) {@link testValueInvalidNegativeNumber1()} 048 * 6. value with invalid positive number. (failure) {@link testValueInvalidPositiveNumber()} 049 * 7. value with valid negative number as allowNegative set to true. (success) {@link testValueValidNegativeNumber1)} 050 * 051 * @author Kuali Rice Team (rice.collab@kuali.org) 052 */ 053 public class FloatingPointPatternConstraintTest { 054 055 private final String PATTERN_CONSTRAINT = "validationPatternRegex.floatingPoint"; 056 057 private AttributeDefinition totalBalanceDefinition; 058 private AttributeDefinition debitBalanceDefinition; 059 060 private BusinessObjectEntry addressEntry; 061 private DictionaryValidationResult dictionaryValidationResult; 062 063 private ValidCharactersConstraintProcessor processor; 064 065 private Account iciciAccount = new Account("11111111","ICICI","5000.00","15000","-10000"); 066 private Account citibankAccount = new Account("22222222","Citi Bank","15000.70","14999.70","1."); 067 private Account wellsFargoAccount = new Account("33333333","Wells Fargo","",".25","-.25"); 068 private Account morganAccount = new Account("44444444","J P Morgan","-1000.00","1000.00","(2000.00)"); 069 070 private FloatingPointPatternConstraint totalBalanceFloatingPointPatternConstraint; 071 private FloatingPointPatternConstraint debitBalanceFloatingPointPatternConstraint; 072 073 @Before 074 public void setUp() throws Exception { 075 076 String regex = getProperty(PATTERN_CONSTRAINT); 077 078 processor = new ValidCharactersConstraintProcessor(); 079 080 dictionaryValidationResult = new DictionaryValidationResult(); 081 dictionaryValidationResult.setErrorLevel(ErrorLevel.NOCONSTRAINT); 082 083 addressEntry = new BusinessObjectEntry(); 084 085 List<AttributeDefinition> attributes = new ArrayList<AttributeDefinition>(); 086 087 totalBalanceFloatingPointPatternConstraint = new FloatingPointPatternConstraint(); 088 totalBalanceFloatingPointPatternConstraint.setValue(regex); 089 090 totalBalanceDefinition = new AttributeDefinition(); 091 totalBalanceDefinition.setName("totalBalance"); 092 totalBalanceDefinition.setValidCharactersConstraint(totalBalanceFloatingPointPatternConstraint); 093 attributes.add(totalBalanceDefinition); 094 095 debitBalanceFloatingPointPatternConstraint = new FloatingPointPatternConstraint(); 096 debitBalanceFloatingPointPatternConstraint.setValue("-?"+regex); 097 debitBalanceFloatingPointPatternConstraint.setAllowNegative(true); 098 099 debitBalanceDefinition = new AttributeDefinition(); 100 debitBalanceDefinition.setName("debitBalance"); 101 debitBalanceDefinition.setValidCharactersConstraint(debitBalanceFloatingPointPatternConstraint); 102 attributes.add(debitBalanceDefinition); 103 104 105 addressEntry.setAttributes(attributes); 106 } 107 108 @Test 109 public void testValueEmpty() { 110 ConstraintValidationResult result = process(wellsFargoAccount, "totalBalance", totalBalanceFloatingPointPatternConstraint); 111 Assert.assertEquals(0, dictionaryValidationResult.getNumberOfWarnings()); 112 Assert.assertEquals(0, dictionaryValidationResult.getNumberOfErrors()); 113 Assert.assertEquals(ErrorLevel.INAPPLICABLE, result.getStatus()); 114 Assert.assertEquals(new ValidCharactersConstraintProcessor().getName(), result.getConstraintName()); 115 } 116 117 @Test 118 public void testValueValidPositiveNumber() { 119 ConstraintValidationResult result = process(citibankAccount, "totalBalance", totalBalanceFloatingPointPatternConstraint); 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 @Ignore 127 @Test 128 public void testValueInvalidNegativeNumber() { 129 ConstraintValidationResult result = process(morganAccount, "totalBalance", totalBalanceFloatingPointPatternConstraint); 130 Assert.assertEquals(0, dictionaryValidationResult.getNumberOfWarnings()); 131 Assert.assertEquals(1, dictionaryValidationResult.getNumberOfErrors()); 132 Assert.assertEquals(ErrorLevel.ERROR, result.getStatus()); 133 Assert.assertEquals(new ValidCharactersConstraintProcessor().getName(), result.getConstraintName()); 134 } 135 136 @Test 137 public void testValueValidNegativeNumber() { 138 ConstraintValidationResult result = process(iciciAccount, "debitBalance", debitBalanceFloatingPointPatternConstraint); 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 @Ignore 146 @Test 147 public void testValueInvalidNegativeNumber1() { 148 ConstraintValidationResult result = process(morganAccount, "debitBalance", debitBalanceFloatingPointPatternConstraint); 149 Assert.assertEquals(0, dictionaryValidationResult.getNumberOfWarnings()); 150 Assert.assertEquals(1, dictionaryValidationResult.getNumberOfErrors()); 151 Assert.assertEquals(ErrorLevel.ERROR, result.getStatus()); 152 Assert.assertEquals(new ValidCharactersConstraintProcessor().getName(), result.getConstraintName()); 153 } 154 155 @Ignore 156 @Test 157 public void testValueInvalidPositiveNumber() { 158 ConstraintValidationResult result = process(citibankAccount, "debitBalance", debitBalanceFloatingPointPatternConstraint); 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 testValueValidNegativeNumber1() { 167 ConstraintValidationResult result = process(wellsFargoAccount, "debitBalance", debitBalanceFloatingPointPatternConstraint); 168 Assert.assertEquals(0, dictionaryValidationResult.getNumberOfWarnings()); 169 Assert.assertEquals(0, dictionaryValidationResult.getNumberOfErrors()); 170 Assert.assertEquals(ErrorLevel.OK, result.getStatus()); 171 Assert.assertEquals(new ValidCharactersConstraintProcessor().getName(), result.getConstraintName()); 172 } 173 174 private ConstraintValidationResult process(Object object, String attributeName, ValidCharactersConstraint constraint) { 175 AttributeValueReader attributeValueReader = new DictionaryObjectAttributeValueReader(object, "org.kuali.rice.kns.datadictionary.validation.MockAddress", addressEntry); 176 attributeValueReader.setAttributeName(attributeName); 177 178 Object value = attributeValueReader.getValue(); 179 return processor.process(dictionaryValidationResult, value, constraint, attributeValueReader).getFirstConstraintValidationResult(); 180 } 181 182 private String getProperty(String key) { 183 String value = null; 184 String filePath = "org/kuali/rice/krad/ApplicationResources.properties"; 185 Properties properties = new Properties(); 186 try { 187 InputStream in = Thread.currentThread().getContextClassLoader().getResourceAsStream(filePath); 188 properties.load(in); 189 value = properties.getProperty(key); 190 } catch (IOException e) { 191 } 192 return value; 193 } 194 }