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
46 public class CharsetPatternConstraintTest {
47
48 private AttributeDefinition countryDefinition;
49 private AttributeDefinition postalCodeDefinition;
50
51 private BusinessObjectEntry addressEntry;
52 private DictionaryValidationResult dictionaryValidationResult;
53
54 private ValidCharactersConstraintProcessor processor;
55
56 private Address washingtonDCAddress = new Address("893 Presidential Ave", "(A_123) Suite 800.", "Washington", "DC", "NHW123A", "United States of America", null);
57 private Address newYorkNYAddress = new Address("Presidential Street", "(A-123) Suite 800", "New York", "NY", "ZH 3456", "USA", null);
58 private Address sydneyAUSAddress = new Address("Presidential Street-Ave.", "Suite_800.", "Sydney", "ZZ", "(ZH-5656)", "USA", null);
59
60 private CharsetPatternConstraint countryCharsetPatternConstraint;
61 private CharsetPatternConstraint postalCodeCharsetPatternConstraint;
62
63 @Before
64 public void setUp() throws Exception {
65
66 processor = new ValidCharactersConstraintProcessor();
67
68 dictionaryValidationResult = new DictionaryValidationResult();
69 dictionaryValidationResult.setErrorLevel(ErrorLevel.NOCONSTRAINT);
70
71 addressEntry = new BusinessObjectEntry();
72
73 List<AttributeDefinition> attributes = new ArrayList<AttributeDefinition>();
74
75 countryCharsetPatternConstraint = new CharsetPatternConstraint();
76 countryCharsetPatternConstraint.setValidChars("USA");
77
78 countryDefinition = new AttributeDefinition();
79 countryDefinition.setName("country");
80 countryDefinition.setValidCharactersConstraint(countryCharsetPatternConstraint);
81 attributes.add(countryDefinition);
82
83
84 postalCodeCharsetPatternConstraint = new CharsetPatternConstraint();
85 postalCodeCharsetPatternConstraint.setValidChars("(ZH-123456)");
86
87 postalCodeDefinition = new AttributeDefinition();
88 postalCodeDefinition.setName("postalCode");
89 postalCodeDefinition.setValidCharactersConstraint(postalCodeCharsetPatternConstraint);
90 attributes.add(postalCodeDefinition);
91
92 addressEntry.setAttributes(attributes);
93 }
94
95 @Test
96 public void testValueAllValidChars() {
97 ConstraintValidationResult result = process(newYorkNYAddress, "country", countryCharsetPatternConstraint);
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(washingtonDCAddress, "country", countryCharsetPatternConstraint);
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 test2ValueAllValidChars() {
115 ConstraintValidationResult result = process(sydneyAUSAddress, "postalCode", postalCodeCharsetPatternConstraint);
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 test2ValueNotValidChars() {
124 ConstraintValidationResult result = process(washingtonDCAddress, "postalCode", postalCodeCharsetPatternConstraint);
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 private ConstraintValidationResult process(Object object, String attributeName, ValidCharactersConstraint constraint) {
132 AttributeValueReader attributeValueReader = new DictionaryObjectAttributeValueReader(object, "org.kuali.rice.kns.datadictionary.validation.MockAddress", addressEntry);
133 attributeValueReader.setAttributeName(attributeName);
134
135 Object value = attributeValueReader.getValue();
136 return processor.process(dictionaryValidationResult, value, constraint, attributeValueReader).getFirstConstraintValidationResult();
137 }
138 }