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.io.IOException; 019import java.io.InputStream; 020import java.util.ArrayList; 021import java.util.List; 022import java.util.Properties; 023 024import org.junit.Assert; 025import org.junit.Before; 026import org.junit.Test; 027import org.kuali.rice.krad.datadictionary.AttributeDefinition; 028import org.kuali.rice.krad.datadictionary.BusinessObjectEntry; 029import org.kuali.rice.krad.datadictionary.validation.Address; 030import org.kuali.rice.krad.datadictionary.validation.AttributeValueReader; 031import org.kuali.rice.krad.datadictionary.validation.DictionaryObjectAttributeValueReader; 032import org.kuali.rice.krad.datadictionary.validation.ErrorLevel; 033import org.kuali.rice.krad.datadictionary.validation.constraint.ValidCharactersConstraint; 034import org.kuali.rice.krad.datadictionary.validation.processor.ValidCharactersConstraintProcessor; 035import org.kuali.rice.krad.datadictionary.validation.result.ConstraintValidationResult; 036import org.kuali.rice.krad.datadictionary.validation.result.DictionaryValidationResult; 037 038 039/** 040 * Things this test should check: 041 * 042 * 1. value with valid zipcode. (success) {@link #testValueValidZipcode()} 043 * 2. value with valid zipcode. (success) {@link #testValueValidZipcode1()} 044 * 3. value with invalid zipcode. (failure) {@link #testValueInvalidZipcode()} 045 * 4. value with invalid zipcode. (failure) {@link #testValueInvalidZipcode1()} 046 * 5. value with invalid zipcode. (failure) {@link #testValueInvalidZipcode2()} 047 * 048 * @author Kuali Rice Team (rice.collab@kuali.org) 049 */ 050public class ZipcodePatternConstraintTest { 051 052 private final String PATTERN_CONSTRAINT = "validationPatternRegex.zipcode"; 053 054 private AttributeDefinition postalCodeDefinition; 055 056 private BusinessObjectEntry addressEntry; 057 private DictionaryValidationResult dictionaryValidationResult; 058 059 private ValidCharactersConstraintProcessor processor; 060 061 private Address washingtonDCAddress = new Address("893 Presidential Ave", "(A_123) Suite 800.", "Washington", "DC", "12345", "USA", null); 062 private Address newYorkNYAddress = new Address("Presidential Street", "(A-123) Suite 800", "New York", "NY", "12345-1234", "USA", null); 063 private Address sydneySDAddress = new Address("Presidential Street-Ave.", "Suite_800.", "Sydney", "SD", "ZH-123456", "Australia", null); 064 private Address holandHDAddress = new Address("Presidential Street-Ave.", "Suite_800.", "Holand", "HD", "12345-123", "Holand", null); 065 private Address bombayMHAddress = new Address("Presidential Street-Ave.", "Suite_800.", "Bombay", "MH", "380002", "India", null); 066 067 private ConfigurationBasedRegexPatternConstraint postalCodePatternConstraint; 068 069 @Before 070 public void setUp() throws Exception { 071 072 String regex = getProperty(PATTERN_CONSTRAINT); 073 074 processor = new ValidCharactersConstraintProcessor(); 075 076 dictionaryValidationResult = new DictionaryValidationResult(); 077 dictionaryValidationResult.setErrorLevel(ErrorLevel.NOCONSTRAINT); 078 079 addressEntry = new BusinessObjectEntry(); 080 081 List<AttributeDefinition> attributes = new ArrayList<AttributeDefinition>(); 082 083 postalCodePatternConstraint = new ConfigurationBasedRegexPatternConstraint(); 084 postalCodePatternConstraint.setValue(regex); 085 086 postalCodeDefinition = new AttributeDefinition(); 087 postalCodeDefinition.setName("postalCode"); 088 postalCodeDefinition.setValidCharactersConstraint(postalCodePatternConstraint); 089 attributes.add(postalCodeDefinition); 090 091 addressEntry.setAttributes(attributes); 092 } 093 094 @Test 095 public void testValueValidZipcode() { 096 ConstraintValidationResult result = process(washingtonDCAddress, "postalCode", postalCodePatternConstraint); 097 Assert.assertEquals(0, dictionaryValidationResult.getNumberOfWarnings()); 098 Assert.assertEquals(0, dictionaryValidationResult.getNumberOfErrors()); 099 Assert.assertEquals(ErrorLevel.OK, result.getStatus()); 100 Assert.assertEquals(new ValidCharactersConstraintProcessor().getName(), result.getConstraintName()); 101 } 102 103 @Test 104 public void testValueValidZipcode1() { 105 ConstraintValidationResult result = process(newYorkNYAddress, "postalCode", postalCodePatternConstraint); 106 Assert.assertEquals(0, dictionaryValidationResult.getNumberOfWarnings()); 107 Assert.assertEquals(0, dictionaryValidationResult.getNumberOfErrors()); 108 Assert.assertEquals(ErrorLevel.OK, result.getStatus()); 109 Assert.assertEquals(new ValidCharactersConstraintProcessor().getName(), result.getConstraintName()); 110 } 111 112 @Test 113 public void testValueInvalidZipcode() { 114 ConstraintValidationResult result = process(sydneySDAddress, "postalCode", postalCodePatternConstraint); 115 Assert.assertEquals(0, dictionaryValidationResult.getNumberOfWarnings()); 116 Assert.assertEquals(1, dictionaryValidationResult.getNumberOfErrors()); 117 Assert.assertEquals(ErrorLevel.ERROR, result.getStatus()); 118 Assert.assertEquals(new ValidCharactersConstraintProcessor().getName(), result.getConstraintName()); 119 } 120 121 @Test 122 public void testValueInvalidZipcode1() { 123 ConstraintValidationResult result = process(holandHDAddress, "postalCode", postalCodePatternConstraint); 124 Assert.assertEquals(0, dictionaryValidationResult.getNumberOfWarnings()); 125 Assert.assertEquals(1, dictionaryValidationResult.getNumberOfErrors()); 126 Assert.assertEquals(ErrorLevel.ERROR, result.getStatus()); 127 Assert.assertEquals(new ValidCharactersConstraintProcessor().getName(), result.getConstraintName()); 128 } 129 130 @Test 131 public void testValueInvalidZipcode2() { 132 ConstraintValidationResult result = process(bombayMHAddress, "postalCode", postalCodePatternConstraint); 133 Assert.assertEquals(0, dictionaryValidationResult.getNumberOfWarnings()); 134 Assert.assertEquals(1, dictionaryValidationResult.getNumberOfErrors()); 135 Assert.assertEquals(ErrorLevel.ERROR, result.getStatus()); 136 Assert.assertEquals(new ValidCharactersConstraintProcessor().getName(), result.getConstraintName()); 137 } 138 139 private ConstraintValidationResult process(Object object, String attributeName, ValidCharactersConstraint constraint) { 140 AttributeValueReader attributeValueReader = new DictionaryObjectAttributeValueReader(object, "org.kuali.rice.kns.datadictionary.validation.MockAddress", addressEntry); 141 attributeValueReader.setAttributeName(attributeName); 142 143 Object value = attributeValueReader.getValue(); 144 return processor.process(dictionaryValidationResult, value, constraint, attributeValueReader).getFirstConstraintValidationResult(); 145 } 146 147 private String getProperty(String key) { 148 String value = null; 149 String filePath = "org/kuali/rice/krad/ApplicationResources.properties"; 150 Properties properties = new Properties(); 151 try { 152 InputStream in = Thread.currentThread().getContextClassLoader().getResourceAsStream(filePath); 153 properties.load(in); 154 value = properties.getProperty(key); 155 } catch (IOException e) { 156 } 157 return value; 158 } 159}