View Javadoc

1   /**
2    * Copyright 2005-2012 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.io.IOException;
19  import java.io.InputStream;
20  import java.util.ArrayList;
21  import java.util.List;
22  import java.util.Properties;
23  
24  import org.junit.Assert;
25  import org.junit.Before;
26  import org.junit.Ignore;
27  import org.junit.Test;
28  import org.kuali.rice.krad.datadictionary.AttributeDefinition;
29  import org.kuali.rice.krad.datadictionary.BusinessObjectEntry;
30  import org.kuali.rice.krad.datadictionary.validation.Account;
31  import org.kuali.rice.krad.datadictionary.validation.AttributeValueReader;
32  import org.kuali.rice.krad.datadictionary.validation.DictionaryObjectAttributeValueReader;
33  import org.kuali.rice.krad.datadictionary.validation.ErrorLevel;
34  import org.kuali.rice.krad.datadictionary.validation.constraint.ValidCharactersConstraint;
35  import org.kuali.rice.krad.datadictionary.validation.processor.ValidCharactersConstraintProcessor;
36  import org.kuali.rice.krad.datadictionary.validation.result.ConstraintValidationResult;
37  import org.kuali.rice.krad.datadictionary.validation.result.DictionaryValidationResult;
38  
39  
40  /**
41   * Things this test should check:
42   *
43   * 1. empty value check. (failure) {@link #testValueEmpty()}
44   * 2. value with valid positive number. (success) {@link #testValueValidPositiveNumber()}
45   * 3. value with invalid negative number as allowNegative set to false. (failure) {@link #testValueInvalidNegativeNumber()}
46   * 4. value with valid negative number as allowNegative set to true. (success) {@link #testValueValidNegativeNumber()}
47   * 5. value with invalid negative number as allowNegative set to true.. (failure) {@link #testValueInvalidNegativeNumber1()}
48   * 6. value with invalid positive number. (failure) {@link #testValueInvalidPositiveNumber()}
49   * 7. value with valid negative number as allowNegative set to true. (success) {@link #testValueValidNegativeNumber1)}
50   *
51   * @author Kuali Rice Team (rice.collab@kuali.org)
52   */
53  public class FloatingPointPatternConstraintTest {
54  
55  	private final String PATTERN_CONSTRAINT = "validationPatternRegex.floatingPoint";
56  
57  	private AttributeDefinition totalBalanceDefinition;
58  	private AttributeDefinition debitBalanceDefinition;
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 FloatingPointPatternConstraint totalBalanceFloatingPointPatternConstraint;
71  	private FloatingPointPatternConstraint debitBalanceFloatingPointPatternConstraint;
72  
73  	@Before
74  	public void setUp() throws Exception {
75  
76  		String regex = getProperty(PATTERN_CONSTRAINT);
77  
78  		processor = new ValidCharactersConstraintProcessor();
79  
80  		dictionaryValidationResult = new DictionaryValidationResult();
81  		dictionaryValidationResult.setErrorLevel(ErrorLevel.NOCONSTRAINT);
82  
83  		addressEntry = new BusinessObjectEntry();
84  
85  		List<AttributeDefinition> attributes = new ArrayList<AttributeDefinition>();
86  
87  		totalBalanceFloatingPointPatternConstraint = new FloatingPointPatternConstraint();
88  		totalBalanceFloatingPointPatternConstraint.setValue(regex);
89  
90  		totalBalanceDefinition = new AttributeDefinition();
91  		totalBalanceDefinition.setName("totalBalance");
92  		totalBalanceDefinition.setValidCharactersConstraint(totalBalanceFloatingPointPatternConstraint);
93  		attributes.add(totalBalanceDefinition);
94  
95  		debitBalanceFloatingPointPatternConstraint = new FloatingPointPatternConstraint();
96  		debitBalanceFloatingPointPatternConstraint.setValue("-?"+regex);
97  		debitBalanceFloatingPointPatternConstraint.setAllowNegative(true);
98  
99  		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 }