001/** 002 * Copyright 2005-2014 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.Test; 024import org.kuali.rice.krad.datadictionary.AttributeDefinition; 025import org.kuali.rice.krad.datadictionary.BusinessObjectEntry; 026import org.kuali.rice.krad.datadictionary.validation.Address; 027import org.kuali.rice.krad.datadictionary.validation.AttributeValueReader; 028import org.kuali.rice.krad.datadictionary.validation.DictionaryObjectAttributeValueReader; 029import org.kuali.rice.krad.datadictionary.validation.ErrorLevel; 030import org.kuali.rice.krad.datadictionary.validation.constraint.ValidCharactersConstraint; 031import org.kuali.rice.krad.datadictionary.validation.processor.ValidCharactersConstraintProcessor; 032import org.kuali.rice.krad.datadictionary.validation.result.ConstraintValidationResult; 033import org.kuali.rice.krad.datadictionary.validation.result.DictionaryValidationResult; 034 035 036/** 037 * Things this test should check: 038 * 039 * 1. value with all valid characters. (success) {@link #testValueAllValidChars()} 040 * 2. value with invalid characters. (failure) {@link #testValueNotValidChars()} 041 * 3. value with all valid characters. Allowing white space.(success) {@link #testValueAllValidCharsAllowWhitespace()} 042 * 043 * @author Kuali Rice Team (rice.collab@kuali.org) 044 */ 045public class AnyCharacterPatternConstraintTest { 046 047 private AttributeDefinition street1Definition; 048 private AttributeDefinition street2Definition; 049 050 private BusinessObjectEntry addressEntry; 051 private DictionaryValidationResult dictionaryValidationResult; 052 053 private ValidCharactersConstraintProcessor processor; 054 055 private Address washingtonDCAddress = new Address("893 Presidential Ave", "(A_123) Suite 800.", "Washington", "DC", "NHW123A", "USA", null); 056 private Address newYorkNYAddress = new Address("Presidential Street", "(A-123) Suite 800", "New York", "NY", "ZH 3456", "USA", null); 057 private Address sydneyAUSAddress = new Address("Presidential Street-Ave.", "Suite_800.", "Sydney", "ZZ", "ZH-5656", "USA", null); 058 059 private AnyCharacterPatternConstraint street1AnyCharacterPatternConstraint; 060 private AnyCharacterPatternConstraint street2AnyCharacterPatternConstraint; 061 062 @Before 063 public void setUp() throws Exception { 064 065 processor = new ValidCharactersConstraintProcessor(); 066 067 dictionaryValidationResult = new DictionaryValidationResult(); 068 dictionaryValidationResult.setErrorLevel(ErrorLevel.NOCONSTRAINT); 069 070 addressEntry = new BusinessObjectEntry(); 071 072 List<AttributeDefinition> attributes = new ArrayList<AttributeDefinition>(); 073 074 street1AnyCharacterPatternConstraint = new AnyCharacterPatternConstraint(); 075 street1AnyCharacterPatternConstraint.setMessageKey("validate.dummykey"); 076 street1AnyCharacterPatternConstraint.setValidationMessageParams( new ArrayList<String>()); 077 street1AnyCharacterPatternConstraint.setAllowWhitespace(true); 078 079 street1Definition = new AttributeDefinition(); 080 street1Definition.setName("street1"); 081 street1Definition.setValidCharactersConstraint(street1AnyCharacterPatternConstraint); 082 attributes.add(street1Definition); 083 084 085 street2AnyCharacterPatternConstraint = new AnyCharacterPatternConstraint(); 086 street2AnyCharacterPatternConstraint.setMessageKey("validate.dummykey"); 087 street2AnyCharacterPatternConstraint.setValidationMessageParams( new ArrayList<String>()); 088 089 street2Definition = new AttributeDefinition(); 090 street2Definition.setName("street2"); 091 street2Definition.setValidCharactersConstraint(street2AnyCharacterPatternConstraint); 092 attributes.add(street2Definition); 093 094 addressEntry.setAttributes(attributes); 095 } 096 097 @Test 098 public void testValueAllValidChars() { 099 ConstraintValidationResult result = process(sydneyAUSAddress, "street2", street2AnyCharacterPatternConstraint); 100 Assert.assertEquals(0, dictionaryValidationResult.getNumberOfWarnings()); 101 Assert.assertEquals(0, dictionaryValidationResult.getNumberOfErrors()); 102 Assert.assertEquals(ErrorLevel.OK, result.getStatus()); 103 Assert.assertEquals(new ValidCharactersConstraintProcessor().getName(), result.getConstraintName()); 104 } 105 106 @Test 107 public void testValueNotValidChars() { 108 ConstraintValidationResult result = process(newYorkNYAddress, "street2", street2AnyCharacterPatternConstraint); 109 Assert.assertEquals(0, dictionaryValidationResult.getNumberOfWarnings()); 110 Assert.assertEquals(1, dictionaryValidationResult.getNumberOfErrors()); 111 Assert.assertEquals(ErrorLevel.ERROR, result.getStatus()); 112 Assert.assertEquals(new ValidCharactersConstraintProcessor().getName(), result.getConstraintName()); 113 } 114 115 @Test 116 public void testValueAllValidCharsAllowWhitespace() { 117 ConstraintValidationResult result = process(washingtonDCAddress, "street1", street1AnyCharacterPatternConstraint); 118 Assert.assertEquals(0, dictionaryValidationResult.getNumberOfWarnings()); 119 Assert.assertEquals(0, dictionaryValidationResult.getNumberOfErrors()); 120 Assert.assertEquals(ErrorLevel.OK, result.getStatus()); 121 Assert.assertEquals(new ValidCharactersConstraintProcessor().getName(), result.getConstraintName()); 122 } 123 124 private ConstraintValidationResult process(Object object, String attributeName, ValidCharactersConstraint constraint) { 125 AttributeValueReader attributeValueReader = new DictionaryObjectAttributeValueReader(object, "org.kuali.rice.kns.datadictionary.validation.MockAddress", addressEntry); 126 attributeValueReader.setAttributeName(attributeName); 127 128 Object value = attributeValueReader.getValue(); 129 return processor.process(dictionaryValidationResult, value, constraint, attributeValueReader).getFirstConstraintValidationResult(); 130 } 131}