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 018 019 020import java.io.IOException; 021import java.io.InputStream; 022import java.util.ArrayList; 023import java.util.Properties; 024 025import org.junit.Assert; 026import org.junit.Before; 027import org.junit.Test; 028import org.kuali.rice.krad.datadictionary.AttributeDefinition; 029import org.kuali.rice.krad.datadictionary.validation.AttributeValueReader; 030import org.kuali.rice.krad.datadictionary.validation.ErrorLevel; 031import org.kuali.rice.krad.datadictionary.validation.SingleAttributeValueReader; 032import org.kuali.rice.krad.datadictionary.validation.constraint.ValidCharactersConstraint; 033import org.kuali.rice.krad.datadictionary.validation.processor.ValidCharactersConstraintProcessor; 034import org.kuali.rice.krad.datadictionary.validation.result.ConstraintValidationResult; 035import org.kuali.rice.krad.datadictionary.validation.result.DictionaryValidationResult; 036 037 038/** 039 * Things this test should check: 040 * 041 * 1. empty value check. (failure) {@link #testValueInvalidMonthEmpty()} 042 * 2. value with valid month. (success) {@link #testValueValidMonth()} 043 * 3. value with valid month. (success) {@link #testValueValidMonth1()} 044 * 4. value with valid month. (success) {@link #testValueValidMonth2()} 045 * 5. value with invalid month. (failure) {@link #testValueInvalidMonth()} 046 * 6. value with invalid month. (failure) {@link #testValueInvalidMonth1()} 047 * 7. value with invalid month. (failure) {@link #testValueInvalidMonth2()} 048 * 8. value with invalid month. (failure) {@link #testValueInvalidMonth3()} 049 * 050 * @author Kuali Rice Team (rice.collab@kuali.org) 051 */ 052public class MonthPatternConstraintTest { 053 054 private final String PATTERN_CONSTRAINT = "validationPatternRegex.month"; 055 056 private AttributeDefinition monthDefinition; 057 058 private DictionaryValidationResult dictionaryValidationResult; 059 060 private ValidCharactersConstraintProcessor processor; 061 062 private String validMonth; 063 private String validMonth1; 064 private String validMonth2; 065 private String invalidMonthEmpty; 066 private String invalidMonth; 067 private String invalidMonth1; 068 private String invalidMonth2; 069 private String invalidMonth3; 070 071 private ConfigurationBasedRegexPatternConstraint monthPatternConstraint; 072 073 @Before 074 public void setUp() throws Exception { 075 076 String regex = getProperty(PATTERN_CONSTRAINT); 077 078 processor = new ValidCharactersConstraintProcessor(); 079 080 dictionaryValidationResult = new DictionaryValidationResult(); 081 dictionaryValidationResult.setErrorLevel(ErrorLevel.NOCONSTRAINT); 082 083 validMonth = "1"; 084 validMonth1 = "05"; 085 validMonth2 = "12"; 086 invalidMonthEmpty = ""; 087 invalidMonth = "00"; 088 invalidMonth1 = "0"; 089 invalidMonth2 = "13"; 090 invalidMonth3 = "JAN"; 091 092 monthPatternConstraint = new ConfigurationBasedRegexPatternConstraint(); 093 monthPatternConstraint.setMessageKey("validate.dummykey"); 094 monthPatternConstraint.setValidationMessageParams( new ArrayList<String>()); 095 monthPatternConstraint.setValue(regex); 096 097 monthDefinition = new AttributeDefinition(); 098 monthDefinition.setName("month"); 099 monthDefinition.setValidCharactersConstraint(monthPatternConstraint); 100 } 101 102 @Test 103 public void testValueInvalidMonthEmpty() { 104 ConstraintValidationResult result = process(invalidMonthEmpty, "month", monthPatternConstraint); 105 Assert.assertEquals(0, dictionaryValidationResult.getNumberOfWarnings()); 106 Assert.assertEquals(0, dictionaryValidationResult.getNumberOfErrors()); 107 Assert.assertEquals(ErrorLevel.INAPPLICABLE, result.getStatus()); 108 Assert.assertEquals(new ValidCharactersConstraintProcessor().getName(), result.getConstraintName()); 109 } 110 111 @Test 112 public void testValueValidMonth() { 113 ConstraintValidationResult result = process(validMonth, "month", monthPatternConstraint); 114 Assert.assertEquals(0, dictionaryValidationResult.getNumberOfWarnings()); 115 Assert.assertEquals(0, dictionaryValidationResult.getNumberOfErrors()); 116 Assert.assertEquals(ErrorLevel.OK, result.getStatus()); 117 Assert.assertEquals(new ValidCharactersConstraintProcessor().getName(), result.getConstraintName()); 118 } 119 120 @Test 121 public void testValueValidMonth1() { 122 ConstraintValidationResult result = process(validMonth1, "Mmonth", monthPatternConstraint); 123 Assert.assertEquals(0, dictionaryValidationResult.getNumberOfWarnings()); 124 Assert.assertEquals(0, dictionaryValidationResult.getNumberOfErrors()); 125 Assert.assertEquals(ErrorLevel.OK, result.getStatus()); 126 Assert.assertEquals(new ValidCharactersConstraintProcessor().getName(), result.getConstraintName()); 127 } 128 129 @Test 130 public void testValueValidMonth2() { 131 ConstraintValidationResult result = process(validMonth2, "month", monthPatternConstraint); 132 Assert.assertEquals(0, dictionaryValidationResult.getNumberOfWarnings()); 133 Assert.assertEquals(0, dictionaryValidationResult.getNumberOfErrors()); 134 Assert.assertEquals(ErrorLevel.OK, result.getStatus()); 135 Assert.assertEquals(new ValidCharactersConstraintProcessor().getName(), result.getConstraintName()); 136 } 137 138 @Test 139 public void testValueInvalidMonth() { 140 ConstraintValidationResult result = process(invalidMonth, "month", monthPatternConstraint); 141 Assert.assertEquals(0, dictionaryValidationResult.getNumberOfWarnings()); 142 Assert.assertEquals(1, dictionaryValidationResult.getNumberOfErrors()); 143 Assert.assertEquals(ErrorLevel.ERROR, result.getStatus()); 144 Assert.assertEquals(new ValidCharactersConstraintProcessor().getName(), result.getConstraintName()); 145 } 146 147 @Test 148 public void testValueInvalidMonth1() { 149 ConstraintValidationResult result = process(invalidMonth1, "month", monthPatternConstraint); 150 Assert.assertEquals(0, dictionaryValidationResult.getNumberOfWarnings()); 151 Assert.assertEquals(1, dictionaryValidationResult.getNumberOfErrors()); 152 Assert.assertEquals(ErrorLevel.ERROR, result.getStatus()); 153 Assert.assertEquals(new ValidCharactersConstraintProcessor().getName(), result.getConstraintName()); 154 } 155 156 @Test 157 public void testValueInvalidMonth2() { 158 ConstraintValidationResult result = process(invalidMonth2, "month", monthPatternConstraint); 159 Assert.assertEquals(0, dictionaryValidationResult.getNumberOfWarnings()); 160 Assert.assertEquals(1, dictionaryValidationResult.getNumberOfErrors()); 161 Assert.assertEquals(ErrorLevel.ERROR, result.getStatus()); 162 Assert.assertEquals(new ValidCharactersConstraintProcessor().getName(), result.getConstraintName()); 163 } 164 165 @Test 166 public void testValueInvalidMonth3() { 167 ConstraintValidationResult result = process(invalidMonth3, "month", monthPatternConstraint); 168 Assert.assertEquals(0, dictionaryValidationResult.getNumberOfWarnings()); 169 Assert.assertEquals(1, dictionaryValidationResult.getNumberOfErrors()); 170 Assert.assertEquals(ErrorLevel.ERROR, result.getStatus()); 171 Assert.assertEquals(new ValidCharactersConstraintProcessor().getName(), result.getConstraintName()); 172 } 173 174 private ConstraintValidationResult process(Object object, String attributeName, ValidCharactersConstraint constraint) { 175 AttributeValueReader attributeValueReader = new SingleAttributeValueReader(object, "org.kuali.rice.kns.datadictionary.validation.MockAddress", attributeName, monthDefinition); 176 Object value = attributeValueReader.getValue(); 177 return processor.process(dictionaryValidationResult, value, constraint, attributeValueReader).getFirstConstraintValidationResult(); 178 } 179 180 private String getProperty(String key) { 181 String value = null; 182 String filePath = "org/kuali/rice/krad/ApplicationResources.properties"; 183 Properties properties = new Properties(); 184 try { 185 InputStream in = Thread.currentThread().getContextClassLoader().getResourceAsStream(filePath); 186 properties.load(in); 187 value = properties.getProperty(key); 188 } catch (IOException e) { 189 } 190 return value; 191 } 192}