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