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 public class NumericPatternConstraintTest {
45
46 private AttributeDefinition postalCodeDefinition;
47
48 private BusinessObjectEntry addressEntry;
49 private DictionaryValidationResult dictionaryValidationResult;
50
51 private ValidCharactersConstraintProcessor processor;
52
53 private Address newYorkNYAddress = new Address("Presidential Street", "(A-123) Suite 800", "New York", "NY", "ZH 3456", "USA", null);
54 private Address sydneyAUSAddress = new Address("Presidential Street-Ave.", "Suite_800.", "Sydney", "ZZ", "999", "USA", null);
55
56 private NumericPatternConstraint postalCodeNumericPatternConstraint;
57
58 @Before
59 public void setUp() throws Exception {
60
61 processor = new ValidCharactersConstraintProcessor();
62
63 dictionaryValidationResult = new DictionaryValidationResult();
64 dictionaryValidationResult.setErrorLevel(ErrorLevel.NOCONSTRAINT);
65
66 addressEntry = new BusinessObjectEntry();
67
68 List<AttributeDefinition> attributes = new ArrayList<AttributeDefinition>();
69
70 postalCodeNumericPatternConstraint = new NumericPatternConstraint();
71
72 postalCodeDefinition = new AttributeDefinition();
73 postalCodeDefinition.setName("postalCode");
74 postalCodeDefinition.setValidCharactersConstraint(postalCodeNumericPatternConstraint);
75 attributes.add(postalCodeDefinition);
76
77 addressEntry.setAttributes(attributes);
78 }
79
80 @Test
81 public void testValueAllValidChars() {
82 ConstraintValidationResult result = process(sydneyAUSAddress, "postalCode", postalCodeNumericPatternConstraint);
83 Assert.assertEquals(0, dictionaryValidationResult.getNumberOfWarnings());
84 Assert.assertEquals(0, dictionaryValidationResult.getNumberOfErrors());
85 Assert.assertEquals(ErrorLevel.OK, result.getStatus());
86 Assert.assertEquals(new ValidCharactersConstraintProcessor().getName(), result.getConstraintName());
87 }
88
89 @Test
90 public void testValueNotValidChars() {
91 ConstraintValidationResult result = process(newYorkNYAddress, "postalCode", postalCodeNumericPatternConstraint);
92 Assert.assertEquals(0, dictionaryValidationResult.getNumberOfWarnings());
93 Assert.assertEquals(1, dictionaryValidationResult.getNumberOfErrors());
94 Assert.assertEquals(ErrorLevel.ERROR, result.getStatus());
95 Assert.assertEquals(new ValidCharactersConstraintProcessor().getName(), result.getConstraintName());
96 }
97
98 private ConstraintValidationResult process(Object object, String attributeName, ValidCharactersConstraint constraint) {
99 AttributeValueReader attributeValueReader = new DictionaryObjectAttributeValueReader(object, "org.kuali.rice.kns.datadictionary.validation.MockAddress", addressEntry);
100 attributeValueReader.setAttributeName(attributeName);
101
102 Object value = attributeValueReader.getValue();
103 return processor.process(dictionaryValidationResult, value, constraint, attributeValueReader).getFirstConstraintValidationResult();
104 }
105 }