001 /** 002 * Copyright 2005-2012 The Kuali Foundation 003 * 004 * Licensed under the Educational Community License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.opensource.org/licenses/ecl2.php 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 package org.kuali.rice.krad.datadictionary.validation.constraint; 017 018 import java.util.ArrayList; 019 import java.util.List; 020 021 import org.junit.Assert; 022 import org.junit.Before; 023 import org.junit.Ignore; 024 import org.junit.Test; 025 import org.kuali.rice.krad.datadictionary.AttributeDefinition; 026 import org.kuali.rice.krad.datadictionary.BusinessObjectEntry; 027 import org.kuali.rice.krad.datadictionary.validation.Address; 028 import org.kuali.rice.krad.datadictionary.validation.AttributeValueReader; 029 import org.kuali.rice.krad.datadictionary.validation.DictionaryObjectAttributeValueReader; 030 import org.kuali.rice.krad.datadictionary.validation.ErrorLevel; 031 import org.kuali.rice.krad.datadictionary.validation.constraint.ValidCharactersConstraint; 032 import org.kuali.rice.krad.datadictionary.validation.processor.ValidCharactersConstraintProcessor; 033 import org.kuali.rice.krad.datadictionary.validation.result.ConstraintValidationResult; 034 import org.kuali.rice.krad.datadictionary.validation.result.DictionaryValidationResult; 035 036 037 /** 038 * Things this test should check: 039 * 040 * 1. value with all valid characters. (success) {@link #testValueAllValidChars()} 041 * 2. value with invalid characters. (failure) {@link #testValueNotValidChars()} 042 * 043 * @author Kuali Rice Team (rice.collab@kuali.org) 044 */ 045 public class NumericPatternConstraintTest { 046 047 private AttributeDefinition postalCodeDefinition; 048 049 private BusinessObjectEntry addressEntry; 050 private DictionaryValidationResult dictionaryValidationResult; 051 052 private ValidCharactersConstraintProcessor processor; 053 054 private Address newYorkNYAddress = new Address("Presidential Street", "(A-123) Suite 800", "New York", "NY", "ZH 3456", "USA", null); 055 private Address sydneyAUSAddress = new Address("Presidential Street-Ave.", "Suite_800.", "Sydney", "ZZ", "999", "USA", null); 056 057 private NumericPatternConstraint postalCodeNumericPatternConstraint; 058 059 @Before 060 public void setUp() throws Exception { 061 062 processor = new ValidCharactersConstraintProcessor(); 063 064 dictionaryValidationResult = new DictionaryValidationResult(); 065 dictionaryValidationResult.setErrorLevel(ErrorLevel.NOCONSTRAINT); 066 067 addressEntry = new BusinessObjectEntry(); 068 069 List<AttributeDefinition> attributes = new ArrayList<AttributeDefinition>(); 070 071 postalCodeNumericPatternConstraint = new NumericPatternConstraint(); 072 073 postalCodeDefinition = new AttributeDefinition(); 074 postalCodeDefinition.setName("postalCode"); 075 postalCodeDefinition.setValidCharactersConstraint(postalCodeNumericPatternConstraint); 076 attributes.add(postalCodeDefinition); 077 078 addressEntry.setAttributes(attributes); 079 } 080 081 @Test 082 public void testValueAllValidChars() { 083 ConstraintValidationResult result = process(sydneyAUSAddress, "postalCode", postalCodeNumericPatternConstraint); 084 Assert.assertEquals(0, dictionaryValidationResult.getNumberOfWarnings()); 085 Assert.assertEquals(0, dictionaryValidationResult.getNumberOfErrors()); 086 Assert.assertEquals(ErrorLevel.OK, result.getStatus()); 087 Assert.assertEquals(new ValidCharactersConstraintProcessor().getName(), result.getConstraintName()); 088 } 089 090 @Ignore 091 @Test 092 public void testValueNotValidChars() { 093 ConstraintValidationResult result = process(newYorkNYAddress, "postalCode", postalCodeNumericPatternConstraint); 094 Assert.assertEquals(0, dictionaryValidationResult.getNumberOfWarnings()); 095 Assert.assertEquals(1, dictionaryValidationResult.getNumberOfErrors()); 096 Assert.assertEquals(ErrorLevel.ERROR, result.getStatus()); 097 Assert.assertEquals(new ValidCharactersConstraintProcessor().getName(), result.getConstraintName()); 098 } 099 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 }