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.ValidCharactersConstraint;
31 import org.kuali.rice.krad.datadictionary.validation.processor.ValidCharactersConstraintProcessor;
32 import org.kuali.rice.krad.datadictionary.validation.result.ConstraintValidationResult;
33 import org.kuali.rice.krad.datadictionary.validation.result.DictionaryValidationResult;
34
35
36
37
38
39
40
41
42
43
44
45 public class AnyCharacterPatternConstraintTest {
46
47 private AttributeDefinition street1Definition;
48 private AttributeDefinition street2Definition;
49
50 private BusinessObjectEntry addressEntry;
51 private DictionaryValidationResult dictionaryValidationResult;
52
53 private ValidCharactersConstraintProcessor processor;
54
55 private Address washingtonDCAddress = new Address("893 Presidential Ave", "(A_123) Suite 800.", "Washington", "DC", "NHW123A", "USA", null);
56 private Address newYorkNYAddress = new Address("Presidential Street", "(A-123) Suite 800", "New York", "NY", "ZH 3456", "USA", null);
57 private Address sydneyAUSAddress = new Address("Presidential Street-Ave.", "Suite_800.", "Sydney", "ZZ", "ZH-5656", "USA", null);
58
59 private AnyCharacterPatternConstraint street1AnyCharacterPatternConstraint;
60 private AnyCharacterPatternConstraint street2AnyCharacterPatternConstraint;
61
62 @Before
63 public void setUp() throws Exception {
64
65 processor = new ValidCharactersConstraintProcessor();
66
67 dictionaryValidationResult = new DictionaryValidationResult();
68 dictionaryValidationResult.setErrorLevel(ErrorLevel.NOCONSTRAINT);
69
70 addressEntry = new BusinessObjectEntry();
71
72 List<AttributeDefinition> attributes = new ArrayList<AttributeDefinition>();
73
74 street1AnyCharacterPatternConstraint = new AnyCharacterPatternConstraint();
75 street1AnyCharacterPatternConstraint.setMessageKey("validate.dummykey");
76 street1AnyCharacterPatternConstraint.setValidationMessageParams( new ArrayList<String>());
77 street1AnyCharacterPatternConstraint.setAllowWhitespace(true);
78
79 street1Definition = new AttributeDefinition();
80 street1Definition.setName("street1");
81 street1Definition.setValidCharactersConstraint(street1AnyCharacterPatternConstraint);
82 attributes.add(street1Definition);
83
84
85 street2AnyCharacterPatternConstraint = new AnyCharacterPatternConstraint();
86 street2AnyCharacterPatternConstraint.setMessageKey("validate.dummykey");
87 street2AnyCharacterPatternConstraint.setValidationMessageParams( new ArrayList<String>());
88
89 street2Definition = new AttributeDefinition();
90 street2Definition.setName("street2");
91 street2Definition.setValidCharactersConstraint(street2AnyCharacterPatternConstraint);
92 attributes.add(street2Definition);
93
94 addressEntry.setAttributes(attributes);
95 }
96
97 @Test
98 public void testValueAllValidChars() {
99 ConstraintValidationResult result = process(sydneyAUSAddress, "street2", street2AnyCharacterPatternConstraint);
100 Assert.assertEquals(0, dictionaryValidationResult.getNumberOfWarnings());
101 Assert.assertEquals(0, dictionaryValidationResult.getNumberOfErrors());
102 Assert.assertEquals(ErrorLevel.OK, result.getStatus());
103 Assert.assertEquals(new ValidCharactersConstraintProcessor().getName(), result.getConstraintName());
104 }
105
106 @Test
107 public void testValueNotValidChars() {
108 ConstraintValidationResult result = process(newYorkNYAddress, "street2", street2AnyCharacterPatternConstraint);
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 @Test
116 public void testValueAllValidCharsAllowWhitespace() {
117 ConstraintValidationResult result = process(washingtonDCAddress, "street1", street1AnyCharacterPatternConstraint);
118 Assert.assertEquals(0, dictionaryValidationResult.getNumberOfWarnings());
119 Assert.assertEquals(0, dictionaryValidationResult.getNumberOfErrors());
120 Assert.assertEquals(ErrorLevel.OK, result.getStatus());
121 Assert.assertEquals(new ValidCharactersConstraintProcessor().getName(), result.getConstraintName());
122 }
123
124 private ConstraintValidationResult process(Object object, String attributeName, ValidCharactersConstraint constraint) {
125 AttributeValueReader attributeValueReader = new DictionaryObjectAttributeValueReader(object, "org.kuali.rice.kns.datadictionary.validation.MockAddress", addressEntry);
126 attributeValueReader.setAttributeName(attributeName);
127
128 Object value = attributeValueReader.getValue();
129 return processor.process(dictionaryValidationResult, value, constraint, attributeValueReader).getFirstConstraintValidationResult();
130 }
131 }