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