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
19
20 import java.io.IOException;
21 import java.io.InputStream;
22 import java.util.ArrayList;
23 import java.util.Properties;
24
25 import org.junit.Assert;
26 import org.junit.Before;
27 import org.junit.Test;
28 import org.kuali.rice.krad.datadictionary.AttributeDefinition;
29 import org.kuali.rice.krad.datadictionary.validation.AttributeValueReader;
30 import org.kuali.rice.krad.datadictionary.validation.ErrorLevel;
31 import org.kuali.rice.krad.datadictionary.validation.SingleAttributeValueReader;
32 import org.kuali.rice.krad.datadictionary.validation.constraint.ValidCharactersConstraint;
33 import org.kuali.rice.krad.datadictionary.validation.processor.ValidCharactersConstraintProcessor;
34 import org.kuali.rice.krad.datadictionary.validation.result.ConstraintValidationResult;
35 import org.kuali.rice.krad.datadictionary.validation.result.DictionaryValidationResult;
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52 public class MonthPatternConstraintTest {
53
54 private final String PATTERN_CONSTRAINT = "validationPatternRegex.month";
55
56 private AttributeDefinition monthDefinition;
57
58 private DictionaryValidationResult dictionaryValidationResult;
59
60 private ValidCharactersConstraintProcessor processor;
61
62 private String validMonth;
63 private String validMonth1;
64 private String validMonth2;
65 private String invalidMonthEmpty;
66 private String invalidMonth;
67 private String invalidMonth1;
68 private String invalidMonth2;
69 private String invalidMonth3;
70
71 private ConfigurationBasedRegexPatternConstraint monthPatternConstraint;
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 validMonth = "1";
84 validMonth1 = "05";
85 validMonth2 = "12";
86 invalidMonthEmpty = "";
87 invalidMonth = "00";
88 invalidMonth1 = "0";
89 invalidMonth2 = "13";
90 invalidMonth3 = "JAN";
91
92 monthPatternConstraint = new ConfigurationBasedRegexPatternConstraint();
93 monthPatternConstraint.setMessageKey("validate.dummykey");
94 monthPatternConstraint.setValidationMessageParams( new ArrayList<String>());
95 monthPatternConstraint.setValue(regex);
96
97 monthDefinition = new AttributeDefinition();
98 monthDefinition.setName("month");
99 monthDefinition.setValidCharactersConstraint(monthPatternConstraint);
100 }
101
102 @Test
103 public void testValueInvalidMonthEmpty() {
104 ConstraintValidationResult result = process(invalidMonthEmpty, "month", monthPatternConstraint);
105 Assert.assertEquals(0, dictionaryValidationResult.getNumberOfWarnings());
106 Assert.assertEquals(0, dictionaryValidationResult.getNumberOfErrors());
107 Assert.assertEquals(ErrorLevel.INAPPLICABLE, result.getStatus());
108 Assert.assertEquals(new ValidCharactersConstraintProcessor().getName(), result.getConstraintName());
109 }
110
111 @Test
112 public void testValueValidMonth() {
113 ConstraintValidationResult result = process(validMonth, "month", monthPatternConstraint);
114 Assert.assertEquals(0, dictionaryValidationResult.getNumberOfWarnings());
115 Assert.assertEquals(0, dictionaryValidationResult.getNumberOfErrors());
116 Assert.assertEquals(ErrorLevel.OK, result.getStatus());
117 Assert.assertEquals(new ValidCharactersConstraintProcessor().getName(), result.getConstraintName());
118 }
119
120 @Test
121 public void testValueValidMonth1() {
122 ConstraintValidationResult result = process(validMonth1, "Mmonth", monthPatternConstraint);
123 Assert.assertEquals(0, dictionaryValidationResult.getNumberOfWarnings());
124 Assert.assertEquals(0, dictionaryValidationResult.getNumberOfErrors());
125 Assert.assertEquals(ErrorLevel.OK, result.getStatus());
126 Assert.assertEquals(new ValidCharactersConstraintProcessor().getName(), result.getConstraintName());
127 }
128
129 @Test
130 public void testValueValidMonth2() {
131 ConstraintValidationResult result = process(validMonth2, "month", monthPatternConstraint);
132 Assert.assertEquals(0, dictionaryValidationResult.getNumberOfWarnings());
133 Assert.assertEquals(0, dictionaryValidationResult.getNumberOfErrors());
134 Assert.assertEquals(ErrorLevel.OK, result.getStatus());
135 Assert.assertEquals(new ValidCharactersConstraintProcessor().getName(), result.getConstraintName());
136 }
137
138 @Test
139 public void testValueInvalidMonth() {
140 ConstraintValidationResult result = process(invalidMonth, "month", monthPatternConstraint);
141 Assert.assertEquals(0, dictionaryValidationResult.getNumberOfWarnings());
142 Assert.assertEquals(1, dictionaryValidationResult.getNumberOfErrors());
143 Assert.assertEquals(ErrorLevel.ERROR, result.getStatus());
144 Assert.assertEquals(new ValidCharactersConstraintProcessor().getName(), result.getConstraintName());
145 }
146
147 @Test
148 public void testValueInvalidMonth1() {
149 ConstraintValidationResult result = process(invalidMonth1, "month", monthPatternConstraint);
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 @Test
157 public void testValueInvalidMonth2() {
158 ConstraintValidationResult result = process(invalidMonth2, "month", monthPatternConstraint);
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 testValueInvalidMonth3() {
167 ConstraintValidationResult result = process(invalidMonth3, "month", monthPatternConstraint);
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 private ConstraintValidationResult process(Object object, String attributeName, ValidCharactersConstraint constraint) {
175 AttributeValueReader attributeValueReader = new SingleAttributeValueReader(object, "org.kuali.rice.kns.datadictionary.validation.MockAddress", attributeName, monthDefinition);
176 Object value = attributeValueReader.getValue();
177 return processor.process(dictionaryValidationResult, value, constraint, attributeValueReader).getFirstConstraintValidationResult();
178 }
179
180 private String getProperty(String key) {
181 String value = null;
182 String filePath = "org/kuali/rice/krad/ApplicationResources.properties";
183 Properties properties = new Properties();
184 try {
185 InputStream in = Thread.currentThread().getContextClassLoader().getResourceAsStream(filePath);
186 properties.load(in);
187 value = properties.getProperty(key);
188 } catch (IOException e) {
189 }
190 return value;
191 }
192 }