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.setMessageKey("validate.dummykey");
85 postalCodePatternConstraint.setValidationMessageParams( new ArrayList<String>());
86 postalCodePatternConstraint.setValue(regex);
87
88 postalCodeDefinition = new AttributeDefinition();
89 postalCodeDefinition.setName("postalCode");
90 postalCodeDefinition.setValidCharactersConstraint(postalCodePatternConstraint);
91 attributes.add(postalCodeDefinition);
92
93 addressEntry.setAttributes(attributes);
94 }
95
96 @Test
97 public void testValueValidZipcode() {
98 ConstraintValidationResult result = process(washingtonDCAddress, "postalCode", postalCodePatternConstraint);
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 @Test
106 public void testValueValidZipcode1() {
107 ConstraintValidationResult result = process(newYorkNYAddress, "postalCode", postalCodePatternConstraint);
108 Assert.assertEquals(0, dictionaryValidationResult.getNumberOfWarnings());
109 Assert.assertEquals(0, dictionaryValidationResult.getNumberOfErrors());
110 Assert.assertEquals(ErrorLevel.OK, result.getStatus());
111 Assert.assertEquals(new ValidCharactersConstraintProcessor().getName(), result.getConstraintName());
112 }
113
114 @Test
115 public void testValueInvalidZipcode() {
116 ConstraintValidationResult result = process(sydneySDAddress, "postalCode", postalCodePatternConstraint);
117 Assert.assertEquals(0, dictionaryValidationResult.getNumberOfWarnings());
118 Assert.assertEquals(1, dictionaryValidationResult.getNumberOfErrors());
119 Assert.assertEquals(ErrorLevel.ERROR, result.getStatus());
120 Assert.assertEquals(new ValidCharactersConstraintProcessor().getName(), result.getConstraintName());
121 }
122
123 @Test
124 public void testValueInvalidZipcode1() {
125 ConstraintValidationResult result = process(holandHDAddress, "postalCode", postalCodePatternConstraint);
126 Assert.assertEquals(0, dictionaryValidationResult.getNumberOfWarnings());
127 Assert.assertEquals(1, dictionaryValidationResult.getNumberOfErrors());
128 Assert.assertEquals(ErrorLevel.ERROR, result.getStatus());
129 Assert.assertEquals(new ValidCharactersConstraintProcessor().getName(), result.getConstraintName());
130 }
131
132 @Test
133 public void testValueInvalidZipcode2() {
134 ConstraintValidationResult result = process(bombayMHAddress, "postalCode", postalCodePatternConstraint);
135 Assert.assertEquals(0, dictionaryValidationResult.getNumberOfWarnings());
136 Assert.assertEquals(1, dictionaryValidationResult.getNumberOfErrors());
137 Assert.assertEquals(ErrorLevel.ERROR, result.getStatus());
138 Assert.assertEquals(new ValidCharactersConstraintProcessor().getName(), result.getConstraintName());
139 }
140
141 private ConstraintValidationResult process(Object object, String attributeName, ValidCharactersConstraint constraint) {
142 AttributeValueReader attributeValueReader = new DictionaryObjectAttributeValueReader(object, "org.kuali.rice.kns.datadictionary.validation.MockAddress", addressEntry);
143 attributeValueReader.setAttributeName(attributeName);
144
145 Object value = attributeValueReader.getValue();
146 return processor.process(dictionaryValidationResult, value, constraint, attributeValueReader).getFirstConstraintValidationResult();
147 }
148
149 private String getProperty(String key) {
150 String value = null;
151 String filePath = "org/kuali/rice/krad/ApplicationResources.properties";
152 Properties properties = new Properties();
153 try {
154 InputStream in = Thread.currentThread().getContextClassLoader().getResourceAsStream(filePath);
155 properties.load(in);
156 value = properties.getProperty(key);
157 } catch (IOException e) {
158 }
159 return value;
160 }
161 }