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.Test;
27 import org.kuali.rice.krad.datadictionary.AttributeDefinition;
28 import org.kuali.rice.krad.datadictionary.BusinessObjectEntry;
29 import org.kuali.rice.krad.datadictionary.validation.AttributeValueReader;
30 import org.kuali.rice.krad.datadictionary.validation.DictionaryObjectAttributeValueReader;
31 import org.kuali.rice.krad.datadictionary.validation.Employee;
32 import org.kuali.rice.krad.datadictionary.validation.ErrorLevel;
33 import org.kuali.rice.krad.datadictionary.validation.constraint.ValidCharactersConstraint;
34 import org.kuali.rice.krad.datadictionary.validation.processor.ValidCharactersConstraintProcessor;
35 import org.kuali.rice.krad.datadictionary.validation.result.ConstraintValidationResult;
36 import org.kuali.rice.krad.datadictionary.validation.result.DictionaryValidationResult;
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55 public class PhoneNumberPatternConstraintTest {
56
57 private final String PATTERN_CONSTRAINT = "validationPatternRegex.phoneNumber";
58
59 private AttributeDefinition contactPhoneDefinition;
60
61 private BusinessObjectEntry addressEntry;
62 private DictionaryValidationResult dictionaryValidationResult;
63
64 private ValidCharactersConstraintProcessor processor;
65
66 private Employee validPhoneEmployee = new Employee();
67 private Employee invalidPhoneEmployeeEmpty = new Employee();
68 private Employee invalidPhoneEmployee = new Employee();
69 private Employee invalidPhoneEmployee1 = new Employee();
70 private Employee invalidPhoneEmployee2 = new Employee();
71 private Employee invalidPhoneEmployee3 = new Employee();
72 private Employee invalidPhoneEmployee4 = new Employee();
73 private Employee invalidPhoneEmployee5 = new Employee();
74 private Employee invalidPhoneEmployee6 = new Employee();
75
76 private ConfigurationBasedRegexPatternConstraint contactPhoneNumberPatternConstraint;
77
78 @Before
79 public void setUp() throws Exception {
80
81 String regex = getProperty(PATTERN_CONSTRAINT);
82
83 processor = new ValidCharactersConstraintProcessor();
84
85 dictionaryValidationResult = new DictionaryValidationResult();
86 dictionaryValidationResult.setErrorLevel(ErrorLevel.NOCONSTRAINT);
87
88 addressEntry = new BusinessObjectEntry();
89
90 List<AttributeDefinition> attributes = new ArrayList<AttributeDefinition>();
91
92 validPhoneEmployee.setContactPhone("056-012-1200");
93 invalidPhoneEmployeeEmpty.setContactPhone("");
94 invalidPhoneEmployee.setContactPhone("09712248474");
95 invalidPhoneEmployee1.setContactPhone("+19712248474");
96 invalidPhoneEmployee2.setContactPhone("+1-972-232-3333");
97 invalidPhoneEmployee3.setContactPhone("+1-(972)-23334444");
98 invalidPhoneEmployee4.setContactPhone("+1-(972)-1223444 xtn.222");
99 invalidPhoneEmployee5.setContactPhone("+1 056 012 1200");
100 invalidPhoneEmployee6.setContactPhone("056\\-012\\-1200");
101
102 contactPhoneNumberPatternConstraint = new ConfigurationBasedRegexPatternConstraint();
103 contactPhoneNumberPatternConstraint.setMessageKey("validate.dummykey");
104 contactPhoneNumberPatternConstraint.setValidationMessageParams( new ArrayList<String>());
105 contactPhoneNumberPatternConstraint.setValue(regex);
106
107 contactPhoneDefinition = new AttributeDefinition();
108 contactPhoneDefinition.setName("contactPhone");
109 contactPhoneDefinition.setValidCharactersConstraint(contactPhoneNumberPatternConstraint);
110 attributes.add(contactPhoneDefinition);
111
112 addressEntry.setAttributes(attributes);
113 }
114
115 @Test
116 public void testValueInvalidPhoneNumberEmpty() {
117 ConstraintValidationResult result = process(invalidPhoneEmployeeEmpty, "contactPhone", contactPhoneNumberPatternConstraint);
118 Assert.assertEquals(0, dictionaryValidationResult.getNumberOfWarnings());
119 Assert.assertEquals(0, dictionaryValidationResult.getNumberOfErrors());
120 Assert.assertEquals(ErrorLevel.INAPPLICABLE, result.getStatus());
121 Assert.assertEquals(new ValidCharactersConstraintProcessor().getName(), result.getConstraintName());
122 }
123
124 @Test
125 public void testValueValidPhoneNumber() {
126 ConstraintValidationResult result = process(validPhoneEmployee, "contactPhone", contactPhoneNumberPatternConstraint);
127 Assert.assertEquals(0, dictionaryValidationResult.getNumberOfWarnings());
128 Assert.assertEquals(0, dictionaryValidationResult.getNumberOfErrors());
129 Assert.assertEquals(ErrorLevel.OK, result.getStatus());
130 Assert.assertEquals(new ValidCharactersConstraintProcessor().getName(), result.getConstraintName());
131 }
132
133
134 @Test
135 public void testValueInvalidPhoneNumber() {
136 ConstraintValidationResult result = process(invalidPhoneEmployee, "contactPhone", contactPhoneNumberPatternConstraint);
137 Assert.assertEquals(0, dictionaryValidationResult.getNumberOfWarnings());
138 Assert.assertEquals(1, dictionaryValidationResult.getNumberOfErrors());
139 Assert.assertEquals(ErrorLevel.ERROR, result.getStatus());
140 Assert.assertEquals(new ValidCharactersConstraintProcessor().getName(), result.getConstraintName());
141 }
142
143 @Test
144 public void testValueInvalidPhoneNumber1() {
145 ConstraintValidationResult result = process(invalidPhoneEmployee1, "contactPhone", contactPhoneNumberPatternConstraint);
146 Assert.assertEquals(0, dictionaryValidationResult.getNumberOfWarnings());
147 Assert.assertEquals(1, dictionaryValidationResult.getNumberOfErrors());
148 Assert.assertEquals(ErrorLevel.ERROR, result.getStatus());
149 Assert.assertEquals(new ValidCharactersConstraintProcessor().getName(), result.getConstraintName());
150 }
151
152 @Test
153 public void testValueInvalidPhoneNumber2() {
154 ConstraintValidationResult result = process(invalidPhoneEmployee2, "contactPhone", contactPhoneNumberPatternConstraint);
155 Assert.assertEquals(0, dictionaryValidationResult.getNumberOfWarnings());
156 Assert.assertEquals(1, dictionaryValidationResult.getNumberOfErrors());
157 Assert.assertEquals(ErrorLevel.ERROR, result.getStatus());
158 Assert.assertEquals(new ValidCharactersConstraintProcessor().getName(), result.getConstraintName());
159 }
160
161 @Test
162 public void testValueInvalidPhoneNumber3() {
163 ConstraintValidationResult result = process(invalidPhoneEmployee3, "contactPhone", contactPhoneNumberPatternConstraint);
164 Assert.assertEquals(0, dictionaryValidationResult.getNumberOfWarnings());
165 Assert.assertEquals(1, dictionaryValidationResult.getNumberOfErrors());
166 Assert.assertEquals(ErrorLevel.ERROR, result.getStatus());
167 Assert.assertEquals(new ValidCharactersConstraintProcessor().getName(), result.getConstraintName());
168 }
169
170 @Test
171 public void testValueInvalidPhoneNumber4() {
172 ConstraintValidationResult result = process(invalidPhoneEmployee4, "contactPhone", contactPhoneNumberPatternConstraint);
173 Assert.assertEquals(0, dictionaryValidationResult.getNumberOfWarnings());
174 Assert.assertEquals(1, dictionaryValidationResult.getNumberOfErrors());
175 Assert.assertEquals(ErrorLevel.ERROR, result.getStatus());
176 Assert.assertEquals(new ValidCharactersConstraintProcessor().getName(), result.getConstraintName());
177 }
178
179 @Test
180 public void testValueInvalidPhoneNumber5() {
181 ConstraintValidationResult result = process(invalidPhoneEmployee5, "contactPhone", contactPhoneNumberPatternConstraint);
182 Assert.assertEquals(0, dictionaryValidationResult.getNumberOfWarnings());
183 Assert.assertEquals(1, dictionaryValidationResult.getNumberOfErrors());
184 Assert.assertEquals(ErrorLevel.ERROR, result.getStatus());
185 Assert.assertEquals(new ValidCharactersConstraintProcessor().getName(), result.getConstraintName());
186 }
187
188 @Test
189 public void testValueInvalidPhoneNumber6() {
190 ConstraintValidationResult result = process(invalidPhoneEmployee6, "contactPhone", contactPhoneNumberPatternConstraint);
191 Assert.assertEquals(0, dictionaryValidationResult.getNumberOfWarnings());
192 Assert.assertEquals(1, dictionaryValidationResult.getNumberOfErrors());
193 Assert.assertEquals(ErrorLevel.ERROR, result.getStatus());
194 Assert.assertEquals(new ValidCharactersConstraintProcessor().getName(), result.getConstraintName());
195 }
196
197 private ConstraintValidationResult process(Object object, String attributeName, ValidCharactersConstraint constraint) {
198 AttributeValueReader attributeValueReader = new DictionaryObjectAttributeValueReader(object, "org.kuali.rice.kns.datadictionary.validation.MockAddress", addressEntry);
199 attributeValueReader.setAttributeName(attributeName);
200
201 Object value = attributeValueReader.getValue();
202 return processor.process(dictionaryValidationResult, value, constraint, attributeValueReader).getFirstConstraintValidationResult();
203 }
204
205 private String getProperty(String key) {
206 String value = null;
207 String filePath = "org/kuali/rice/krad/ApplicationResources.properties";
208 Properties properties = new Properties();
209 try {
210 InputStream in = Thread.currentThread().getContextClassLoader().getResourceAsStream(filePath);
211 properties.load(in);
212 value = properties.getProperty(key);
213 } catch (IOException e) {
214 }
215 return value;
216 }
217 }