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
80 cityDefinition = new AttributeDefinition();
81 cityDefinition.setName("city");
82 cityDefinition.setValidCharactersConstraint(cityAlphaPatternConstraint);
83 attributes.add(cityDefinition);
84
85 street1AlphaPatternConstraint = new AlphaPatternConstraint();
86 street1AlphaPatternConstraint.setAllowWhitespace(true);
87
88 street1Definition = new AttributeDefinition();
89 street1Definition.setName("street1");
90 street1Definition.setValidCharactersConstraint(street1AlphaPatternConstraint);
91 attributes.add(street1Definition);
92
93 addressEntry.setAttributes(attributes);
94 }
95
96 @Test
97 public void testValueAllValidChars() {
98 ConstraintValidationResult result = process(washingtonDCAddress, "city", cityAlphaPatternConstraint);
99 Assert.assertEquals(0, dictionaryValidationResult.getNumberOfWarnings());
100 Assert.assertEquals(0, dictionaryValidationResult.getNumberOfErrors());
101 Assert.assertEquals(ErrorLevel.OK, result.getStatus());
102 Assert.assertEquals(new ValidCharactersConstraintProcessor().getName(), result.getConstraintName());
103 }
104
105 @Ignore
106 @Test
107 public void testValueNotValidChars() {
108 ConstraintValidationResult result = process(newYorkNYAddress, "city", cityAlphaPatternConstraint);
109 Assert.assertEquals(0, dictionaryValidationResult.getNumberOfWarnings());
110 Assert.assertEquals(1, dictionaryValidationResult.getNumberOfErrors());
111 Assert.assertEquals(ErrorLevel.ERROR, result.getStatus());
112 Assert.assertEquals(new ValidCharactersConstraintProcessor().getName(), result.getConstraintName());
113 }
114
115 @Ignore
116 @Test
117 public void testValueAllValidCharsAllowWhitespace() {
118 ConstraintValidationResult result = process(newYorkNYAddress, "street1", street1AlphaPatternConstraint);
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 @Ignore
126 @Test
127 public void testValueNotValidCharsAllowWhitespace() {
128 ConstraintValidationResult result = process(washingtonDCAddress, "street1", street1AlphaPatternConstraint);
129 Assert.assertEquals(0, dictionaryValidationResult.getNumberOfWarnings());
130 Assert.assertEquals(1, dictionaryValidationResult.getNumberOfErrors());
131 Assert.assertEquals(ErrorLevel.ERROR, result.getStatus());
132 Assert.assertEquals(new ValidCharactersConstraintProcessor().getName(), result.getConstraintName());
133 }
134
135 @Ignore
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 }