1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
42
43
44
45
46
47
48
49
50
51
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.setMessageKey("validate.dummykey");
89 totalBalanceFloatingPointPatternConstraint.setValidationMessageParams( new ArrayList<String>());
90 totalBalanceFloatingPointPatternConstraint.setValue(regex);
91
92 totalBalanceDefinition = new AttributeDefinition();
93 totalBalanceDefinition.setName("totalBalance");
94 totalBalanceDefinition.setValidCharactersConstraint(totalBalanceFloatingPointPatternConstraint);
95 attributes.add(totalBalanceDefinition);
96
97 debitBalanceFloatingPointPatternConstraint = new FloatingPointPatternConstraint();
98 debitBalanceFloatingPointPatternConstraint.setMessageKey("validate.dummykey");
99 debitBalanceFloatingPointPatternConstraint.setValidationMessageParams( new ArrayList<String>());
100 debitBalanceFloatingPointPatternConstraint.setValue("-?"+regex);
101 debitBalanceFloatingPointPatternConstraint.setAllowNegative(true);
102
103 debitBalanceDefinition = new AttributeDefinition();
104 debitBalanceDefinition.setName("debitBalance");
105 debitBalanceDefinition.setValidCharactersConstraint(debitBalanceFloatingPointPatternConstraint);
106 attributes.add(debitBalanceDefinition);
107
108
109 addressEntry.setAttributes(attributes);
110 }
111
112 @Test
113 public void testValueEmpty() {
114 ConstraintValidationResult result = process(wellsFargoAccount, "totalBalance", totalBalanceFloatingPointPatternConstraint);
115 Assert.assertEquals(0, dictionaryValidationResult.getNumberOfWarnings());
116 Assert.assertEquals(0, dictionaryValidationResult.getNumberOfErrors());
117 Assert.assertEquals(ErrorLevel.INAPPLICABLE, result.getStatus());
118 Assert.assertEquals(new ValidCharactersConstraintProcessor().getName(), result.getConstraintName());
119 }
120
121 @Test
122 public void testValueValidPositiveNumber() {
123 ConstraintValidationResult result = process(citibankAccount, "totalBalance", totalBalanceFloatingPointPatternConstraint);
124 Assert.assertEquals(0, dictionaryValidationResult.getNumberOfWarnings());
125 Assert.assertEquals(0, dictionaryValidationResult.getNumberOfErrors());
126 Assert.assertEquals(ErrorLevel.OK, result.getStatus());
127 Assert.assertEquals(new ValidCharactersConstraintProcessor().getName(), result.getConstraintName());
128 }
129
130 @Test
131 public void testValueInvalidNegativeNumber() {
132 ConstraintValidationResult result = process(morganAccount, "totalBalance", totalBalanceFloatingPointPatternConstraint);
133 Assert.assertEquals(0, dictionaryValidationResult.getNumberOfWarnings());
134 Assert.assertEquals(1, dictionaryValidationResult.getNumberOfErrors());
135 Assert.assertEquals(ErrorLevel.ERROR, result.getStatus());
136 Assert.assertEquals(new ValidCharactersConstraintProcessor().getName(), result.getConstraintName());
137 }
138
139 @Test
140 public void testValueValidNegativeNumber() {
141 ConstraintValidationResult result = process(iciciAccount, "debitBalance", debitBalanceFloatingPointPatternConstraint);
142 Assert.assertEquals(0, dictionaryValidationResult.getNumberOfWarnings());
143 Assert.assertEquals(0, dictionaryValidationResult.getNumberOfErrors());
144 Assert.assertEquals(ErrorLevel.OK, result.getStatus());
145 Assert.assertEquals(new ValidCharactersConstraintProcessor().getName(), result.getConstraintName());
146 }
147
148 @Test
149 public void testValueInvalidNegativeNumber1() {
150 ConstraintValidationResult result = process(morganAccount, "debitBalance", debitBalanceFloatingPointPatternConstraint);
151 Assert.assertEquals(0, dictionaryValidationResult.getNumberOfWarnings());
152 Assert.assertEquals(1, dictionaryValidationResult.getNumberOfErrors());
153 Assert.assertEquals(ErrorLevel.ERROR, result.getStatus());
154 Assert.assertEquals(new ValidCharactersConstraintProcessor().getName(), result.getConstraintName());
155 }
156
157 @Test
158 public void testValueInvalidPositiveNumber() {
159 ConstraintValidationResult result = process(citibankAccount, "debitBalance", debitBalanceFloatingPointPatternConstraint);
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 testValueValidNegativeNumber1() {
168 ConstraintValidationResult result = process(wellsFargoAccount, "debitBalance", debitBalanceFloatingPointPatternConstraint);
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 private ConstraintValidationResult process(Object object, String attributeName, ValidCharactersConstraint constraint) {
176 AttributeValueReader attributeValueReader = new DictionaryObjectAttributeValueReader(object, "org.kuali.rice.kns.datadictionary.validation.MockAddress", addressEntry);
177 attributeValueReader.setAttributeName(attributeName);
178
179 Object value = attributeValueReader.getValue();
180 return processor.process(dictionaryValidationResult, value, constraint, attributeValueReader).getFirstConstraintValidationResult();
181 }
182
183 private String getProperty(String key) {
184 String value = null;
185 String filePath = "org/kuali/rice/krad/ApplicationResources.properties";
186 Properties properties = new Properties();
187 try {
188 InputStream in = Thread.currentThread().getContextClassLoader().getResourceAsStream(filePath);
189 properties.load(in);
190 value = properties.getProperty(key);
191 } catch (IOException e) {
192 }
193 return value;
194 }
195 }