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    
019    
020    import java.util.ArrayList;
021    import java.util.List;
022    import java.util.Properties;
023    
024    import org.junit.Assert;
025    import org.junit.Before;
026    import org.junit.Test;
027    import org.kuali.rice.core.api.CoreConstants;
028    import org.kuali.rice.core.api.config.property.Config;
029    import org.kuali.rice.core.api.config.property.ConfigContext;
030    import org.kuali.rice.core.framework.config.property.SimpleConfig;
031    import org.kuali.rice.core.framework.config.property.SimpleConfig;
032    import org.kuali.rice.krad.datadictionary.AttributeDefinition;
033    import org.kuali.rice.krad.datadictionary.validation.AttributeValueReader;
034    import org.kuali.rice.krad.datadictionary.validation.ErrorLevel;
035    import org.kuali.rice.krad.datadictionary.validation.SingleAttributeValueReader;
036    import org.kuali.rice.krad.datadictionary.validation.constraint.ValidCharactersConstraint;
037    import org.kuali.rice.krad.datadictionary.validation.processor.ValidCharactersConstraintProcessor;
038    import org.kuali.rice.krad.datadictionary.validation.result.ConstraintValidationResult;
039    import org.kuali.rice.krad.datadictionary.validation.result.DictionaryValidationResult;
040    
041    
042    /**
043     * Things this test should check:
044     * 
045     * 1. empty value check. (failure) {@link #testValueInvalidDateEmpty()}
046     * 2. value with valid date. (success) {@link #testValueValidDate()}
047     * 3. value with valid date. (success) {@link #testValueValidDate1()}
048     * 4. value with invalid date. (failure) {@link testValueInvalidDate()}
049     * 5. value with invalid date. (failure) {@link testValueInvalidDate1()}
050     * 6. value with invalid date. (failure) {@link testValueInvalidDate2()}
051     * 7. value with invalid date. (failure) {@link testValueInvalidDate3()}  
052     * 
053     * @author Kuali Rice Team (rice.collab@kuali.org) 
054     */
055    public class DatePatternConstraintTest {
056            
057            private AttributeDefinition dateDefinition;
058            
059            private DictionaryValidationResult dictionaryValidationResult;
060            
061            private ValidCharactersConstraintProcessor processor;
062            
063            private String validDate;
064            private String validDate1;
065            private String invalidDateEmpty;
066            private String invalidDate;
067            private String invalidDate1;
068            private String invalidDate2;
069            private String invalidDate3;
070                    
071            private DatePatternConstraint datePatternConstraint;            
072            
073            @Before
074            public void setUp() throws Exception {
075                    
076                    String formats = "MM/dd/yy;MM/dd/yyyy;MM-dd-yy;MM-dd-yyyy"; 
077                    
078                    Properties props = new Properties();
079                    props.put(CoreConstants.STRING_TO_DATE_FORMATS, formats);
080                    Config config = new SimpleConfig(props);
081                    
082                    ConfigContext.init(config);
083                    ConfigContext.getCurrentContextConfig().putProperty(CoreConstants.STRING_TO_DATE_FORMATS, formats);
084                    
085                    processor = new ValidCharactersConstraintProcessor();
086                                    
087                    dictionaryValidationResult = new DictionaryValidationResult();
088                    dictionaryValidationResult.setErrorLevel(ErrorLevel.NOCONSTRAINT);
089                    
090                    validDate = "07-28-2011";
091                    validDate1 = "12-31-83";                                
092                    invalidDateEmpty = "";
093                    invalidDate = "31-12-2010";
094                    invalidDate1 = "31-12-2010 12:30:45.456";
095                    invalidDate2 = "2011-07-28 IST";
096                    invalidDate3 = "12/31/2011";                                            
097                                            
098                    List<String> allowedFormats = new ArrayList<String>();
099                    allowedFormats.add("MM-dd-yy");
100                    allowedFormats.add("MM-dd-yyyy");
101                    
102                    datePatternConstraint = new DatePatternConstraint();
103                    datePatternConstraint.setAllowedFormats(allowedFormats);
104    
105                    dateDefinition = new AttributeDefinition();
106                    dateDefinition.setName("date");
107                    dateDefinition.setValidCharactersConstraint(datePatternConstraint);                                                                                             
108            }
109            
110            @Test
111            public void testValueInvalidDateEmpty() {
112                    ConstraintValidationResult result = process(invalidDateEmpty, "date", datePatternConstraint);
113                    Assert.assertEquals(0, dictionaryValidationResult.getNumberOfWarnings());
114                    Assert.assertEquals(0, dictionaryValidationResult.getNumberOfErrors());
115                    Assert.assertEquals(ErrorLevel.INAPPLICABLE, result.getStatus());
116                    Assert.assertEquals(new ValidCharactersConstraintProcessor().getName(), result.getConstraintName());
117            }
118            
119            @Test
120            public void testValueValidDate() {
121                    ConstraintValidationResult result = process(validDate, "date", datePatternConstraint);
122                    Assert.assertEquals(0, dictionaryValidationResult.getNumberOfWarnings());
123                    Assert.assertEquals(0, dictionaryValidationResult.getNumberOfErrors());
124                    Assert.assertEquals(ErrorLevel.OK, result.getStatus());
125                    Assert.assertEquals(new ValidCharactersConstraintProcessor().getName(), result.getConstraintName());
126            }
127            
128            @Test
129            public void testValueValidDate1() {
130                    ConstraintValidationResult result = process(validDate1, "Mdate", datePatternConstraint);
131                    Assert.assertEquals(0, dictionaryValidationResult.getNumberOfWarnings());
132                    Assert.assertEquals(0, dictionaryValidationResult.getNumberOfErrors());
133                    Assert.assertEquals(ErrorLevel.OK, result.getStatus());
134                    Assert.assertEquals(new ValidCharactersConstraintProcessor().getName(), result.getConstraintName());
135            }                               
136            
137            @Test
138            public void testValueInvalidDate() {
139                    ConstraintValidationResult result = process(invalidDate, "date", datePatternConstraint);
140                    Assert.assertEquals(0, dictionaryValidationResult.getNumberOfWarnings());
141                    Assert.assertEquals(1, dictionaryValidationResult.getNumberOfErrors());
142                    Assert.assertEquals(ErrorLevel.ERROR, result.getStatus());
143                    Assert.assertEquals(new ValidCharactersConstraintProcessor().getName(), result.getConstraintName());
144            }
145            
146            @Test
147            public void testValueInvalidDate1() {
148                    ConstraintValidationResult result = process(invalidDate1, "date", datePatternConstraint);
149                    Assert.assertEquals(0, dictionaryValidationResult.getNumberOfWarnings());
150                    Assert.assertEquals(1, dictionaryValidationResult.getNumberOfErrors());
151                    Assert.assertEquals(ErrorLevel.ERROR, result.getStatus());
152                    Assert.assertEquals(new ValidCharactersConstraintProcessor().getName(), result.getConstraintName());
153            }
154            
155            @Test
156            public void testValueInvalidDate2() {
157                    ConstraintValidationResult result = process(invalidDate2, "date", datePatternConstraint);
158                    Assert.assertEquals(0, dictionaryValidationResult.getNumberOfWarnings());
159                    Assert.assertEquals(1, dictionaryValidationResult.getNumberOfErrors());
160                    Assert.assertEquals(ErrorLevel.ERROR, result.getStatus());
161                    Assert.assertEquals(new ValidCharactersConstraintProcessor().getName(), result.getConstraintName());
162            }
163            
164            @Test
165            public void testValueInvalidDate3() {
166                    ConstraintValidationResult result = process(invalidDate3, "date", datePatternConstraint);
167                    Assert.assertEquals(0, dictionaryValidationResult.getNumberOfWarnings());
168                    Assert.assertEquals(1, dictionaryValidationResult.getNumberOfErrors());
169                    Assert.assertEquals(ErrorLevel.ERROR, result.getStatus());
170                    Assert.assertEquals(new ValidCharactersConstraintProcessor().getName(), result.getConstraintName());
171            }               
172            
173            private ConstraintValidationResult process(Object object, String attributeName, ValidCharactersConstraint constraint) {
174                    AttributeValueReader attributeValueReader = new SingleAttributeValueReader(object, "org.kuali.rice.kns.datadictionary.validation.MockAddress", attributeName,  dateDefinition);         
175                    Object value = attributeValueReader.getValue();
176                    return processor.process(dictionaryValidationResult, value, constraint, attributeValueReader).getFirstConstraintValidationResult();
177            }
178    }