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.Test;
24 import org.kuali.rice.krad.datadictionary.AttributeDefinition;
25 import org.kuali.rice.krad.datadictionary.BusinessObjectEntry;
26 import org.kuali.rice.krad.datadictionary.validation.Address;
27 import org.kuali.rice.krad.datadictionary.validation.AttributeValueReader;
28 import org.kuali.rice.krad.datadictionary.validation.DictionaryObjectAttributeValueReader;
29 import org.kuali.rice.krad.datadictionary.validation.ErrorLevel;
30 import org.kuali.rice.krad.datadictionary.validation.constraint.AlphaPatternConstraint;
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 public class AlphaPatternConstraintTest {
49
50 private AttributeDefinition street1Definition;
51 private AttributeDefinition cityDefinition;
52
53 private BusinessObjectEntry addressEntry;
54 private DictionaryValidationResult dictionaryValidationResult;
55
56 private ValidCharactersConstraintProcessor processor;
57
58 private Address washingtonDCAddress = new Address("893 Presidential Ave", "(A_123) Suite 800.", "Washington", "DC", "NHW123A", "USA", null);
59 private Address newYorkNYAddress = new Address("Presidential Street", "(A-123) Suite 800", "New York", "NY", "ZH 3456", "USA", null);
60 private Address sydneyAUSAddress = new Address("Presidential Street-Ave.", "Suite_800.", "Sydney", "ZZ", "ZH-5656", "USA", null);
61
62 private AlphaPatternConstraint street1AlphaPatternConstraint;
63 private AlphaPatternConstraint cityAlphaPatternConstraint;
64
65 @Before
66 public void setUp() throws Exception {
67
68 processor = new ValidCharactersConstraintProcessor();
69
70 dictionaryValidationResult = new DictionaryValidationResult();
71 dictionaryValidationResult.setErrorLevel(ErrorLevel.NOCONSTRAINT);
72
73 addressEntry = new BusinessObjectEntry();
74
75 List<AttributeDefinition> attributes = new ArrayList<AttributeDefinition>();
76
77 cityAlphaPatternConstraint = new AlphaPatternConstraint();
78
79 cityDefinition = new AttributeDefinition();
80 cityDefinition.setName("city");
81 cityDefinition.setValidCharactersConstraint(cityAlphaPatternConstraint);
82 attributes.add(cityDefinition);
83
84 street1AlphaPatternConstraint = new AlphaPatternConstraint();
85 street1AlphaPatternConstraint.setAllowWhitespace(true);
86
87 street1Definition = new AttributeDefinition();
88 street1Definition.setName("street1");
89 street1Definition.setValidCharactersConstraint(street1AlphaPatternConstraint);
90 attributes.add(street1Definition);
91
92 addressEntry.setAttributes(attributes);
93 }
94
95 @Test
96 public void testValueAllValidChars() {
97 ConstraintValidationResult result = process(washingtonDCAddress, "city", cityAlphaPatternConstraint);
98 Assert.assertEquals(0, dictionaryValidationResult.getNumberOfWarnings());
99 Assert.assertEquals(0, dictionaryValidationResult.getNumberOfErrors());
100 Assert.assertEquals(ErrorLevel.OK, result.getStatus());
101 Assert.assertEquals(new ValidCharactersConstraintProcessor().getName(), result.getConstraintName());
102 }
103
104 @Test
105 public void testValueNotValidChars() {
106 ConstraintValidationResult result = process(newYorkNYAddress, "city", cityAlphaPatternConstraint);
107 Assert.assertEquals(0, dictionaryValidationResult.getNumberOfWarnings());
108 Assert.assertEquals(1, dictionaryValidationResult.getNumberOfErrors());
109 Assert.assertEquals(ErrorLevel.ERROR, result.getStatus());
110 Assert.assertEquals(new ValidCharactersConstraintProcessor().getName(), result.getConstraintName());
111 }
112
113 @Test
114 public void testValueAllValidCharsAllowWhitespace() {
115 ConstraintValidationResult result = process(newYorkNYAddress, "street1", street1AlphaPatternConstraint);
116 Assert.assertEquals(0, dictionaryValidationResult.getNumberOfWarnings());
117 Assert.assertEquals(0, dictionaryValidationResult.getNumberOfErrors());
118 Assert.assertEquals(ErrorLevel.OK, result.getStatus());
119 Assert.assertEquals(new ValidCharactersConstraintProcessor().getName(), result.getConstraintName());
120 }
121
122 @Test
123 public void testValueNotValidCharsAllowWhitespace() {
124 ConstraintValidationResult result = process(washingtonDCAddress, "street1", street1AlphaPatternConstraint);
125 Assert.assertEquals(0, dictionaryValidationResult.getNumberOfWarnings());
126 Assert.assertEquals(1, dictionaryValidationResult.getNumberOfErrors());
127 Assert.assertEquals(ErrorLevel.ERROR, result.getStatus());
128 Assert.assertEquals(new ValidCharactersConstraintProcessor().getName(), result.getConstraintName());
129 }
130
131 @Test
132 public void testValueWithSpecialCharsAllowWhitespace() {
133 ConstraintValidationResult result = process(sydneyAUSAddress, "street1", street1AlphaPatternConstraint);
134 Assert.assertEquals(0, dictionaryValidationResult.getNumberOfWarnings());
135 Assert.assertEquals(1, dictionaryValidationResult.getNumberOfErrors());
136 Assert.assertEquals(ErrorLevel.ERROR, result.getStatus());
137 Assert.assertEquals(new ValidCharactersConstraintProcessor().getName(), result.getConstraintName());
138 }
139
140 private ConstraintValidationResult process(Object object, String attributeName, ValidCharactersConstraint constraint) {
141 AttributeValueReader attributeValueReader = new DictionaryObjectAttributeValueReader(object, "org.kuali.rice.kns.datadictionary.validation.MockAddress", addressEntry);
142 attributeValueReader.setAttributeName(attributeName);
143
144 Object value = attributeValueReader.getValue();
145 return processor.process(dictionaryValidationResult, value, constraint, attributeValueReader).getFirstConstraintValidationResult();
146 }
147 }