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.util.ArrayList;
19 import java.util.List;
20
21 import org.junit.Assert;
22 import org.junit.Before;
23 import org.junit.Ignore;
24 import org.junit.Test;
25 import org.kuali.rice.krad.datadictionary.AttributeDefinition;
26 import org.kuali.rice.krad.datadictionary.BusinessObjectEntry;
27 import org.kuali.rice.krad.datadictionary.validation.Address;
28 import org.kuali.rice.krad.datadictionary.validation.AttributeValueReader;
29 import org.kuali.rice.krad.datadictionary.validation.DictionaryObjectAttributeValueReader;
30 import org.kuali.rice.krad.datadictionary.validation.ErrorLevel;
31 import org.kuali.rice.krad.datadictionary.validation.constraint.ValidCharactersConstraint;
32 import org.kuali.rice.krad.datadictionary.validation.processor.ValidCharactersConstraintProcessor;
33 import org.kuali.rice.krad.datadictionary.validation.result.ConstraintValidationResult;
34 import org.kuali.rice.krad.datadictionary.validation.result.DictionaryValidationResult;
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49 public class AlphaNumericPatternConstraintTest {
50
51 private AttributeDefinition street1Definition;
52 private AttributeDefinition street2Definition;
53 private AttributeDefinition postalCodeDefinition;
54
55 private BusinessObjectEntry addressEntry;
56 private DictionaryValidationResult dictionaryValidationResult;
57
58 private ValidCharactersConstraintProcessor processor;
59
60 private Address washingtonDCAddress = new Address("893 Presidential Ave", "(A_123) Suite 800.", "Washington", "DC", "NHW123A", "USA", null);
61 private Address newYorkNYAddress = new Address("Presidential Street", "(A-123) Suite 800", "New York", "NY", "ZH 3456", "USA", null);
62 private Address sydneyAUSAddress = new Address("Presidential Street-Ave.", "Suite_800.", "Sydney", "ZZ", "ZH-5656", "USA", null);
63
64 private AlphaNumericPatternConstraint street1AlphaNumericPatternConstraint;
65 private AlphaNumericPatternConstraint street2AlphaNumericPatternConstraint;
66 private AlphaNumericPatternConstraint postalCodeAlphaNumericPatternConstraint;
67
68 @Before
69 public void setUp() throws Exception {
70
71 processor = new ValidCharactersConstraintProcessor();
72
73 dictionaryValidationResult = new DictionaryValidationResult();
74 dictionaryValidationResult.setErrorLevel(ErrorLevel.NOCONSTRAINT);
75
76 addressEntry = new BusinessObjectEntry();
77
78 List<AttributeDefinition> attributes = new ArrayList<AttributeDefinition>();
79
80 street1AlphaNumericPatternConstraint = new AlphaNumericPatternConstraint();
81 street1AlphaNumericPatternConstraint.setMessageKey("validate.dummykey");
82 street1AlphaNumericPatternConstraint.setValidationMessageParams( new ArrayList<String>());
83 street1AlphaNumericPatternConstraint.setAllowWhitespace(true);
84
85 street1Definition = new AttributeDefinition();
86 street1Definition.setName("street1");
87 street1Definition.setValidCharactersConstraint(street1AlphaNumericPatternConstraint);
88 attributes.add(street1Definition);
89
90
91 street2AlphaNumericPatternConstraint = new AlphaNumericPatternConstraint();
92 street2AlphaNumericPatternConstraint.setMessageKey("validate.dummykey");
93 street2AlphaNumericPatternConstraint.setValidationMessageParams( new ArrayList<String>());
94 street2AlphaNumericPatternConstraint.setAllowWhitespace(true);
95 street2AlphaNumericPatternConstraint.setAllowParenthesis(true);
96 street2AlphaNumericPatternConstraint.setAllowPeriod(true);
97 street2AlphaNumericPatternConstraint.setAllowUnderscore(true);
98
99 street2Definition = new AttributeDefinition();
100 street2Definition.setName("street2");
101 street2Definition.setValidCharactersConstraint(street2AlphaNumericPatternConstraint);
102 attributes.add(street2Definition);
103
104 postalCodeAlphaNumericPatternConstraint = new AlphaNumericPatternConstraint();
105 postalCodeAlphaNumericPatternConstraint.setMessageKey("validate.dummykey");
106 postalCodeAlphaNumericPatternConstraint.setValidationMessageParams( new ArrayList<String>());
107
108 postalCodeDefinition = new AttributeDefinition();
109 postalCodeDefinition.setName("postalCode");
110 postalCodeDefinition.setValidCharactersConstraint(postalCodeAlphaNumericPatternConstraint);
111 attributes.add(postalCodeDefinition);
112
113 addressEntry.setAttributes(attributes);
114 }
115
116 @Test
117 public void testValueAllValidChars() {
118 ConstraintValidationResult result = process(washingtonDCAddress, "postalCode", postalCodeAlphaNumericPatternConstraint);
119 Assert.assertEquals(0, dictionaryValidationResult.getNumberOfWarnings());
120 Assert.assertEquals(0, dictionaryValidationResult.getNumberOfErrors());
121 Assert.assertEquals(ErrorLevel.OK, result.getStatus());
122 Assert.assertEquals(new ValidCharactersConstraintProcessor().getName(), result.getConstraintName());
123 }
124
125 @Test
126 public void testValueNotValidChars() {
127 ConstraintValidationResult result = process(newYorkNYAddress, "postalCode", postalCodeAlphaNumericPatternConstraint);
128 Assert.assertEquals(0, dictionaryValidationResult.getNumberOfWarnings());
129 Assert.assertEquals(1, dictionaryValidationResult.getNumberOfErrors());
130 Assert.assertEquals(ErrorLevel.ERROR, result.getStatus());
131 Assert.assertEquals(new ValidCharactersConstraintProcessor().getName(), result.getConstraintName());
132 }
133
134 @Test
135 public void testValueAllValidCharsAllowWhitespace() {
136 ConstraintValidationResult result = process(newYorkNYAddress, "street1", street1AlphaNumericPatternConstraint);
137 Assert.assertEquals(0, dictionaryValidationResult.getNumberOfWarnings());
138 Assert.assertEquals(0, dictionaryValidationResult.getNumberOfErrors());
139 Assert.assertEquals(ErrorLevel.OK, result.getStatus());
140 Assert.assertEquals(new ValidCharactersConstraintProcessor().getName(), result.getConstraintName());
141 }
142
143 @Test
144 public void testValueNotValidCharsAllowWhitespace() {
145 ConstraintValidationResult result = process(sydneyAUSAddress, "street1", street1AlphaNumericPatternConstraint);
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 testValueAllValidCharsAllowWhitespaceAndPeriodAndUnderscoreAndParenthesis() {
154 ConstraintValidationResult result = process(washingtonDCAddress, "street2", street2AlphaNumericPatternConstraint);
155 Assert.assertEquals(0, dictionaryValidationResult.getNumberOfWarnings());
156 Assert.assertEquals(0, dictionaryValidationResult.getNumberOfErrors());
157 Assert.assertEquals(ErrorLevel.OK, result.getStatus());
158 Assert.assertEquals(new ValidCharactersConstraintProcessor().getName(), result.getConstraintName());
159 }
160
161 @Test
162 public void testValueNotValidCharsAllowWhitespaceAndPeriodAndUnderscoreAndParenthesis() {
163 ConstraintValidationResult result = process(newYorkNYAddress, "street2", street2AlphaNumericPatternConstraint);
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 private ConstraintValidationResult process(Object object, String attributeName, ValidCharactersConstraint constraint) {
171 AttributeValueReader attributeValueReader = new DictionaryObjectAttributeValueReader(object, "org.kuali.rice.kns.datadictionary.validation.MockAddress", addressEntry);
172 attributeValueReader.setAttributeName(attributeName);
173
174 Object value = attributeValueReader.getValue();
175 return processor.process(dictionaryValidationResult, value, constraint, attributeValueReader).getFirstConstraintValidationResult();
176 }
177 }