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.setAllowWhitespace(true);
82
83 street1Definition = new AttributeDefinition();
84 street1Definition.setName("street1");
85 street1Definition.setValidCharactersConstraint(street1AlphaNumericPatternConstraint);
86 attributes.add(street1Definition);
87
88
89 street2AlphaNumericPatternConstraint = new AlphaNumericPatternConstraint();
90 street2AlphaNumericPatternConstraint.setAllowWhitespace(true);
91 street2AlphaNumericPatternConstraint.setAllowParenthesis(true);
92 street2AlphaNumericPatternConstraint.setAllowPeriod(true);
93 street2AlphaNumericPatternConstraint.setAllowUnderscore(true);
94
95 street2Definition = new AttributeDefinition();
96 street2Definition.setName("street2");
97 street2Definition.setValidCharactersConstraint(street2AlphaNumericPatternConstraint);
98 attributes.add(street2Definition);
99
100 postalCodeAlphaNumericPatternConstraint = new AlphaNumericPatternConstraint();
101
102 postalCodeDefinition = new AttributeDefinition();
103 postalCodeDefinition.setName("postalCode");
104 postalCodeDefinition.setValidCharactersConstraint(postalCodeAlphaNumericPatternConstraint);
105 attributes.add(postalCodeDefinition);
106
107 addressEntry.setAttributes(attributes);
108 }
109
110 @Test
111 public void testValueAllValidChars() {
112 ConstraintValidationResult result = process(washingtonDCAddress, "postalCode", postalCodeAlphaNumericPatternConstraint);
113 Assert.assertEquals(0, dictionaryValidationResult.getNumberOfWarnings());
114 Assert.assertEquals(0, dictionaryValidationResult.getNumberOfErrors());
115 Assert.assertEquals(ErrorLevel.OK, result.getStatus());
116 Assert.assertEquals(new ValidCharactersConstraintProcessor().getName(), result.getConstraintName());
117 }
118
119 @Ignore
120 @Test
121 public void testValueNotValidChars() {
122 ConstraintValidationResult result = process(newYorkNYAddress, "postalCode", postalCodeAlphaNumericPatternConstraint);
123 Assert.assertEquals(0, dictionaryValidationResult.getNumberOfWarnings());
124 Assert.assertEquals(1, dictionaryValidationResult.getNumberOfErrors());
125 Assert.assertEquals(ErrorLevel.ERROR, result.getStatus());
126 Assert.assertEquals(new ValidCharactersConstraintProcessor().getName(), result.getConstraintName());
127 }
128
129 @Test
130 public void testValueAllValidCharsAllowWhitespace() {
131 ConstraintValidationResult result = process(newYorkNYAddress, "street1", street1AlphaNumericPatternConstraint);
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 @Ignore
139 @Test
140 public void testValueNotValidCharsAllowWhitespace() {
141 ConstraintValidationResult result = process(sydneyAUSAddress, "street1", street1AlphaNumericPatternConstraint);
142 Assert.assertEquals(0, dictionaryValidationResult.getNumberOfWarnings());
143 Assert.assertEquals(1, dictionaryValidationResult.getNumberOfErrors());
144 Assert.assertEquals(ErrorLevel.ERROR, result.getStatus());
145 Assert.assertEquals(new ValidCharactersConstraintProcessor().getName(), result.getConstraintName());
146 }
147
148 @Test
149 public void testValueAllValidCharsAllowWhitespaceAndPeriodAndUnderscoreAndParenthesis() {
150 ConstraintValidationResult result = process(washingtonDCAddress, "street2", street2AlphaNumericPatternConstraint);
151 Assert.assertEquals(0, dictionaryValidationResult.getNumberOfWarnings());
152 Assert.assertEquals(0, dictionaryValidationResult.getNumberOfErrors());
153 Assert.assertEquals(ErrorLevel.OK, result.getStatus());
154 Assert.assertEquals(new ValidCharactersConstraintProcessor().getName(), result.getConstraintName());
155 }
156
157 @Ignore
158 @Test
159 public void testValueNotValidCharsAllowWhitespaceAndPeriodAndUnderscoreAndParenthesis() {
160 ConstraintValidationResult result = process(newYorkNYAddress, "street2", street2AlphaNumericPatternConstraint);
161 Assert.assertEquals(0, dictionaryValidationResult.getNumberOfWarnings());
162 Assert.assertEquals(1, dictionaryValidationResult.getNumberOfErrors());
163 Assert.assertEquals(ErrorLevel.ERROR, result.getStatus());
164 Assert.assertEquals(new ValidCharactersConstraintProcessor().getName(), result.getConstraintName());
165 }
166
167 private ConstraintValidationResult process(Object object, String attributeName, ValidCharactersConstraint constraint) {
168 AttributeValueReader attributeValueReader = new DictionaryObjectAttributeValueReader(object, "org.kuali.rice.kns.datadictionary.validation.MockAddress", addressEntry);
169 attributeValueReader.setAttributeName(attributeName);
170
171 Object value = attributeValueReader.getValue();
172 return processor.process(dictionaryValidationResult, value, constraint, attributeValueReader).getFirstConstraintValidationResult();
173 }
174 }