001/** 002 * Copyright 2005-2015 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 */ 016package org.kuali.rice.krad.datadictionary.validation.constraint; 017 018import java.util.ArrayList; 019import java.util.List; 020 021import org.junit.Assert; 022import org.junit.Before; 023import org.junit.Ignore; 024import org.junit.Test; 025import org.kuali.rice.krad.datadictionary.AttributeDefinition; 026import org.kuali.rice.krad.datadictionary.BusinessObjectEntry; 027import org.kuali.rice.krad.datadictionary.validation.Address; 028import org.kuali.rice.krad.datadictionary.validation.AttributeValueReader; 029import org.kuali.rice.krad.datadictionary.validation.DictionaryObjectAttributeValueReader; 030import org.kuali.rice.krad.datadictionary.validation.ErrorLevel; 031import org.kuali.rice.krad.datadictionary.validation.constraint.ValidCharactersConstraint; 032import org.kuali.rice.krad.datadictionary.validation.processor.ValidCharactersConstraintProcessor; 033import org.kuali.rice.krad.datadictionary.validation.result.ConstraintValidationResult; 034import 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 * 3. value with all valid characters. Allowing white space.(success) {@link #testValueAllValidCharsAllowWhitespace()} 043 * 4. value with invalid characters. Allowing white space. (failure) {@link #testValueNotValidCharsAllowWhitespace()} 044 * 5. value with all valid characters. Allowing white space, period, underscore and parenthesis. (success) {@link #testValueAllValidCharsAllowWhitespaceAndPeriodAndUnderscoreAndParenthesis()} 045 * 6. value with invalid characters. Allowing white space, period, underscore and parenthesis. (failure) {@link #testValueNotValidCharsAllowWhitespaceAndPeriodAndUnderscoreAndParenthesis()} 046 * 047 * @author Kuali Rice Team (rice.collab@kuali.org) 048 */ 049public class AlphaNumericPatternConstraintTest { 050 051 private AttributeDefinition street1Definition; 052 private AttributeDefinition street2Definition; 053 private AttributeDefinition postalCodeDefinition; 054 055 private BusinessObjectEntry addressEntry; 056 private DictionaryValidationResult dictionaryValidationResult; 057 058 private ValidCharactersConstraintProcessor processor; 059 060 private Address washingtonDCAddress = new Address("893 Presidential Ave", "(A_123) Suite 800.", "Washington", "DC", "NHW123A", "USA", null); 061 private Address newYorkNYAddress = new Address("Presidential Street", "(A-123) Suite 800", "New York", "NY", "ZH 3456", "USA", null); 062 private Address sydneyAUSAddress = new Address("Presidential Street-Ave.", "Suite_800.", "Sydney", "ZZ", "ZH-5656", "USA", null); 063 064 private AlphaNumericPatternConstraint street1AlphaNumericPatternConstraint; 065 private AlphaNumericPatternConstraint street2AlphaNumericPatternConstraint; 066 private AlphaNumericPatternConstraint postalCodeAlphaNumericPatternConstraint; 067 068 @Before 069 public void setUp() throws Exception { 070 071 processor = new ValidCharactersConstraintProcessor(); 072 073 dictionaryValidationResult = new DictionaryValidationResult(); 074 dictionaryValidationResult.setErrorLevel(ErrorLevel.NOCONSTRAINT); 075 076 addressEntry = new BusinessObjectEntry(); 077 078 List<AttributeDefinition> attributes = new ArrayList<AttributeDefinition>(); 079 080 street1AlphaNumericPatternConstraint = new AlphaNumericPatternConstraint(); 081 street1AlphaNumericPatternConstraint.setMessageKey("validate.dummykey"); 082 street1AlphaNumericPatternConstraint.setValidationMessageParams( new ArrayList<String>()); 083 street1AlphaNumericPatternConstraint.setAllowWhitespace(true); 084 085 street1Definition = new AttributeDefinition(); 086 street1Definition.setName("street1"); 087 street1Definition.setValidCharactersConstraint(street1AlphaNumericPatternConstraint); 088 attributes.add(street1Definition); 089 090 091 street2AlphaNumericPatternConstraint = new AlphaNumericPatternConstraint(); 092 street2AlphaNumericPatternConstraint.setMessageKey("validate.dummykey"); 093 street2AlphaNumericPatternConstraint.setValidationMessageParams( new ArrayList<String>()); 094 street2AlphaNumericPatternConstraint.setAllowWhitespace(true); 095 street2AlphaNumericPatternConstraint.setAllowParenthesis(true); 096 street2AlphaNumericPatternConstraint.setAllowPeriod(true); 097 street2AlphaNumericPatternConstraint.setAllowUnderscore(true); 098 099 street2Definition = new AttributeDefinition(); 100 street2Definition.setName("street2"); 101 street2Definition.setValidCharactersConstraint(street2AlphaNumericPatternConstraint); 102 attributes.add(street2Definition); 103 104 postalCodeAlphaNumericPatternConstraint = new AlphaNumericPatternConstraint(); 105 postalCodeAlphaNumericPatternConstraint.setMessageKey("validate.dummykey"); 106 postalCodeAlphaNumericPatternConstraint.setValidationMessageParams( new ArrayList<String>()); 107 108 postalCodeDefinition = new AttributeDefinition(); 109 postalCodeDefinition.setName("postalCode"); 110 postalCodeDefinition.setValidCharactersConstraint(postalCodeAlphaNumericPatternConstraint); 111 attributes.add(postalCodeDefinition); 112 113 addressEntry.setAttributes(attributes); 114 } 115 116 @Test 117 public void testValueAllValidChars() { 118 ConstraintValidationResult result = process(washingtonDCAddress, "postalCode", postalCodeAlphaNumericPatternConstraint); 119 Assert.assertEquals(0, dictionaryValidationResult.getNumberOfWarnings()); 120 Assert.assertEquals(0, dictionaryValidationResult.getNumberOfErrors()); 121 Assert.assertEquals(ErrorLevel.OK, result.getStatus()); 122 Assert.assertEquals(new ValidCharactersConstraintProcessor().getName(), result.getConstraintName()); 123 } 124 125 @Test 126 public void testValueNotValidChars() { 127 ConstraintValidationResult result = process(newYorkNYAddress, "postalCode", postalCodeAlphaNumericPatternConstraint); 128 Assert.assertEquals(0, dictionaryValidationResult.getNumberOfWarnings()); 129 Assert.assertEquals(1, dictionaryValidationResult.getNumberOfErrors()); 130 Assert.assertEquals(ErrorLevel.ERROR, result.getStatus()); 131 Assert.assertEquals(new ValidCharactersConstraintProcessor().getName(), result.getConstraintName()); 132 } 133 134 @Test 135 public void testValueAllValidCharsAllowWhitespace() { 136 ConstraintValidationResult result = process(newYorkNYAddress, "street1", street1AlphaNumericPatternConstraint); 137 Assert.assertEquals(0, dictionaryValidationResult.getNumberOfWarnings()); 138 Assert.assertEquals(0, dictionaryValidationResult.getNumberOfErrors()); 139 Assert.assertEquals(ErrorLevel.OK, result.getStatus()); 140 Assert.assertEquals(new ValidCharactersConstraintProcessor().getName(), result.getConstraintName()); 141 } 142 143 @Test 144 public void testValueNotValidCharsAllowWhitespace() { 145 ConstraintValidationResult result = process(sydneyAUSAddress, "street1", street1AlphaNumericPatternConstraint); 146 Assert.assertEquals(0, dictionaryValidationResult.getNumberOfWarnings()); 147 Assert.assertEquals(1, dictionaryValidationResult.getNumberOfErrors()); 148 Assert.assertEquals(ErrorLevel.ERROR, result.getStatus()); 149 Assert.assertEquals(new ValidCharactersConstraintProcessor().getName(), result.getConstraintName()); 150 } 151 152 @Test 153 public void testValueAllValidCharsAllowWhitespaceAndPeriodAndUnderscoreAndParenthesis() { 154 ConstraintValidationResult result = process(washingtonDCAddress, "street2", street2AlphaNumericPatternConstraint); 155 Assert.assertEquals(0, dictionaryValidationResult.getNumberOfWarnings()); 156 Assert.assertEquals(0, dictionaryValidationResult.getNumberOfErrors()); 157 Assert.assertEquals(ErrorLevel.OK, result.getStatus()); 158 Assert.assertEquals(new ValidCharactersConstraintProcessor().getName(), result.getConstraintName()); 159 } 160 161 @Test 162 public void testValueNotValidCharsAllowWhitespaceAndPeriodAndUnderscoreAndParenthesis() { 163 ConstraintValidationResult result = process(newYorkNYAddress, "street2", street2AlphaNumericPatternConstraint); 164 Assert.assertEquals(0, dictionaryValidationResult.getNumberOfWarnings()); 165 Assert.assertEquals(1, dictionaryValidationResult.getNumberOfErrors()); 166 Assert.assertEquals(ErrorLevel.ERROR, result.getStatus()); 167 Assert.assertEquals(new ValidCharactersConstraintProcessor().getName(), result.getConstraintName()); 168 } 169 170 private ConstraintValidationResult process(Object object, String attributeName, ValidCharactersConstraint constraint) { 171 AttributeValueReader attributeValueReader = new DictionaryObjectAttributeValueReader(object, "org.kuali.rice.kns.datadictionary.validation.MockAddress", addressEntry); 172 attributeValueReader.setAttributeName(attributeName); 173 174 Object value = attributeValueReader.getValue(); 175 return processor.process(dictionaryValidationResult, value, constraint, attributeValueReader).getFirstConstraintValidationResult(); 176 } 177}