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.io.IOException;
19 import java.io.InputStream;
20 import java.util.ArrayList;
21 import java.util.List;
22 import java.util.Properties;
23
24 import org.junit.Assert;
25 import org.junit.Before;
26 import org.junit.Test;
27 import org.kuali.rice.krad.datadictionary.AttributeDefinition;
28 import org.kuali.rice.krad.datadictionary.BusinessObjectEntry;
29 import org.kuali.rice.krad.datadictionary.validation.Address;
30 import org.kuali.rice.krad.datadictionary.validation.AttributeValueReader;
31 import org.kuali.rice.krad.datadictionary.validation.DictionaryObjectAttributeValueReader;
32 import org.kuali.rice.krad.datadictionary.validation.ErrorLevel;
33 import org.kuali.rice.krad.datadictionary.validation.constraint.ValidCharactersConstraint;
34 import org.kuali.rice.krad.datadictionary.validation.processor.ValidCharactersConstraintProcessor;
35 import org.kuali.rice.krad.datadictionary.validation.result.ConstraintValidationResult;
36 import org.kuali.rice.krad.datadictionary.validation.result.DictionaryValidationResult;
37
38
39
40
41
42
43
44
45
46
47
48
49
50 public class ZipcodePatternConstraintTest {
51
52 private final String PATTERN_CONSTRAINT = "validationPatternRegex.zipcode";
53
54 private AttributeDefinition postalCodeDefinition;
55
56 private BusinessObjectEntry addressEntry;
57 private DictionaryValidationResult dictionaryValidationResult;
58
59 private ValidCharactersConstraintProcessor processor;
60
61 private Address washingtonDCAddress = new Address("893 Presidential Ave", "(A_123) Suite 800.", "Washington", "DC", "12345", "USA", null);
62 private Address newYorkNYAddress = new Address("Presidential Street", "(A-123) Suite 800", "New York", "NY", "12345-1234", "USA", null);
63 private Address sydneySDAddress = new Address("Presidential Street-Ave.", "Suite_800.", "Sydney", "SD", "ZH-123456", "Australia", null);
64 private Address holandHDAddress = new Address("Presidential Street-Ave.", "Suite_800.", "Holand", "HD", "12345-123", "Holand", null);
65 private Address bombayMHAddress = new Address("Presidential Street-Ave.", "Suite_800.", "Bombay", "MH", "380002", "India", null);
66
67 private ConfigurationBasedRegexPatternConstraint postalCodePatternConstraint;
68
69 @Before
70 public void setUp() throws Exception {
71
72 String regex = getProperty(PATTERN_CONSTRAINT);
73
74 processor = new ValidCharactersConstraintProcessor();
75
76 dictionaryValidationResult = new DictionaryValidationResult();
77 dictionaryValidationResult.setErrorLevel(ErrorLevel.NOCONSTRAINT);
78
79 addressEntry = new BusinessObjectEntry();
80
81 List<AttributeDefinition> attributes = new ArrayList<AttributeDefinition>();
82
83 postalCodePatternConstraint = new ConfigurationBasedRegexPatternConstraint();
84 postalCodePatternConstraint.setValue(regex);
85
86 postalCodeDefinition = new AttributeDefinition();
87 postalCodeDefinition.setName("postalCode");
88 postalCodeDefinition.setValidCharactersConstraint(postalCodePatternConstraint);
89 attributes.add(postalCodeDefinition);
90
91 addressEntry.setAttributes(attributes);
92 }
93
94 @Test
95 public void testValueValidZipcode() {
96 ConstraintValidationResult result = process(washingtonDCAddress, "postalCode", postalCodePatternConstraint);
97 Assert.assertEquals(0, dictionaryValidationResult.getNumberOfWarnings());
98 Assert.assertEquals(0, dictionaryValidationResult.getNumberOfErrors());
99 Assert.assertEquals(ErrorLevel.OK, result.getStatus());
100 Assert.assertEquals(new ValidCharactersConstraintProcessor().getName(), result.getConstraintName());
101 }
102
103 @Test
104 public void testValueValidZipcode1() {
105 ConstraintValidationResult result = process(newYorkNYAddress, "postalCode", postalCodePatternConstraint);
106 Assert.assertEquals(0, dictionaryValidationResult.getNumberOfWarnings());
107 Assert.assertEquals(0, dictionaryValidationResult.getNumberOfErrors());
108 Assert.assertEquals(ErrorLevel.OK, result.getStatus());
109 Assert.assertEquals(new ValidCharactersConstraintProcessor().getName(), result.getConstraintName());
110 }
111
112 @Test
113 public void testValueInvalidZipcode() {
114 ConstraintValidationResult result = process(sydneySDAddress, "postalCode", postalCodePatternConstraint);
115 Assert.assertEquals(0, dictionaryValidationResult.getNumberOfWarnings());
116 Assert.assertEquals(1, dictionaryValidationResult.getNumberOfErrors());
117 Assert.assertEquals(ErrorLevel.ERROR, result.getStatus());
118 Assert.assertEquals(new ValidCharactersConstraintProcessor().getName(), result.getConstraintName());
119 }
120
121 @Test
122 public void testValueInvalidZipcode1() {
123 ConstraintValidationResult result = process(holandHDAddress, "postalCode", postalCodePatternConstraint);
124 Assert.assertEquals(0, dictionaryValidationResult.getNumberOfWarnings());
125 Assert.assertEquals(1, dictionaryValidationResult.getNumberOfErrors());
126 Assert.assertEquals(ErrorLevel.ERROR, result.getStatus());
127 Assert.assertEquals(new ValidCharactersConstraintProcessor().getName(), result.getConstraintName());
128 }
129
130 @Test
131 public void testValueInvalidZipcode2() {
132 ConstraintValidationResult result = process(bombayMHAddress, "postalCode", postalCodePatternConstraint);
133 Assert.assertEquals(0, dictionaryValidationResult.getNumberOfWarnings());
134 Assert.assertEquals(1, dictionaryValidationResult.getNumberOfErrors());
135 Assert.assertEquals(ErrorLevel.ERROR, result.getStatus());
136 Assert.assertEquals(new ValidCharactersConstraintProcessor().getName(), result.getConstraintName());
137 }
138
139 private ConstraintValidationResult process(Object object, String attributeName, ValidCharactersConstraint constraint) {
140 AttributeValueReader attributeValueReader = new DictionaryObjectAttributeValueReader(object, "org.kuali.rice.kns.datadictionary.validation.MockAddress", addressEntry);
141 attributeValueReader.setAttributeName(attributeName);
142
143 Object value = attributeValueReader.getValue();
144 return processor.process(dictionaryValidationResult, value, constraint, attributeValueReader).getFirstConstraintValidationResult();
145 }
146
147 private String getProperty(String key) {
148 String value = null;
149 String filePath = "org/kuali/rice/krad/ApplicationResources.properties";
150 Properties properties = new Properties();
151 try {
152 InputStream in = Thread.currentThread().getContextClassLoader().getResourceAsStream(filePath);
153 properties.load(in);
154 value = properties.getProperty(key);
155 } catch (IOException e) {
156 }
157 return value;
158 }
159 }